Skip to content

Commit 2228d64

Browse files
authored
Merge pull request #213 from Steelskin/fabrice/cmake-install-paths
[cmake] Install libraries in standard directories
2 parents 849d005 + bebd7c1 commit 2228d64

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ cmake_minimum_required(VERSION 3.16.0)
1111
project(SwiftSystem
1212
LANGUAGES C Swift)
1313

14+
include(GNUInstallDirs)
15+
1416
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
1517

1618
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

cmake/modules/SwiftSupport.cmake

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ function(get_swift_host_os result_var_name)
6969
endif()
7070
endfunction()
7171

72+
if(NOT Swift_MODULE_TRIPLE)
73+
# Attempt to get the module triple from the Swift compiler.
74+
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
75+
if(CMAKE_Swift_COMPILER_TARGET)
76+
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
77+
endif()
78+
execute_process(COMMAND ${module_triple_command}
79+
OUTPUT_VARIABLE target_info_json)
80+
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
81+
82+
# Exit now if we failed to infer the triple.
83+
if(NOT module_triple)
84+
message(FATAL_ERROR
85+
"Failed to get module triple from Swift compiler. "
86+
"Compiler output: ${target_info_json}")
87+
endif()
88+
89+
# Cache the module triple for future use.
90+
set(Swift_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
91+
mark_as_advanced(Swift_MODULE_TRIPLE)
92+
endif()
93+
7294
function(_install_target module)
7395
get_swift_host_os(swift_os)
7496
get_target_property(type ${module} TYPE)
@@ -79,24 +101,20 @@ function(_install_target module)
79101
set(swift swift)
80102
endif()
81103

82-
install(TARGETS ${module}
83-
ARCHIVE DESTINATION lib/${swift}/${swift_os}
84-
LIBRARY DESTINATION lib/${swift}/${swift_os}
85-
RUNTIME DESTINATION bin)
104+
install(TARGETS ${module})
86105
if(type STREQUAL EXECUTABLE)
87106
return()
88107
endif()
89108

90-
get_swift_host_arch(swift_arch)
91109
get_target_property(module_name ${module} Swift_MODULE_NAME)
92110
if(NOT module_name)
93111
set(module_name ${module})
94112
endif()
95113

96114
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
97-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
98-
RENAME ${swift_arch}.swiftdoc)
115+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${swift}/${swift_os}/${module_name}.swiftmodule
116+
RENAME ${Swift_MODULE_TRIPLE}.swiftdoc)
99117
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
100-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
101-
RENAME ${swift_arch}.swiftmodule)
118+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${swift}/${swift_os}/${module_name}.swiftmodule
119+
RENAME ${Swift_MODULE_TRIPLE}.swiftmodule)
102120
endfunction()

0 commit comments

Comments
 (0)