GNUInstallDirs.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. GNUInstallDirs
  5. --------------
  6. Define GNU standard installation directories
  7. Provides install directory variables as defined by the
  8. `GNU Coding Standards`_.
  9. .. _`GNU Coding Standards`: https://www.gnu.org/prep/standards/html_node/Directory-Variables.html
  10. Result Variables
  11. ^^^^^^^^^^^^^^^^
  12. Inclusion of this module defines the following variables:
  13. ``CMAKE_INSTALL_<dir>``
  14. Destination for files of a given type. This value may be passed to
  15. the ``DESTINATION`` options of :command:`install` commands for the
  16. corresponding file type.
  17. ``CMAKE_INSTALL_FULL_<dir>``
  18. The absolute path generated from the corresponding ``CMAKE_INSTALL_<dir>``
  19. value. If the value is not already an absolute path, an absolute path
  20. is constructed typically by prepending the value of the
  21. :variable:`CMAKE_INSTALL_PREFIX` variable. However, there are some
  22. `special cases`_ as documented below.
  23. where ``<dir>`` is one of:
  24. ``BINDIR``
  25. user executables (``bin``)
  26. ``SBINDIR``
  27. system admin executables (``sbin``)
  28. ``LIBEXECDIR``
  29. program executables (``libexec``)
  30. ``SYSCONFDIR``
  31. read-only single-machine data (``etc``)
  32. ``SHAREDSTATEDIR``
  33. modifiable architecture-independent data (``com``)
  34. ``LOCALSTATEDIR``
  35. modifiable single-machine data (``var``)
  36. ``RUNSTATEDIR``
  37. run-time variable data (``LOCALSTATEDIR/run``)
  38. ``LIBDIR``
  39. object code libraries (``lib`` or ``lib64``
  40. or ``lib/<multiarch-tuple>`` on Debian)
  41. ``INCLUDEDIR``
  42. C header files (``include``)
  43. ``OLDINCLUDEDIR``
  44. C header files for non-gcc (``/usr/include``)
  45. ``DATAROOTDIR``
  46. read-only architecture-independent data root (``share``)
  47. ``DATADIR``
  48. read-only architecture-independent data (``DATAROOTDIR``)
  49. ``INFODIR``
  50. info documentation (``DATAROOTDIR/info``)
  51. ``LOCALEDIR``
  52. locale-dependent data (``DATAROOTDIR/locale``)
  53. ``MANDIR``
  54. man documentation (``DATAROOTDIR/man``)
  55. ``DOCDIR``
  56. documentation root (``DATAROOTDIR/doc/PROJECT_NAME``)
  57. If the includer does not define a value the above-shown default will be
  58. used and the value will appear in the cache for editing by the user.
  59. Special Cases
  60. ^^^^^^^^^^^^^
  61. The following values of :variable:`CMAKE_INSTALL_PREFIX` are special:
  62. ``/``
  63. For ``<dir>`` other than the ``SYSCONFDIR``, ``LOCALSTATEDIR`` and
  64. ``RUNSTATEDIR``, the value of ``CMAKE_INSTALL_<dir>`` is prefixed
  65. with ``usr/`` if it is not user-specified as an absolute path.
  66. For example, the ``INCLUDEDIR`` value ``include`` becomes ``usr/include``.
  67. This is required by the `GNU Coding Standards`_, which state:
  68. When building the complete GNU system, the prefix will be empty
  69. and ``/usr`` will be a symbolic link to ``/``.
  70. ``/usr``
  71. For ``<dir>`` equal to ``SYSCONFDIR``, ``LOCALSTATEDIR`` or
  72. ``RUNSTATEDIR``, the ``CMAKE_INSTALL_FULL_<dir>`` is computed by
  73. prepending just ``/`` to the value of ``CMAKE_INSTALL_<dir>``
  74. if it is not user-specified as an absolute path.
  75. For example, the ``SYSCONFDIR`` value ``etc`` becomes ``/etc``.
  76. This is required by the `GNU Coding Standards`_.
  77. ``/opt/...``
  78. For ``<dir>`` equal to ``SYSCONFDIR``, ``LOCALSTATEDIR`` or
  79. ``RUNSTATEDIR``, the ``CMAKE_INSTALL_FULL_<dir>`` is computed by
  80. *appending* the prefix to the value of ``CMAKE_INSTALL_<dir>``
  81. if it is not user-specified as an absolute path.
  82. For example, the ``SYSCONFDIR`` value ``etc`` becomes ``/etc/opt/...``.
  83. This is defined by the `Filesystem Hierarchy Standard`_.
  84. .. _`Filesystem Hierarchy Standard`: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html
  85. Macros
  86. ^^^^^^
  87. .. command:: GNUInstallDirs_get_absolute_install_dir
  88. ::
  89. GNUInstallDirs_get_absolute_install_dir(absvar var)
  90. Set the given variable ``absvar`` to the absolute path contained
  91. within the variable ``var``. This is to allow the computation of an
  92. absolute path, accounting for all the special cases documented
  93. above. While this macro is used to compute the various
  94. ``CMAKE_INSTALL_FULL_<dir>`` variables, it is exposed publicly to
  95. allow users who create additional path variables to also compute
  96. absolute paths where necessary, using the same logic.
  97. #]=======================================================================]
  98. cmake_policy(PUSH)
  99. cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
  100. # Convert a cache variable to PATH type
  101. macro(_GNUInstallDirs_cache_convert_to_path var description)
  102. get_property(_GNUInstallDirs_cache_type CACHE ${var} PROPERTY TYPE)
  103. if(_GNUInstallDirs_cache_type STREQUAL "UNINITIALIZED")
  104. file(TO_CMAKE_PATH "${${var}}" _GNUInstallDirs_cmakepath)
  105. set_property(CACHE ${var} PROPERTY TYPE PATH)
  106. set_property(CACHE ${var} PROPERTY VALUE "${_GNUInstallDirs_cmakepath}")
  107. set_property(CACHE ${var} PROPERTY HELPSTRING "${description}")
  108. unset(_GNUInstallDirs_cmakepath)
  109. endif()
  110. unset(_GNUInstallDirs_cache_type)
  111. endmacro()
  112. # Create a cache variable with default for a path.
  113. macro(_GNUInstallDirs_cache_path var default description)
  114. if(NOT DEFINED ${var})
  115. set(${var} "${default}" CACHE PATH "${description}")
  116. endif()
  117. _GNUInstallDirs_cache_convert_to_path("${var}" "${description}")
  118. endmacro()
  119. # Create a cache variable with not default for a path, with a fallback
  120. # when unset; used for entries slaved to other entries such as
  121. # DATAROOTDIR.
  122. macro(_GNUInstallDirs_cache_path_fallback var default description)
  123. if(NOT ${var})
  124. set(${var} "" CACHE PATH "${description}")
  125. set(${var} "${default}")
  126. endif()
  127. _GNUInstallDirs_cache_convert_to_path("${var}" "${description}")
  128. endmacro()
  129. # Installation directories
  130. #
  131. _GNUInstallDirs_cache_path(CMAKE_INSTALL_BINDIR "bin"
  132. "User executables (bin)")
  133. _GNUInstallDirs_cache_path(CMAKE_INSTALL_SBINDIR "sbin"
  134. "System admin executables (sbin)")
  135. _GNUInstallDirs_cache_path(CMAKE_INSTALL_LIBEXECDIR "libexec"
  136. "Program executables (libexec)")
  137. _GNUInstallDirs_cache_path(CMAKE_INSTALL_SYSCONFDIR "etc"
  138. "Read-only single-machine data (etc)")
  139. _GNUInstallDirs_cache_path(CMAKE_INSTALL_SHAREDSTATEDIR "com"
  140. "Modifiable architecture-independent data (com)")
  141. _GNUInstallDirs_cache_path(CMAKE_INSTALL_LOCALSTATEDIR "var"
  142. "Modifiable single-machine data (var)")
  143. # We check if the variable was manually set and not cached, in order to
  144. # allow projects to set the values as normal variables before including
  145. # GNUInstallDirs to avoid having the entries cached or user-editable. It
  146. # replaces the "if(NOT DEFINED CMAKE_INSTALL_XXX)" checks in all the
  147. # other cases.
  148. # If CMAKE_INSTALL_LIBDIR is defined, if _libdir_set is false, then the
  149. # variable is a normal one, otherwise it is a cache one.
  150. get_property(_libdir_set CACHE CMAKE_INSTALL_LIBDIR PROPERTY TYPE SET)
  151. if(NOT DEFINED CMAKE_INSTALL_LIBDIR OR (_libdir_set
  152. AND DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX
  153. AND NOT "${_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX}" STREQUAL "${CMAKE_INSTALL_PREFIX}"))
  154. # If CMAKE_INSTALL_LIBDIR is not defined, it is always executed.
  155. # Otherwise:
  156. # * if _libdir_set is false it is not executed (meaning that it is
  157. # not a cache variable)
  158. # * if _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX is not defined it is
  159. # not executed
  160. # * if _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX and
  161. # CMAKE_INSTALL_PREFIX are the same string it is not executed.
  162. # _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX is updated after the
  163. # execution, of this part of code, therefore at the next inclusion
  164. # of the file, CMAKE_INSTALL_LIBDIR is defined, and the 2 strings
  165. # are equal, meaning that the if is not executed the code the
  166. # second time.
  167. set(_LIBDIR_DEFAULT "lib")
  168. # Override this default 'lib' with 'lib64' iff:
  169. # - we are on Linux system but NOT cross-compiling
  170. # - we are NOT on debian
  171. # - we are on a 64 bits system
  172. # reason is: amd64 ABI: https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI
  173. # For Debian with multiarch, use 'lib/${CMAKE_LIBRARY_ARCHITECTURE}' if
  174. # CMAKE_LIBRARY_ARCHITECTURE is set (which contains e.g. "i386-linux-gnu"
  175. # and CMAKE_INSTALL_PREFIX is "/usr"
  176. # See http://wiki.debian.org/Multiarch
  177. if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
  178. set(__LAST_LIBDIR_DEFAULT "lib")
  179. # __LAST_LIBDIR_DEFAULT is the default value that we compute from
  180. # _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX, not a cache entry for
  181. # the value that was last used as the default.
  182. # This value is used to figure out whether the user changed the
  183. # CMAKE_INSTALL_LIBDIR value manually, or if the value was the
  184. # default one. When CMAKE_INSTALL_PREFIX changes, the value is
  185. # updated to the new default, unless the user explicitly changed it.
  186. endif()
  187. if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
  188. AND NOT CMAKE_CROSSCOMPILING
  189. AND NOT EXISTS "/etc/arch-release")
  190. if (EXISTS "/etc/debian_version") # is this a debian system ?
  191. if(CMAKE_LIBRARY_ARCHITECTURE)
  192. if("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
  193. set(_LIBDIR_DEFAULT "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
  194. endif()
  195. if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX
  196. AND "${_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
  197. set(__LAST_LIBDIR_DEFAULT "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
  198. endif()
  199. endif()
  200. else() # not debian, rely on CMAKE_SIZEOF_VOID_P:
  201. if(NOT DEFINED CMAKE_SIZEOF_VOID_P)
  202. message(AUTHOR_WARNING
  203. "Unable to determine default CMAKE_INSTALL_LIBDIR directory because no target architecture is known. "
  204. "Please enable at least one language before including GNUInstallDirs.")
  205. else()
  206. if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
  207. set(_LIBDIR_DEFAULT "lib64")
  208. if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
  209. set(__LAST_LIBDIR_DEFAULT "lib64")
  210. endif()
  211. endif()
  212. endif()
  213. endif()
  214. endif()
  215. if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
  216. set(CMAKE_INSTALL_LIBDIR "${_LIBDIR_DEFAULT}" CACHE PATH "Object code libraries (${_LIBDIR_DEFAULT})")
  217. elseif(DEFINED __LAST_LIBDIR_DEFAULT
  218. AND "${__LAST_LIBDIR_DEFAULT}" STREQUAL "${CMAKE_INSTALL_LIBDIR}")
  219. set_property(CACHE CMAKE_INSTALL_LIBDIR PROPERTY VALUE "${_LIBDIR_DEFAULT}")
  220. endif()
  221. endif()
  222. _GNUInstallDirs_cache_convert_to_path(CMAKE_INSTALL_LIBDIR "Object code libraries (lib)")
  223. # Save for next run
  224. set(_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" CACHE INTERNAL "CMAKE_INSTALL_PREFIX during last run")
  225. unset(_libdir_set)
  226. unset(__LAST_LIBDIR_DEFAULT)
  227. _GNUInstallDirs_cache_path(CMAKE_INSTALL_INCLUDEDIR "include"
  228. "C header files (include)")
  229. _GNUInstallDirs_cache_path(CMAKE_INSTALL_OLDINCLUDEDIR "/usr/include"
  230. "C header files for non-gcc (/usr/include)")
  231. _GNUInstallDirs_cache_path(CMAKE_INSTALL_DATAROOTDIR "share"
  232. "Read-only architecture-independent data root (share)")
  233. #-----------------------------------------------------------------------------
  234. # Values whose defaults are relative to DATAROOTDIR. Store empty values in
  235. # the cache and store the defaults in local variables if the cache values are
  236. # not set explicitly. This auto-updates the defaults as DATAROOTDIR changes.
  237. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_DATADIR "${CMAKE_INSTALL_DATAROOTDIR}"
  238. "Read-only architecture-independent data (DATAROOTDIR)")
  239. if(CMAKE_SYSTEM_NAME MATCHES "^(([^kF].*)?BSD|DragonFly)$")
  240. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_INFODIR "info"
  241. "Info documentation (info)")
  242. else()
  243. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_INFODIR "${CMAKE_INSTALL_DATAROOTDIR}/info"
  244. "Info documentation (DATAROOTDIR/info)")
  245. endif()
  246. if(CMAKE_SYSTEM_NAME MATCHES "^(([^k].*)?BSD|DragonFly)$")
  247. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_MANDIR "man"
  248. "Man documentation (man)")
  249. else()
  250. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_MANDIR "${CMAKE_INSTALL_DATAROOTDIR}/man"
  251. "Man documentation (DATAROOTDIR/man)")
  252. endif()
  253. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_LOCALEDIR "${CMAKE_INSTALL_DATAROOTDIR}/locale"
  254. "Locale-dependent data (DATAROOTDIR/locale)")
  255. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_DOCDIR "${CMAKE_INSTALL_DATAROOTDIR}/doc/${PROJECT_NAME}"
  256. "Documentation root (DATAROOTDIR/doc/PROJECT_NAME)")
  257. _GNUInstallDirs_cache_path_fallback(CMAKE_INSTALL_RUNSTATEDIR "${CMAKE_INSTALL_LOCALSTATEDIR}/run"
  258. "Run-time variable data (LOCALSTATEDIR/run)")
  259. #-----------------------------------------------------------------------------
  260. mark_as_advanced(
  261. CMAKE_INSTALL_BINDIR
  262. CMAKE_INSTALL_SBINDIR
  263. CMAKE_INSTALL_LIBEXECDIR
  264. CMAKE_INSTALL_SYSCONFDIR
  265. CMAKE_INSTALL_SHAREDSTATEDIR
  266. CMAKE_INSTALL_LOCALSTATEDIR
  267. CMAKE_INSTALL_RUNSTATEDIR
  268. CMAKE_INSTALL_LIBDIR
  269. CMAKE_INSTALL_INCLUDEDIR
  270. CMAKE_INSTALL_OLDINCLUDEDIR
  271. CMAKE_INSTALL_DATAROOTDIR
  272. CMAKE_INSTALL_DATADIR
  273. CMAKE_INSTALL_INFODIR
  274. CMAKE_INSTALL_LOCALEDIR
  275. CMAKE_INSTALL_MANDIR
  276. CMAKE_INSTALL_DOCDIR
  277. )
  278. macro(GNUInstallDirs_get_absolute_install_dir absvar var)
  279. if(NOT IS_ABSOLUTE "${${var}}")
  280. # Handle special cases:
  281. # - CMAKE_INSTALL_PREFIX == /
  282. # - CMAKE_INSTALL_PREFIX == /usr
  283. # - CMAKE_INSTALL_PREFIX == /opt/...
  284. if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/")
  285. if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR" OR "${dir}" STREQUAL "RUNSTATEDIR")
  286. set(${absvar} "/${${var}}")
  287. else()
  288. if (NOT "${${var}}" MATCHES "^usr/")
  289. set(${var} "usr/${${var}}")
  290. endif()
  291. set(${absvar} "/${${var}}")
  292. endif()
  293. elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
  294. if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR" OR "${dir}" STREQUAL "RUNSTATEDIR")
  295. set(${absvar} "/${${var}}")
  296. else()
  297. set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  298. endif()
  299. elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/opt/.*")
  300. if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR" OR "${dir}" STREQUAL "RUNSTATEDIR")
  301. set(${absvar} "/${${var}}${CMAKE_INSTALL_PREFIX}")
  302. else()
  303. set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  304. endif()
  305. else()
  306. set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  307. endif()
  308. else()
  309. set(${absvar} "${${var}}")
  310. endif()
  311. endmacro()
  312. # Result directories
  313. #
  314. foreach(dir
  315. BINDIR
  316. SBINDIR
  317. LIBEXECDIR
  318. SYSCONFDIR
  319. SHAREDSTATEDIR
  320. LOCALSTATEDIR
  321. RUNSTATEDIR
  322. LIBDIR
  323. INCLUDEDIR
  324. OLDINCLUDEDIR
  325. DATAROOTDIR
  326. DATADIR
  327. INFODIR
  328. LOCALEDIR
  329. MANDIR
  330. DOCDIR
  331. )
  332. GNUInstallDirs_get_absolute_install_dir(CMAKE_INSTALL_FULL_${dir} CMAKE_INSTALL_${dir})
  333. endforeach()
  334. cmake_policy(POP)