generate_apple_*_selection_file: Save/restore PACKAGE_PREFIX_DIR

Issue: #25827
This commit is contained in:
Craig Scott 2024-04-19 16:10:47 +10:00 committed by Brad King
parent bf88879f1f
commit 8ac7958e3a
5 changed files with 102 additions and 0 deletions

View File

@ -1,2 +1,23 @@
# Save this now so we can restore it before returning
if(NOT DEFINED PACKAGE_PREFIX_DIR)
list(APPEND _gasf_PACKAGE_PREFIX_DIR "<__CMAKE_UNDEFINED__>")
elseif("${PACKAGE_PREFIX_DIR}" STREQUAL "")
list(APPEND _gasf_PACKAGE_PREFIX_DIR "<__CMAKE_EMPTY__>")
else()
list(APPEND _gasf_PACKAGE_PREFIX_DIR "${PACKAGE_PREFIX_DIR}")
endif()
@PACKAGE_INIT@
@_branch_code@
# Restore PACKAGE_PREFIX_DIR
list(LENGTH _gasf_PACKAGE_PREFIX_DIR _gasf_tmp)
math(EXPR _gasf_tmp "${_gasf_tmp} - 1")
list(GET _gasf_PACKAGE_PREFIX_DIR ${_gasf_tmp} PACKAGE_PREFIX_DIR)
list(REMOVE_AT _gasf_PACKAGE_PREFIX_DIR ${_gasf_tmp})
unset(_gasf_tmp)
if("${PACKAGE_PREFIX_DIR}" STREQUAL "<__CMAKE_UNDEFINED__>")
unset(PACKAGE_PREFIX_DIR)
elseif("${PACKAGE_PREFIX_DIR}" STREQUAL "<__CMAKE_EMPTY__>")
set(PACKAGE_PREFIX_DIR "")
endif()

View File

@ -1,3 +1,12 @@
# Save this now so we can restore it before returning
if(NOT DEFINED PACKAGE_PREFIX_DIR)
list(APPEND _gpsf_PACKAGE_PREFIX_DIR "<__CMAKE_UNDEFINED__>")
elseif("${PACKAGE_PREFIX_DIR}" STREQUAL "")
list(APPEND _gpsf_PACKAGE_PREFIX_DIR "<__CMAKE_EMPTY__>")
else()
list(APPEND _gpsf_PACKAGE_PREFIX_DIR "${PACKAGE_PREFIX_DIR}")
endif()
@PACKAGE_INIT@
string(TOLOWER "${CMAKE_OSX_SYSROOT}" _CMAKE_OSX_SYSROOT_LOWER)
@ -23,3 +32,15 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
else()
@_branch_ELSE@
endif()
# Restore PACKAGE_PREFIX_DIR
list(LENGTH _gpsf_PACKAGE_PREFIX_DIR _gpsf_tmp)
math(EXPR _gpsf_tmp "${_gpsf_tmp} - 1")
list(GET _gpsf_PACKAGE_PREFIX_DIR ${_gpsf_tmp} PACKAGE_PREFIX_DIR)
list(REMOVE_AT _gpsf_PACKAGE_PREFIX_DIR ${_gpsf_tmp})
unset(_gpsf_tmp)
if("${PACKAGE_PREFIX_DIR}" STREQUAL "<__CMAKE_UNDEFINED__>")
unset(PACKAGE_PREFIX_DIR)
elseif("${PACKAGE_PREFIX_DIR}" STREQUAL "<__CMAKE_EMPTY__>")
set(PACKAGE_PREFIX_DIR "")
endif()

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. 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

@ -75,6 +75,13 @@ 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}
)
unset(macos_archs_for_cmd)
# Place all export/import steps in a single install prefix.
set(apple_install ${RunCMake_BINARY_DIR}/apple-install)
file(REMOVE_RECURSE "${apple_install}")