是否可以将安装后命令添加到cmake生成的顶级Makefile中?

joa*_*ast 14 cmake

cmake为安装规则生成如下内容:

# Special rule for the target install
install: preinstall
        @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
        /usr/local/bin/cmake -P cmake_install.cmake
.PHONY : install
Run Code Online (Sandbox Code Playgroud)

我想要做的是在调用cmake_install.cmake后执行一些自定义命令,因此它看起来像:

# Special rule for the target install
install: preinstall
        @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
        /usr/local/bin/cmake -P cmake_install.cmake
        post_install_command_1
        ...
        post_install_command_n
.PHONY : install
Run Code Online (Sandbox Code Playgroud)

对于我们编写的内容(6-10个要更新的宏),我可以使用"add_custom_command(TARGET ... POST_BUILD ...)"执行我想要的操作.但是,有很多第三方的东西安装完毕,我真的不想为所有这些东西添加POST_BUILD自定义命令(目前有19个项目有更多的东西,很难确定需要处理什么建成后而不是安装后).我认为如果自定义命令仅在一个地方使用(即作为安装处理的最后一部分)并且我知道他们将完成所有必要的操作,那么维护会更容易.

是否有可能让cmake将命令添加到顶级Makefile的安装规则中?

sak*_*kra 10

您可以使用install命令的变体SCRIPTCODE变体.如果将所需命令放在项目根目录的脚本中,请将以下调用添加到最外层:PostInstall.cmakeCMakeLists.txt

install (SCRIPT "${CMAKE_SOURCE_DIR}/PostInstall.cmake")
Run Code Online (Sandbox Code Playgroud)

install命令cmake_install.cmake按顺序添加到脚本中,因此应CMakeLists.txt在所有其他安装完成后将调用添加到运行结束.

  • 这对我的子目录构建不起作用.在从子目录中包含所有cmake_install.cmake之前,执行最外层CMakeLists.txt的结尾. (5认同)

ric*_*usa 6

要添加安装后步骤,您需要在顶级 CMakeLists.txt 添加一个目录。您必须有一个包含 CMakeLists.txt 的目录,以便设置在安装中最后执行的安装后步骤。

第一步是添加安装后脚本要使用的变量和值。构建过程中可用的变量在安装后都不可用,因此必须在此处设置您需要的一切。

在顶级 CMakeLists.txt 中,在执行完所有先前的 add_subdirectory 命令后,添加类似这样的内容。

# Workaround for the lack of post_install steps.
# add_subdirectory is executed in order, this one must be last.
if(CMAKE_PROGRAM_PREFIX)
    # Make sure this is the LAST directory added.
    add_subdirectory(${CMAKE_SOURCE_DIR}/cmake/postinstall)
    # Add any variables you need during post install.
    install(CODE "set(CMAKE_PROGRAM_PREFIX \"${CMAKE_PROGRAM_PREFIX}\")")
    # Add any properties to your post install.
    get_property(PROGRAM_PREFIX_FILES GLOBAL PROPERTY PROGRAM_PREFIX_FILES)
    install(CODE "set(PROGRAM_PREFIX_FILES \"${PROGRAM_PREFIX_FILES}\")")
endif()
Run Code Online (Sandbox Code Playgroud)

现在我们有了变量,并将属性转换为可在安装后使用的变量。

接下来我们需要安装后目录中的 CMakeLists.txt 文件。Cmake 将在构建结束时执行此文件。那时我们会安装一个 SCRIPT 来完成安装后的工作。

# CMake will execute this last in the build.
# Install the script that does the post install work.
install(SCRIPT "${CMAKE_SOURCE_DIR}/cmake/postinstall/ProgramPrefix.cmake")
Run Code Online (Sandbox Code Playgroud)

现在我们将在 ProgramPrefix.cmake 中在安装后获得控制权。CMake 将添加我们之前设置的变量。

# Make sure this was requested.
if(CMAKE_PROGRAM_PREFIX)
    # CMake builds a manifest of all files it has installed.
    foreach(file ${CMAKE_INSTALL_MANIFEST_FILES})
        # Make a list of installed files to compare.
        get_filename_component(nm ${file} NAME)
        list(APPEND fileindex ${nm})
    endforeach()

    # Process program prefix files.
    foreach(nm ${PROGRAM_PREFIX_FILES})
        list(FIND fileindex ${nm} efound)
        # Did we match a manifest file with our list of files?
        if(NOT efound LESS 0)
            # Process the file.
            program_prefix_file(${efound})
        endif()
    endforeach()
endif()
Run Code Online (Sandbox Code Playgroud)

实际执行程序前缀还有一些工作要做,但是此框架将允许您在安装完所有内容后执行 cmake 命令。