Skip to content

Commit 3eff559

Browse files
committed
Get the platform name from the compiler
This also refactors the code so we only do a single call to the Swift compiler to gather the target info JSON.
1 parent 5301c96 commit 3eff559

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

CMakeLists.txt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,7 @@ add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:HAVE_CONFIG_H>)
308308
if(ENABLE_SWIFT)
309309
include(SwiftSupport)
310310

311-
if(NOT SWIFT_SYSTEM_NAME)
312-
if(APPLE)
313-
set(SWIFT_SYSTEM_NAME macosx)
314-
else()
315-
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
316-
endif()
317-
endif()
318-
319-
set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME}/${dispatch_Swift_ARCH}" CACHE PATH "Path where the libraries will be installed")
311+
set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${dispatch_Swift_PLATFORM}/${dispatch_Swift_ARCH}" CACHE PATH "Path where the libraries will be installed")
320312
set(INSTALL_DISPATCH_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
321313
set(INSTALL_BLOCK_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
322314
set(INSTALL_OS_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/os" CACHE PATH "Path where the os/ headers will be installed")

cmake/modules/SwiftSupport.cmake

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,67 @@
11
include_guard()
22

3-
if(NOT dispatch_MODULE_TRIPLE)
3+
if(NOT dispatch_Swift_MODULE_TRIPLE OR NOT dispatch_Swift_ARCH OR NOT dispatch_Swift_SYSTEM)
4+
# Get the target information from the Swift compiler.
45
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
56
if(CMAKE_Swift_COMPILER_TARGET)
67
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
78
endif()
89
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
10+
endif()
911

12+
if(NOT dispatch_Swift_MODULE_TRIPLE)
1013
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
11-
set(dispatch_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files")
12-
mark_as_advanced(dispatch_MODULE_TRIPLE)
13-
14+
set(dispatch_Swift_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files")
15+
mark_as_advanced(dispatch_Swift_MODULE_TRIPLE)
1416
message(CONFIGURE_LOG "Swift module triple: ${module_triple}")
1517
endif()
1618

1719
if(NOT dispatch_Swift_ARCH)
1820
if(CMAKE_Swift_COMPILER_VERSION VERSION_EQUAL 0.0.0 OR CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2)
1921
# For newer compilers, we can use the -print-target-info command to get the architecture.
20-
set(module_arch_command "${CMAKE_Swift_COMPILER}" -print-target-info)
21-
if(CMAKE_Swift_COMPILER_TARGET)
22-
list(APPEND module_arch_command -target ${CMAKE_Swift_COMPILER_TARGET})
23-
endif()
24-
execute_process(COMMAND ${module_arch_command} OUTPUT_VARIABLE target_info_json)
2522
string(JSON module_arch GET "${target_info_json}" "target" "arch")
2623
else()
27-
# For older compilers, extract the value from `dispatch_MODULE_TRIPLE`.
28-
string(REGEX MATCH "^[^-]+" module_arch "${dispatch_MODULE_TRIPLE}")
24+
# For older compilers, extract the value from `dispatch_Swift_MODULE_TRIPLE`.
25+
string(REGEX MATCH "^[^-]+" module_arch "${dispatch_Swift_MODULE_TRIPLE}")
2926
endif()
3027

3128
set(dispatch_Swift_ARCH "${module_arch}" CACHE STRING "Arch folder name used to install libraries")
3229
mark_as_advanced(dispatch_Swift_ARCH)
33-
3430
message(CONFIGURE_LOG "Swift arch: ${dispatch_Swift_ARCH}")
3531
endif()
3632

33+
if(NOT dispatch_Swift_PLATFORM)
34+
if(CMAKE_Swift_COMPILER_VERSION VERSION_EQUAL 0.0.0 OR CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2)
35+
# For newer compilers, we can use the -print-target-info command to get the platform.
36+
string(JSON swift_platform GET "${target_info_json}" "target" "platform")
37+
else()
38+
# For older compilers, compile the value from `CMAKE_SYSTEM_NAME`.
39+
if(APPLE)
40+
set(swift_platform macosx)
41+
else()
42+
set(swift_platform "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
43+
endif()
44+
endif()
45+
46+
set(dispatch_Swift_PLATFORM "${swift_platform}" CACHE STRING "Platform folder name used to install libraries")
47+
mark_as_advanced(dispatch_Swift_PLATFORM)
48+
message(CONFIGURE_LOG "Swift platform: ${dispatch_Swift_PLATFORM}")
49+
endif()
50+
3751
function(install_swift_module target)
3852
get_target_property(module ${target} Swift_MODULE_NAME)
3953
if(NOT module)
4054
set(module ${target})
4155
endif()
4256

43-
if(NOT SWIFT_SYSTEM_NAME)
44-
if(APPLE)
45-
set(SWIFT_SYSTEM_NAME macosx)
46-
else()
47-
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
48-
endif()
49-
endif()
50-
set(INSTALL_SWIFT_MODULE_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME}" CACHE PATH "Path where the swift modules will be installed")
57+
set(INSTALL_SWIFT_MODULE_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${dispatch_Swift_PLATFORM}" CACHE PATH "Path where the swift modules will be installed")
5158

5259
install(
5360
FILES $<TARGET_PROPERTY:${target},Swift_MODULE_DIRECTORY>/${module}.swiftdoc
5461
DESTINATION ${INSTALL_SWIFT_MODULE_DIR}/${module}.swiftmodule
55-
RENAME ${dispatch_MODULE_TRIPLE}.swiftdoc)
62+
RENAME ${dispatch_Swift_MODULE_TRIPLE}.swiftdoc)
5663
install(
5764
FILES $<TARGET_PROPERTY:${target},Swift_MODULE_DIRECTORY>/${module}.swiftmodule
5865
DESTINATION ${INSTALL_SWIFT_MODULE_DIR}/${module}.swiftmodule
59-
RENAME ${dispatch_MODULE_TRIPLE}.swiftmodule)
66+
RENAME ${dispatch_Swift_MODULE_TRIPLE}.swiftmodule)
6067
endfunction()

0 commit comments

Comments
 (0)