GNUInstallDirs.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. # Copyright 2016, 2019 D. R. Commander
  121. # Copyright 2016 Dmitry Marakasov
  122. # Copyright 2016 Roger Leigh
  123. # Copyright 2015 Alex Turbov
  124. # Copyright 2014 Rolf Eike Beer
  125. # Copyright 2014 Daniele E. Domenichelli
  126. # Copyright 2013 Dimitri John Ledkov
  127. # Copyright 2011 Alex Neundorf
  128. # Copyright 2011 Eric NOULARD
  129. # Copyright 2011, 2013-2015 Kitware, Inc.
  130. # Copyright 2011 Nikita Krupen'ko
  131. #
  132. # Redistribution and use in source and binary forms, with or without
  133. # modification, are permitted provided that the following conditions
  134. # are met:
  135. #
  136. # * Redistributions of source code must retain the above copyright
  137. # notice, this list of conditions and the following disclaimer.
  138. #
  139. # * Redistributions in binary form must reproduce the above copyright
  140. # notice, this list of conditions and the following disclaimer in the
  141. # documentation and/or other materials provided with the distribution.
  142. #
  143. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  144. # nor the names of their contributors may be used to endorse or promote
  145. # products derived from this software without specific prior written
  146. # permission.
  147. #
  148. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  149. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  150. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  151. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  152. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  153. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  154. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  155. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  156. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  157. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  158. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  159. #=============================================================================
  160. # Installation directories
  161. #
  162. macro(GNUInstallDirs_set_install_dir var docstring)
  163. # If CMAKE_INSTALL_PREFIX changes and CMAKE_INSTALL_*DIR is still set to the
  164. # default value, then modify it accordingly. This presumes that the default
  165. # value may change based on the prefix.
  166. set(_GNUInstallDirs_CMAKE_INSTALL_FORCE_${var} "")
  167. if(NOT DEFINED CMAKE_INSTALL_${var})
  168. set(_GNUInstallDirs_CMAKE_INSTALL_DEFAULT_${var} 1 CACHE INTERNAL
  169. "CMAKE_INSTALL_${var} has default value")
  170. elseif(DEFINED _GNUInstallDirs_CMAKE_INSTALL_LAST_DEFAULT_${var} AND
  171. NOT "${_GNUInstallDirs_CMAKE_INSTALL_LAST_DEFAULT_${var}}" STREQUAL
  172. "${CMAKE_INSTALL_DEFAULT_${var}}" AND
  173. _GNUInstallDirs_CMAKE_INSTALL_DEFAULT_${var} AND
  174. "${_GNUInstallDirs_CMAKE_INSTALL_LAST_${var}}" STREQUAL
  175. "${CMAKE_INSTALL_${var}}")
  176. set(_GNUInstallDirs_CMAKE_INSTALL_FORCE_${var} "FORCE")
  177. endif()
  178. set(CMAKE_INSTALL_${var} "${CMAKE_INSTALL_DEFAULT_${var}}" CACHE PATH
  179. "${docstring} (Default: ${CMAKE_INSTALL_DEFAULT_${var}})"
  180. ${_GNUInstallDirs_CMAKE_INSTALL_FORCE_${var}})
  181. if(NOT CMAKE_INSTALL_${var} STREQUAL CMAKE_INSTALL_DEFAULT_${var})
  182. unset(_GNUInstallDirs_CMAKE_INSTALL_DEFAULT_${var} CACHE)
  183. endif()
  184. # Save for next run
  185. set(_GNUInstallDirs_CMAKE_INSTALL_LAST_${var} "${CMAKE_INSTALL_${var}}"
  186. CACHE INTERNAL "CMAKE_INSTALL_${var} during last run")
  187. set(_GNUInstallDirs_CMAKE_INSTALL_LAST_DEFAULT_${var}
  188. "${CMAKE_INSTALL_DEFAULT_${var}}" CACHE INTERNAL
  189. "CMAKE_INSTALL_DEFAULT_${var} during last run")
  190. endmacro()
  191. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_BINDIR)
  192. set(CMAKE_INSTALL_DEFAULT_BINDIR "bin")
  193. endif()
  194. GNUInstallDirs_set_install_dir(BINDIR
  195. "Directory into which user executables should be installed")
  196. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_SBINDIR)
  197. set(CMAKE_INSTALL_DEFAULT_SBINDIR "sbin")
  198. endif()
  199. GNUInstallDirs_set_install_dir(SBINDIR
  200. "Directory into which system admin executables should be installed")
  201. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LIBEXECDIR)
  202. set(CMAKE_INSTALL_DEFAULT_LIBEXECDIR "libexec")
  203. endif()
  204. GNUInstallDirs_set_install_dir(LIBEXECDIR
  205. "Directory under which executables run by other programs should be installed")
  206. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_SYSCONFDIR)
  207. set(CMAKE_INSTALL_DEFAULT_SYSCONFDIR "etc")
  208. endif()
  209. GNUInstallDirs_set_install_dir(SYSCONFDIR
  210. "Directory into which machine-specific read-only ASCII data and configuration files should be installed")
  211. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_SHAREDSTATEDIR)
  212. set(CMAKE_INSTALL_DEFAULT_SHAREDSTATEDIR "com")
  213. endif()
  214. GNUInstallDirs_set_install_dir(SHAREDSTATEDIR
  215. "Directory into which architecture-independent run-time-modifiable data files should be installed")
  216. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LOCALSTATEDIR)
  217. set(CMAKE_INSTALL_DEFAULT_LOCALSTATEDIR "var")
  218. endif()
  219. GNUInstallDirs_set_install_dir(LOCALSTATEDIR
  220. "Directory into which machine-specific run-time-modifiable data files should be installed")
  221. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LIBDIR)
  222. set(CMAKE_INSTALL_DEFAULT_LIBDIR "lib")
  223. # Override this default 'lib' with 'lib64' iff:
  224. # - we are on Linux system but NOT cross-compiling
  225. # - we are NOT on debian
  226. # - we are on a 64 bits system
  227. # reason is: amd64 ABI: http://www.x86-64.org/documentation/abi.pdf
  228. # For Debian with multiarch, use 'lib/${CMAKE_LIBRARY_ARCHITECTURE}' if
  229. # CMAKE_LIBRARY_ARCHITECTURE is set (which contains e.g. "i386-linux-gnu"
  230. # and CMAKE_INSTALL_PREFIX is "/usr"
  231. # See http://wiki.debian.org/Multiarch
  232. if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
  233. AND NOT CMAKE_CROSSCOMPILING)
  234. if (EXISTS "/etc/debian_version") # is this a debian system ?
  235. if(CMAKE_LIBRARY_ARCHITECTURE)
  236. if("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
  237. set(CMAKE_INSTALL_DEFAULT_LIBDIR "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
  238. endif()
  239. endif()
  240. else() # not debian, rely on CMAKE_SIZEOF_VOID_P:
  241. if(NOT DEFINED CMAKE_SIZEOF_VOID_P)
  242. message(AUTHOR_WARNING
  243. "Unable to determine default CMAKE_INSTALL_LIBDIR directory because no target architecture is known. "
  244. "Please enable at least one language before including GNUInstallDirs.")
  245. else()
  246. if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
  247. set(CMAKE_INSTALL_DEFAULT_LIBDIR "lib64")
  248. endif()
  249. endif()
  250. endif()
  251. endif()
  252. endif()
  253. GNUInstallDirs_set_install_dir(LIBDIR
  254. "Directory into which object files and object code libraries should be installed")
  255. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_INCLUDEDIR)
  256. set(CMAKE_INSTALL_DEFAULT_INCLUDEDIR "include")
  257. endif()
  258. GNUInstallDirs_set_install_dir(INCLUDEDIR
  259. "Directory into which C header files should be installed")
  260. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_OLDINCLUDEDIR)
  261. set(CMAKE_INSTALL_DEFAULT_OLDINCLUDEDIR "/usr/include")
  262. endif()
  263. GNUInstallDirs_set_install_dir(OLDINCLUDEDIR
  264. PATH "Directory into which C header files for non-GCC compilers should be installed")
  265. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DATAROOTDIR)
  266. set(CMAKE_INSTALL_DEFAULT_DATAROOTDIR "share")
  267. endif()
  268. GNUInstallDirs_set_install_dir(DATAROOTDIR
  269. "The root of the directory tree for read-only architecture-independent data files")
  270. #-----------------------------------------------------------------------------
  271. # Values whose defaults are relative to DATAROOTDIR. Store empty values in
  272. # the cache and store the defaults in local variables if the cache values are
  273. # not set explicitly. This auto-updates the defaults as DATAROOTDIR changes.
  274. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DATADIR)
  275. set(CMAKE_INSTALL_DEFAULT_DATADIR "<CMAKE_INSTALL_DATAROOTDIR>")
  276. endif()
  277. GNUInstallDirs_set_install_dir(DATADIR
  278. "The directory under which read-only architecture-independent data files should be installed")
  279. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_INFODIR)
  280. if(CMAKE_SYSTEM_NAME MATCHES "^(.*BSD|DragonFly)$")
  281. set(CMAKE_INSTALL_DEFAULT_INFODIR "info")
  282. else()
  283. set(CMAKE_INSTALL_DEFAULT_INFODIR "<CMAKE_INSTALL_DATAROOTDIR>/info")
  284. endif()
  285. endif()
  286. GNUInstallDirs_set_install_dir(INFODIR
  287. "The directory into which info documentation files should be installed")
  288. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_MANDIR)
  289. if(CMAKE_SYSTEM_NAME MATCHES "^(.*BSD|DragonFly)$")
  290. set(CMAKE_INSTALL_DEFAULT_MANDIR "man")
  291. else()
  292. set(CMAKE_INSTALL_DEFAULT_MANDIR "<CMAKE_INSTALL_DATAROOTDIR>/man")
  293. endif()
  294. endif()
  295. GNUInstallDirs_set_install_dir(MANDIR
  296. "The directory under which man pages should be installed")
  297. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_LOCALEDIR)
  298. set(CMAKE_INSTALL_DEFAULT_LOCALEDIR "<CMAKE_INSTALL_DATAROOTDIR>/locale")
  299. endif()
  300. GNUInstallDirs_set_install_dir(LOCALEDIR
  301. "The directory under which locale-specific message catalogs should be installed")
  302. if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DOCDIR)
  303. set(CMAKE_INSTALL_DEFAULT_DOCDIR "<CMAKE_INSTALL_DATAROOTDIR>/doc/${PROJECT_NAME}")
  304. endif()
  305. GNUInstallDirs_set_install_dir(DOCDIR
  306. "The directory into which documentation files (other than info files) should be installed")
  307. #-----------------------------------------------------------------------------
  308. mark_as_advanced(
  309. CMAKE_INSTALL_BINDIR
  310. CMAKE_INSTALL_SBINDIR
  311. CMAKE_INSTALL_LIBEXECDIR
  312. CMAKE_INSTALL_SYSCONFDIR
  313. CMAKE_INSTALL_SHAREDSTATEDIR
  314. CMAKE_INSTALL_LOCALSTATEDIR
  315. CMAKE_INSTALL_LIBDIR
  316. CMAKE_INSTALL_INCLUDEDIR
  317. CMAKE_INSTALL_OLDINCLUDEDIR
  318. CMAKE_INSTALL_DATAROOTDIR
  319. CMAKE_INSTALL_DATADIR
  320. CMAKE_INSTALL_INFODIR
  321. CMAKE_INSTALL_LOCALEDIR
  322. CMAKE_INSTALL_MANDIR
  323. CMAKE_INSTALL_DOCDIR
  324. )
  325. macro(GNUInstallDirs_get_absolute_install_dir absvar var)
  326. string(REGEX REPLACE "[<>]" "@" ${var} "${${var}}")
  327. # Handle the specific case of an empty CMAKE_INSTALL_DATAROOTDIR
  328. if(NOT CMAKE_INSTALL_DATAROOTDIR AND
  329. ${var} MATCHES "\@CMAKE_INSTALL_DATAROOTDIR\@/")
  330. string(CONFIGURE "${${var}}" ${var} @ONLY)
  331. string(REGEX REPLACE "^/" "" ${var} "${${var}}")
  332. else()
  333. string(CONFIGURE "${${var}}" ${var} @ONLY)
  334. endif()
  335. if(NOT IS_ABSOLUTE "${${var}}")
  336. # Handle special cases:
  337. # - CMAKE_INSTALL_PREFIX == /
  338. # - CMAKE_INSTALL_PREFIX == /usr
  339. # - CMAKE_INSTALL_PREFIX == /opt/...
  340. if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/")
  341. if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
  342. set(${absvar} "/${${var}}")
  343. else()
  344. if (NOT "${${var}}" MATCHES "^usr/")
  345. set(${var} "usr/${${var}}")
  346. endif()
  347. set(${absvar} "/${${var}}")
  348. endif()
  349. elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
  350. if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
  351. set(${absvar} "/${${var}}")
  352. else()
  353. set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  354. endif()
  355. elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/opt/.*")
  356. if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
  357. set(${absvar} "/${${var}}${CMAKE_INSTALL_PREFIX}")
  358. else()
  359. set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  360. endif()
  361. else()
  362. set(${absvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  363. endif()
  364. else()
  365. set(${absvar} "${${var}}")
  366. endif()
  367. string(REGEX REPLACE "/$" "" ${absvar} "${${absvar}}")
  368. endmacro()
  369. # Result directories
  370. #
  371. foreach(dir
  372. BINDIR
  373. SBINDIR
  374. LIBEXECDIR
  375. SYSCONFDIR
  376. SHAREDSTATEDIR
  377. LOCALSTATEDIR
  378. LIBDIR
  379. INCLUDEDIR
  380. OLDINCLUDEDIR
  381. DATAROOTDIR
  382. DATADIR
  383. INFODIR
  384. LOCALEDIR
  385. MANDIR
  386. DOCDIR
  387. )
  388. GNUInstallDirs_get_absolute_install_dir(CMAKE_INSTALL_FULL_${dir} CMAKE_INSTALL_${dir})
  389. endforeach()