From 0cb24132198cca35d1036bf70675ff874bd453f9 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Fri, 10 Jan 2025 11:46:36 +0100 Subject: [PATCH] CMake: Fix sbom git vars not being available in various scopes Initially the git vars were assigned to the parent scope of the _qt_internal_sbom_begin_project function, with the intent to set them in the global scope. But the function was later wrapped in other functions, so the variables stopped being accessible. Instead of playing with recursive PARENT_SCOPEs, save the variables in global properties like we do for other info, and use a new _qt_internal_sbom_get_git_version_vars() function to query the vars in the code that needs them. This fixes generated purls to contain the git version and hashes. Also add a new internal API wrapper macro called qt_internal_sbom_get_git_version_vars to allow calling it in other repos. Pick-to: 6.8 Task-number: QTBUG-122899 Change-Id: I061b34f418c1ecc1c66c8c01ef758d2f40611ede Reviewed-by: Alexey Edelev (cherry picked from commit 41a92bf6f1486259ef25db775520cba647e1cc15) Reviewed-by: Qt Cherry-pick Bot --- cmake/QtPublicSbomGenerationHelpers.cmake | 2 ++ cmake/QtPublicSbomHelpers.cmake | 26 +++++++++++++++++------ cmake/QtPublicSbomPurlHelpers.cmake | 2 ++ cmake/QtSbomHelpers.cmake | 4 ++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/cmake/QtPublicSbomGenerationHelpers.cmake b/cmake/QtPublicSbomGenerationHelpers.cmake index b78b30593f5..e9460eb277b 100644 --- a/cmake/QtPublicSbomGenerationHelpers.cmake +++ b/cmake/QtPublicSbomGenerationHelpers.cmake @@ -78,6 +78,8 @@ function(_qt_internal_sbom_begin_project_generate) qt_internal_sbom_set_default_option_value(PROJECT "${PROJECT_NAME}") + _qt_internal_sbom_get_git_version_vars() + set(default_sbom_file_name "${arg_PROJECT}/${arg_PROJECT}-sbom-${QT_SBOM_GIT_VERSION_PATH}.spdx") set(default_install_sbom_path diff --git a/cmake/QtPublicSbomHelpers.cmake b/cmake/QtPublicSbomHelpers.cmake index 82685935eac..025beb58007 100644 --- a/cmake/QtPublicSbomHelpers.cmake +++ b/cmake/QtPublicSbomHelpers.cmake @@ -115,12 +115,11 @@ function(_qt_internal_sbom_begin_project) endif() endif() - # Set the variables in the outer scope, so they can be accessed by the generation functions - # in QtPublicSbomGenerationHelpers.cmake - set(QT_SBOM_GIT_VERSION "${QT_SBOM_GIT_VERSION}" PARENT_SCOPE) - set(QT_SBOM_GIT_VERSION_PATH "${QT_SBOM_GIT_VERSION_PATH}" PARENT_SCOPE) - set(QT_SBOM_GIT_HASH "${QT_SBOM_GIT_HASH}" PARENT_SCOPE) - set(QT_SBOM_GIT_HASH_SHORT "${QT_SBOM_GIT_HASH_SHORT}" PARENT_SCOPE) + # Save the variables in a global property to later query them in other functions. + set_property(GLOBAL PROPERTY QT_SBOM_GIT_VERSION "${QT_SBOM_GIT_VERSION}") + set_property(GLOBAL PROPERTY QT_SBOM_GIT_VERSION_PATH "${QT_SBOM_GIT_VERSION_PATH}") + set_property(GLOBAL PROPERTY QT_SBOM_GIT_HASH "${QT_SBOM_GIT_HASH}") + set_property(GLOBAL PROPERTY QT_SBOM_GIT_HASH_SHORT "${QT_SBOM_GIT_HASH_SHORT}") if(arg_DOCUMENT_NAMESPACE) set(repo_spdx_namespace "${arg_DOCUMENT_NAMESPACE}") @@ -1659,12 +1658,27 @@ endfunction() function(_qt_internal_sbom_get_qt_repo_source_download_location out_var) _qt_internal_sbom_get_root_project_name_lower_case(repo_project_name_lowercase) set(download_location "git://code.qt.io/qt/${repo_project_name_lowercase}.git") + + _qt_internal_sbom_get_git_version_vars() if(QT_SBOM_GIT_HASH) string(APPEND download_location "@${QT_SBOM_GIT_HASH}") endif() set(${out_var} "${download_location}" PARENT_SCOPE) endfunction() +# Queries the current project git version variables and sets them in the parent scope. +function(_qt_internal_sbom_get_git_version_vars) + get_cmake_property(QT_SBOM_GIT_VERSION QT_SBOM_GIT_VERSION) + get_cmake_property(QT_SBOM_GIT_VERSION_PATH QT_SBOM_GIT_VERSION_PATH) + get_cmake_property(QT_SBOM_GIT_HASH QT_SBOM_GIT_HASH) + get_cmake_property(QT_SBOM_GIT_HASH_SHORT QT_SBOM_GIT_HASH_SHORT) + + set(QT_SBOM_GIT_VERSION "${QT_SBOM_GIT_VERSION}" PARENT_SCOPE) + set(QT_SBOM_GIT_VERSION_PATH "${QT_SBOM_GIT_VERSION_PATH}" PARENT_SCOPE) + set(QT_SBOM_GIT_HASH "${QT_SBOM_GIT_HASH}" PARENT_SCOPE) + set(QT_SBOM_GIT_HASH_SHORT "${QT_SBOM_GIT_HASH_SHORT}" PARENT_SCOPE) +endfunction() + # Returns the configure line used to configure the current repo or top-level build, by reading # the config.opt file that the configure script writes out. # Returns an empty string if configure was not called, but CMake was called directly. diff --git a/cmake/QtPublicSbomPurlHelpers.cmake b/cmake/QtPublicSbomPurlHelpers.cmake index f8ea421b73c..dc5efc5361f 100644 --- a/cmake/QtPublicSbomPurlHelpers.cmake +++ b/cmake/QtPublicSbomPurlHelpers.cmake @@ -98,6 +98,8 @@ function(_qt_internal_sbom_handle_purl_values target) # List of purl variants to process. set(purl_variants "") + _qt_internal_sbom_get_git_version_vars() + set(third_party_types QT_THIRD_PARTY_MODULE QT_THIRD_PARTY_SOURCES diff --git a/cmake/QtSbomHelpers.cmake b/cmake/QtSbomHelpers.cmake index dc5b476f766..7ff1baf3598 100644 --- a/cmake/QtSbomHelpers.cmake +++ b/cmake/QtSbomHelpers.cmake @@ -101,3 +101,7 @@ function(qt_internal_sbom_get_external_document_ref_spdx_id project_name out_var set(${out_var} "${result}" PARENT_SCOPE) endfunction() + +macro(qt_internal_sbom_get_git_version_vars) + _qt_internal_sbom_get_git_version_vars() +endmacro()