• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#.rst:
2# GNUInstallDirs
3# --------------
4#
5# Define GNU standard installation directories
6#
7# Provides install directory variables as defined by the
8# `GNU Coding Standards`_.
9#
10# .. _`GNU Coding Standards`: https://www.gnu.org/prep/standards/html_node/Directory-Variables.html
11#
12# Result Variables
13# ^^^^^^^^^^^^^^^^
14#
15# Inclusion of this module defines the following variables:
16#
17# ``CMAKE_INSTALL_<dir>``
18#
19#   Destination for files of a given type.  This value may be passed to
20#   the ``DESTINATION`` options of :command:`install` commands for the
21#   corresponding file type.
22#
23# ``CMAKE_INSTALL_FULL_<dir>``
24#
25#   The absolute path generated from the corresponding ``CMAKE_INSTALL_<dir>``
26#   value.  If the value is not already an absolute path, an absolute path
27#   is constructed typically by prepending the value of the
28#   :variable:`CMAKE_INSTALL_PREFIX` variable.  However, there are some
29#   `special cases`_ as documented below.
30#
31# where ``<dir>`` is one of:
32#
33# ``BINDIR``
34#   user executables (``bin``)
35# ``SBINDIR``
36#   system admin executables (``sbin``)
37# ``LIBEXECDIR``
38#   program executables (``libexec``)
39# ``SYSCONFDIR``
40#   read-only single-machine data (``etc``)
41# ``SHAREDSTATEDIR``
42#   modifiable architecture-independent data (``com``)
43# ``LOCALSTATEDIR``
44#   modifiable single-machine data (``var``)
45# ``LIBDIR``
46#   object code libraries (``lib`` or ``lib64``
47#   or ``lib/<multiarch-tuple>`` on Debian)
48# ``INCLUDEDIR``
49#   C header files (``include``)
50# ``OLDINCLUDEDIR``
51#   C header files for non-gcc (``/usr/include``)
52# ``DATAROOTDIR``
53#   read-only architecture-independent data root (``share``)
54# ``DATADIR``
55#   read-only architecture-independent data (``DATAROOTDIR``)
56# ``INFODIR``
57#   info documentation (``DATAROOTDIR/info``)
58# ``LOCALEDIR``
59#   locale-dependent data (``DATAROOTDIR/locale``)
60# ``MANDIR``
61#   man documentation (``DATAROOTDIR/man``)
62# ``DOCDIR``
63#   documentation root (``DATAROOTDIR/doc/PROJECT_NAME``)
64#
65# If the includer does not define a value the above-shown default will be
66# used and the value will appear in the cache for editing by the user.
67#
68# Special Cases
69# ^^^^^^^^^^^^^
70#
71# The following values of :variable:`CMAKE_INSTALL_PREFIX` are special:
72#
73# ``/``
74#
75#   For ``<dir>`` other than the ``SYSCONFDIR`` and ``LOCALSTATEDIR``,
76#   the value of ``CMAKE_INSTALL_<dir>`` is prefixed with ``usr/`` if
77#   it is not user-specified as an absolute path.  For example, the
78#   ``INCLUDEDIR`` value ``include`` becomes ``usr/include``.
79#   This is required by the `GNU Coding Standards`_, which state:
80#
81#     When building the complete GNU system, the prefix will be empty
82#     and ``/usr`` will be a symbolic link to ``/``.
83#
84# ``/usr``
85#
86#   For ``<dir>`` equal to ``SYSCONFDIR`` or ``LOCALSTATEDIR``, the
87#   ``CMAKE_INSTALL_FULL_<dir>`` is computed by prepending just ``/``
88#   to the value of ``CMAKE_INSTALL_<dir>`` if it is not user-specified
89#   as an absolute path.  For example, the ``SYSCONFDIR`` value ``etc``
90#   becomes ``/etc``.  This is required by the `GNU Coding Standards`_.
91#
92# ``/opt/...``
93#
94#   For ``<dir>`` equal to ``SYSCONFDIR`` or ``LOCALSTATEDIR``, the
95#   ``CMAKE_INSTALL_FULL_<dir>`` is computed by *appending* the prefix
96#   to the value of ``CMAKE_INSTALL_<dir>`` if it is not user-specified
97#   as an absolute path.  For example, the ``SYSCONFDIR`` value ``etc``
98#   becomes ``/etc/opt/...``.  This is defined by the
99#   `Filesystem Hierarchy Standard`_.
100#
101# .. _`Filesystem Hierarchy Standard`: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html
102#
103# Macros
104# ^^^^^^
105#
106# .. command:: GNUInstallDirs_get_absolute_install_dir
107#
108#   ::
109#
110#     GNUInstallDirs_get_absolute_install_dir(absvar var)
111#
112#   Set the given variable ``absvar`` to the absolute path contained
113#   within the variable ``var``.  This is to allow the computation of an
114#   absolute path, accounting for all the special cases documented
115#   above.  While this macro is used to compute the various
116#   ``CMAKE_INSTALL_FULL_<dir>`` variables, it is exposed publicly to
117#   allow users who create additional path variables to also compute
118#   absolute paths where necessary, using the same logic.
119
120#=============================================================================
121# Copyright 2016, 2019 D. R. Commander
122# Copyright 2016 Dmitry Marakasov
123# Copyright 2016 Roger Leigh
124# Copyright 2015 Alex Turbov
125# Copyright 2014 Rolf Eike Beer
126# Copyright 2014 Daniele E. Domenichelli
127# Copyright 2013 Dimitri John Ledkov
128# Copyright 2011 Alex Neundorf
129# Copyright 2011 Eric NOULARD
130# Copyright 2011, 2013-2015 Kitware, Inc.
131# Copyright 2011 Nikita Krupen'ko
132#
133# Redistribution and use in source and binary forms, with or without
134# modification, are permitted provided that the following conditions
135# are met:
136#
137# * Redistributions of source code must retain the above copyright
138#   notice, this list of conditions and the following disclaimer.
139#
140# * Redistributions in binary form must reproduce the above copyright
141#   notice, this list of conditions and the following disclaimer in the
142#   documentation and/or other materials provided with the distribution.
143#
144# * Neither the names of Kitware, Inc., the Insight Software Consortium,
145#   nor the names of their contributors may be used to endorse or promote
146#   products derived from this software without specific prior written
147#   permission.
148#
149# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
150# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
151# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
152# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
153# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
154# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
155# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
156# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
157# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
158# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
159# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
160#=============================================================================
161
162# Installation directories
163#
164
165macro(GNUInstallDirs_set_install_dir var docstring)
166  # If CMAKE_INSTALL_PREFIX changes and CMAKE_INSTALL_*DIR is still set to the
167  # default value, then modify it accordingly.  This presumes that the default
168  # value may change based on the prefix.
169
170  set(_GNUInstallDirs_CMAKE_INSTALL_FORCE_${var} "")
171  if(NOT DEFINED CMAKE_INSTALL_${var})
172    set(_GNUInstallDirs_CMAKE_INSTALL_DEFAULT_${var} 1 CACHE INTERNAL
173      "CMAKE_INSTALL_${var} has default value")
174  elseif(DEFINED _GNUInstallDirs_CMAKE_INSTALL_LAST_DEFAULT_${var} AND
175    NOT "${_GNUInstallDirs_CMAKE_INSTALL_LAST_DEFAULT_${var}}" STREQUAL
176      "${CMAKE_INSTALL_DEFAULT_${var}}" AND
177    _GNUInstallDirs_CMAKE_INSTALL_DEFAULT_${var} AND
178    "${_GNUInstallDirs_CMAKE_INSTALL_LAST_${var}}" STREQUAL
179      "${CMAKE_INSTALL_${var}}")
180    set(_GNUInstallDirs_CMAKE_INSTALL_FORCE_${var} "FORCE")
181  endif()
182
183  set(CMAKE_INSTALL_${var} "${CMAKE_INSTALL_DEFAULT_${var}}" CACHE PATH
184    "${docstring} (Default: ${CMAKE_INSTALL_DEFAULT_${var}})"
185    ${_GNUInstallDirs_CMAKE_INSTALL_FORCE_${var}})
186
187  if(NOT CMAKE_INSTALL_${var} STREQUAL CMAKE_INSTALL_DEFAULT_${var})
188    unset(_GNUInstallDirs_CMAKE_INSTALL_DEFAULT_${var} CACHE)
189  endif()
190
191  # Save for next run
192  set(_GNUInstallDirs_CMAKE_INSTALL_LAST_${var} "${CMAKE_INSTALL_${var}}"
193    CACHE INTERNAL "CMAKE_INSTALL_${var} during last run")
194  set(_GNUInstallDirs_CMAKE_INSTALL_LAST_DEFAULT_${var}
195    "${CMAKE_INSTALL_DEFAULT_${var}}" CACHE INTERNAL
196    "CMAKE_INSTALL_DEFAULT_${var} during last run")
197endmacro()
198
199if(NOT DEFINED CMAKE_INSTALL_DEFAULT_BINDIR)
200  set(CMAKE_INSTALL_DEFAULT_BINDIR "bin")
201endif()
202GNUInstallDirs_set_install_dir(BINDIR
203  "Directory into which user executables should be installed")
204
205if(NOT DEFINED CMAKE_INSTALL_DEFAULT_SBINDIR)
206  set(CMAKE_INSTALL_DEFAULT_SBINDIR "sbin")
207endif()
208GNUInstallDirs_set_install_dir(SBINDIR
209  "Directory into which system admin executables should be installed")
210
211if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LIBEXECDIR)
212  set(CMAKE_INSTALL_DEFAULT_LIBEXECDIR "libexec")
213endif()
214GNUInstallDirs_set_install_dir(LIBEXECDIR
215  "Directory under which executables run by other programs should be installed")
216
217if(NOT DEFINED CMAKE_INSTALL_DEFAULT_SYSCONFDIR)
218  set(CMAKE_INSTALL_DEFAULT_SYSCONFDIR "etc")
219endif()
220GNUInstallDirs_set_install_dir(SYSCONFDIR
221  "Directory into which machine-specific read-only ASCII data and configuration files should be installed")
222
223if(NOT DEFINED CMAKE_INSTALL_DEFAULT_SHAREDSTATEDIR)
224  set(CMAKE_INSTALL_DEFAULT_SHAREDSTATEDIR "com")
225endif()
226GNUInstallDirs_set_install_dir(SHAREDSTATEDIR
227  "Directory into which architecture-independent run-time-modifiable data files should be installed")
228
229if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LOCALSTATEDIR)
230  set(CMAKE_INSTALL_DEFAULT_LOCALSTATEDIR "var")
231endif()
232GNUInstallDirs_set_install_dir(LOCALSTATEDIR
233  "Directory into which machine-specific run-time-modifiable data files should be installed")
234
235if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LIBDIR)
236  set(CMAKE_INSTALL_DEFAULT_LIBDIR "lib")
237  # Override this default 'lib' with 'lib64' iff:
238  #  - we are on Linux system but NOT cross-compiling
239  #  - we are NOT on debian
240  #  - we are on a 64 bits system
241  # reason is: amd64 ABI: http://www.x86-64.org/documentation/abi.pdf
242  # For Debian with multiarch, use 'lib/${CMAKE_LIBRARY_ARCHITECTURE}' if
243  # CMAKE_LIBRARY_ARCHITECTURE is set (which contains e.g. "i386-linux-gnu"
244  # and CMAKE_INSTALL_PREFIX is "/usr"
245  # See http://wiki.debian.org/Multiarch
246  if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
247      AND NOT CMAKE_CROSSCOMPILING)
248    if (EXISTS "/etc/debian_version") # is this a debian system ?
249      if(CMAKE_LIBRARY_ARCHITECTURE)
250        if("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
251          set(CMAKE_INSTALL_DEFAULT_LIBDIR "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
252        endif()
253      endif()
254    else() # not debian, rely on CMAKE_SIZEOF_VOID_P:
255      if(NOT DEFINED CMAKE_SIZEOF_VOID_P)
256        message(AUTHOR_WARNING
257          "Unable to determine default CMAKE_INSTALL_LIBDIR directory because no target architecture is known. "
258          "Please enable at least one language before including GNUInstallDirs.")
259      else()
260        if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
261          set(CMAKE_INSTALL_DEFAULT_LIBDIR "lib64")
262        endif()
263      endif()
264    endif()
265  endif()
266endif()
267GNUInstallDirs_set_install_dir(LIBDIR
268  "Directory into which object files and object code libraries should be installed")
269
270if(NOT DEFINED CMAKE_INSTALL_DEFAULT_INCLUDEDIR)
271  set(CMAKE_INSTALL_DEFAULT_INCLUDEDIR "include")
272endif()
273GNUInstallDirs_set_install_dir(INCLUDEDIR
274  "Directory into which C header files should be installed")
275
276if(NOT DEFINED CMAKE_INSTALL_DEFAULT_OLDINCLUDEDIR)
277  set(CMAKE_INSTALL_DEFAULT_OLDINCLUDEDIR "/usr/include")
278endif()
279GNUInstallDirs_set_install_dir(OLDINCLUDEDIR
280  PATH "Directory into which C header files for non-GCC compilers should be installed")
281
282if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DATAROOTDIR)
283  set(CMAKE_INSTALL_DEFAULT_DATAROOTDIR "share")
284endif()
285GNUInstallDirs_set_install_dir(DATAROOTDIR
286  "The root of the directory tree for read-only architecture-independent data files")
287
288#-----------------------------------------------------------------------------
289# Values whose defaults are relative to DATAROOTDIR.  Store empty values in
290# the cache and store the defaults in local variables if the cache values are
291# not set explicitly.  This auto-updates the defaults as DATAROOTDIR changes.
292
293if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DATADIR)
294  set(CMAKE_INSTALL_DEFAULT_DATADIR "<CMAKE_INSTALL_DATAROOTDIR>")
295endif()
296GNUInstallDirs_set_install_dir(DATADIR
297  "The directory under which read-only architecture-independent data files should be installed")
298
299if(NOT DEFINED CMAKE_INSTALL_DEFAULT_INFODIR)
300  if(CMAKE_SYSTEM_NAME MATCHES "^(.*BSD|DragonFly)$")
301    set(CMAKE_INSTALL_DEFAULT_INFODIR "info")
302  else()
303    set(CMAKE_INSTALL_DEFAULT_INFODIR "<CMAKE_INSTALL_DATAROOTDIR>/info")
304  endif()
305endif()
306GNUInstallDirs_set_install_dir(INFODIR
307  "The directory into which info documentation files should be installed")
308
309if(NOT DEFINED CMAKE_INSTALL_DEFAULT_MANDIR)
310  if(CMAKE_SYSTEM_NAME MATCHES "^(.*BSD|DragonFly)$")
311    set(CMAKE_INSTALL_DEFAULT_MANDIR "man")
312  else()
313    set(CMAKE_INSTALL_DEFAULT_MANDIR "<CMAKE_INSTALL_DATAROOTDIR>/man")
314  endif()
315endif()
316GNUInstallDirs_set_install_dir(MANDIR
317  "The directory under which man pages should be installed")
318
319if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LOCALEDIR)
320  set(CMAKE_INSTALL_DEFAULT_LOCALEDIR "<CMAKE_INSTALL_DATAROOTDIR>/locale")
321endif()
322GNUInstallDirs_set_install_dir(LOCALEDIR
323  "The directory under which locale-specific message catalogs should be installed")
324
325if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DOCDIR)
326  set(CMAKE_INSTALL_DEFAULT_DOCDIR "<CMAKE_INSTALL_DATAROOTDIR>/doc/${PROJECT_NAME}")
327endif()
328GNUInstallDirs_set_install_dir(DOCDIR
329  "The directory into which documentation files (other than info files) should be installed")
330
331#-----------------------------------------------------------------------------
332
333mark_as_advanced(
334  CMAKE_INSTALL_BINDIR
335  CMAKE_INSTALL_SBINDIR
336  CMAKE_INSTALL_LIBEXECDIR
337  CMAKE_INSTALL_SYSCONFDIR
338  CMAKE_INSTALL_SHAREDSTATEDIR
339  CMAKE_INSTALL_LOCALSTATEDIR
340  CMAKE_INSTALL_LIBDIR
341  CMAKE_INSTALL_INCLUDEDIR
342  CMAKE_INSTALL_OLDINCLUDEDIR
343  CMAKE_INSTALL_DATAROOTDIR
344  CMAKE_INSTALL_DATADIR
345  CMAKE_INSTALL_INFODIR
346  CMAKE_INSTALL_LOCALEDIR
347  CMAKE_INSTALL_MANDIR
348  CMAKE_INSTALL_DOCDIR
349  )
350
351macro(GNUInstallDirs_get_absolute_install_dir absvar var)
352  string(REGEX REPLACE "[<>]" "@" ${var} "${${var}}")
353  # Handle the specific case of an empty CMAKE_INSTALL_DATAROOTDIR
354  if(NOT CMAKE_INSTALL_DATAROOTDIR AND
355    ${var} MATCHES "\@CMAKE_INSTALL_DATAROOTDIR\@/")
356    string(CONFIGURE "${${var}}" ${var} @ONLY)
357    string(REGEX REPLACE "^/" "" ${var} "${${var}}")
358  else()
359    string(CONFIGURE "${${var}}" ${var} @ONLY)
360  endif()
361  if(NOT IS_ABSOLUTE "${${var}}")
362    # Handle special cases:
363    # - CMAKE_INSTALL_PREFIX == /
364    # - CMAKE_INSTALL_PREFIX == /usr
365    # - CMAKE_INSTALL_PREFIX == /opt/...
366    if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/")
367      if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
368        set(${absvar} "/${${var}}")
369      else()
370        if (NOT "${${var}}" MATCHES "^usr/")
371          set(${var} "usr/${${var}}")
372        endif()
373        set(${absvar} "/${${var}}")
374      endif()
375    elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
376      if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
377        set(${absvar} "/${${var}}")
378      else()
379        set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
380      endif()
381    elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/opt/.*")
382      if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
383        set(${absvar} "/${${var}}${CMAKE_INSTALL_PREFIX}")
384      else()
385        set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
386      endif()
387    else()
388      set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
389    endif()
390  else()
391    set(${absvar} "${${var}}")
392  endif()
393  string(REGEX REPLACE "/$" "" ${absvar} "${${absvar}}")
394endmacro()
395
396# Result directories
397#
398foreach(dir
399    BINDIR
400    SBINDIR
401    LIBEXECDIR
402    SYSCONFDIR
403    SHAREDSTATEDIR
404    LOCALSTATEDIR
405    LIBDIR
406    INCLUDEDIR
407    OLDINCLUDEDIR
408    DATAROOTDIR
409    DATADIR
410    INFODIR
411    LOCALEDIR
412    MANDIR
413    DOCDIR
414    )
415  GNUInstallDirs_get_absolute_install_dir(CMAKE_INSTALL_FULL_${dir} CMAKE_INSTALL_${dir})
416endforeach()
417