Skip to content

Commit 7a8ab19

Browse files
committed
build: add support for installing the image
This replicates much of the logic from the new runtimes build to support installing the Swift interface and the runtime components. Additionally, we are now able to generate the targets configuration to allow dependencies between projects.
1 parent 94b8d46 commit 7a8ab19

File tree

6 files changed

+164
-3
lines changed

6 files changed

+164
-3
lines changed

CMakeLists.txt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,35 @@
1010
##===----------------------------------------------------------------------===##
1111

1212
cmake_minimum_required(VERSION 3.26...3.29)
13-
project(Subprocess LANGUAGES C Swift)
14-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
1513

16-
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-package-name ${PROJECT_NAME}>")
14+
set(CMAKE_C_VISIBILITY_PRESET "hidden")
1715

16+
project(Subprocess
17+
LANGUAGES C Swift)
18+
19+
list(APPEND CMAKE_MODULE_PATH
20+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
21+
22+
include(EmitSwiftInterface)
23+
include(GNUInstallDirs)
1824
include(InstallExternalDependencies)
25+
include(PlatformInfo)
26+
include(InstallSwiftInterface)
27+
28+
option(${PROJECT_NAME}_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" ON)
29+
set(${PROJECT_NAME}_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>$<$<BOOL:${${PROJECT_NAME}_INSTALL_NESTED_SUBDIR}>:/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}>")
30+
set(${PROJECT_NAME}_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>$<$<BOOL:${${PROJECT_NAME}_INSTALL_NESTED_SUBDIR}>:/${${PROJECT_NAME}_PLATFORM_SUBDIR}>")
31+
32+
option(${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION "Generate ABI resilient runtime libraries" NO)
33+
option(${PROJECT_NAME}_ENABLE_PRESPECIALIZATION "Enable generic metadata prespecialization" NO)
34+
35+
add_compile_options(
36+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-package-name ${PROJECT_NAME}>"
37+
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION}>,$<COMPILE_LANGUAGE:Swift>>:-enable-library-evolution>"
38+
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_PRESPECIALIZATION}>,$<COMPILE_LANGUAGE:Swift>>:SHELL:-Xfrontend -prespecialize-generic-metadata>")
1939

2040
add_subdirectory(Sources)
41+
42+
export(EXPORT SwiftSubprocessTargets
43+
FILE "cmake/SwiftSubprocess/SwiftSubprocessTargets.cmake"
44+
NAMESPACE "SwiftSubprocess::")

Sources/Subprocess/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ target_link_libraries(Subprocess PUBLIC
4646
_SubprocessCShims)
4747
target_link_libraries(Subprocess PRIVATE
4848
SwiftSystem::SystemPackage)
49+
50+
install(TARGETS Subprocess
51+
EXPORT SwiftSubprocessTargets
52+
ARCHIVE DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
53+
LIBRARY DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
54+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
55+
emit_swift_interface(Subprocess)
56+
install_swift_interface(Subprocess)

Sources/_SubprocessCShims/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ add_library(_SubprocessCShims STATIC
1313
process_shims.c)
1414
target_include_directories(_SubprocessCShims PUBLIC
1515
include)
16+
17+
install(TARGETS _SubprocessCShims
18+
EXPORT SwiftSubprocessTargets)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift.org open source project
4+
##
5+
## Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
## Licensed under Apache License v2.0 with Runtime Library Exception
7+
##
8+
## See https://swift.org/LICENSE.txt for license information
9+
##
10+
##===----------------------------------------------------------------------===##
11+
12+
function(emit_swift_interface target)
13+
# Generate the target-variant binary swift module when performing zippered
14+
# build
15+
#
16+
# Clean this up once CMake has nested swiftmodules in the build directory:
17+
# https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10664
18+
# https://cmake.org/cmake/help/git-stage/policy/CMP0195.html
19+
20+
# We can't expand the Swift_MODULE_NAME target property in a generator
21+
# expression or it will fail saying that the target doesn't exist.
22+
get_target_property(module_name ${target} Swift_MODULE_NAME)
23+
if(NOT module_name)
24+
set(module_name ${target})
25+
endif()
26+
27+
# Account for an existing swiftmodule file generated with the previous logic
28+
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule"
29+
AND NOT IS_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule")
30+
message(STATUS "Removing regular file ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule to support nested swiftmodule generation")
31+
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule")
32+
endif()
33+
34+
target_compile_options(${target} PRIVATE
35+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-module-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule>")
36+
if(${PROJECT_NAME}_VARIANT_MODULE_TRIPLE)
37+
target_compile_options(${target} PRIVATE
38+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-variant-module-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_VARIANT_MODULE_TRIPLE}.swiftmodule>")
39+
endif()
40+
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule"
41+
DEPENDS ${target})
42+
target_sources(${target}
43+
INTERFACE
44+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule>)
45+
46+
# Generate textual swift interfaces is library-evolution is enabled
47+
if(${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION)
48+
target_compile_options(${target} PRIVATE
49+
$<$<COMPILE_LANGUAGE:Swift>:-emit-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftinterface>
50+
$<$<COMPILE_LANGUAGE:Swift>:-emit-private-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.private.swiftinterface>)
51+
if(${PROJECT_NAME}_VARIANT_MODULE_TRIPLE)
52+
target_compile_options(${target} PRIVATE
53+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-variant-module-interface-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_VARIANT_MODULE_TRIPLE}.swiftinterface>"
54+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-variant-private-module-interface-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_VARIANT_MODULE_TRIPLE}.private.swiftinterface>")
55+
endif()
56+
target_compile_options(${target} PRIVATE
57+
$<$<COMPILE_LANGUAGE:Swift>:-library-level$<SEMICOLON>api>
58+
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend$<SEMICOLON>-require-explicit-availability=ignore>)
59+
endif()
60+
endfunction()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift.org open source project
4+
##
5+
## Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
## Licensed under Apache License v2.0 with Runtime Library Exception
7+
##
8+
## See https://swift.org/LICENSE.txt for license information
9+
##
10+
##===----------------------------------------------------------------------===##
11+
12+
# Install the generated swift interface files for the target.
13+
function(install_swift_interface target)
14+
# Swiftmodules are already in the directory structure
15+
get_target_property(module_name ${target} Swift_MODULE_NAME)
16+
if(NOT module_name)
17+
set(module_name ${target})
18+
endif()
19+
20+
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule"
21+
DESTINATION "${${PROJECT_NAME}_INSTALL_SWIFTMODULEDIR}"
22+
COMPONENT SwiftSubprocess_development)
23+
endfunction()

cmake/modules/PlatformInfo.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift.org open source project
4+
##
5+
## Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
## Licensed under Apache License v2.0 with Runtime Library Exception
7+
##
8+
## See https://swift.org/LICENSE.txt for license information
9+
##
10+
##===----------------------------------------------------------------------===##
11+
12+
# TODO: This logic should migrate to CMake once CMake supports installing swiftmodules
13+
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
14+
if(CMAKE_Swift_COMPILER_TARGET)
15+
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
16+
endif()
17+
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
18+
message(CONFIGURE_LOG "Swift target info: ${module_triple_command}\n"
19+
"${target_info_json}")
20+
21+
if(NOT ${PROJECT_NAME}_MODULE_TRIPLE)
22+
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
23+
set(${PROJECT_NAME}_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used for installed swift{doc,module,interface} files")
24+
mark_as_advanced(${PROJECT_NAME}_MODULE_TRIPLE)
25+
26+
message(CONFIGURE_LOG "Swift module triple: ${module_triple}")
27+
endif()
28+
29+
if(NOT ${PROJECT_NAME}_PLATFORM_SUBDIR)
30+
string(JSON platform GET "${target_info_json}" "target" "platform")
31+
set(${PROJECT_NAME}_PLATFORM_SUBDIR "${platform}" CACHE STRING "Platform name used for installed swift{doc,module,interface} files")
32+
mark_as_advanced(${PROJECT_NAME}_PLATFORM_SUBDIR)
33+
34+
message(CONFIGURE_LOG "Swift platform: ${platform}")
35+
endif()
36+
37+
if(NOT ${PROJECT_NAME}_ARCH_SUBDIR)
38+
string(JSON arch GET "${target_info_json}" "target" "arch")
39+
set(${PROJECT_NAME}_ARCH_SUBDIR "${arch}" CACHE STRING "Architecture used for setting the architecture subdirectory")
40+
mark_as_advanced(${PROJECT_NAME}_ARCH_SUBDIR)
41+
42+
message(CONFIGURE_LOG "Swift Arch: ${arch}")
43+
endif()

0 commit comments

Comments
 (0)