我有一个包含以下目录树的CMake项目:
build/
assets/
dest/
<other files>
dest
是所有已安装文件的目录:
可执行文件,使用简单dest/
转到make
,由CMAKE_RUNTIME_OUTPUT_DIRECTORY
位于assets/
的资产,在dest/
之后转到make install
。
但我不想发布make install
来复制dest/
目录中的所有文件:我想要一个简单的make
来执行此操作。
从这个意义上说,如何将install
目标添加到默认目标(all
)?或者,有没有更好的方法来解决这个问题?
答案 0 :(得分:3)
这将在构建install
(可能是<target_name>
)后执行all
目标:
add_custom_command(
TARGET <target_name>
POST_BUILD
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target install
)
答案 1 :(得分:1)
使用以下内容不会导致递归。需要CMake> = 3.15。
add_custom_command(
TARGET ${MY_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --config $<CONFIG>
)
其他:您可能想提供默认的(本地)安装路径,这样在Windows上就不会失败。
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "install" CACHE PATH "Default install path." FORCE)
endif()
答案 2 :(得分:0)
使用CMake的常规方法是在项目外创建一个构建目录,并将所有已编译的二进制文件放在那里。完成开发并希望在系统中安装一些二进制文件后,请调用make install
。通过这种方式,您可以使项目源文件夹不受所有编译器生成的内容的影响。
示例文件目录结构:
my_project/
my_project_build/
从my_project_build 调用cmake ../my_project
来生成构建文件。调用make
构建它,所有二进制文件都在my_project_build
。
答案 3 :(得分:0)
我通过使用以下宏为指定目标生成数据依赖性规则来解决此问题。这些规则只需要make
而不是make install
。
宏将源树中的任意文件映射到“临时”树,其中每个树结构可能不同,您可以选择重命名该文件。分阶段数据保持最新,因此如果您在源代码树中更改它,则会在下次make
时更新。
# Macro used to create dependencies for staging data files (e.g. config files, assets) and keeping them up-to-date.
# The given "source" file is copied (and possibly renamed) to "staged" for the given "target".
#
# It works by creating a rule that creates "staged" by copying "source", then creating a target that depends upon "staged",
# then making the given "target" depend upon the new target. Or in makefile speak:
#
# staged: source
# cp source staged
#
# targetData1: staged
#
# target: <existing dependencies...> targetData1
# <existing commands...>
#
# The intermediate rule is used for parallel build robustness. For details, see:
# http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_custom_command
#
# Example:
# target = myExeTarget
# source = "${CMAKE_CURRENT_SOURCE_DIR}/../../data/images/bush1.png"
# staged = "${STAGING_DATA_DIR}/images/bush1.png"
macro(add_data_dependency target source staged)
add_custom_command(
OUTPUT "${staged}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${source}" "${staged}" # Dir(s) will be created if needed.
DEPENDS "${source}"
)
if (NOT DEFINED add_data_dependency_counter)
#message(status "Setting tmp counter......")
set(add_data_dependency_counter "0")
endif()
math(EXPR add_data_dependency_counter "${add_data_dependency_counter} + 1")
#message(status "tmp counter is ${add_data_dependency_counter}......")
set(customTarget "${target}Data${add_data_dependency_counter}")
add_custom_target(${customTarget} DEPENDS "${staged}")
add_dependencies(${target} ${customTarget})
endmacro()
在您的情况下,使用方式如下:
add_data_dependency(myTarget "${CMAKE_CURRENT_SOURCE_DIR}/assets/file1.ext" "${CMAKE_CURRENT_SOURCE_DIR}/dest/file1.ext")
add_data_dependency(myTarget "${CMAKE_CURRENT_SOURCE_DIR}/assets/file2.ext" "${CMAKE_CURRENT_SOURCE_DIR}/dest/file2.ext")
add_data_dependency(myTarget "${CMAKE_CURRENT_SOURCE_DIR}/assets/rename_me.ext" "${CMAKE_CURRENT_SOURCE_DIR}/dest/renamed.ext")
:
:
然后,无论何时make
,如果文件丢失或过期,都会被复制(假设myTarget
属于all
)。