From 05798c17fb17d339d66e064a407e75ceae4c0316 Mon Sep 17 00:00:00 2001 From: Evgeny Proydakov Date: Sun, 7 Feb 2021 15:37:03 +0300 Subject: [PATCH] Fixed issues 2496. Used option 4: parse_version.cmake to avoid python3 usage. https://github.com/KhronosGroup/glslang/issues/2496 --- CMakeLists.txt | 44 ++++++++++++-------------------------------- build_info.h.tmpl | 8 ++++---- build_info.py | 14 +++++++------- parse_version.cmake | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 parse_version.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b176e451..d582926f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -234,55 +234,33 @@ if(NOT COMMAND find_host_package) endmacro() endif() -# CMake needs to find the right version of python, right from the beginning, -# otherwise, it will find the wrong version and fail later -find_host_package(PythonInterp 3 REQUIRED) - # Root directory for build-time generated include files set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include") ################################################################################ # Build version information generation ################################################################################ +include(parse_version.cmake) set(GLSLANG_CHANGES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md") -set(GLSLANG_BUILD_INFO_PY "${CMAKE_CURRENT_SOURCE_DIR}/build_info.py") set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl") set(GLSLANG_BUILD_INFO_H "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h") -# Command to build the build_info.h file -add_custom_command( - OUTPUT ${GLSLANG_BUILD_INFO_H} - COMMAND ${PYTHON_EXECUTABLE} "${GLSLANG_BUILD_INFO_PY}" - ${CMAKE_CURRENT_SOURCE_DIR} - "-i" ${GLSLANG_BUILD_INFO_H_TMPL} - "-o" ${GLSLANG_BUILD_INFO_H} - DEPENDS ${GLSLANG_BUILD_INFO_PY} - ${GLSLANG_CHANGES_FILE} - ${GLSLANG_BUILD_INFO_H_TMPL} - COMMENT "Generating ${GLSLANG_BUILD_INFO_H}") +parse_version(${GLSLANG_CHANGES_FILE} GLSLANG) -# Target to build the build_info.h file -add_custom_target(glslang-build-info DEPENDS ${GLSLANG_BUILD_INFO_H}) +function(configurate_version) + set(major ${GLSLANG_VERSION_MAJOR}) + set(minor ${GLSLANG_VERSION_MINOR}) + set(patch ${GLSLANG_VERSION_PATCH}) + set(flavor ${GLSLANG_VERSION_FLAVOR}) + configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY) +endfunction() -# Populate the CMake GLSLANG_VERSION* variables with the build version -# information. -execute_process( - COMMAND ${PYTHON_EXECUTABLE} "${GLSLANG_BUILD_INFO_PY}" - ${CMAKE_CURRENT_SOURCE_DIR} "..<-flavor>;;;;" - OUTPUT_VARIABLE "GLSLANG_VERSIONS" - OUTPUT_STRIP_TRAILING_WHITESPACE) -list(GET "GLSLANG_VERSIONS" 0 "GLSLANG_VERSION") -list(GET "GLSLANG_VERSIONS" 1 "GLSLANG_VERSION_MAJOR") -list(GET "GLSLANG_VERSIONS" 2 "GLSLANG_VERSION_MINOR") -list(GET "GLSLANG_VERSIONS" 3 "GLSLANG_VERSION_PATCH") -list(GET "GLSLANG_VERSIONS" 4 "GLSLANG_VERSION_FLAVOR") -configure_file(${GLSLANG_CHANGES_FILE} "${CMAKE_CURRENT_BINARY_DIR}/CHANGES.md") # Required to re-run cmake on version change +configurate_version() # glslang_add_build_info_dependency() adds the glslang-build-info dependency and # generated include directories to target. function(glslang_add_build_info_dependency target) target_include_directories(${target} PUBLIC $) - add_dependencies(${target} glslang-build-info) endfunction() # glslang_only_export_explicit_symbols() makes the symbol visibility hidden by @@ -316,6 +294,8 @@ else() endif() if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External) + find_package(PythonInterp 3 REQUIRED) + # We depend on these for later projects, so they should come first. add_subdirectory(External) endif() diff --git a/build_info.h.tmpl b/build_info.h.tmpl index eacecb09a..5b5e9e62c 100644 --- a/build_info.h.tmpl +++ b/build_info.h.tmpl @@ -34,10 +34,10 @@ #ifndef GLSLANG_BUILD_INFO #define GLSLANG_BUILD_INFO -#define GLSLANG_VERSION_MAJOR -#define GLSLANG_VERSION_MINOR -#define GLSLANG_VERSION_PATCH -#define GLSLANG_VERSION_FLAVOR "" +#define GLSLANG_VERSION_MAJOR @major@ +#define GLSLANG_VERSION_MINOR @minor@ +#define GLSLANG_VERSION_PATCH @patch@ +#define GLSLANG_VERSION_FLAVOR "@flavor@" #define GLSLANG_VERSION_GREATER_THAN(major, minor, patch) \ (((major) > GLSLANG_VERSION_MAJOR) || ((major) == GLSLANG_VERSION_MAJOR && \ diff --git a/build_info.py b/build_info.py index 2ac864b03..7c1998f6e 100755 --- a/build_info.py +++ b/build_info.py @@ -201,13 +201,13 @@ def main(): software_version = deduce_software_version(directory) commit = describe(directory) output = template \ - .replace("", software_version["major"]) \ - .replace("", software_version["minor"]) \ - .replace("", software_version["patch"]) \ - .replace("", software_version["flavor"]) \ - .replace("<-flavor>", software_version["-flavor"]) \ - .replace("", software_version["date"]) \ - .replace("", commit) + .replace("@major@", software_version["major"]) \ + .replace("@minor@", software_version["minor"]) \ + .replace("@patch@", software_version["patch"]) \ + .replace("@flavor@", software_version["flavor"]) \ + .replace("@-flavor@", software_version["-flavor"]) \ + .replace("@date@", software_version["date"]) \ + .replace("@commit@", commit) if output_file is None: print(output) diff --git a/parse_version.cmake b/parse_version.cmake new file mode 100644 index 000000000..b826096a3 --- /dev/null +++ b/parse_version.cmake @@ -0,0 +1,41 @@ +# Copyright 2020 The Marl Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# parse_version() reads and parses the version string from FILE, assigning the +# version string to ${PROJECT}_VERSION and the parsed version to +# ${PROJECT}_VERSION_MAJOR, ${PROJECT}_VERSION_MINOR, ${PROJECT}_VERSION_PATCH, +# and the optional ${PROJECT}_VERSION_FLAVOR. +# +# The version string take one of the forms: +# .. +# ..- +function(parse_version FILE PROJECT) + configure_file(${FILE} "${CMAKE_CURRENT_BINARY_DIR}/CHANGES.md") # Required to re-run cmake on version change + file(READ ${FILE} CHANGES) + if(${CHANGES} MATCHES "#+ *([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[a-zA-Z0-9]+)?") + set(FLAVOR "") + if(NOT "${CMAKE_MATCH_4}" STREQUAL "") + string(SUBSTRING ${CMAKE_MATCH_4} 1 -1 FLAVOR) + endif() + set("${PROJECT}_VERSION_MAJOR" ${CMAKE_MATCH_1} PARENT_SCOPE) + set("${PROJECT}_VERSION_MINOR" ${CMAKE_MATCH_2} PARENT_SCOPE) + set("${PROJECT}_VERSION_PATCH" ${CMAKE_MATCH_3} PARENT_SCOPE) + set("${PROJECT}_VERSION_FLAVOR" ${FLAVOR} PARENT_SCOPE) + set("${PROJECT}_VERSION" + "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}${CMAKE_MATCH_4}" + PARENT_SCOPE) + else() + message(FATAL_ERROR "Unable to parse version from '${FILE}'") + endif() +endfunction()