问题描述
作为安装后挂钩,我需要执行一个安装命令,例如
install(CODE execute_process(COMMAND some_command $ {CMAKE_INSTALL_PREFIX} / some_folder))
会在中创建文件some_folder
基于先前安装到 some_folder
的文件(它会编译这些文件的索引/缓存)。
这对于安装目标来说效果很好,但是,一旦使用cpack $ {CMAKE_INSTALL_PREFIX}
不再是正确的位置。
是否有像$ {CMAKE_CURRENT_INSTALL_PREFIX}这样的变量始终指向当前安装目录,而不管是否使用了默认安装目标或cpack且可用于此目的? p>
我看到的唯一替代方法是尝试在较早的阶段对原始文件执行命令,创建一个临时文件并安装该临时文件。不幸的是,这更容易出错,因为 some_command
应该在安装后在最终文件上运行(以创建有效的缓存)
答案非常简单(IRC的Nils Gladitz表示敬意):
用反斜杠转义变量 $ {CMAKE_INSTALL_PREFIX}
会延迟其扩展,直到安装时它也保持正确的值(也适用于通过CPack进行安装):
安装(代码 execute_process(命令some_command \ $ {CMAKE_INSTALL_PREFIX} / some_folder))
as a "post-install hook" I need to execute an install command like
install(CODE "execute_process(COMMAND some_command ${CMAKE_INSTALL_PREFIX}/some_folder"))
which creates a file in some_folder
based on the files which were previously installed into some_folder
(it compiles an index/cache of those files).
This works fine for the install target, however as soon as using cpack ${CMAKE_INSTALL_PREFIX}
is not the correct location anymore.
Is there a variable like ${CMAKE_CURRENT_INSTALL_PREFIX} that always points towards the current installation directory, regardless of wether the default install target or cpack is used and can be used for this purpose?
The only alternative I see is to try to execute the command at an earlier stage on the original files, create a temporary file and install the temporary file. Unfortunately this is much more error prone, as some_command
should be run on the "final" files after installation (in order to create a valid cache)
The answer turns out to be extremely simple (kudos to Nils Gladitz from IRC):
Escaping the variable ${CMAKE_INSTALL_PREFIX}
with a backslash delays its expansion until install time at which it holds the correct value also for installs via CPack:
install(CODE "execute_process(COMMAND some_command \${CMAKE_INSTALL_PREFIX}/some_folder"))
这篇关于cpack和安装(CODE ...)-CPACK_PACKAGING_INSTALL_PREFIX与CMAKE_INSTALL_PREFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!