Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are CMAKE_SOURCE_DIR and PROJECT_SOURCE_DIR the same in CMake?

Tags:

cmake

This page contains a good summary of variables CMake already defines for us. I feel that some variables are the same. Take the example of CMAKE_SOURCE_DIR and PROJECT_SOURCE_DIR for example. They are the same, referring to the folder where the top level CMakeLists.txt is defined. So my question is: are there subtle difference between them? Thanks.

like image 532
feelfree Avatar asked Aug 15 '15 19:08

feelfree


People also ask

What is CMake Project_source_dir?

This is the source directory of the last call to the project() command made in the current directory scope or one of its parents. Note, it is not affected by calls to project() made within a child directory scope (i.e. from within a call to add_subdirectory() from the current scope).

What is Cmake_source_dir?

The path to the top level of the source tree. This is the full path to the top level of the current CMake source tree. For an in-source build, this would be the same as CMAKE_BINARY_DIR .

What is Cmake_current_source_dir?

CMAKE_CURRENT_SOURCE_DIR. The path to the source directory currently being processed. This is the full path to the source directory that is currently being processed by cmake.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.


1 Answers

There is a difference between these variables. CMAKE_SOURCE_DIR does indeed refer to the folder where the top-level CMakeLists.txt is defined. However, PROJECT_SOURCE_DIR refers to the folder of the CMakeLists.txt containing the most recent project() command.

For example, say you have a top-level project called Outer and this contains a subdirectory with its own project called Inner. Outer's CMakeLists.txt has:

project(Outer) add_subdirectory(Inner) 

and Inner's:

project(Inner) 

Then in both of these CMakeLists files, CMAKE_SOURCE_DIR will refer to Outer's source dir. But while PROJECT_SOURCE_DIR for Outer is also this same dir, this is not the case for Inner. Inner's PROJECT_SOURCE_DIR is the subdirectory containing its CMakeLists.txt.

This difference applies to all PROJECT_<var> vs CMAKE_<var> variables.

like image 83
Fraser Avatar answered Sep 19 '22 06:09

Fraser