Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

cmake

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.

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:

add_custom_target(runcommand
  COMMAND echo '\#!/bin/bash -eu' > run.command &&
          echo 'cd `dirname $$0`' >> 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.

like image 229
Alex Avatar asked Aug 21 '14 18:08

Alex


1 Answers

You can use:

file(RELATIVE_PATH variable directory file)

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

file(RELATIVE_PATH rel ${MY_ROOT} ${MY_EXECUTABLE})
like image 98
Lindydancer Avatar answered Sep 27 '22 18:09

Lindydancer