[CMake] Reading settings from generated files

Eric Noulard eric.noulard at gmail.com
Wed May 14 02:19:52 EDT 2008


2008/5/13 Andrea Gualano <gualano at imavis.com>:
>  Hello Eric,
>
>  The custom command is something like this (I have removed all other
> parameters for simplicity):
>
>  ADD_CUSTOM_COMMAND (
>     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/compiler.opt
>     COMMAND xs ${CMAKE_CURRENT_SOURCE_DIR}/project.cfg
>     DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/project.cfg
>     COMMENT "Running xs"
>     VERBATIM
>  )
>
>  It reads project.cfg, does a lot of slow things and then generates a
> compiler.opt which contains compiler flags.

OK.

>  What I'd like to do is:
>
>  FILE (READ ${CMAKE_CURRENT_BINARY_DIR}/compiler.opt MORE_FLAGS)
>  SET (CMAKE_C_FLAGS "${MORE_FLAGS}" CACHE STRING "generated flags")
>  MESSAGE ("Hello, the flags: ${CMAKE_C_FLAGS}")
>  ...and then use those flags in subsequent targets.
>
>  I have added a:
>     COMMAND cmake -P ${CMAKE_CURRENT_SOURCE_DIR}/read_flags.cmake
>  to the custom target, where read_flags.cmake contains exactly the three
> lines above.
>
>  The read_flags.cmake script runs and reads correctly the file, but it seems
> to have no effect on the compilation.
>  Also, the CMAKE_C_FLAGS variable is not set in the cache.

If you want to override a CACHE entry which may already exist you'll have
to use FORCE
SET (CMAKE_C_FLAGS "${MORE_FLAGS}" CACHE STRING "generated flags" FORCE)
But in fact I don't know if  CMake -P may/currently shares the CMakeCache.txt?

Basically your need is to be able to run CMake twice, the first one
fort generating
some files and the second one to build.

May be your read_flags.cmake could generate a file more_flags.cmake
then your CMakeList.txt should do

IF(EXISTS more_flags.cmake)
   INCLUDE(more_flags.cmake)
ENDIF(EXISTS more_flags.cmake)


or
INCLUDE(more_flags.cmake OPTIONAL)

(but I don't know since when the OPTIONAL keyword is there)

I have never tried this but I think that IF more_flags.cmake exist then you'll
have your CMAKE_C_FLAGS updated, because INCLUDE occurs in the context
of your CMakeLists.txt and not from within a cmake -P  script.

the "more_flags.cmake" should contain your previous SET statement.
SET (CMAKE_C_FLAGS "${MORE_FLAGS}" CACHE STRING "generated flags" FORCE)

I'm sorry I have no time to try this, but may be some other CMake user may
have more experience than me for such  "two-step" scheme.

-- 
Erk


More information about the CMake mailing list