parse_version.cmake 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (C) 2020 Google, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # parse_version() reads and parses the version string from FILE, assigning the
  15. # version string to ${PROJECT}_VERSION and the parsed version to
  16. # ${PROJECT}_VERSION_MAJOR, ${PROJECT}_VERSION_MINOR, ${PROJECT}_VERSION_PATCH,
  17. # and the optional ${PROJECT}_VERSION_FLAVOR.
  18. #
  19. # The version string take one of the forms:
  20. # <major>.<minor>.<patch>
  21. # <major>.<minor>.<patch>-<flavor>
  22. function(parse_version FILE PROJECT)
  23. configure_file(${FILE} "${CMAKE_CURRENT_BINARY_DIR}/CHANGES.md") # Required to re-run cmake on version change
  24. file(READ ${FILE} CHANGES)
  25. if(${CHANGES} MATCHES "#+ *([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[a-zA-Z0-9]+)?")
  26. set(FLAVOR "")
  27. if(NOT "${CMAKE_MATCH_4}" STREQUAL "")
  28. string(SUBSTRING ${CMAKE_MATCH_4} 1 -1 FLAVOR)
  29. endif()
  30. set("${PROJECT}_VERSION_MAJOR" ${CMAKE_MATCH_1} PARENT_SCOPE)
  31. set("${PROJECT}_VERSION_MINOR" ${CMAKE_MATCH_2} PARENT_SCOPE)
  32. set("${PROJECT}_VERSION_PATCH" ${CMAKE_MATCH_3} PARENT_SCOPE)
  33. set("${PROJECT}_VERSION_FLAVOR" ${FLAVOR} PARENT_SCOPE)
  34. set("${PROJECT}_VERSION"
  35. "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}${CMAKE_MATCH_4}"
  36. PARENT_SCOPE)
  37. else()
  38. message(FATAL_ERROR "Unable to parse version from '${FILE}'")
  39. endif()
  40. endfunction()