Fix SPDX generation with older CMake (#53440)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Billy O'Neal 2026-08-17 13:00:04 -07:00 committed by GitHub
parent e4ed9022bd
commit b558d01407
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 91 additions and 4 deletions

View File

@ -1,3 +1,39 @@
function(z_vcpkg_spdx_json_string_encode_compat out_var value)
set(encoded "${value}")
string(REPLACE "\\" "\\\\" encoded "${encoded}") # that is, replace \ with \\
string(REPLACE "\"" "\\\"" encoded "${encoded}") # that is, replace " with \"
string(ASCII 8 backspace)
string(ASCII 12 form_feed)
string(REPLACE "${backspace}" "\\b" encoded "${encoded}")
string(REPLACE "${form_feed}" "\\f" encoded "${encoded}")
string(REPLACE "\n" "\\n" encoded "${encoded}")
string(REPLACE "\r" "\\r" encoded "${encoded}")
string(REPLACE "\t" "\\t" encoded "${encoded}")
foreach(code RANGE 1 31)
string(ASCII "${code}" character)
math(EXPR hexadecimal "${code}" OUTPUT_FORMAT HEXADECIMAL)
string(SUBSTRING "${hexadecimal}" 2 -1 hexadecimal)
if(code LESS 16)
set(escape "\\u000${hexadecimal}")
else()
set(escape "\\u00${hexadecimal}")
endif()
string(REPLACE "${character}" "${escape}" encoded "${encoded}")
endforeach()
set("${out_var}" "\"${encoded}\"" PARENT_SCOPE)
endfunction()
function(z_vcpkg_spdx_json_string_encode out_var value)
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.3")
string(JSON encoded STRING_ENCODE "${value}")
else()
z_vcpkg_spdx_json_string_encode_compat(encoded "${value}")
endif()
set("${out_var}" "${encoded}" PARENT_SCOPE)
endfunction()
function(z_vcpkg_add_spdx_resource)
cmake_parse_arguments(PARSE_ARGV 0 "arg"
""
@ -12,13 +48,13 @@ function(z_vcpkg_add_spdx_resource)
set(resource "{}")
string(JSON resource SET "${resource}" "SPDXID" "\"SPDXRef-resource-${resource_index}\"")
string(JSON value STRING_ENCODE "${arg_NAME}")
z_vcpkg_spdx_json_string_encode(value "${arg_NAME}")
string(JSON resource SET "${resource}" "name" "${value}")
if(DEFINED arg_FILENAME)
string(JSON value STRING_ENCODE "${arg_FILENAME}")
z_vcpkg_spdx_json_string_encode(value "${arg_FILENAME}")
string(JSON resource SET "${resource}" "packageFileName" "${value}")
endif()
string(JSON value STRING_ENCODE "${arg_DOWNLOAD_LOCATION}")
z_vcpkg_spdx_json_string_encode(value "${arg_DOWNLOAD_LOCATION}")
string(JSON resource SET "${resource}" "downloadLocation" "${value}")
foreach(property IN ITEMS licenseConcluded licenseDeclared copyrightText)
string(JSON resource SET "${resource}" "${property}" [["NOASSERTION"]])

View File

@ -27,6 +27,9 @@ endif()
if("setup-pkgconfig-path" IN_LIST FEATURES)
include("${CMAKE_CURRENT_LIST_DIR}/test-z_vcpkg_setup_pkgconfig_path.cmake")
endif()
if("spdx" IN_LIST FEATURES)
include("${CMAKE_CURRENT_LIST_DIR}/test-z_vcpkg_spdx.cmake")
endif()
if("fixup-pkgconfig" IN_LIST FEATURES)
include("${CMAKE_CURRENT_LIST_DIR}/test-vcpkg_fixup_pkgconfig.cmake")
endif()

View File

@ -0,0 +1,44 @@
string(ASCII 1 control_character)
string(ASCII 8 backspace)
string(ASCII 12 form_feed)
set(expected_name "name; with \"quotes\", \\slashes\\, /solidus, ${backspace}backspace, ${form_feed}form feed,\nnewlines,\rcarriage returns,\ttabs, and ${control_character}controls")
set(expected_filename "archive;\"name\".tar.gz")
set(expected_download_location "https://example.com/a;b?query=\"value\"")
set(expected_encoded_name [["name; with \"quotes\", \\slashes\\, /solidus, \bbackspace, \fform feed,\nnewlines,\rcarriage returns,\ttabs, and \u0001controls"]])
foreach(encoder IN ITEMS z_vcpkg_spdx_json_string_encode z_vcpkg_spdx_json_string_encode_compat)
cmake_language(CALL "${encoder}" encoded_name "${expected_name}")
if(NOT encoded_name STREQUAL expected_encoded_name)
message(SEND_ERROR "${encoder} does not match string(JSON STRING_ENCODE):
expected: ${expected_encoded_name}
actual : ${encoded_name}")
set_has_error()
endif()
endforeach()
set_property(GLOBAL PROPERTY Z_VCPKG_SPDX_OBJECTS "")
z_vcpkg_add_spdx_resource(
NAME "${expected_name}"
FILENAME "${expected_filename}"
DOWNLOAD_LOCATION "${expected_download_location}"
)
get_property(resources GLOBAL PROPERTY Z_VCPKG_SPDX_OBJECTS)
vcpkg_list(GET resources 0 resource)
foreach(property IN ITEMS name packageFileName downloadLocation)
string(JSON actual_value GET "${resource}" "${property}")
if(property STREQUAL "name")
set(expected_value "${expected_name}")
elseif(property STREQUAL "packageFileName")
set(expected_value "${expected_filename}")
else()
set(expected_value "${expected_download_location}")
endif()
if(NOT actual_value STREQUAL expected_value)
message(SEND_ERROR "SPDX ${property} was encoded incorrectly:
expected: \"${expected_value}\"
actual : \"${actual_value}\"")
set_has_error()
endif()
endforeach()

View File

@ -17,7 +17,8 @@
"list",
"merge-libs",
"minimum-required",
"setup-pkgconfig-path"
"setup-pkgconfig-path",
"spdx"
],
"features": {
"backup-restore-env-vars": {
@ -56,6 +57,9 @@
},
"setup-pkgconfig-path": {
"description": "Test the z_vcpkg_setup/restore_pkgconfig_path functions"
},
"spdx": {
"description": "Test SPDX resource generation"
}
}
}