1 # Copyright (C) 2020 Google, Inc.
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
7 # https://www.apache.org/licenses/LICENSE-2.0
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.
15 # parse_version() reads and parses the version string from FILE, assigning the
16 # version string to ${PROJECT}_VERSION and the parsed version to
17 # ${PROJECT}_VERSION_MAJOR, ${PROJECT}_VERSION_MINOR, ${PROJECT}_VERSION_PATCH,
18 # and the optional ${PROJECT}_VERSION_FLAVOR.
20 # The version string take one of the forms:
21 # <major>.<minor>.<patch>
22 # <major>.<minor>.<patch>-<flavor>
23 function(parse_version FILE PROJECT)
24 configure_file(${FILE} "${CMAKE_CURRENT_BINARY_DIR}/CHANGES.md") # Required to re-run cmake on version change
25 file(READ ${FILE} CHANGES)
26 if(${CHANGES} MATCHES "#+ *([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[a-zA-Z0-9]+)?")
28 if(NOT "${CMAKE_MATCH_4}" STREQUAL "")
29 string(SUBSTRING ${CMAKE_MATCH_4} 1 -1 FLAVOR)
31 set("${PROJECT}_VERSION_MAJOR" ${CMAKE_MATCH_1} PARENT_SCOPE)
32 set("${PROJECT}_VERSION_MINOR" ${CMAKE_MATCH_2} PARENT_SCOPE)
33 set("${PROJECT}_VERSION_PATCH" ${CMAKE_MATCH_3} PARENT_SCOPE)
34 set("${PROJECT}_VERSION_FLAVOR" ${FLAVOR} PARENT_SCOPE)
35 set("${PROJECT}_VERSION"
36 "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}${CMAKE_MATCH_4}"
39 message(FATAL_ERROR "Unable to parse version from '${FILE}'")