GNUInstallDirs.cmake 14 KB

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