[CMake] PROJECT_SOURCE_DIR ...

Matthew Woehlke matthew.woehlke at kitware.com
Sun Nov 10 16:13:01 EST 2013


On 2013-11-10 14:12, Witold E Wolski wrote:
> And [<project>_{SOURCE,BINARY}_DIR] are the same as
> CMAKE_BINARY_DIR, or CMAKE_SOURCE_DIR.
>
> So why one would use the ${PROJECT}_BINARY_DIR variables?

Not necessarily. Consider:

src/CMakeLists.txt
project(A)

src/b/CMakeLists.txt
project(B)

src/b/tests/CMakeLists.txt

...etc. Now, e.g. B_BINARY_DIR can be different from both 
CMAKE_BINARY_DIR and CMAKE_CURRENT_BINARY_DIR.

Additionally, your entire project might be included by some other 
CMakeLists.txt such that (continuing with the above example) 
A_BINARY_DIR != CMAKE_BINARY_DIR. So it's good practice when defining 
project-specific macros to use <project>_{SOURCE,BINARY}_DIR where 
<project> is the project you declare at the top level directory of your 
project to refer to the top level directory of your project. (And only 
use CMAKE_{SOURCE,BINARY}_DIR when you really mean the source/build root 
and don't care if that's your project's top level directory or not.)

Basically, you can be more confident that you know where in your tree 
<project>_{SOURCE,BINARY}_DIR refers than you can of 
CMAKE_{SOURCE,BINARY}_DIR.

-- 
Matthew



More information about the CMake mailing list