在CMake中添加自定义构建步骤

时间:2022-06-03 16:32:54

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

我正在尝试在CMake中添加一个定制的构建步骤,以生成一些文件。我还没有找到它是如何工作的。

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.

我有一个项目,需要ODB为c++生成源文件、头文件和实现文件。ODB将类头作为参数,并生成我想在项目中使用的源文件。

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

现在我在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.

对一个文件的人。hpp ODB应该生成person-odb。hxx person-odb。cxx person-odb。但是我使用的CMake命令没有生成任何东西。在终端中,这个命令可以正常工作。

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})

2 个解决方案

#1


14  

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.

DEPENDS选项指定必须在构建给此选项的项目之后才能执行命令。

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.

注意,PRE_BUILD选项只支持Visual Studio 7或更高版本。对于所有其他生成器,PRE_BUILD将被视为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
  • 在其上添加自定义命令作为POST_BUILD
  • Make you current project dependent on the fake one
  • 让你当前的项目依赖于虚假的项目

#2


3  

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.

关键是将.hpp文件添加到add_executiveblock中。

#1


14  

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.

DEPENDS选项指定必须在构建给此选项的项目之后才能执行命令。

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.

注意,PRE_BUILD选项只支持Visual Studio 7或更高版本。对于所有其他生成器,PRE_BUILD将被视为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
  • 在其上添加自定义命令作为POST_BUILD
  • Make you current project dependent on the fake one
  • 让你当前的项目依赖于虚假的项目

#2


3  

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.

关键是将.hpp文件添加到add_executiveblock中。