使用shell命令作为INSTALL_COMMAND到ExternalProject_Add

时间:2015-05-13 03:54:00

标签: shell cmake external-project

是否可以对cmake INSTALL_COMMAND的{​​{1}}阶段使用任何shell命令? e.g。

ExternalProject_Add

我注释掉了我想使用的INSTALL_COMMAND,似乎不允许使用find和cp,“没有这样的文件或目录,错误127”是使用它的结果。

1 个答案:

答案 0 :(得分:2)

直接答案:

install(CODE "execute_process(...)")
  

SCRIPT和CODE签名:

     

install([[SCRIPT <file>] [CODE <code>]] [...])

     

SCRIPT表单将在安装期间调用给定的CMake脚本文件。如果脚本文件名是相对路径,则将根据当前源目录解释它。 CODE表单将在安装期间调用给定的CMake代码。代码在双引号字符串中指定为单个参数。例如,代码

     

install(CODE "MESSAGE(\"Sample install message.\")")

     

将在安装过程中打印一条消息。

深思熟虑:

你可能急于快速完成任务。如果你有时间,或者你可以回到它并修复它,请考虑这个。

人们喜欢cmake的一个主要原因是因为它具有跨平台性质。正确编码此方面的任何项目都可以在Linux或Windows或任何其他受操作支持的系统上使用。如果开发人员考虑到这一点,各种发电机将愉快地工作。我的建议是以跨平台的方式将shell命令转换为cmake,将它们放在一个单独的* .cmake文件中,并使用cmake -E选项执行它们。

以下是我过去曾参与的一个工作项目的摘录。

<强> project_build_steps.cmake

message(VAR1=${VAR1}) # These variables can be passed from the invocation place
message(VAR2=${VAR2}) # You can use them in the build steps

if("${BUILD_STEP}" STREQUAL "patch")
    message("BUILD_STEP: patch")
    # Put your patch steps using cmake
endif()
if("${BUILD_STEP}" STREQUAL "configure")
    message("BUILD_STEP: configure")
    # Put your configure steps using cmake
endif()

if("${BUILD_STEP}" STREQUAL "build")
    message("BUILD_STEP: build")
    # Put your build steps using cmake
endif()

if("${BUILD_STEP}" STREQUAL "install")
    message("BUILD_STEP: install")
    # Put your install steps using cmake
endif()

CMakeLists.txt(选项1)

set(CMAKE_COMMAND /usr/bin/cmake)
set(PROJECT_BUILD_STEPS_FILE project_build_steps.cmake)

ExternalProject_Add(
    project_name
    SOURCE_DIR /path/to/project/source
    PATCH_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=patch -P ${PROJECT_BUILD_STEPS_FILE}
    CONFIGURE_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=configure -P ${PROJECT_BUILD_STEPS_FILE}
    BUILD_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=build -P ${PROJECT_BUILD_STEPS_FILE}
    INSTALL_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=install -P ${PROJECT_BUILD_STEPS_FILE}
)

如果您不想使用ExternalProject_Add,可以使用以下内容。这也将为您提供单独的构建目标,如make project_patch,make project_configure,make project_build,make project_install。

CMakeLists.txt(选项2)

set(CMAKE_COMMAND /usr/bin/cmake)
set(PROJECT_BUILD_STEPS_FILE project_build_steps.cmake)

set(STAMP_FILE_PROJECT_PATCH .project_patch_done)
add_custom_command(
    OUTPUT ${STAMP_FILE_PROJECT_PATCH}
    COMMAND ${CMAKE_COMMAND} -DVAR1=value1 -DSTEP=patch -P ${PROJECT_BUILD_STEPS_FILE}
    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_PATCH}
)

add_custom_target(project_patch DEPENDS ${STAMP_FILE_PROJECT_PATCH})

set(STAMP_FILE_PROJECT_CONFIGURE .project_configure_done)
add_custom_command(
    OUTPUT ${STAMP_FILE_PROJECT_CONFIGURE}
    COMMAND ${CMAKE_COMMAND} -DSTEP=configure -P ${PROJECT_BUILD_STEPS_FILE}
    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_CONFIGURE}
)

add_custom_target(project_configure DEPENDS project_patch ${STAMP_FILE_PROJECT_CONFIGURE})


set(STAMP_FILE_PROJECT_BUILD .project_build_done)
add_custom_command(
    OUTPUT ${STAMP_FILE_PROJECT_BUILD}
    COMMAND ${CMAKE_COMMAND} -DSTEP=build -P ${PROJECT_BUILD_STEPS_FILE}
    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_BUILD}
    VERBATIM
)

add_custom_target(project_build DEPENDS project_configure ${STAMP_FILE_PROJECT_BUILD})
set(STAMP_FILE_PROJECT_INSTALL .project_install_done)
add_custom_command(
    OUTPUT ${STAMP_FILE_PROJECT_INSTALL}
    COMMAND ${CMAKE_COMMAND} -DSTEP=install -P ${PROJECT_INSTALL_STEPS_FILE}
    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_INSTALL}
    VERBATIM
)

add_custom_target(project_install DEPENDS project_build ${STAMP_FILE_PROJECT_INSTALL})