[CMake] Reading settings from generated files

Andrea Gualano gualano at imavis.com
Tue May 13 09:38:32 EDT 2008


Eric NOULARD wrote:
> * if the problem is to read those file after they have been generated
>   at make time then you may run a cmake script (which use FILE(READ )
>   inside a ADD_CUSTOM_COMMAND which depends on the OUTPUT of the first
>   one
> 
> something like:
>   ADD_CUSTOM_COMMAND(...
>                      COMMAND cmake -P yourCMakeScript.cmake
>                      DEPENDS output_file.txt
>                      ...)
> 
> this way you can execute any [scriptable] CMake command at build time.
> 
> however I do not know if _file_ (and not target) dependency works.
>   
> It would be easier if you give us excerpt of your CMakeLists.txt
> at least the add_custom_command part including file name.

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.

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.

> --
> Erk

Thank you,
Andrea

-- 
Andrea Gualano
ImaVis S.r.l.
email: gualano at imavis.com


More information about the CMake mailing list