1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Define GNU standard installation directories
10 Provides install directory variables as defined by the
11 `GNU Coding Standards`_.
13 .. _`GNU Coding Standards`: https://www.gnu.org/prep/standards/html_node/Directory-Variables.html
18 Inclusion of this module defines the following variables:
20 ``CMAKE_INSTALL_<dir>``
22 Destination for files of a given type. This value may be passed to
23 the ``DESTINATION`` options of :command:`install` commands for the
24 corresponding file type. It should typically be a path relative to
25 the installation prefix so that it can be converted to an absolute
26 path in a relocatable way (see ``CMAKE_INSTALL_FULL_<dir>``).
27 However, an absolute path is also allowed.
29 ``CMAKE_INSTALL_FULL_<dir>``
31 The absolute path generated from the corresponding ``CMAKE_INSTALL_<dir>``
32 value. If the value is not already an absolute path, an absolute path
33 is constructed typically by prepending the value of the
34 :variable:`CMAKE_INSTALL_PREFIX` variable. However, there are some
35 `special cases`_ as documented below.
37 where ``<dir>`` is one of:
40 user executables (``bin``)
42 system admin executables (``sbin``)
44 program executables (``libexec``)
46 read-only single-machine data (``etc``)
48 modifiable architecture-independent data (``com``)
50 modifiable single-machine data (``var``)
53 run-time variable data (``LOCALSTATEDIR/run``)
55 object code libraries (``lib`` or ``lib64``
56 or ``lib/<multiarch-tuple>`` on Debian)
58 C header files (``include``)
60 C header files for non-gcc (``/usr/include``)
62 read-only architecture-independent data root (``share``)
64 read-only architecture-independent data (``DATAROOTDIR``)
66 info documentation (``DATAROOTDIR/info``)
68 locale-dependent data (``DATAROOTDIR/locale``)
70 man documentation (``DATAROOTDIR/man``)
72 documentation root (``DATAROOTDIR/doc/PROJECT_NAME``)
74 If the includer does not define a value the above-shown default will be
75 used and the value will appear in the cache for editing by the user.
82 The following values of :variable:`CMAKE_INSTALL_PREFIX` are special:
86 For ``<dir>`` other than the ``SYSCONFDIR``, ``LOCALSTATEDIR`` and
87 ``RUNSTATEDIR``, the value of ``CMAKE_INSTALL_<dir>`` is prefixed
88 with ``usr/`` if it is not user-specified as an absolute path.
89 For example, the ``INCLUDEDIR`` value ``include`` becomes ``usr/include``.
90 This is required by the `GNU Coding Standards`_, which state:
92 When building the complete GNU system, the prefix will be empty
93 and ``/usr`` will be a symbolic link to ``/``.
97 For ``<dir>`` equal to ``SYSCONFDIR``, ``LOCALSTATEDIR`` or
98 ``RUNSTATEDIR``, the ``CMAKE_INSTALL_FULL_<dir>`` is computed by
99 prepending just ``/`` to the value of ``CMAKE_INSTALL_<dir>``
100 if it is not user-specified as an absolute path.
101 For example, the ``SYSCONFDIR`` value ``etc`` becomes ``/etc``.
102 This is required by the `GNU Coding Standards`_.
106 For ``<dir>`` equal to ``SYSCONFDIR``, ``LOCALSTATEDIR`` or
107 ``RUNSTATEDIR``, the ``CMAKE_INSTALL_FULL_<dir>`` is computed by
108 *appending* the prefix to the value of ``CMAKE_INSTALL_<dir>``
109 if it is not user-specified as an absolute path.
110 For example, the ``SYSCONFDIR`` value ``etc`` becomes ``/etc/opt/...``.
111 This is defined by the `Filesystem Hierarchy Standard`_.
113 .. _`Filesystem Hierarchy Standard`: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html
118 .. command:: GNUInstallDirs_get_absolute_install_dir
122 GNUInstallDirs_get_absolute_install_dir(absvar var dirname)
124 .. versionadded:: 3.7
126 Set the given variable ``absvar`` to the absolute path contained
127 within the variable ``var``. This is to allow the computation of an
128 absolute path, accounting for all the special cases documented
129 above. While this macro is used to compute the various
130 ``CMAKE_INSTALL_FULL_<dir>`` variables, it is exposed publicly to
131 allow users who create additional path variables to also compute
132 absolute paths where necessary, using the same logic. ``dirname`` is
133 the directory name to get, e.g. ``BINDIR``.
135 .. versionchanged:: 3.20
136 Added the ``<dirname>`` parameter. Previous versions of CMake passed
137 this value through the variable ``${dir}``.
138 #]=======================================================================]
141 cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
143 # Convert a cache variable to PATH type
145 macro(_GNUInstallDirs_cache_convert_to_path var description)
146 get_property(_GNUInstallDirs_cache_type CACHE ${var} PROPERTY TYPE)
147 if(_GNUInstallDirs_cache_type STREQUAL "UNINITIALIZED")
148 file(TO_CMAKE_PATH "${${var}}" _GNUInstallDirs_cmakepath)
149 set_property(CACHE ${var} PROPERTY TYPE PATH)
150 set_property(CACHE ${var} PROPERTY VALUE "${_GNUInstallDirs_cmakepath}")
151 set_property(CACHE ${var} PROPERTY HELPSTRING "${description}")
152 unset(_GNUInstallDirs_cmakepath)
154 unset(_GNUInstallDirs_cache_type)
157 # Create a cache variable with default for a path.
158 macro(_GNUInstallDirs_cache_path var default description)
159 if(NOT DEFINED ${var})
160 set(${var} "${default}" CACHE PATH "${description}")
162 _GNUInstallDirs_cache_convert_to_path("${var}" "${description}")
165 # Create a cache variable with not default for a path, with a fallback
166 # when unset; used for entries slaved to other entries such as
168 macro(_GNUInstallDirs_cache_path_fallback var default description)
170 set(${var} "" CACHE PATH "${description}")
171 set(${var} "${default}")
173 _GNUInstallDirs_cache_convert_to_path("${var}" "${description}")
176 # Installation directories
179 _GNUInstallDirs_cache_path(CMAKE_INSTALL_BINDIR "bin"
180 "User executables (bin)")
181 _GNUInstallDirs_cache_path(CMAKE_INSTALL_SBINDIR "sbin"
182 "System admin executables (sbin)")
183 _GNUInstallDirs_cache_path(CMAKE_INSTALL_SYSCONFDIR "etc"
184 "Read-only single-machine data (etc)")
185 _GNUInstallDirs_cache_path(CMAKE_INSTALL_SHAREDSTATEDIR "com"
186 "Modifiable architecture-independent data (com)")
187 _GNUInstallDirs_cache_path(CMAKE_INSTALL_LOCALSTATEDIR "var"
188 "Modifiable single-machine data (var)")
190 # We check if the variable was manually set and not cached, in order to
191 # allow projects to set the values as normal variables before including
192 # GNUInstallDirs to avoid having the entries cached or user-editable. It
193 # replaces the "if(NOT DEFINED CMAKE_INSTALL_XXX)" checks in all the
195 # If CMAKE_INSTALL_LIBDIR is defined, if _libdir_set is false, then the
196 # variable is a normal one, otherwise it is a cache one.
197 get_property(_libdir_set CACHE CMAKE_INSTALL_LIBDIR PROPERTY TYPE SET)
198 if(NOT DEFINED CMAKE_INSTALL_LIBDIR OR (_libdir_set
199 AND DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX
200 AND NOT "${_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX}" STREQUAL "${CMAKE_INSTALL_PREFIX}"))
201 # If CMAKE_INSTALL_LIBDIR is not defined, it is always executed.
203 # * if _libdir_set is false it is not executed (meaning that it is
204 # not a cache variable)
205 # * if _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX is not defined it is
207 # * if _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX and
208 # CMAKE_INSTALL_PREFIX are the same string it is not executed.
209 # _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX is updated after the
210 # execution, of this part of code, therefore at the next inclusion
211 # of the file, CMAKE_INSTALL_LIBDIR is defined, and the 2 strings
212 # are equal, meaning that the if is not executed the code the
215 set(_LIBDIR_DEFAULT "lib")
216 # Override this default 'lib' with 'lib64' iff:
217 # - we are on Linux system but NOT cross-compiling
218 # - we are NOT on debian
219 # - we are on a 64 bits system
220 # reason is: amd64 ABI: https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI
221 # For Debian with multiarch, use 'lib/${CMAKE_LIBRARY_ARCHITECTURE}' if
222 # CMAKE_LIBRARY_ARCHITECTURE is set (which contains e.g. "i386-linux-gnu"
223 # and CMAKE_INSTALL_PREFIX is "/usr"
224 # See http://wiki.debian.org/Multiarch
225 if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
226 set(__LAST_LIBDIR_DEFAULT "lib")
227 # __LAST_LIBDIR_DEFAULT is the default value that we compute from
228 # _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX, not a cache entry for
229 # the value that was last used as the default.
230 # This value is used to figure out whether the user changed the
231 # CMAKE_INSTALL_LIBDIR value manually, or if the value was the
232 # default one. When CMAKE_INSTALL_PREFIX changes, the value is
233 # updated to the new default, unless the user explicitly changed it.
235 if (NOT DEFINED CMAKE_SYSTEM_NAME OR NOT DEFINED CMAKE_SIZEOF_VOID_P)
236 message(AUTHOR_WARNING
237 "Unable to determine default CMAKE_INSTALL_LIBDIR directory because no target architecture is known. "
238 "Please enable at least one language before including GNUInstallDirs.")
240 if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
241 AND NOT CMAKE_CROSSCOMPILING
242 AND NOT EXISTS "/etc/alpine-release"
243 AND NOT EXISTS "/etc/arch-release")
244 if (EXISTS "/etc/debian_version") # is this a debian system ?
245 if(CMAKE_LIBRARY_ARCHITECTURE)
246 if("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
247 set(_LIBDIR_DEFAULT "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
249 if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX
250 AND "${_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
251 set(__LAST_LIBDIR_DEFAULT "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
254 else() # not debian, rely on CMAKE_SIZEOF_VOID_P:
255 if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
256 set(_LIBDIR_DEFAULT "lib64")
257 if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
258 set(__LAST_LIBDIR_DEFAULT "lib64")
263 if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
264 set(CMAKE_INSTALL_LIBDIR "${_LIBDIR_DEFAULT}" CACHE PATH "Object code libraries (${_LIBDIR_DEFAULT})")
265 elseif(DEFINED __LAST_LIBDIR_DEFAULT
266 AND "${__LAST_LIBDIR_DEFAULT}" STREQUAL "${CMAKE_INSTALL_LIBDIR}")
267 set_property(CACHE CMAKE_INSTALL_LIBDIR PROPERTY VALUE "${_LIBDIR_DEFAULT}")
270 _GNUInstallDirs_cache_convert_to_path(CMAKE_INSTALL_LIBDIR "Object code libraries (lib)")
273 set(_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" CACHE INTERNAL "CMAKE_INSTALL_PREFIX during last run")
275 unset(__LAST_LIBDIR_DEFAULT)
277 if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
278 AND NOT CMAKE_CROSSCOMPILING
279 AND NOT EXISTS "/etc/arch-release"
280 AND EXISTS "/etc/debian_version" # is this a debian system ?
281 AND "${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
282 # see https://refspecs.linuxfoundation.org/FHS_3.0/fhs-3.0.html#usrlibexec
283 # and https://www.debian.org/doc/debian-policy/ch-opersys#file-system-structure (section 9.1.1 bullet point 4)
284 _GNUInstallDirs_cache_path(CMAKE_INSTALL_LIBEXECDIR "${CMAKE_INSTALL_LIBDIR}"
285 "Program executables (${CMAKE_INSTALL_LIBDIR})")
287 _GNUInstallDirs_cache_path(CMAKE_INSTALL_LIBEXECDIR "libexec"
288 "Program executables (libexec)")
290 _GNUInstallDirs_cache_path(CMAKE_INSTALL_INCLUDEDIR "include"
291 "C header files (include)")
292 _GNUInstallDirs_cache_path(CMAKE_INSTALL_OLDINCLUDEDIR "/usr/include"
293 "C header files for non-gcc (/usr/include)")
294 _GNUInstallDirs_cache_path(CMAKE_INSTALL_DATAROOTDIR "share"
295 "Read-only architecture-independent data root (share)")
297 #-----------------------------------------------------------------------------
298 # Values whose defaults are relative to DATAROOTDIR. Store empty values in
299 # the cache and store the defaults in local variables if the cache values are
300 # not set explicitly. This auto-updates the defaults as DATAROOTDIR changes.
302 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_DATADIR "${CMAKE_INSTALL_DATAROOTDIR}"
303 "Read-only architecture-independent data (DATAROOTDIR)")
305 if(CMAKE_SYSTEM_NAME MATCHES "^(([^kF].*)?BSD|DragonFly)$")
306 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_INFODIR "info"
307 "Info documentation (info)")
309 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_INFODIR "${CMAKE_INSTALL_DATAROOTDIR}/info"
310 "Info documentation (DATAROOTDIR/info)")
313 if(CMAKE_SYSTEM_NAME MATCHES "^(([^k].*)?BSD|DragonFly)$")
314 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_MANDIR "man"
315 "Man documentation (man)")
317 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_MANDIR "${CMAKE_INSTALL_DATAROOTDIR}/man"
318 "Man documentation (DATAROOTDIR/man)")
321 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_LOCALEDIR "${CMAKE_INSTALL_DATAROOTDIR}/locale"
322 "Locale-dependent data (DATAROOTDIR/locale)")
323 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_DOCDIR "${CMAKE_INSTALL_DATAROOTDIR}/doc/${PROJECT_NAME}"
324 "Documentation root (DATAROOTDIR/doc/PROJECT_NAME)")
326 _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_RUNSTATEDIR "${CMAKE_INSTALL_LOCALSTATEDIR}/run"
327 "Run-time variable data (LOCALSTATEDIR/run)")
329 #-----------------------------------------------------------------------------
333 CMAKE_INSTALL_SBINDIR
334 CMAKE_INSTALL_LIBEXECDIR
335 CMAKE_INSTALL_SYSCONFDIR
336 CMAKE_INSTALL_SHAREDSTATEDIR
337 CMAKE_INSTALL_LOCALSTATEDIR
338 CMAKE_INSTALL_RUNSTATEDIR
340 CMAKE_INSTALL_INCLUDEDIR
341 CMAKE_INSTALL_OLDINCLUDEDIR
342 CMAKE_INSTALL_DATAROOTDIR
343 CMAKE_INSTALL_DATADIR
344 CMAKE_INSTALL_INFODIR
345 CMAKE_INSTALL_LOCALEDIR
350 macro(GNUInstallDirs_get_absolute_install_dir absvar var)
351 set(GGAID_extra_args ${ARGN})
352 list(LENGTH GGAID_extra_args GGAID_extra_arg_count)
353 if(GGAID_extra_arg_count GREATER "0")
354 list(GET GGAID_extra_args 0 GGAID_dir)
356 # Historical behavior: use ${dir} from caller's scope
357 set(GGAID_dir "${dir}")
358 message(AUTHOR_WARNING
359 "GNUInstallDirs_get_absolute_install_dir called without third argument. "
360 "Using \${dir} from the caller's scope for compatibility with CMake 3.19 and below.")
363 if(NOT IS_ABSOLUTE "${${var}}")
364 # Handle special cases:
365 # - CMAKE_INSTALL_PREFIX == /
366 # - CMAKE_INSTALL_PREFIX == /usr
367 # - CMAKE_INSTALL_PREFIX == /opt/...
368 if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/")
369 if("${GGAID_dir}" STREQUAL "SYSCONFDIR" OR "${GGAID_dir}" STREQUAL "LOCALSTATEDIR" OR "${GGAID_dir}" STREQUAL "RUNSTATEDIR")
370 set(${absvar} "/${${var}}")
372 if (NOT "${${var}}" MATCHES "^usr/")
373 set(${var} "usr/${${var}}")
375 set(${absvar} "/${${var}}")
377 elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
378 if("${GGAID_dir}" STREQUAL "SYSCONFDIR" OR "${GGAID_dir}" STREQUAL "LOCALSTATEDIR" OR "${GGAID_dir}" STREQUAL "RUNSTATEDIR")
379 set(${absvar} "/${${var}}")
381 set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
383 elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/opt/.*")
384 if("${GGAID_dir}" STREQUAL "SYSCONFDIR" OR "${GGAID_dir}" STREQUAL "LOCALSTATEDIR" OR "${GGAID_dir}" STREQUAL "RUNSTATEDIR")
385 set(${absvar} "/${${var}}${CMAKE_INSTALL_PREFIX}")
387 set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
390 set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
393 set(${absvar} "${${var}}")
397 unset(GGAID_extra_arg_count)
398 unset(GGAID_extra_args)
421 GNUInstallDirs_get_absolute_install_dir(CMAKE_INSTALL_FULL_${dir} CMAKE_INSTALL_${dir} ${dir})