[CMake] Correct handling of absolute/relative installation paths

David Demelier demelier.david at gmail.com
Thu Jul 27 07:55:24 EDT 2017


Le 27/07/2017 à 13:08, Eric Noulard a écrit :
> 
> 
> 2017-07-27 12:28 GMT+02:00 David Demelier <demelier.david at gmail.com 
> <mailto:demelier.david at gmail.com>>:
> 
>     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
> 
> 
> Do you mean here that you setup CMAKE_<XXXX>_OUTPUT_DIRECTORY (variable 
> or target property) to your favorite value
> or
> 
Both, I set the output directory for executable to WITH_BINDIR value and 
also install it as destination.

> My opinion is that you should never use absolute path (besides some very 
> specific case on unix where you want to put something in /etc/...)
> You should ask your user for
> 
>     DATADIR
>     BINDIR
>     etc...
> 

This is what I have done in my CMakeLists.txt, the cmake invocation 
fails if BINDIR and/or DATADIR are absolute. But again, then with some 
package managers you get in troubles because they specify absolute path.

See the RPM default macro:

   /usr/bin/cmake \
         -DCMAKE_C_FLAGS_RELEASE:STRING="-DNDEBUG" \
         -DCMAKE_CXX_FLAGS_RELEASE:STRING="-DNDEBUG" \
         -DCMAKE_Fortran_FLAGS_RELEASE:STRING="-DNDEBUG" \
         -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
         -DCMAKE_INSTALL_PREFIX:PATH=/usr \
         -DINCLUDE_INSTALL_DIR:PATH=/usr/include \
         -DLIB_INSTALL_DIR:PATH=/usr/lib64 \
         -DSYSCONF_INSTALL_DIR:PATH=/etc \
         -DSHARE_INSTALL_PREFIX:PATH=/usr/share \

And if you add any new variable, you should prefix by %{prefix} which is 
also absolute...

> I wouldn't try to mimic install tree during the build (if it is what you 
> are doing),

The nice thing is that you can easily run/debug the application 
especially if that application needs to find some plugins/data while 
running by evaluating paths to them.

I'll probably go for something like a pre-install target like you said 
instead of mimic'ing the build tree. And disable that target if any of 
the paths are absolute.

Thanks for the answers!


More information about the CMake mailing list