[CMake] Using cache in -P scripts?

Eric Noulard eric.noulard at gmail.com
Thu Oct 23 15:51:13 EDT 2008


2008/10/23 Abe Stephens <abe at sci.utah.edu>:
> Hi, Thanks for the reply. But perhaps I should clarify--I'd like to avoid
> using lots and lots of -D arguments. So instead of doing something like
> this:
>
> add_custom_command(
>  output file1.cc
>  command ${CMAKE_COMMAND}
>  args -D var0="${cached0}"
>       -D var1="${cached1}"
>       ...
>       -D var127="${cached127}"
>       -P myscript.cmake
>  )
>
> It would be great if there was a simple way to just load the cache within
> myscript.cmake. I think it would be possible to write a macro to read in the
> cache file and parse it, but I'm curious if there is an existing mechanism.

I was thinking of a kinf of a hack I was using when I was using plain makefiles
I used to write reentrant makefile. I think you may just do the same with CMake.

In fact you want to execute "myscript.cmake" in the same "context" that you
were running CMake itself, so you may achieve this by re-running CMake
from within add_custum_command.

Try to add this to your CMakeLists.txt:

include(${CMAKE_BINARY_DIR}/myscript.cmake OPTIONAL)

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/tt.txt
                   COMMAND ${CMAKE_COMMAND} ARGS -P
${CMAKE_SOURCE_DIR}/createmyscript.cmake
                   COMMAND ${CMAKE_COMMAND} ARGS .
		   WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_custom_target(toto
                  DEPENDS ${CMAKE_BINARY_DIR}/tt.txt)


The idea is that when you run CMake the
${CMAKE_BINARY_DIR}/myscript.cmake does not exists
thus the
include(${CMAKE_BINARY_DIR}/myscript.cmake OPTIONAL)
won't include anything.

after the first CMake run you may do:

make toto
and thus the myscript.cmake will be created  by
the "createmyscript.cmake" script (attached to this mail)
the second command of the custom target is run which is
precisely CMake itself in the builddir :=)
So the freshly created
${CMAKE_BINARY_DIR}/myscript.cmake
is executed in the CMake context, WITH the cache of the project
loaded by CMake itself :=)

And you are done, at least if I did understand your needs.

Not so sure that this is a "clean" way to do it but it seems to work
here.

-- 
Erk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: createmyscript.cmake
Type: text/x-cmake
Size: 191 bytes
Desc: not available
URL: <http://www.cmake.org/pipermail/cmake/attachments/20081023/da85a8d0/attachment.bin>


More information about the CMake mailing list