[CMake] decision based on being a nested project
Michael Hertling
mhertling at online.de
Tue Jun 8 19:05:43 EDT 2010
On 06/08/2010 09:00 PM, Nathan Huesken wrote:
> Hi,
>
> As can be read in an earlier thread, I am trying to nest a cmake
> project (call it "inner") into another cmake project (call it "outer").
> The "inner" project should be extracable and run as its own project.
>
> Having trouble with EXTERNALPROJECT_ADD, I am trying to add the project
> as a subdir. So I have something like this:
> - rootdir
> CMakeFile: Project(outer)
> - lib
> CMakeFile: Add_Subdirectory(inner)
> - inner
> CMakeFile: Project(inner)
> - doc
> CMakeFile: Add_target(doc)
> - doc
> CMakeFile: Add_target(doc)
> ...
>
> The problem now is, that the target "doc" exists twice, cmake
> complaints.
>
> So I would rename the inner target to "inner_doc", and add it as a
> dependency of "doc".
> But as said the inner project should also work on is own.
> So I would do:
>
> IF_IS_NESTED_PROJECT
> add_target(inner_doc)
> ELSE
> add_target(doc)
> ENDIF
>
> My question: How can I find out if inner is configured as inner project
> of outer or as a standalone project? (How can I do the
> IF_IS_NESTED_PROJECT)?
You can use PROJECT_SOURCE_DIR and CMAKE_SOURCE_DIR to distinguish:
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(OUTER NONE)
MESSAGE("PROJECT: OUTER")
ADD_SUBDIRECTORY(inner)
inner/CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(INNER NONE)
IF(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
MESSAGE("PROJECT: INNER (STAND-ALONE)")
ELSE()
MESSAGE("PROJECT: INNER (SUBORDINATE)")
ENDIF()
Issuing "cmake <path/to/outer>" yields
PROJECT: OUTER
PROJECT: INNER (SUBORDINATE)
[...]
and "cmake <path/to/outer>/inner" results in
PROJECT: INNER (STAND-ALONE)
[...]
i.e. your "IF_IS_NESTED_PROJECT" effectively is:
IF(NOT PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
'hope that helps.
Regards,
Michael
More information about the CMake
mailing list