CMake允许在构建目标之前或之后(使用、和)add_custom_command()运行命令(通过选项) 。该命令可以是可执行文件或外部“脚本”,但不能是 CMake 宏/函数。COMMANDPRE_BUILDPRE_LINKPOST_BUILD
是否有类似的命令add_custom_command(),例如 ,它允许传递 CMake 函数/宏并在构建目标之前/之后安排它?如果没有,除了使用启用COMMAND函数/宏内部功能的标志来重新运行 CMake 之外,还有哪些选项?
[编辑] 函数/宏将 CMake 目标名称和目标特定目录路径作为输入。
该命令可以是 CMake 宏或函数。您只需将其封装在 CMake 文件中即可。CMake支持使用选项add_custom_command()将进一步的 CMake 代码作为脚本运行。您也可以使用该选项将参数传递给函数。对于这个例子,我们将传递两个参数,并且:-P-DTARGET_NAMETARGET_PATH
# Define the executable target.
add_executable(MyExecutable ${MY_SRCS})
add_custom_command(TARGET MyExecutable
POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DTARGET_NAME=MyExecutable
-DTARGET_PATH=${CMAKE_CURRENT_SOURCE_DIR}
-P ${CMAKE_SOURCE_DIR}/my_script.cmake
COMMENT "Running script..."
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
Run Code Online (Sandbox Code Playgroud)
该my_script.cmake文件可以包含一个或多个预定义的 CMake 函数,然后调用这些函数,如下所示:
include(my_function.cmake)
# Call the function, passing the arguments we defined in add_custom_command.
my_function(${TARGET_NAME} ${TARGET_PATH})
Run Code Online (Sandbox Code Playgroud)
为了完整起见,CMake 函数my_function可能如下所示:
# My CMake function.
function(my_function target_name target_path)
message("Target name: ${target_name}")
message("Target directory: ${target_path}")
endfunction()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2493 次 |
| 最近记录: |