Skip to content

Commit 44be5d5

Browse files
authored
build: add support for installing the image (#124)
* 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. * GHA: use the nightly snapshots Update to the nightly snapshots to make the CMake build easier to maintain. * build: remove variant support
1 parent 25357a8 commit 44be5d5

File tree

7 files changed

+156
-4
lines changed

7 files changed

+156
-4
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
cmake_build:
5959
name: CMake Build
6060
runs-on: ubuntu-latest
61-
container: swift:6.1-noble
61+
container: swiftlang/swift:nightly-noble
6262
steps:
6363
- name: checkout sources
6464
uses: actions/checkout@v1

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
@@ -55,3 +55,11 @@ target_link_libraries(Subprocess PUBLIC
5555
_SubprocessCShims)
5656
target_link_libraries(Subprocess PRIVATE
5757
SwiftSystem::SystemPackage)
58+
59+
install(TARGETS Subprocess
60+
EXPORT SwiftSubprocessTargets
61+
ARCHIVE DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
62+
LIBRARY DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
63+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
64+
emit_swift_interface(Subprocess)
65+
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule"
37+
DEPENDS ${target})
38+
target_sources(${target}
39+
INTERFACE
40+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule>)
41+
42+
# Generate textual swift interfaces is library-evolution is enabled
43+
if(${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION)
44+
target_compile_options(${target} PRIVATE
45+
$<$<COMPILE_LANGUAGE:Swift>:-emit-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftinterface>
46+
$<$<COMPILE_LANGUAGE:Swift>:-emit-private-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.private.swiftinterface>)
47+
target_compile_options(${target} PRIVATE
48+
$<$<COMPILE_LANGUAGE:Swift>:-library-level$<SEMICOLON>api>
49+
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend$<SEMICOLON>-require-explicit-availability=ignore>)
50+
endif()
51+
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)