bash 如何使用 CMake 获取目标的相对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25433033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:12:21  来源:igfitidea点击:

How to get a relative path to a target with CMake?

bashcmake

提问by Alex

I have a project that uses CMake to generate build scripts, and each platform puts the executable that are generated in a different place. We have a non-programmer on our team who does not have build tools and I want to be able to bundle up all the files, scripts and executables necessary to run the project so that he can run the project.

我有一个使用CMake生成构建脚本的项目,每个平台将生成的可执行文件放在不同的地方。我们团队中有一位非程序员,他没有构建工具,我希望能够将运行项目所需的所有文件、脚本和可执行文件捆绑在一起,以便他可以运行项目。

To do that, I've added a custom target that takes all the necessary files and zips them up. It also generates a very simple script (which is included in the zip file) that he can just click on that should run a script then launch the executable, like so:

为此,我添加了一个自定义目标,用于获取所有必要的文件并将它们压缩。它还会生成一个非常简单的脚本(包含在 zip 文件中),他只需单击该脚本即可运行脚本,然后启动可执行文件,如下所示:

add_custom_target(runcommand
  COMMAND echo '\#!/bin/bash -eu' > run.command &&
          echo 'cd `dirname $
file(RELATIVE_PATH variable directory file)
`' >> run.command && echo './scripts/prerun_script.sh && ${MY_EXECUTABLE}' >> run.command && chmod +x run.command)

The problem with this is that MY_EXECUTABLE is a hardcoded path to the executable on my system. I would like it to be a relative path so that I can just take this resultant zip file, unzip it anywhere and run it from there. What I would like is to get the path to MY_EXECUTABLE relative to the root directory of my project so that this script can be run from anywhere.

问题在于 MY_EXECUTABLE 是我系统上可执行文件的硬编码路径。我希望它是一个相对路径,这样我就可以把这个生成的 zip 文件解压到任何地方,然后从那里运行它。我想要的是获取相对于我的项目根目录的 MY_EXECUTABLE 路径,以便可以从任何地方运行该脚本。

回答by Lindydancer

You can use:

您可以使用:

file(RELATIVE_PATH rel ${MY_ROOT} ${MY_EXECUTABLE})

Concretely, assume that MY_ROOTcontains the root directory (there might be a suitable predefined CMake variable for this), this will set relto the relative path of MY_EXECUTABLE:

具体来说,假设MY_ROOT包含根目录(可能有一个合适的预定义 CMake 变量),这将设置rel为的相对路径MY_EXECUTABLE

file(RELATIVE_PATH rel ${PROJECT_SOURCE_DIR} ${MY_EXECUTABLE})

回答by Innokentiy Alaytsev

In case where you want to get path relative to project root, you may use PROJECT_SOURCE_DIRCMake variable:

如果您想获取相对于项目根目录的路径,您可以使用PROJECT_SOURCE_DIRCMake 变量:

THISDIR=`pwd`
MY_EXECUTABLE=$THISDIR/test/someexec
REL_EXECUTABLE=${MY_EXECUTABLE##$THISDIR/}
echo $REL_EXECUTABLE 

Remember, that MY_EXECUTABLEmust contain full path to file.

请记住,它MY_EXECUTABLE必须包含文件的完整路径。

回答by zerodiff

Something like this would work:

像这样的事情会起作用:

${MY_EXECUTABLE##`pwd`/}

Basically, this chops off the base path from $MY_EXECUTABLE. In this example, I just set up MY_EXECUTABLEas a test to give it a full path. The one-line equivalent for what you want is:

基本上,这从$MY_EXECUTABLE. 在这个例子中,我只是设置了MY_EXECUTABLE一个测试来给它一个完整的路径。您想要的单行等效项是:

##代码##

I guess this assumes pwdis the root of your project. Otherwise, see the Cmake variabledoc.

我想这假设pwd是您项目的根。否则,请参阅Cmake 变量文档。

(As an aside, this makes me nostalgic for the kshdays.)

(顺便说一句,这让我怀念那些ksh日子。)