2

Good day everyone.

I have the following situation: I have a CMake file, which is supposed to compile my application, which consists of:

  1. one or more cpp files
  2. some template files (ecpp), which on their turn are generated into cpp files, which are compiled into the application (they are listed below in the WEB_COMPONENTS so for each component there is the associated .ecpp file and the .cpp that will be generated from it).

And here is the CMakeLists.txt (simplified)

cmake_minimum_required (VERSION 2.6)
set (PROJECT sinfonifry)
set (ECPPC /usr/local/bin/ecppc)
set (WEB_COMPONENTS 
     images 
     menu
     css
)

set(${PROJECT}_SOURCES
  ""
  CACHE INTERNAL ${PROJECT}_SOURCES
)

foreach(comp ${WEB_COMPONENTS})
  list(APPEND ${PROJECT}_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${comp}.cpp )

  execute_process(COMMAND ${ECPPC} -o ${CMAKE_CURRENT_BINARY_DIR}/${comp}.cpp -v
                  ${CMAKE_CURRENT_SOURCE_DIR}/${comp}.ecpp 
                  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_QUIET
                 )
endforeach()

list(APPEND ${PROJECT}_SOURCES main.cpp )
add_executable(${PROJECT}_exe ${${PROJECT}_SOURCES})
target_link_libraries(${PROJECT}_exe cxxtools dl tntnet tntdb)

Now, what happens: for the very first time (ie: make the build directory, run cmake-gui, select web component, configure, generate, make) the CMake nicely executes the ${ECPPC} command, ie. it generates the required CPP files in the binary directory, and links them together.

After a while, obviously while I work, I modify one of the component files (such as images.ecpp) and run make again in the build directory. But now, CMake does not pick up the changes of the ecpp files. I have to go to cmake-gui, delete cache, restart everything from zero. This is very tiresome and slow.

So, two questions:

  1. Cand I tell CMake to track the changes of the images.ecpp and call the ${ECPPC} compiler on it if it changed?

  2. How can I make clean so that it also removes the generated cpp files.

Thank you for your time, f.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167

1 Answers1

5

Instead of execute_process() you want to use add_custom_command(). See here: https://stackoverflow.com/a/2362222/4323

Basically you tell CMake the OUTPUT (the generated filename), COMMAND, and DEPENDS (the .ecpp filename). This makes it understand how to turn the source into the necessary C++ generated file. Then, add the generated file to some target, e.g. add_executable(), or to an add_custom_command() dependency (if it didn't need to be compiled you'd more likely need that).

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    Excellent. Just to share the command: `add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${comp}.cpp COMMAND ${ECPPC} -o ${CMAKE_CURRENT_BINARY_DIR}/${comp}.cpp -v -n ${comp} ${CMAKE_CURRENT_SOURCE_DIR}/${comp}.ecpp DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${comp}.ecpp)` – Ferenc Deak Apr 25 '13 at 06:13