Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以将安装后命令添加到CMake生成的顶级Makefile中?_Cmake - Fatal编程技术网

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

是否可以将安装后命令添加到CMake生成的顶级Makefile中?,cmake,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 我想做的是在cm

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
我想做的是在
cmake\u安装后执行一些自定义命令。调用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
我可以使用
add_custom_命令(TARGET…POST_BUILD…
为我们编写的内容(需要更新6-10个宏)执行我想要的操作。但是,有很多第三方的东西需要安装,我真的不想为所有这些东西添加
POST_BUILD
自定义命令(目前有19个项目,还有更多项目要来,很难确定在构建后而不是安装后需要处理什么)。我认为,如果自定义命令只在一个地方使用(即作为安装处理的最后一部分),并且我知道它们将执行所有必要的操作,那么维护起来就会容易得多


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

您可以使用该命令的
脚本或
code
变体。如果将所需命令放入项目根目录中的脚本
PostInstall.cmake
,请将以下调用添加到最外层的
CMakeLists.txt

install (SCRIPT "${CMAKE_SOURCE_DIR}/PostInstall.cmake")

install
命令按顺序添加到
cmake_install.cmake
脚本中,因此调用应添加到
CMakeLists.txt
的末尾,以便在所有其他安装完成后运行。

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

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

在顶级的CMakeLists.txt中,在执行了所有之前的add_子目录命令之后,添加如下内容

# 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()
现在,我们有了变量,并将属性转换为变量,以便在安装后使用

# 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")
接下来,我们需要postinstall目录中的CMakeLists.txt文件。Cmake将在构建结束时执行此文件。当时,我们安装了一个脚本,该脚本在安装后工作

# 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")
现在,我们将在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()

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

谢谢!我发誓我试过这样的东西,但没用。再试一次,它似乎有效。我猜我一定是因为打字错误(或其他原因)使它无法工作。这对我的子目录构建不起作用。最外层的CMakeLists.txt的结尾在所有cmake_安装之前执行。cmake包含在子目录中。这有点棘手,特别是关于从根目录指定子目录安装路径和目标,但最终使其工作。我不理解有关程序前缀的内容。这有关系吗?或者只是一个随机代码示例,说明如何在安装后运行脚本?我想在支持autoconf和cmake构建的树中添加一个程序前缀函数,如autoconf。程序前缀就是一个例子。cmake安装后步骤可用于任何用途。