Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set installation prefix automatically to custom path if not explicitly specified on the command line

For some internal tests, I would like the install prefix to default to a subdirectory of the build directory, unless explicitly overridden by the user. I know the user can specify a install prefix by:

$ cmake -DCMAKE_INSTALL_PREFIX=/foo/bar .. 

But if the user does not specify this, it should default to, e.g. ${PWD}/installed.

The variable CMAKE_INSTALL_PREFIX is already set to /usr/local, so I cannot just check to see if it unset/empty before setting it.

My current solution is to add a custom switch that the user has to invoke to specify that the CMAKE_INSTALL_PREFIX variable gets respected:

option(ENABLE_INSTALL_PREFIX "Install build targets to system (path given by '-DCMAKE_INSTALL_PREFIX' or '${CMAKE_INSTALL_PREFIX}' if not specified)." OFF) if ( ENABLE_INSTALL_PREFIX )     set (CMAKE_INSTALL_PREFIX installed CACHE PATH "Installation root") else()     set (CMAKE_INSTALL_PREFIX installed CACHE PATH "Installation root" FORCE) endif() 

My questions are:

(a) Are there any issues with the above, beyond the annoyance of the extra flag needing to be passed to CMake to get CMAKE_INSTALL_PREFIX to have an effect?

(b) Is there a better, cleaner, more robust, more idiomatic and/or less annoying way to achieve the above?

Thanks.

like image 427
Jeet Avatar asked Apr 18 '13 04:04

Jeet


People also ask

What is the prefix path of a config file?

The prefix path is a list of paths. Use this: Also you don't have to point directly into the config file path, you also can point into the directory containing the lib/cmake/... Show activity on this post.

How do I find the prefix path of a CMake config file?

The prefix path is a list of paths. Use this: list (APPEND CMAKE_PREFIX_PATH "/home/rip/Qt/5.12.1/gcc_64") Also you don't have to point directly into the config file path, you also can point into the directory containing the lib/cmake/... Share.

How to use the install_PATH variable in CMake?

There are two ways to use this variable: passing it as a command line argument just like Job mentioned: cmake -DCMAKE_INSTALL_PREFIX=< install_path > .. assigning value to it in CMakeLists.txt: SET(CMAKE_INSTALL_PREFIX < install_path >) But do remember to place it BEFOREPROJECT(< project_name>)command, otherwise it will not work!

Is it possible to append to the prefix in CMake?

The CMAKE_PREFIX_PATH is set, but everything that's on it is ignored. I found out that you can access the environment variable, split that into a list and append to that to actually append to the prefix path instead of replacing it.


2 Answers

CMake sets the boolean variable CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT if CMAKE_INSTALL_PREFIX has not been explicitly specified and is initialized to its default setting. You can override it in the following way:

if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)     set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/installed"            CACHE PATH "default install path" FORCE) endif() 
like image 184
sakra Avatar answered Sep 22 '22 00:09

sakra


A few years ago, I wanted to attempt the same thing. Unfortunately I could never find a fully satisfactory solution. Instead, I chose to use a regex to check if CMAKE_INSTALL_PREFIX matches the default.

E.g. on windows I use the following to create the install dir in the build-directory:

if ( CMAKE_INSTALL_PREFIX MATCHES "^C:/Program Files" )     # Override to an "Install" directory in the current build-dir     message( STATUS "    Install-prefix was at default -> forcing it to the build-dir" )     message( STATUS "        'set( CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install )'" )     set( CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install ) endif ( CMAKE_INSTALL_PREFIX MATCHES "^C:/Program Files" ) 

The biggest problem with this construct is that you can specify manually that CMake should install into C:\Program Files. As I distribute my tool with an NSIS-installer this is not an issue (for me).

Perhaps you could try something similar on Unix.

like image 34
André Avatar answered Sep 19 '22 00:09

André