[CMake] Recompiling source file multiple times

Michael Hertling mhertling at online.de
Mon Oct 11 20:31:19 EDT 2010


On 10/11/2010 10:04 PM, Belcourt, Kenneth wrote:
> Hi,
> 
> I did a quick scan looking for this but didn't see this mentioned in  
> the FAQ.
> 
> Is there a documented process for compiling a single source file  
> multiple times?  For example, the UMFPACK library need to compile most  
> source files at least twice with a different set of defined macros for  
> each build.  They often use -DDINT for one compilation and then  
> undefined -DDINT and define -DDLONG.  Anyone know a good way to handle  
> this?

A source file is compiled individually for each target it becomes part
of, so the definitions can by applied per target as target properties.
Furthermore, you could create symlinks to the concerned sources which
can be used to apply different definitions as source file properties:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(MULTCOMP C)
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
ADD_LIBRARY(f1 SHARED f.c)
ADD_LIBRARY(f2 SHARED f.c)
SET_TARGET_PROPERTIES(f1 PROPERTIES COMPILE_DEFINITIONS T1)
SET_TARGET_PROPERTIES(f2 PROPERTIES COMPILE_DEFINITIONS T2)
ADD_CUSTOM_COMMAND(OUTPUT ff.c
    COMMAND ${CMAKE_COMMAND} -E create_symlink f.c ff.c DEPENDS f.c)
SET_SOURCE_FILES_PROPERTIES(ff.c PROPERTIES COMPILE_DEFINITIONS FF)
ADD_LIBRARY(ff SHARED ff.c)

The sole source file f.c is compiled with -DT1 for target t1, -DT2 for
target t2 and - as symlink ff.c - with -DFF for target ff. You could
even consider to link or copy entire directories of source files in
order to apply different definitions as directory properties.

'hope that helps.

Regards,

Michael


More information about the CMake mailing list