Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom build step in CMake

I'm trying to add a custom build step in CMake that generates some files. I haven't found a description how it works.

I have an project where source, header & implementation files have to be generated by ODB for C++. ODB takes class headers as arguments and generates source files that I want to use in my project.

Right now I have the following command in my CMakeLists.txt:

add_custom_command(TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND odb -o /home/david/dev/ --std c++11 -I/home/david/dev/ -d sqlite --generate-    query --generate-schema ${PROMOTER_LIB_PREFIX}/entities/person.hpp
    DEPENDS ${PROJECT_NAME}
    VERBATIM
)

For a file person.hpp ODB should generate person-odb.hxx, person-odb.cxx, person-odb.ixx but the CMake command I''ve used doesn't generate anything. In a terminal this command works fine.

What am I doing wrong?

EDIT: The problem can be solved by adding the following lines:

set(FAKE_TARGET fakeTarget)
add_custom_target(fakeTarget
    odb -o /home/david/dev/ --std c++11 -I/home/david/dev/ -d sqlite --generate-query --generate-schema ${PROMOTER_LIB_PREFIX}/entities/person.hpp
)
add_dependencies(${PROJECT_NAME} ${FAKE_TARGET})
like image 975
David Bulczak Avatar asked Aug 25 '13 10:08

David Bulczak


People also ask

How do I set up a build on CMake?

Run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool. Run the install step by using the install option of the cmake command (introduced in 3.15, older versions of CMake must use make install ) from the command line, or build the INSTALL target from an IDE.

Does CMake create makefiles?

CMake is a generator of build systems that can produce Makefiles for Unix like systems, Visual Studio Solutions for Windows and XCode projects for Mac OS. All these from the same base – a single CMakeLists. txt file.

How do I add a target in CMake?

Adds a target with the given name that executes the given commands. The target has no output file and is always considered out of date even if the commands try to create a file with the name of the target. Use the add_custom_command() command to generate a file with dependencies.

What is add_custom_command?

The add_custom_command() documentation says "This defines a command to generate specified OUTPUT file(s). A target created in the same directory (CMakeLists. txt file) that specifies any output of the custom command as a source file is given a rule to generate the file using the command at build time."


2 Answers

For me, with something similar, I just use :

add_custom_command(TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND odb -o /home/david/dev/ --std c++11 -I/home/david/dev/ -d sqlite --generate-    query --generate-schema ${PROMOTER_LIB_PREFIX}/entities/person.hpp
)

We don't use DEPENDS or VERBATIM.

The DEPENDS option specify that the command must be executed only after that the project you gave to this option is built.

EDIT :

Note that the PRE_BUILD option is only supported on Visual Studio 7 or later. For all other generators PRE_BUILD will be treated as PRE_LINK.

Maybe that's why it doesn't work for you.

A work around could be (a bit ugly) :

  • Create a fake project
  • Add your custom command on it as POST_BUILD
  • Make you current project dependent on the fake one
like image 186
Pierre Fourgeaud Avatar answered Oct 24 '22 09:10

Pierre Fourgeaud


Way I'm using it is:

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/gen_icinstrtab.hpp
    COMMAND xsltproc --output ${CMAKE_CURRENT_BINARY_DIR}/gen_icinstrtab.hpp ${CMAKE_SOURCE_DIR}/xml/genictabc.xslt ${CMAKE_SOURCE_DIR}/xml/icminstr.xml
)

add_executable(
    du4

    ${CMAKE_CURRENT_BINARY_DIR}/gen_icinstrtab.hpp
    .
    .
    .
)

The key was to add even .hpp files into add_executable block.

like image 3
graywolf Avatar answered Oct 24 '22 10:10

graywolf