[CMake] Touching files on cache var changes

Michael Hertling mhertling at online.de
Tue May 17 13:30:10 EDT 2011


On 05/17/2011 05:45 PM, Robert Bielik wrote:
> Hi all,
> 
> I'm wondering if there's a way to touch files on cached var changes. Let's say I have an option to enable or disable a feature
> in my application, and depending on its setting a preprocessor macro is defined (or not defined) in the CMakeLists.txt file:
> 
> OPTION(FEATURE_X "Check to enable feature X" OFF)
> 
> IF(FEATURE_X)
> ADD_DEFINITIONS(-DUSE_FEATURE_X=1)
> ELSE(FEATURE_X)
> ADD_DEFINITIONS(-DUSE_FEATURE_X=0)
> ENDIF(FEATURE_X)
> 
> and in "features.c":
> 
> ...
> #if USE_FEATURE_X
> // Code for feature X
> 
> #endif
> ...
> 
> The source code file "features.c" that have code depending on the preprocessor macro needs to be recompiled each time the option changes, which does not automatically
> happen if the macro is defined like above (and I _really_ need to have it defined, either to zero or one...).
> 
> So I pretty much need to hook a dependency of "features.c" to change of cached cmake options/vars...
> 
> Ideas?
> TIA
> /Rob

AFAICS, with 2.8.4, the concerned files *are* recompiled:

# CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.4 FATAL_ERROR)
PROJECT(RECOMPILE C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
OPTION(FEATURE_X "Check to enable feature X" OFF)
IF(FEATURE_X)
ADD_DEFINITIONS(-DUSE_FEATURE_X=1)
ELSE(FEATURE_X)
ADD_DEFINITIONS(-DUSE_FEATURE_X=0)
ENDIF(FEATURE_X)
ADD_EXECUTABLE(main main.c)

/* main.c: */
int main(void){return 0;}

After reconfiguring the project with an altered FEATURE_X value,
you will see main.c being recompiled. This is due to the line

CMakeFiles/main.dir/main.c.o: CMakeFiles/main.dir/flags.make

in ${CMAKE_BINARY_DIR}/CMakeFiles/main.dir/build.make with the
${CMAKE_BINARY_DIR}/CMakeFiles/main.dir/flags.make containing:

C_DEFINES = -DUSE_FEATURE_X=...

It's this latter file which is modified when the preprocessor
definitions change, and the object file's dependency provides
for the recompilation. Can you confirm that?

Regards,

Michael


More information about the CMake mailing list