Skip to content

Commit 27e280f

Browse files
authored
[cmake] Install libraries in standard directories (#685)
* [cmake] Install libraries in standard directories Previously, libraries were installed under `lib/swift/${os}/`. They should be installed in the default library directory for the relevant target system. In addition, swiftmodules were installed in the older layout format on non-Darwin platforms. This changes to use the standard modern layout format for swiftmodules. * Use full triple for swiftmodule filenames * Extract Swift_MODULE_TRIPLE from the function * Fail early in case the triple cannot be found * Enable C language before using GNUInstallDirs * Add C language in project definition Enabling the C language after Swift was causing issues with using the C compiler.
1 parent 5f48a16 commit 27e280f

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
cmake_minimum_required(VERSION 3.19)
22

3+
# Due to a bug in CMake, we need to enable the C language before we can use
4+
# GNUInstallDirs.
35
project(swift-argument-parser
4-
LANGUAGES Swift)
6+
LANGUAGES C Swift)
57

68
option(BUILD_EXAMPLES "Build Example Programs" TRUE)
79
option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
@@ -15,6 +17,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
1517
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
1618

1719
include(CTest)
20+
21+
include(GNUInstallDirs)
1822
include(SwiftSupport)
1923

2024
find_package(dispatch CONFIG)

cmake/modules/SwiftSupport.cmake

+29-18
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ function(get_swift_host_os result_var_name)
6161
endif()
6262
endfunction()
6363

64+
if(NOT Swift_MODULE_TRIPLE)
65+
# Attempt to get the module triple from the Swift compiler.
66+
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
67+
if(CMAKE_Swift_COMPILER_TARGET)
68+
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
69+
endif()
70+
execute_process(COMMAND ${module_triple_command}
71+
OUTPUT_VARIABLE target_info_json)
72+
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
73+
74+
# Exit now if we failed to infer the triple.
75+
if(NOT module_triple)
76+
message(FATAL_ERROR
77+
"Failed to get module triple from Swift compiler. "
78+
"Compiler output: ${target_info_json}")
79+
endif()
80+
81+
# Cache the module triple for future use.
82+
set(Swift_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
83+
mark_as_advanced(Swift_MODULE_TRIPLE)
84+
endif()
85+
6486
function(_install_target module)
6587
get_swift_host_os(swift_os)
6688
get_target_property(type ${module} TYPE)
@@ -71,31 +93,20 @@ function(_install_target module)
7193
set(swift swift)
7294
endif()
7395

74-
install(TARGETS ${module}
75-
ARCHIVE DESTINATION lib/${swift}/${swift_os}
76-
LIBRARY DESTINATION lib/${swift}/${swift_os}
77-
RUNTIME DESTINATION bin)
96+
install(TARGETS ${module})
7897
if(type STREQUAL EXECUTABLE)
7998
return()
8099
endif()
81100

82-
get_swift_host_arch(swift_arch)
83101
get_target_property(module_name ${module} Swift_MODULE_NAME)
84102
if(NOT module_name)
85103
set(module_name ${module})
86104
endif()
87105

88-
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
89-
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
90-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
91-
RENAME ${swift_arch}.swiftdoc)
92-
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
93-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
94-
RENAME ${swift_arch}.swiftmodule)
95-
else()
96-
install(FILES
97-
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
98-
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
99-
DESTINATION lib/${swift}/${swift_os}/${swift_arch})
100-
endif()
106+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
107+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${swift}/${swift_os}/${module_name}.swiftmodule
108+
RENAME ${Swift_MODULE_TRIPLE}.swiftdoc)
109+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
110+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${swift}/${swift_os}/${module_name}.swiftmodule
111+
RENAME ${Swift_MODULE_TRIPLE}.swiftmodule)
101112
endfunction()

0 commit comments

Comments
 (0)