我有一个cmake项目,其中包含许多不同的目标。首先,它构建一个可执行文件,用于处理一些数据文件(让我们调用这个DataProcessor)。然后它使用可执行文件处理这些数据文件。然后,构建第二个可执行文件(我们称之为MyApp),它与处理过的数据文件一起运行。
还有一个目标,它将所有已处理的数据文件和MyApp捆绑在一个tar文件中,以便我们分发它们。
在我的CMakeLists.txt文件中,我有以下几行:
get_target_property(DATA_PROCESSOR_EXE DataProcessor LOCATION)
get_target_property(MY_APP_EXE MyApp LOCATION)
我需要这些来在我的CMakeLists文件中运行各种其他命令。例如,DATA_PROCESSOR_EXE在custom命令中用于处理数据文件,如下所示:
add_custom_command(
OUTPUT ${DATA_OUT}
COMMAND ${DATA_PROCESSOR_EXE} -o ${DATA_OUT} ${DATA_IN}
DEPENDS DataProcessor)
当我将所有内容捆绑在一起时,我也会使用MyApp的位置:
# Convert executable paths to relative paths.
string(REPLACE "${CMAKE_SOURCE_DIR}/" "" MY_APP_EXE_RELATIVE
"${MY_APP_EXE}")
string(REPLACE "${CMAKE_SOURCE_DIR}/" "" DATA_PROCESSOR_EXE_RELATIVE
"${DATA_PROCESSOR_EXE}")
# The set of files and folders to export
set(EXPORT_FILES
assets
src/rawdata
${MY_APP_EXE_RELATIVE}
${DATA_PROCESSOR_EXE_RELATIVE})
# Create a zipped tar of all the necessary files to run the game.
add_custom_target(export
COMMAND cd ${CMAKE_SOURCE_DIR} && tar -czvf myapp.tar.gz ${EXPORT_FILES}
DEPENDS splat
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/..)
问题是尝试获取目标的位置属性现在会发出警告:
CMake Warning (dev) at CMakeLists.txt:86 (get_target_property):
Policy CMP0026 is not set: Disallow use of the LOCATION target property.
Run "cmake --help-policy CMP0026" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
The LOCATION property should not be read from target "DataProcessor".
Use the target name directly with add_custom_command, or use the generator
expression $<TARGET_FILE>, as appropriate.
我不明白我希望如何使用add_custome_command
或use the generator expression $<TARGET_FILE>
答案 0 :(得分:1)
add_custom_command
了解目标名称。您无需自己获取该位置。
替换
之类的代码 add_custom_command(
OUTPUT ${DATA_OUT}
COMMAND ${DATA_PROCESSOR_EXE}
代码如
add_custom_command(
OUTPUT ${DATA_OUT}
COMMAND DataProcessor
即,使用目标的名称。
在“配置时”,CMake在命令式代码上运行时,输出可执行文件的最终位置是未知的。但是,可以告诉add_custom_target
创建内容的构建规则,该内容仅在生成时(在配置时间之后)知道。这样做的方法是使用生成器表达式。
使用$&lt; TARGET_FILE_DIR:MyApp&gt;例如,在add_custom_target调用中代替$ {MY_APP_EXE_RELATIVE}。