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

Alexander Neundorf a.neundorf-work at gmx.net
Thu May 15 12:17:40 EDT 2008


On Thursday 15 May 2008, 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)
>
> 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.

You do this by writing a macro or function which you can then use for all 
files of one type.
Something like that: (I didn't test)

macro(gen_floof name)
  #make name uppercase here using STRING() and then use that for the define:
  add_custom_command(OUTPUT ${name}_floof.c
                            COMMAND echo '\#define FLOOF 1' >
   ${EXAMPLE_BINARY_DIR}/test_${name}.c
                            COMMAND cat ${EXAMPLE_SOURCE_DIR}/${name}.c >>
  ${EXAMPLE_BINARY_DIR}/${name}_floof.c
                           DEPENDS ${EXAMPLE_SOURCE_DIR}/${name}.c
  )

  ADD_EXECUTABLE(test_floof ${EXAMPLE_BINARY_DIR}/test_floof.c)
  TARGET_LINK_LIBRARIES(test_floof floof)
endmacro(gen_floof name)


Alex


More information about the CMake mailing list