[CMake] How to deal with silly custom Gnu Make recipes with CMake

Timenkov Yuri ytimenkov at parallels.com
Thu May 15 12:21:45 EDT 2008


On Thursday 15 May 2008 20:01:02 kent williams wrote:
> I won't name the package because I find this construction really
> stupid, but I'm writing CMakeLists.txt files for a package we want to
> use and came across this:
> 
> 
> %_floof: %.c
>     $(CC) $(CFLAGS) -DFLOOF -o $@ -lfloof
> 
> So I don't really see how CMake could do something like this.  And
> that doesn't even adding C Preprocessor definitions to the command
> line, which is hard to do portably.
> 
> What I ended up doing is this Unix-only workaround:
> 
> PROJECT(EXAMPLE)
> add_custom_command(OUTPUT test_floof.c
>                           COMMAND echo '\#define FLOOF 1' >
> ${EXAMPLE_BINARY_DIR}/test_floof.c
>                           COMMAND cat ${EXAMPLE_SOURCE_DIR}/test.c >>
> ${EXAMPLE_BINARY_DIR}/test_floof.c
>                           DEPENDS ${EXAMPLE_SOURCE_DIR}/test.c
> )
> 
> ADD_EXECUTABLE(test_floof ${EXAMPLE_BINARY_DIR}/test_floof.c)
> TARGET_LINK_LIBRARIES(test_floof floof)
Why following solution doesn't suit you?
PROJECT(example)
ADD_DEFINITIONS(-DFLOOF)
ADD_EXECUTABLE(test_floof test.c)

> 
> This seems incredibly rickety.  Is there an elegant way to do this?
> 
> And am I missing something in the CMake documentation, but it's
> confusing to me how you'd write a custom command that would be general
> -- i.e. replace the Gnu Make pattern rules as given above.
If you don't need names for object files but 1 source -> 1 executable you can use following approach:

MACRO(generate_floof floof_source)
	ADD_EXECUTABLE(${floof_source}_floof ${floof_source})

	# If you need kind of "project indicator" you may use following line
	# this will add -DFLOOF to every source file which is compiled into this target.
	SET_TARGET_PROPERTIES(${floof_source}_floof PROPERTIES DEFINE_SYMBOL FLOOF

	# If you must specify multiple defines, you can use following (but this is less portable)
	SET_SOURCE_FILES_PROPERTIES(${floof_source} PROPERTIES "-DFLOOF")
ENDMACRO(generate_floof)

Next, you can use this macro, for example following way:
SET(FLOOFS test.c test2.c)

FOREACH(myfloof ${FLOOFS})
	generate_floof(${myfloof})
ENDFOREACH(myfloof)

Of course, if you should have single -DFLOOF for whole CMakeLists.txt file, using ADD_DEFINITIONS(-DFLOOF) is preferable.

> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
> 




More information about the CMake mailing list