1617a3babSopenharmony_ci# Copyright (C) 2020 Google, Inc.
2617a3babSopenharmony_ci#
3617a3babSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4617a3babSopenharmony_ci# you may not use this file except in compliance with the License.
5617a3babSopenharmony_ci# You may obtain a copy of the License at
6617a3babSopenharmony_ci#
7617a3babSopenharmony_ci#     https://www.apache.org/licenses/LICENSE-2.0
8617a3babSopenharmony_ci#
9617a3babSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10617a3babSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
11617a3babSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12617a3babSopenharmony_ci# See the License for the specific language governing permissions and
13617a3babSopenharmony_ci# limitations under the License.
14617a3babSopenharmony_ci
15617a3babSopenharmony_ci# parse_version() reads and parses the version string from FILE, assigning the
16617a3babSopenharmony_ci# version string to ${PROJECT}_VERSION and the parsed version to
17617a3babSopenharmony_ci# ${PROJECT}_VERSION_MAJOR, ${PROJECT}_VERSION_MINOR, ${PROJECT}_VERSION_PATCH,
18617a3babSopenharmony_ci# and the optional ${PROJECT}_VERSION_FLAVOR.
19617a3babSopenharmony_ci#
20617a3babSopenharmony_ci# The version string take one of the forms:
21617a3babSopenharmony_ci#    <major>.<minor>.<patch>
22617a3babSopenharmony_ci#    <major>.<minor>.<patch>-<flavor>
23617a3babSopenharmony_cifunction(parse_version FILE PROJECT)
24617a3babSopenharmony_ci    configure_file(${FILE} "${CMAKE_CURRENT_BINARY_DIR}/CHANGES.md") # Required to re-run cmake on version change
25617a3babSopenharmony_ci    file(READ ${FILE} CHANGES)
26617a3babSopenharmony_ci    if(${CHANGES} MATCHES "#+ *([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[a-zA-Z0-9]+)?")
27617a3babSopenharmony_ci        set(FLAVOR "")
28617a3babSopenharmony_ci        if(NOT "${CMAKE_MATCH_4}" STREQUAL "")
29617a3babSopenharmony_ci            string(SUBSTRING ${CMAKE_MATCH_4} 1 -1 FLAVOR)
30617a3babSopenharmony_ci        endif()
31617a3babSopenharmony_ci        set("${PROJECT}_VERSION_MAJOR"  ${CMAKE_MATCH_1} PARENT_SCOPE)
32617a3babSopenharmony_ci        set("${PROJECT}_VERSION_MINOR"  ${CMAKE_MATCH_2} PARENT_SCOPE)
33617a3babSopenharmony_ci        set("${PROJECT}_VERSION_PATCH"  ${CMAKE_MATCH_3} PARENT_SCOPE)
34617a3babSopenharmony_ci        set("${PROJECT}_VERSION_FLAVOR" ${FLAVOR}        PARENT_SCOPE)
35617a3babSopenharmony_ci        set("${PROJECT}_VERSION"
36617a3babSopenharmony_ci            "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}${CMAKE_MATCH_4}"
37617a3babSopenharmony_ci            PARENT_SCOPE)
38617a3babSopenharmony_ci    else()
39617a3babSopenharmony_ci        message(FATAL_ERROR "Unable to parse version from '${FILE}'")
40617a3babSopenharmony_ci    endif()
41617a3babSopenharmony_ciendfunction()
42