cmake的"add_custom_command"预处理头文件? [英] cmake 'add_custom_command' to pre-process header files?
问题描述
我正在从事需要cmake的项目.我想在我的makefile文件中添加一些自定义规则,但不能完全理解该怎么做.
i'm working on a project requiring cmake. i'd like to add some custom rules to my makefile, but can't quite get my head around how to do it.
c源文件和头文件都在同一目录中.在同一目录中也有许多 .def
文件,它们是编译过程中源文件中#include的某些头文件的源文件.
both c source files and header files are in the same directory. also in this same directory are a number of .def
files, which are the sources for some of the header files #included in the source during compilation.
如果我要在Makefile中执行此操作,我会使用一条简单的规则,例如
if i were to do this in a makefile, i'd use a simple rule like
.SUFFIXES: .def
.def.h:
$(PREPROC) $< > $@
我该如何用cmake做到这一点?
how can i do this with cmake ??
我尝试了以下各种排列,无论是否包含cmake工作目录规范:
i've tried various permutations of the following, both with and without cmake working directory specifications :
add_custom_command(
OUTPUT vvr_const.h
PRE_BUILD
COMMAND preproc vvr_const.def > vvr_const.h
DEPENDS vvr_const.def
)
add_custom_target(vvr_const.h DEPENDS vvr_const.def)
但是在编译c源文件时尚未生成头文件,因此编译失败.我还尝试了一种变体,其中我将上面的最后一行替换为
but the header file isn't generated by the time the c source file is compiled, so the compile fails. i've also tried a variation where i replace the last line above with
set_property(SOURCE main.c APPEND PROPERTY OBJECT_DEPENDS vvr_const.h)
在这种情况下,头文件是事先正确生成的,但是 make
无法找到它,并抱怨没有规则来创建目标 .h
in this case, the header file is correctly generated in advance, but make
can't find it, and complains that there's no rule to make the target .h
.
理想情况下,这将是一条通用规则,就像上面的 make
规则一样,但是如果出现以下情况,我不反对为每个 .def
文件制定单独的规则就是这样.
ideally this would be a general rule, like the make
rule above, but i'm not opposed to making a separate rule for each of the .def
files if that's what it takes.
欢呼.
推荐答案
您提出的 add_custom_command
方法有两个问题:
There are 2 problems with the add_custom_command
approach you present:
- 您未指定工作目录;默认情况下,该命令在构建目录中运行,而不在源目录中运行.
- 您在这里依靠外壳程序功能(重定向到文件).即使这可能仍然有效.您应该采用不依赖于Shell的方法.
要解决问题1和2,我建议创建一个单独的cmake脚本文件,以接收输入和输出文件的绝对路径,并在custom命令中使用它们.这样,您就可以使用 execute_process
来指定要写入的文件,而无需依赖平台.
To solve issues 1 and 2 I recommend creating a seperate cmake script file receiving the absolute paths to input and output files and using those in the custom command. This allows you to use execute_process
to specify the file to write without relying on the platform.
preprocess_def.cmake
# preprocess def file
# parameters INPUT_FILE and OUTPUT_FILE denote the file to use as source
# and the file to write the results to respectively
# use preproc tool to get data to write to the output file
execute_process(COMMAND preproc "${INPUT_FILE}"
RESULT_VARIABLE _EXIT_CODE
OUTPUT_FILE "${OUTPUT_FILE}")
if (_EXIT_CODE)
message(FATAL_ERROR "An error occured when preprocessing the file ${INPUT_FILE}")
endif()
CMakeLists.txt
set(_INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vvr_const.def")
set(_OUTPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vvr_const.h")
# not necessary to use build event here, if we mark the output file as generated
add_custom_command(OUTPUT "${_OUTPUT_FILE}"
COMMAND "${CMAKE_BUILD_TOOL}" -D "OUPUT_FILE=${_OUTPUT_FILE}" -D "INPUT_FILE=${_INPUT_FILE}" -P "${CMAKE_CURRENT_SOURCE_DIR}/preprocess_def.cmake"
DEPENDS "${_INPUT_FILE}")
add_executable(my_target vvr_const.h ...)
set_source_files_properties(vvr_const.h PROPERTIES GENERATED 1)
这篇关于cmake的"add_custom_command"预处理头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!