1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5GNUInstallDirs
6--------------
7
8Define GNU standard installation directories
9
10Provides install directory variables as defined by the
11`GNU Coding Standards`_.
12
13.. _`GNU Coding Standards`: https://www.gnu.org/prep/standards/html_node/Directory-Variables.html
14
15Result Variables
16^^^^^^^^^^^^^^^^
17
18Inclusion of this module defines the following variables:
19
20``CMAKE_INSTALL_<dir>``
21
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.
28
29``CMAKE_INSTALL_FULL_<dir>``
30
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.
36
37where ``<dir>`` is one of:
38
39``BINDIR``
40  user executables (``bin``)
41``SBINDIR``
42  system admin executables (``sbin``)
43``LIBEXECDIR``
44  program executables (``libexec``)
45``SYSCONFDIR``
46  read-only single-machine data (``etc``)
47``SHAREDSTATEDIR``
48  modifiable architecture-independent data (``com``)
49``LOCALSTATEDIR``
50  modifiable single-machine data (``var``)
51``RUNSTATEDIR``
52  .. versionadded:: 3.9
53    run-time variable data (``LOCALSTATEDIR/run``)
54``LIBDIR``
55  object code libraries (``lib`` or ``lib64``
56  or ``lib/<multiarch-tuple>`` on Debian)
57``INCLUDEDIR``
58  C header files (``include``)
59``OLDINCLUDEDIR``
60  C header files for non-gcc (``/usr/include``)
61``DATAROOTDIR``
62  read-only architecture-independent data root (``share``)
63``DATADIR``
64  read-only architecture-independent data (``DATAROOTDIR``)
65``INFODIR``
66  info documentation (``DATAROOTDIR/info``)
67``LOCALEDIR``
68  locale-dependent data (``DATAROOTDIR/locale``)
69``MANDIR``
70  man documentation (``DATAROOTDIR/man``)
71``DOCDIR``
72  documentation root (``DATAROOTDIR/doc/PROJECT_NAME``)
73
74If the includer does not define a value the above-shown default will be
75used and the value will appear in the cache for editing by the user.
76
77Special Cases
78^^^^^^^^^^^^^
79
80.. versionadded:: 3.4
81
82The following values of :variable:`CMAKE_INSTALL_PREFIX` are special:
83
84``/``
85
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:
91
92    When building the complete GNU system, the prefix will be empty
93    and ``/usr`` will be a symbolic link to ``/``.
94
95``/usr``
96
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`_.
103
104``/opt/...``
105
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`_.
112
113.. _`Filesystem Hierarchy Standard`: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html
114
115Macros
116^^^^^^
117
118.. command:: GNUInstallDirs_get_absolute_install_dir
119
120  ::
121
122    GNUInstallDirs_get_absolute_install_dir(absvar var dirname)
123
124  .. versionadded:: 3.7
125
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``.
134
135  .. versionchanged:: 3.20
136    Added the ``<dirname>`` parameter.  Previous versions of CMake passed
137    this value through the variable ``${dir}``.
138#]=======================================================================]
139
140cmake_policy(PUSH)
141cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
142
143# Convert a cache variable to PATH type
144
145macro(_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)
153  endif()
154  unset(_GNUInstallDirs_cache_type)
155endmacro()
156
157# Create a cache variable with default for a path.
158macro(_GNUInstallDirs_cache_path var default description)
159  if(NOT DEFINED ${var})
160    set(${var} "${default}" CACHE PATH "${description}")
161  endif()
162  _GNUInstallDirs_cache_convert_to_path("${var}" "${description}")
163endmacro()
164
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
167# DATAROOTDIR.
168macro(_GNUInstallDirs_cache_path_fallback var default description)
169  if(NOT ${var})
170    set(${var} "" CACHE PATH "${description}")
171    set(${var} "${default}")
172  endif()
173  _GNUInstallDirs_cache_convert_to_path("${var}" "${description}")
174endmacro()
175
176# Installation directories
177#
178
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_LIBEXECDIR "libexec"
184  "Program executables (libexec)")
185_GNUInstallDirs_cache_path(CMAKE_INSTALL_SYSCONFDIR "etc"
186  "Read-only single-machine data (etc)")
187_GNUInstallDirs_cache_path(CMAKE_INSTALL_SHAREDSTATEDIR "com"
188  "Modifiable architecture-independent data (com)")
189_GNUInstallDirs_cache_path(CMAKE_INSTALL_LOCALSTATEDIR "var"
190  "Modifiable single-machine data (var)")
191
192# We check if the variable was manually set and not cached, in order to
193# allow projects to set the values as normal variables before including
194# GNUInstallDirs to avoid having the entries cached or user-editable. It
195# replaces the "if(NOT DEFINED CMAKE_INSTALL_XXX)" checks in all the
196# other cases.
197# If CMAKE_INSTALL_LIBDIR is defined, if _libdir_set is false, then the
198# variable is a normal one, otherwise it is a cache one.
199get_property(_libdir_set CACHE CMAKE_INSTALL_LIBDIR PROPERTY TYPE SET)
200if(NOT DEFINED CMAKE_INSTALL_LIBDIR OR (_libdir_set
201    AND DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX
202    AND NOT "${_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX}" STREQUAL "${CMAKE_INSTALL_PREFIX}"))
203  # If CMAKE_INSTALL_LIBDIR is not defined, it is always executed.
204  # Otherwise:
205  #  * if _libdir_set is false it is not executed (meaning that it is
206  #    not a cache variable)
207  #  * if _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX is not defined it is
208  #    not executed
209  #  * if _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX and
210  #    CMAKE_INSTALL_PREFIX are the same string it is not executed.
211  #    _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX is updated after the
212  #    execution, of this part of code, therefore at the next inclusion
213  #    of the file, CMAKE_INSTALL_LIBDIR is defined, and the 2 strings
214  #    are equal, meaning that the if is not executed the code the
215  #    second time.
216
217  set(_LIBDIR_DEFAULT "lib")
218  # Override this default 'lib' with 'lib64' iff:
219  #  - we are on Linux system but NOT cross-compiling
220  #  - we are NOT on debian
221  #  - we are NOT building for conda
222  #  - we are on a 64 bits system
223  # reason is: amd64 ABI: https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI
224  # For Debian with multiarch, use 'lib/${CMAKE_LIBRARY_ARCHITECTURE}' if
225  # CMAKE_LIBRARY_ARCHITECTURE is set (which contains e.g. "i386-linux-gnu"
226  # and CMAKE_INSTALL_PREFIX is "/usr"
227  # See http://wiki.debian.org/Multiarch
228  if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
229    set(__LAST_LIBDIR_DEFAULT "lib")
230    # __LAST_LIBDIR_DEFAULT is the default value that we compute from
231    # _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX, not a cache entry for
232    # the value that was last used as the default.
233    # This value is used to figure out whether the user changed the
234    # CMAKE_INSTALL_LIBDIR value manually, or if the value was the
235    # default one. When CMAKE_INSTALL_PREFIX changes, the value is
236    # updated to the new default, unless the user explicitly changed it.
237  endif()
238  if (NOT DEFINED CMAKE_SYSTEM_NAME OR NOT DEFINED CMAKE_SIZEOF_VOID_P)
239    message(AUTHOR_WARNING
240      "Unable to determine default CMAKE_INSTALL_LIBDIR directory because no target architecture is known. "
241      "Please enable at least one language before including GNUInstallDirs.")
242  endif()
243
244  if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
245      AND NOT CMAKE_CROSSCOMPILING)
246    unset(__system_type_for_install)
247    if(DEFINED ENV{CONDA_BUILD} AND DEFINED ENV{PREFIX})
248      set(conda_prefix "$ENV{PREFIX}")
249      cmake_path(ABSOLUTE_PATH conda_prefix NORMALIZE)
250      if("${CMAKE_INSTALL_PREFIX}" STREQUAL conda_prefix)
251        set(__system_type_for_install "conda")
252      endif()
253    elseif(DEFINED ENV{CONDA_PREFIX})
254      set(conda_prefix "$ENV{CONDA_PREFIX}")
255      cmake_path(ABSOLUTE_PATH conda_prefix NORMALIZE)
256      if("${CMAKE_INSTALL_PREFIX}" STREQUAL conda_prefix AND
257         NOT ("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$" OR
258              "${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/local/?$"))
259        set(__system_type_for_install "conda")
260      endif()
261    endif()
262    if(NOT __system_type_for_install)
263      if (EXISTS "/etc/alpine-release")
264        set(__system_type_for_install "alpine")
265      elseif (EXISTS "/etc/arch-release")
266        set(__system_type_for_install "arch linux")
267      elseif (EXISTS "/etc/debian_version")
268        set(__system_type_for_install "debian")
269      endif()
270    endif()
271
272    if(__system_type_for_install STREQUAL "debian")
273      if(CMAKE_LIBRARY_ARCHITECTURE)
274        if("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
275          set(_LIBDIR_DEFAULT "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
276        endif()
277        if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX
278            AND "${_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
279          set(__LAST_LIBDIR_DEFAULT "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
280        endif()
281      endif()
282    elseif(NOT DEFINED __system_type_for_install)
283      # not debian, alpine, arch, or conda so rely on CMAKE_SIZEOF_VOID_P:
284      if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
285        set(_LIBDIR_DEFAULT "lib64")
286        if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
287          set(__LAST_LIBDIR_DEFAULT "lib64")
288        endif()
289      endif()
290    endif()
291  endif()
292  unset(__system_type_for_install)
293
294  if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
295    set(CMAKE_INSTALL_LIBDIR "${_LIBDIR_DEFAULT}" CACHE PATH "Object code libraries (${_LIBDIR_DEFAULT})")
296  elseif(DEFINED __LAST_LIBDIR_DEFAULT
297      AND "${__LAST_LIBDIR_DEFAULT}" STREQUAL "${CMAKE_INSTALL_LIBDIR}")
298    set_property(CACHE CMAKE_INSTALL_LIBDIR PROPERTY VALUE "${_LIBDIR_DEFAULT}")
299  endif()
300endif()
301_GNUInstallDirs_cache_convert_to_path(CMAKE_INSTALL_LIBDIR "Object code libraries (lib)")
302
303# Save for next run
304set(_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" CACHE INTERNAL "CMAKE_INSTALL_PREFIX during last run")
305unset(_libdir_set)
306unset(__LAST_LIBDIR_DEFAULT)
307
308_GNUInstallDirs_cache_path(CMAKE_INSTALL_INCLUDEDIR "include"
309  "C header files (include)")
310_GNUInstallDirs_cache_path(CMAKE_INSTALL_OLDINCLUDEDIR "/usr/include"
311  "C header files for non-gcc (/usr/include)")
312_GNUInstallDirs_cache_path(CMAKE_INSTALL_DATAROOTDIR "share"
313  "Read-only architecture-independent data root (share)")
314
315#-----------------------------------------------------------------------------
316# Values whose defaults are relative to DATAROOTDIR.  Store empty values in
317# the cache and store the defaults in local variables if the cache values are
318# not set explicitly.  This auto-updates the defaults as DATAROOTDIR changes.
319
320_GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_DATADIR "${CMAKE_INSTALL_DATAROOTDIR}"
321  "Read-only architecture-independent data (DATAROOTDIR)")
322
323if(CMAKE_SYSTEM_NAME MATCHES "^(([^kF].*)?BSD|DragonFly)$")
324  _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_INFODIR "info"
325    "Info documentation (info)")
326else()
327  _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_INFODIR "${CMAKE_INSTALL_DATAROOTDIR}/info"
328    "Info documentation (DATAROOTDIR/info)")
329endif()
330
331if(CMAKE_SYSTEM_NAME MATCHES "^(([^k].*)?BSD|DragonFly)$")
332  _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_MANDIR "man"
333    "Man documentation (man)")
334else()
335  _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_MANDIR "${CMAKE_INSTALL_DATAROOTDIR}/man"
336    "Man documentation (DATAROOTDIR/man)")
337endif()
338
339_GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_LOCALEDIR "${CMAKE_INSTALL_DATAROOTDIR}/locale"
340  "Locale-dependent data (DATAROOTDIR/locale)")
341_GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_DOCDIR "${CMAKE_INSTALL_DATAROOTDIR}/doc/${PROJECT_NAME}"
342  "Documentation root (DATAROOTDIR/doc/PROJECT_NAME)")
343
344_GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_RUNSTATEDIR "${CMAKE_INSTALL_LOCALSTATEDIR}/run"
345  "Run-time variable data (LOCALSTATEDIR/run)")
346
347#-----------------------------------------------------------------------------
348
349mark_as_advanced(
350  CMAKE_INSTALL_BINDIR
351  CMAKE_INSTALL_SBINDIR
352  CMAKE_INSTALL_LIBEXECDIR
353  CMAKE_INSTALL_SYSCONFDIR
354  CMAKE_INSTALL_SHAREDSTATEDIR
355  CMAKE_INSTALL_LOCALSTATEDIR
356  CMAKE_INSTALL_RUNSTATEDIR
357  CMAKE_INSTALL_LIBDIR
358  CMAKE_INSTALL_INCLUDEDIR
359  CMAKE_INSTALL_OLDINCLUDEDIR
360  CMAKE_INSTALL_DATAROOTDIR
361  CMAKE_INSTALL_DATADIR
362  CMAKE_INSTALL_INFODIR
363  CMAKE_INSTALL_LOCALEDIR
364  CMAKE_INSTALL_MANDIR
365  CMAKE_INSTALL_DOCDIR
366  )
367
368macro(GNUInstallDirs_get_absolute_install_dir absvar var)
369  set(GGAID_extra_args ${ARGN})
370  list(LENGTH GGAID_extra_args GGAID_extra_arg_count)
371  if(GGAID_extra_arg_count GREATER "0")
372    list(GET GGAID_extra_args 0 GGAID_dir)
373  else()
374    # Historical behavior: use ${dir} from caller's scope
375    set(GGAID_dir "${dir}")
376    message(AUTHOR_WARNING
377      "GNUInstallDirs_get_absolute_install_dir called without third argument. "
378      "Using \${dir} from the caller's scope for compatibility with CMake 3.19 and below.")
379  endif()
380
381  if(NOT IS_ABSOLUTE "${${var}}")
382    # Handle special cases:
383    # - CMAKE_INSTALL_PREFIX == /
384    # - CMAKE_INSTALL_PREFIX == /usr
385    # - CMAKE_INSTALL_PREFIX == /opt/...
386    if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/")
387      if("${GGAID_dir}" STREQUAL "SYSCONFDIR" OR "${GGAID_dir}" STREQUAL "LOCALSTATEDIR" OR "${GGAID_dir}" STREQUAL "RUNSTATEDIR")
388        set(${absvar} "/${${var}}")
389      else()
390        if (NOT "${${var}}" MATCHES "^usr/")
391          set(${var} "usr/${${var}}")
392        endif()
393        set(${absvar} "/${${var}}")
394      endif()
395    elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
396      if("${GGAID_dir}" STREQUAL "SYSCONFDIR" OR "${GGAID_dir}" STREQUAL "LOCALSTATEDIR" OR "${GGAID_dir}" STREQUAL "RUNSTATEDIR")
397        set(${absvar} "/${${var}}")
398      else()
399        set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
400      endif()
401    elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/opt/.*")
402      if("${GGAID_dir}" STREQUAL "SYSCONFDIR" OR "${GGAID_dir}" STREQUAL "LOCALSTATEDIR" OR "${GGAID_dir}" STREQUAL "RUNSTATEDIR")
403        set(${absvar} "/${${var}}${CMAKE_INSTALL_PREFIX}")
404      else()
405        set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
406      endif()
407    else()
408      set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
409    endif()
410  else()
411    set(${absvar} "${${var}}")
412  endif()
413
414  unset(GGAID_dir)
415  unset(GGAID_extra_arg_count)
416  unset(GGAID_extra_args)
417endmacro()
418
419# Result directories
420#
421foreach(dir
422    BINDIR
423    SBINDIR
424    LIBEXECDIR
425    SYSCONFDIR
426    SHAREDSTATEDIR
427    LOCALSTATEDIR
428    RUNSTATEDIR
429    LIBDIR
430    INCLUDEDIR
431    OLDINCLUDEDIR
432    DATAROOTDIR
433    DATADIR
434    INFODIR
435    LOCALEDIR
436    MANDIR
437    DOCDIR
438    )
439  GNUInstallDirs_get_absolute_install_dir(CMAKE_INSTALL_FULL_${dir} CMAKE_INSTALL_${dir} ${dir})
440endforeach()
441
442cmake_policy(POP)
443