blob: b9ba4137663117bae9817411791ef4e9668c7be0 [file] [log] [blame]
Evgeny Proydakove3dab6f2021-02-07 21:18:31 +03001# Copyright (C) 2020 Google, Inc.
Evgeny Proydakov05798c12021-02-07 15:37:03 +03002#
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
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.
19#
20# The version string take one of the forms:
21# <major>.<minor>.<patch>
22# <major>.<minor>.<patch>-<flavor>
23function(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]+)?")
27 set(FLAVOR "")
28 if(NOT "${CMAKE_MATCH_4}" STREQUAL "")
29 string(SUBSTRING ${CMAKE_MATCH_4} 1 -1 FLAVOR)
30 endif()
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}"
37 PARENT_SCOPE)
38 else()
39 message(FATAL_ERROR "Unable to parse version from '${FILE}'")
40 endif()
41endfunction()