GNUInstallDirs.cmake 13 KB

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