Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CMAKE_CURRENT_SOURCE_DIR & CMAKE_SOURCE_DIR variables?

Tags:

build

cmake

as I understood, these variables are used in case project consist of sub directories e.g. every sub directory further have CMakeLists.txt files.

CMAKE_CURRENT_SOURCE_DIR

refers to the path of source director under process ? and

CMAKE_SOURCE_DIR

refers to top most source directory ? I am not sure about it.

like image 856
TonyParker Avatar asked Nov 07 '18 18:11

TonyParker


1 Answers

CMAKE_SOURCE_DIR is where cmake was originally invoked, and CMAKE_CURRENT_SOURCE_DIR is where cmake is currently working. For instance, if you use add_subdirectory command to include a dependency to your project, the dependency will have its own CMAKE_CURRENT_SOURCE_DIR but CMAKE_SOURCE_DIR will remain the same.

Expanding on Some programmer dude comment, imagine you have the following three CMakeFiles on different directories on your project

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)

message("root dir CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
message("root dir CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")

add_subdirectory(sub1)
add_subdirectory(sub2)

sub1/CMakeLists.txt

message("sub1 dir CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
message("sub1 dir CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")

sub2/CMakeLists.txt

message("sub2 dir CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
message("sub2 dir CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")

Running cmake . on the root directory gives me this output

root dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests
root dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests
sub1 dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests
sub1 dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests/sub1
sub2 dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests
sub2 dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests/sub2

Now if I run cmake . on the sub1 directory I get the same value for both variables:

sub1 dir CMAKE_SOURCE_DIR = /Users/henrique/cmake_tests/sub1
sub1 dir CMAKE_CURRENT_SOURCE_DIR = /Users/henrique/cmake_tests/sub1
like image 199
Henrique Jung Avatar answered Sep 23 '22 00:09

Henrique Jung