1

I want to have something in CMake that will be executed whenever I enter make

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build_date.cc
    PRE_BUILD
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/mk_build_date.py 
            ${CMAKE_CURRENT_BINARY_DIR}/build_date.cc
)
add_custom_target(build-date-xxx 
                  ALL
                  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build_date.cc)

thats what I'm currently doing. unfortunately make build-date-xxx will generate the file only once.

even without the add_custom_target declaration the file is only build once.

the result should be something like this in GNU Make

.PHONY all: 
    echo "hallo welt"
all: foo.c bar.c
%.c:
    touch $@

in that makefile whenever make is entered. since all is the first target it will always be invoked and the custom command echo "hallo welt" is actually executed.

4

2 回答 2

1

尝试使用 ADD_CUSTOM_TARGET 并在其中使用参数 ALL。然后让你的主要目标依赖于这个自定义目标。

于 2013-07-17T12:03:02.827 回答
1

颠倒你的顺序......有一个没有依赖关系的自定义目标(no DEPENDS)生成你的文件,并添加一个依赖于这个目标的自定义命令,提到它OUTPUT是文件,实际上不做任何事情(例如COMMAND ${CMAKE_COMMAND} -E echo)。然后在某处提及输出文件(大概您将其作为库或可执行文件的源)。(您也可以ALL用于自定义目标,但我假设某些代码对象实际使用输出文件,因此您希望所述代码对象依赖于输出文件。)

理想情况下,您希望避免修改文件,除非实际发生了变化,否则您将永远无法获得无操作构建。(如何做到这一点留给读者作为练习。)

于 2015-10-16T17:42:39.590 回答