1#------------------------------------------------------------------------------- 2# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. 3# 4# SPDX-License-Identifier: BSD-3-Clause 5# 6#------------------------------------------------------------------------------- 7 8include_guard(DIRECTORY) 9 10include(${TS_ROOT}/tools/cmake/common/Utils.cmake REQUIRED) 11 12#[===[.rst: 13.. cmake:command:: version_semver_read 14 15 .. code:: cmake 16 17 version_semver_read(FILE <path> MAJOR <major> MINOR <minor> PATCH <patch>) 18 19 Parse version number from file to variables. The file must contain the version number in 20 semantic versioning format (https://semver.org). The file must not contain anything else, e.g. 21 no newline at the end, etc. 22 23 INPUTS: 24 25 ``FILE`` 26 Path to file that contains the version number. 27 28 OUTPUTS: 29 30 ``MAJOR`` 31 Major version parsed from the file. 32 33 ``MINOR`` 34 Minor version parsed from the file. 35 36 ``PATCH`` 37 Patch version parsed from the file. 38 39#]===] 40function(version_semver_read) 41 set(options) 42 set(oneValueArgs FILE MAJOR MINOR PATCH) 43 set(multiValueArgs) 44 cmake_parse_arguments(_MY_PARAMS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 45 46 check_args(version_semver_read FILE MAJOR MINOR PATCH) 47 48 file(READ "${_MY_PARAMS_FILE}" version_string) 49 50 # Note: double backslash is parsed in a quoted argument as just a single backslash. So the regex 51 # itself contains a single backslash, which escapes the period to a literal period. 52 string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" regexOut "${version_string}") 53 54 if(NOT CMAKE_MATCH_COUNT EQUAL 3) 55 message(FATAL_ERROR 56 "${_MY_PARAMS_FILE} contains invalid semantic version: \"${version_string}\"") 57 endif() 58 59 # CMAKE_MATCH_0 is the entire match, 1, 2, etc. are the actual capture groups 60 set(${_MY_PARAMS_MAJOR} ${CMAKE_MATCH_1} PARENT_SCOPE) 61 set(${_MY_PARAMS_MINOR} ${CMAKE_MATCH_2} PARENT_SCOPE) 62 set(${_MY_PARAMS_PATCH} ${CMAKE_MATCH_3} PARENT_SCOPE) 63endfunction() 64