CMakePackageConfigHelpers: Use unique variable name for pkg prefix

Fixes: #25827
This commit is contained in:
Craig Scott
2024-03-30 18:51:47 +11:00
parent 91b9d35608
commit 6ddf8712cd
6 changed files with 137 additions and 7 deletions

View File

@@ -377,6 +377,24 @@ macro(WRITE_BASIC_PACKAGE_VERSION_FILE)
write_basic_config_version_file(${ARGN})
endmacro()
function(__CMAKE_get_config_file_package_prefix outvar)
get_property(counter GLOBAL PROPERTY CMAKE_CONFIGURE_PACKAGE_CONFIG_FILE_COUNTER)
if(NOT counter)
set(counter 1)
set_property(GLOBAL PROPERTY CMAKE_CONFIGURE_PACKAGE_CONFIG_FILE_COUNTER ${counter})
endif()
set(${outvar} "PACKAGE_\${CMAKE_FIND_PACKAGE_NAME}_COUNTER_${counter}" PARENT_SCOPE)
endfunction()
function(__CMAKE_increment_config_file_counter)
get_property(counter GLOBAL PROPERTY CMAKE_CONFIGURE_PACKAGE_CONFIG_FILE_COUNTER)
if(NOT counter)
message(FATAL_ERROR "Internal error: global counter unexpectedly not set")
endif()
math(EXPR counter "${counter} + 1")
set_property(GLOBAL PROPERTY CMAKE_CONFIGURE_PACKAGE_CONFIG_FILE_COUNTER ${counter})
endfunction()
function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO)
set(oneValueArgs INSTALL_DESTINATION INSTALL_PREFIX)
@@ -412,15 +430,17 @@ function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${installPrefix}" )
__CMAKE_get_config_file_package_prefix(prefix_dir)
foreach(var ${CCF_PATH_VARS})
if(NOT DEFINED ${var})
message(FATAL_ERROR "Variable ${var} does not exist")
else()
if(IS_ABSOLUTE "${${var}}")
string(REPLACE "${installPrefix}" "\${PACKAGE_PREFIX_DIR}"
string(REPLACE "${installPrefix}" "\${${prefix_dir}}"
PACKAGE_${var} "${${var}}")
else()
set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
set(PACKAGE_${var} "\${${prefix_dir}}/${${var}}")
endif()
endif()
endforeach()
@@ -432,7 +452,7 @@ function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was ${inputFileName} ########
get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
get_filename_component(${prefix_dir} \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
")
if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
@@ -443,7 +463,7 @@ get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE
get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
if(_realCurr STREQUAL _realOrig)
set(PACKAGE_PREFIX_DIR \"${installPrefix}\")
set(${prefix_dir} \"${installPrefix}\")
endif()
unset(_realOrig)
unset(_realCurr)
@@ -481,6 +501,10 @@ endmacro()
configure_file("${_inputFile}" "${_outputFile}" @ONLY)
# We're done writing the file. Make sure the next one has a unique prefix dir
# variable name (we may have multiple config files for the one package)
__CMAKE_increment_config_file_counter()
endfunction()
function(generate_apple_platform_selection_file _output_file)
@@ -521,12 +545,14 @@ function(generate_apple_platform_selection_file _output_file)
set(_branch_INIT "")
endif()
__CMAKE_get_config_file_package_prefix(prefix_dir)
set(_else ELSE)
foreach(_opt IN LISTS _config_file_options _else)
if(_gpsf_${_opt})
set(_config_file "${_gpsf_${_opt}}")
if(NOT IS_ABSOLUTE "${_config_file}")
string(PREPEND _config_file [[${PACKAGE_PREFIX_DIR}/]])
string(PREPEND _config_file "\${${prefix_dir}}/")
endif()
set(_branch_${_opt} "include(\"${_config_file}\")")
elseif(_gpsf_ERROR_VARIABLE)
@@ -600,11 +626,13 @@ function(generate_apple_architecture_selection_file _output_file)
"endif()\n"
)
__CMAKE_get_config_file_package_prefix(prefix_dir)
foreach(pair IN ZIP_LISTS _gasf_SINGLE_ARCHITECTURES _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES)
set(arch "${pair_0}")
set(config_file "${pair_1}")
if(NOT IS_ABSOLUTE "${config_file}")
string(PREPEND config_file [[${PACKAGE_PREFIX_DIR}/]])
string(PREPEND config_file "\${${prefix_dir}}/")
endif()
string(APPEND _branch_code
"\n"
@@ -619,7 +647,7 @@ function(generate_apple_architecture_selection_file _output_file)
string(JOIN " " universal_archs "${_gasf_UNIVERSAL_ARCHITECTURES}")
set(config_file "${_gasf_UNIVERSAL_INCLUDE_FILE}")
if(NOT IS_ABSOLUTE "${config_file}")
string(PREPEND config_file [[${PACKAGE_PREFIX_DIR}/]])
string(PREPEND config_file "\${${prefix_dir}}/")
endif()
string(APPEND _branch_code
"\n"

View File

@@ -0,0 +1,3 @@
(-- )?Hello from platform switch
(-- )?Hello from arch switch
(-- )?Hello from pkg_a

View File

@@ -0,0 +1,50 @@
set(CMAKE_INSTALL_DATADIR share)
set(SWITCH_DIR platform/cmake)
include(CMakePackageConfigHelpers)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pkg_a-config.cmake.in [[
@PACKAGE_INIT@
include("@PACKAGE_SWITCH_DIR@/platform-switch.cmake")
include("@PACKAGE_CMAKE_INSTALL_DATADIR@/pkg_a_included.cmake")
]])
configure_package_config_file(
${CMAKE_CURRENT_BINARY_DIR}/pkg_a-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/install/pkg_a-config.cmake
INSTALL_DESTINATION .
PATH_VARS CMAKE_INSTALL_DATADIR SWITCH_DIR
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/install/${CMAKE_INSTALL_DATADIR}/pkg_a_included.cmake
[[message(STATUS "Hello from pkg_a")]]
)
# To expose re-using the same package prefix variable, we need to use a
# different install prefix but still with the same package name. This is
# really contrived and not representative of what a package should do.
generate_apple_platform_selection_file(
${CMAKE_CURRENT_BINARY_DIR}/install/platform/cmake/platform-switch.cmake
INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/platform
INSTALL_DESTINATION cmake
MACOS_INCLUDE_FILE cmake/switch_included.cmake # relative to install prefix
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/install/platform/cmake/switch_included.cmake
[[
message(STATUS "Hello from platform switch")
include("${CMAKE_CURRENT_LIST_DIR}/../arch/cmake/arch-switch.cmake")
]]
)
generate_apple_architecture_selection_file(
${CMAKE_CURRENT_BINARY_DIR}/install/platform/arch/cmake/arch-switch.cmake
INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/platform/arch
INSTALL_DESTINATION cmake
UNIVERSAL_ARCHITECTURES i386 x86_64 arm64 $(ARCHS_STANDARD)
UNIVERSAL_INCLUDE_FILE cmake/switch_included.cmake # relative to install prefix
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/install/platform/arch/cmake/switch_included.cmake
[[message(STATUS "Hello from arch switch")]]
)
find_package(pkg_a REQUIRED NO_DEFAULT_PATH
PATHS ${CMAKE_CURRENT_BINARY_DIR}/install
)

View File

@@ -0,0 +1,2 @@
(-- )?Hello from pkg_a
(-- )?Hello from pkg_b

View File

@@ -0,0 +1,39 @@
set(CMAKE_INSTALL_DATADIR share)
include(CMakePackageConfigHelpers)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pkg_a-config.cmake.in [[
@PACKAGE_INIT@
include("@PACKAGE_CMAKE_INSTALL_DATADIR@/pkg_a_included.cmake")
]])
configure_package_config_file(
${CMAKE_CURRENT_BINARY_DIR}/pkg_a-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/install_pkg_a/pkg_a-config.cmake
INSTALL_DESTINATION .
PATH_VARS CMAKE_INSTALL_DATADIR
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/install_pkg_a/share/pkg_a_included.cmake
[[message(STATUS "Hello from pkg_a")]]
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pkg_b-config.cmake.in [[
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(pkg_a NO_DEFAULT_PATH
PATHS "@CMAKE_CURRENT_BINARY_DIR@/install_pkg_a"
)
include("@PACKAGE_CMAKE_INSTALL_DATADIR@/pkg_b_included.cmake")
]])
configure_package_config_file(
${CMAKE_CURRENT_BINARY_DIR}/pkg_b-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/install_pkg_b/pkg_b-config.cmake
INSTALL_DESTINATION .
PATH_VARS CMAKE_INSTALL_DATADIR
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/install_pkg_b/share/pkg_b_included.cmake
[[message(STATUS "Hello from pkg_b")]]
)
find_package(pkg_b REQUIRED NO_DEFAULT_PATH
PATHS ${CMAKE_CURRENT_BINARY_DIR}/install_pkg_b
)

View File

@@ -4,6 +4,8 @@ if(NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
set(maybe_CMAKE_BUILD_TYPE -DCMAKE_BUILD_TYPE=Release)
endif()
run_cmake_with_options(NestedConfigFile ${maybe_CMAKE_BUILD_TYPE})
function(apple_export platform system_name archs sysroot)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/apple-export-${platform}-build)
string(REPLACE ";" "\\;" archs "${archs}")
@@ -78,6 +80,12 @@ if(APPLE AND CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
set(enable_visionos 1)
endif()
string(REPLACE ";" "\\;" macos_archs_for_cmd "${macos_archs}")
run_cmake_with_options(ApplePlatformGenSubdir
"-DCMAKE_OSX_ARCHITECTURES=${macos_archs_for_cmd}"
${maybe_CMAKE_BUILD_TYPE}
)
apple_export(macos Darwin "${macos_archs}" macosx)
apple_export(ios iOS "arm64" iphoneos)
apple_export(tvos tvOS "arm64" appletvos)