[CMake] Generating Source Files

Jeremy Cowgar jeremy at cowgar.com
Fri Apr 3 15:48:54 EDT 2009


Hello,

I use CMake with quite a few simple projects, however, I am migrating 
one project that has a few Makefiles for different platforms/compilers. 
I am running into a problem with a custom command, it's output and how 
to link against it.

Here is the situation. I have 1 file, say parser.e. It will be 
translated into C. However, the tool doing the translating will split 
the file into many C files if too large, but more than that, parser.e 
may include common.e, version.e, johndoe.e. The translator will also 
translate all of those. So, from parser.e, I wind up with parser_1.c, 
parser_2.c, common.c, version.c, johndoe.c. Now, someone ads a feature 
to parser.e that requires regex.e, well, now the translation will 
include regex_1.c, regex_2.c. So, the thing is, I don't want to maintain 
that parser.e translates into XYZ, ABC files because it's in such a 
state of flux. So, I did this:

FILE( MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/int )
ADD_CUSTOM_COMMAND(
  OUTPUT int/main.c
  DEPENDS ${EU_CORE_FILES} ${EU_INTERPRETER_FILES}
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/int
  COMMAND ${TRANSLATOR} ${CMAKE_CURRENT_SOURCE_DIR}/int.ex
)
FILE( GLOB EU_INTERPRETER_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/int/*.c" )

That makes the directory where the translated files will go, adds a 
custom command with the output set to int/main.c which I know will 
*always* exist after translation. Then, I set the source file list that 
I can later link into the main application via FILE( GLOB ... ).

The problem with this is, it seems that FILE( GLOB .. ) is running 
before the custom command, thus, the first time I try to build, I get 
all sorts of linking problems because EU_INTERPRETER_SOURCES is empty. 
Now, the next time I run, it finds the files and things link properly.

How can I handle this situation?

Thank you,

Jeremy Cowgar



More information about the CMake mailing list