[Cmake] Setting Variables in CMAKE
Brad King
brad.king at kitware.com
Mon, 26 Apr 2004 10:03:50 -0400
Linton, Tom wrote:
> I have a source tree with a top level CMakeLists.txt file that contains
> something like:
>
> INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineSystem.cmake)
> SET(MYFLAG "defaultValue")
> IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
> SET(MYFLAG "linuxValue")
> ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
> PROJECT(TRY)
> SUBDIRS(a b)
>
> The behavior of CMAKE is that (1) in the top-level CMakeLists.txt the variable
> "MYFLAG" gets set to "linuxValue" wherease (2) in the subdirectories
> "MYFLAG" is left at "defaultValue".
>
> I tried changing the SET command like:
> SET(MYFLAG "defaultValue" CACHE INTERNAL "myflag")
> but this had no effect.
>
> I want to set variables at top-level based on the system name and have these
> variables propagate to the subdirectories and it seems like what I'm doing is
> correct, according to section 3.4 in the CMake book, which say that variable
> scope is limited to a CMakeList file and any subdirectory CMakeList files.
>
> This seems like a really basic thing to want to do. Any suggestions on
> what's going wrong?
The CMakeDetermineSystem.cmake module makes use of the EXEC_PROGRAM
command, which is not inherited by subdirectories. Therefore it will
never set CMAKE_SYSTEM_NAME correctly when CMake is configuring a
subdirectory. However, module is included automatically by the PROJECT
command, and CMAKE_SYSTEM_NAME is automatically set. Change your code
to look like this:
PROJECT(TRY)
SET(MYFLAG "defaultValue")
IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
SET(MYFLAG "linuxValue")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
SUBDIRS(a b)
The top level CMakeLists.txt file should always have the PROJECT command
first except in a few special cases.
-Brad