[CMake] [Cmake] Detect caller project for a sub-project

Andreas Mohr andi at lisas.de
Thu Jun 10 06:53:59 EDT 2010


On Thu, Jun 10, 2010 at 06:26:27AM -0400, cmake-request at cmake.org wrote:
> If I have a CMake project organized in more than two level, i.e.
> 
> CMakeLists.txt:
> PROJECT(A)
> add_subdirectories(./projectB)
> 
> ./projectB/CMakeLists.txt:
> PROJECT(B)
> add_subdirectories(${PROJECT_SOURCE_DIR}/projectC)
> 
> ./projectB/projectC/CMakeLists.txt:
> PROJECT(C)
> 
> is there some kind of way for a project to detect the name of the project
> from which it was added? In this example, can C know that was added by B?
> (and B was added by A?)

Why not something like:
./projectB/projectC/CMakeLists.txt:
if(PROJECT_NAME)
  message(STATUS "we are a sub project, added by ${PROJECT_NAME} in ${PROJECT_SOURCE_DIR}")
endif(PROJECT_NAME)
project(C)

This should work I think, since projects should (famous last words...) have hierarchical scope
I guess, thus any projects added in sub directories in the meantime
shouldn't manage to influence evaluation of the parent project's name.

HTH,

Andreas Mohr


More information about the CMake mailing list