ECMSetupVersion.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #.rst:
  2. # ECMSetupVersion
  3. # ---------------
  4. #
  5. # Handle library version information.
  6. #
  7. # ::
  8. #
  9. # ecm_setup_version(<version>
  10. # VARIABLE_PREFIX <prefix>
  11. # [SOVERSION <soversion>]
  12. # [VERSION_HEADER <filename>]
  13. # [PACKAGE_VERSION_FILE <filename> [COMPATIBILITY <compat>]] )
  14. #
  15. # This parses a version string and sets up a standard set of version variables.
  16. # It can optionally also create a C version header file and a CMake package
  17. # version file to install along with the library.
  18. #
  19. # If the ``<version>`` argument is of the form ``<major>.<minor>.<patch>``
  20. # (or ``<major>.<minor>.<patch>.<tweak>``), The following CMake variables are
  21. # set::
  22. #
  23. # <prefix>_VERSION_MAJOR - <major>
  24. # <prefix>_VERSION_MINOR - <minor>
  25. # <prefix>_VERSION_PATCH - <patch>
  26. # <prefix>_VERSION - <version>
  27. # <prefix>_VERSION_STRING - <version> (for compatibility: use <prefix>_VERSION instead)
  28. # <prefix>_SOVERSION - <soversion>, or <major> if SOVERSION was not given
  29. #
  30. # If CMake policy CMP0048 is not NEW, the following CMake variables will also
  31. # be set::
  32. #
  33. # PROJECT_VERSION_MAJOR - <major>
  34. # PROJECT_VERSION_MINOR - <minor>
  35. # PROJECT_VERSION_PATCH - <patch>
  36. # PROJECT_VERSION - <version>
  37. # PROJECT_VERSION_STRING - <version> (for compatibility: use PROJECT_VERSION instead)
  38. #
  39. # If the VERSION_HEADER option is used, a simple C header is generated with the
  40. # given filename. If filename is a relative path, it is interpreted as relative
  41. # to CMAKE_CURRENT_BINARY_DIR. The generated header contains the following
  42. # macros::
  43. #
  44. # <prefix>_VERSION_MAJOR - <major> as an integer
  45. # <prefix>_VERSION_MINOR - <minor> as an integer
  46. # <prefix>_VERSION_PATCH - <patch> as an integer
  47. # <prefix>_VERSION_STRING - <version> as a C string
  48. # <prefix>_VERSION - the version as an integer
  49. #
  50. # ``<prefix>_VERSION`` has ``<patch>`` in the bottom 8 bits, ``<minor>`` in the
  51. # next 8 bits and ``<major>`` in the remaining bits. Note that ``<patch>`` and
  52. # ``<minor>`` must be less than 256.
  53. #
  54. # If the PACKAGE_VERSION_FILE option is used, a simple CMake package version
  55. # file is created using the write_basic_package_version_file() macro provided by
  56. # CMake. It should be installed in the same location as the Config.cmake file of
  57. # the library so that it can be found by find_package(). If the filename is a
  58. # relative path, it is interpreted as relative to CMAKE_CURRENT_BINARY_DIR. The
  59. # optional COMPATIBILITY option is forwarded to
  60. # write_basic_package_version_file(), and defaults to AnyNewerVersion.
  61. #
  62. # If CMake policy CMP0048 is NEW, an alternative form of the command is
  63. # available::
  64. #
  65. # ecm_setup_version(PROJECT
  66. # [VARIABLE_PREFIX <prefix>]
  67. # [SOVERSION <soversion>]
  68. # [VERSION_HEADER <filename>]
  69. # [PACKAGE_VERSION_FILE <filename>] )
  70. #
  71. # This will use the version information set by the project() command.
  72. # VARIABLE_PREFIX defaults to the project name. Note that PROJECT must be the
  73. # first argument. In all other respects, it behaves like the other form of the
  74. # command.
  75. #
  76. # Since pre-1.0.0.
  77. #
  78. # COMPATIBILITY option available since 1.6.0.
  79. #=============================================================================
  80. # SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org>
  81. # SPDX-FileCopyrightText: 2012 Alexander Neundorf <neundorf@kde.org>
  82. #
  83. # SPDX-License-Identifier: BSD-3-Clause
  84. include(CMakePackageConfigHelpers)
  85. # save the location of the header template while CMAKE_CURRENT_LIST_DIR
  86. # has the value we want
  87. set(_ECM_SETUP_VERSION_HEADER_TEMPLATE "${CMAKE_CURRENT_LIST_DIR}/ECMVersionHeader.h.in")
  88. function(ecm_setup_version _version)
  89. set(options )
  90. set(oneValueArgs VARIABLE_PREFIX SOVERSION VERSION_HEADER PACKAGE_VERSION_FILE COMPATIBILITY)
  91. set(multiValueArgs )
  92. cmake_parse_arguments(ESV "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  93. if(ESV_UNPARSED_ARGUMENTS)
  94. message(FATAL_ERROR "Unknown keywords given to ECM_SETUP_VERSION(): \"${ESV_UNPARSED_ARGUMENTS}\"")
  95. endif()
  96. set(project_manages_version FALSE)
  97. set(use_project_version FALSE)
  98. # CMP0048 only exists in CMake 3.0.0 and later
  99. if(CMAKE_VERSION VERSION_LESS 3.0.0)
  100. set(project_version_policy "OLD")
  101. else()
  102. cmake_policy(GET CMP0048 project_version_policy)
  103. endif()
  104. if(project_version_policy STREQUAL "NEW")
  105. set(project_manages_version TRUE)
  106. if(_version STREQUAL "PROJECT")
  107. set(use_project_version TRUE)
  108. endif()
  109. elseif(_version STREQUAL "PROJECT")
  110. message(FATAL_ERROR "ecm_setup_version given PROJECT argument, but CMP0048 is not NEW")
  111. endif()
  112. set(should_set_prefixed_vars TRUE)
  113. if(NOT ESV_VARIABLE_PREFIX)
  114. if(use_project_version)
  115. set(ESV_VARIABLE_PREFIX "${PROJECT_NAME}")
  116. set(should_set_prefixed_vars FALSE)
  117. else()
  118. message(FATAL_ERROR "Required argument PREFIX missing in ECM_SETUP_VERSION() call")
  119. endif()
  120. endif()
  121. if(use_project_version)
  122. set(_version "${PROJECT_VERSION}")
  123. set(_major "${PROJECT_VERSION_MAJOR}")
  124. set(_minor "${PROJECT_VERSION_MINOR}")
  125. set(_patch "${PROJECT_VERSION_PATCH}")
  126. else()
  127. string(REGEX REPLACE "^0*([0-9]+)\\.[0-9]+\\.[0-9]+.*" "\\1" _major "${_version}")
  128. string(REGEX REPLACE "^[0-9]+\\.0*([0-9]+)\\.[0-9]+.*" "\\1" _minor "${_version}")
  129. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.0*([0-9]+).*" "\\1" _patch "${_version}")
  130. endif()
  131. if(NOT ESV_SOVERSION)
  132. set(ESV_SOVERSION ${_major})
  133. endif()
  134. if(should_set_prefixed_vars)
  135. set(${ESV_VARIABLE_PREFIX}_VERSION "${_version}")
  136. set(${ESV_VARIABLE_PREFIX}_VERSION_MAJOR ${_major})
  137. set(${ESV_VARIABLE_PREFIX}_VERSION_MINOR ${_minor})
  138. set(${ESV_VARIABLE_PREFIX}_VERSION_PATCH ${_patch})
  139. endif()
  140. set(${ESV_VARIABLE_PREFIX}_SOVERSION ${ESV_SOVERSION})
  141. if(NOT project_manages_version)
  142. set(PROJECT_VERSION "${_version}")
  143. set(PROJECT_VERSION_MAJOR "${_major}")
  144. set(PROJECT_VERSION_MINOR "${_minor}")
  145. set(PROJECT_VERSION_PATCH "${_patch}")
  146. endif()
  147. # compat
  148. set(PROJECT_VERSION_STRING "${PROJECT_VERSION}")
  149. set(${ESV_VARIABLE_PREFIX}_VERSION_STRING "${${ESV_VARIABLE_PREFIX}_VERSION}")
  150. if(ESV_VERSION_HEADER)
  151. set(HEADER_PREFIX "${ESV_VARIABLE_PREFIX}")
  152. set(HEADER_VERSION "${_version}")
  153. set(HEADER_VERSION_MAJOR "${_major}")
  154. set(HEADER_VERSION_MINOR "${_minor}")
  155. set(HEADER_VERSION_PATCH "${_patch}")
  156. configure_file("${_ECM_SETUP_VERSION_HEADER_TEMPLATE}" "${ESV_VERSION_HEADER}")
  157. endif()
  158. if(ESV_PACKAGE_VERSION_FILE)
  159. if(NOT ESV_COMPATIBILITY)
  160. set(ESV_COMPATIBILITY AnyNewerVersion)
  161. endif()
  162. write_basic_package_version_file("${ESV_PACKAGE_VERSION_FILE}" VERSION ${_version} COMPATIBILITY ${ESV_COMPATIBILITY})
  163. endif()
  164. if(should_set_prefixed_vars)
  165. set(${ESV_VARIABLE_PREFIX}_VERSION_MAJOR "${${ESV_VARIABLE_PREFIX}_VERSION_MAJOR}" PARENT_SCOPE)
  166. set(${ESV_VARIABLE_PREFIX}_VERSION_MINOR "${${ESV_VARIABLE_PREFIX}_VERSION_MINOR}" PARENT_SCOPE)
  167. set(${ESV_VARIABLE_PREFIX}_VERSION_PATCH "${${ESV_VARIABLE_PREFIX}_VERSION_PATCH}" PARENT_SCOPE)
  168. set(${ESV_VARIABLE_PREFIX}_VERSION "${${ESV_VARIABLE_PREFIX}_VERSION}" PARENT_SCOPE)
  169. endif()
  170. # always set the soversion
  171. set(${ESV_VARIABLE_PREFIX}_SOVERSION "${${ESV_VARIABLE_PREFIX}_SOVERSION}" PARENT_SCOPE)
  172. if(NOT project_manages_version)
  173. set(PROJECT_VERSION "${PROJECT_VERSION}" PARENT_SCOPE)
  174. set(PROJECT_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}" PARENT_SCOPE)
  175. set(PROJECT_VERSION_MINOR "${PROJECT_VERSION_MINOR}" PARENT_SCOPE)
  176. set(PROJECT_VERSION_PATCH "${PROJECT_VERSION_PATCH}" PARENT_SCOPE)
  177. endif()
  178. # always set the compatibility variables
  179. set(PROJECT_VERSION_STRING "${PROJECT_VERSION_STRING}" PARENT_SCOPE)
  180. set(${ESV_VARIABLE_PREFIX}_VERSION_STRING "${${ESV_VARIABLE_PREFIX}_VERSION}" PARENT_SCOPE)
  181. endfunction()