2

I'm working on a project that is partially generated from an XSD schema using codesynthesis. I have used the FindXSD.cmake at the following address to have CMake properly search for the XSD header: http://wiki.codesynthesis.com/uploads/8/86/FindXSD.cmake.gz

This FindXSD.cmake sets the XSD_EXECUTABLE variable, which I would like to use to also generate the code from the schema prior to building any target (the main target of the project is a library), but being unfamiliar with CMake, I have a hard time understanding how to integrate such a custom command.

Here is what I did so far:

add_library (mylibrary ${MY_PROJECT_SRC})

add_custom_command(TARGET mylibrary PRE_BUILD
                COMMAND "${XSD_EXECUTABLE} cxx-tree --type-naming knr 
                -- hxx-suffix .hpp --cxx-suffix .cpp 
                ${MY_PROJECT_SOURCE_DIR}/src/model/Model.xsd")

But it doesn't seem to do anything and besides, if it did, I wouldn't know where the files are generated (the command should generate Model.hpp and Model.cpp), so I don't know what commands to add to have Model.cpp being compiled within the target library and Model.hpp being found by other source files that require it.

4

1 回答 1

3

add_custom_target()您可以使用withALL关键字实现此目的:

add_custom_target(xsdgen
                  COMMAND ${XSD_EXECUTABLE} cxx-tree --type-naming knr
                  --hxx-suffix .hpp --cxx-suffix .cpp
                  ${MY_PROJECT_SOURCE_DIR}/src/model/Model.xsd)

这会将这个命令添加到所有目标的列表中。现在您需要确保此目标将在mylibrary之前构建:

add_dependencies(mylibrary xsdgen)

还有另一种方法 - 使用add_custom_command来定义产生Model.hpp和的规则Model.cpp。在这种情况下,CMake 将为您计算依赖项:每当您Model.{c,h}pp用作add_{library, executable}参数时,CMake 都会将您的自定义命令设置为目标的依赖项。

实际上,这是生成文件之类的首选方式,因为它允许 CMake 跳过已经生成的文件。相反,add_custom_target每次运行时都会构建make.

于 2013-07-25T09:34:31.793 回答