[CMake] Correct handling of absolute/relative installation paths

David Demelier demelier.david at gmail.com
Thu Jul 27 06:28:41 EDT 2017


Hello,

I'm still trying to find a correct solution to handle user specified 
installation paths.

Let's consider two kind of paths:

   - WITH_BINDIR (default: bin/) where to install executables,
   - WITH_DATADIR (default: share/project_name/) where to install extra 
data.

I want to let the user configuring those paths because not all 
distributions use the same paths (e.g. bin vs usr/bin).

Then, I also like to build the whole CMake project by creating the 
hierarchy as it would be installed like:

<binarydir>/WITH_BINDIR/myapp
<binarydir>/WITH_DATADIR/somestuff.txt

Using relative paths makes the application relocatable, if I keep 
WITH_BINDIR set as "bin" I can get the executable path at runtime, going 
through its parent (bin/../) and then I can go share/project_name. 
Obviously this is only valid for relative paths.

However, a very high number of package managers build programs by 
specifying absolute paths, it is not an issue: instead of getting the 
directories at runtime, I use directly the absolute ones.

On unix it can still work because it will just be translated as:

<binarydir>/usr/local/bin/myapp
<binarydir>/usr/local/share/project_name/somestuff.txt

This is much bigger an issue on Windows if the user set WITH_BINDIR to 
something like D:/alt/bin

The path would be translated to

<binarydir>D:/alt/bin

which is invalid on Windows.

I like very much having this kind of `fakeroot' directory where you can 
have a preview on how the project looks like but I don't know what to do 
if paths are absolute, especially on Windows.

What are your thoughts on that, recommandations?

Regards,

-- 
David Demelier


More information about the CMake mailing list