GNUInstallDirs.cmake 16 KB

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