我正在写一个C ++项目,该项目使用代码生成器(perl的xsubpp)。 它产生从XS文件C / C ++源代码。 作为xsubpp有时会产生不完整的输出文件,我想拥有它运行实际的二进制目标是建立之前,无论存在生成的源文件。
我可以找出两种方式来实现这一目标:
# the target is always out-of-date, so the command is always run
add_custom_target(...)
add_library(lib_name ...)
add_dependencies(lib_name ...)
和
add_library(lib_name ...)
# the command is always run before lib_name is build
add_custom_command(TARGET lib_name PRE_BUILD ...)
然而,他们没有工作,因为add_library()
在配置时检查源文件。 源文件必须要么存在,或作为输出对象add_custom_command()
对于第一种方式,所述add_custom_target()
没有输出对象的概念; 和用于第二方式, add_custom_command()
被用作lib_name的辅助,这也没有输出对象的概念。
以下为我工作。 我希望这是你想要的。
源(Foo.cpp中)被重新生成每次运行腾出时间。
SRC / C_generated /的CMakeLists.txt:
add_custom_target(generate_foo
touch ${CMAKE_CURRENT_SOURCE_DIR}/script.sh
COMMENT "add_custom_target, touch script.sh"
)
ADD_CUSTOM_COMMAND(
TARGET generate_foo
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/script.sh
ARGS "some args"
COMMENT "custom commands, executing script.sh"
)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/foo.cpp PROPERTIES GENERATED 1)
add_library(LIBC ${CMAKE_CURRENT_BINARY_DIR}/foo.cpp)
ADD_DEPENDENCIES(LIBC generate_foo)
SRC / C_generated / script.sh:
#!/bin/bash
echo "Running script.sh"
echo "#include <stdio.h>" > foo.cpp
echo "/*" >> foo.cpp
date >> foo.cpp
echo "*/" >> foo.cpp
echo >> foo.cpp
echo "void testC()" >> foo.cpp
echo "{" >> foo.cpp
echo " printf(\"Generated source.\");" >> foo.cpp
echo "}" >> foo.cpp
主要的CMakeLists.txt其结合与非生成的源生成的源:
project(test)
cmake_minimum_required(VERSION 2.8)
INCLUDE_DIRECTORIES(src)
ADD_SUBDIRECTORY(src/A)
ADD_SUBDIRECTORY(src/B)
# Generated files only.
ADD_SUBDIRECTORY(src/C_generated)
# Combine the different libraries into one.
add_library(TESTLIB STATIC src/dummy.c)
ADD_DEPENDENCIES(TESTLIB LIBA)
ADD_DEPENDENCIES(TESTLIB LIBB)
ADD_DEPENDENCIES(TESTLIB LIBC)
GET_TARGET_PROPERTY(LIBA_LOC LIBA LOCATION)
GET_TARGET_PROPERTY(LIBB_LOC LIBB LOCATION)
GET_TARGET_PROPERTY(LIBC_LOC LIBC LOCATION)
SET_TARGET_PROPERTIES(TESTLIB PROPERTIES STATIC_LIBRARY_FLAGS "${LIBA_LOC} ${LIBB_LOC} ${LIBC_LOC}")
从下载例如:
https://dl.dropboxusercontent.com/u/68798379/cmake-code-generator.tar.bz2