Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
cmake_build:
name: CMake Build
runs-on: ubuntu-latest
container: swift:6.1-noble
container: swiftlang/swift:nightly-noble
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this bump intentional? I had used 6.1 to keep it in sync with the required version in Package.swift.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need the CMake build to be compatible with 6.1, since it's only used for the build of the toolchain itself

and it would require more effort to make this compatible with 6.1; no reason to add support for something we will never use

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this was for the platform and arch subdirectories wasn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's only used for the build of the toolchain itself.

It isn't though. I have projects that are non-SwiftPM that use this build today.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could set Subprocess_INSTALL_NESTED_SUBDIR to OFF when using something earlier than 6.2 and the arch and platform subdirs aren't set. Then we wouldn't need to request that info from the compiler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows will want that, so we will need to set that to true for CI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's only used for the build of the toolchain itself.

It isn't though. I have projects that are non-SwiftPM that use this build today.

Ah, that's unfortunate...

steps:
- name: checkout sources
uses: actions/checkout@v1
Expand Down
30 changes: 27 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,35 @@
##===----------------------------------------------------------------------===##

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

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

project(Subprocess
LANGUAGES C Swift)

list(APPEND CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")

include(EmitSwiftInterface)
include(GNUInstallDirs)
include(InstallExternalDependencies)
include(PlatformInfo)
include(InstallSwiftInterface)

option(${PROJECT_NAME}_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" ON)
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}>")
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}>")

option(${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION "Generate ABI resilient runtime libraries" NO)
option(${PROJECT_NAME}_ENABLE_PRESPECIALIZATION "Enable generic metadata prespecialization" NO)

add_compile_options(
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-package-name ${PROJECT_NAME}>"
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION}>,$<COMPILE_LANGUAGE:Swift>>:-enable-library-evolution>"
"$<$<AND:$<BOOL:${${PROJECT_NAME}_ENABLE_PRESPECIALIZATION}>,$<COMPILE_LANGUAGE:Swift>>:SHELL:-Xfrontend -prespecialize-generic-metadata>")

add_subdirectory(Sources)

export(EXPORT SwiftSubprocessTargets
FILE "cmake/SwiftSubprocess/SwiftSubprocessTargets.cmake"
NAMESPACE "SwiftSubprocess::")
8 changes: 8 additions & 0 deletions Sources/Subprocess/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ target_link_libraries(Subprocess PUBLIC
_SubprocessCShims)
target_link_libraries(Subprocess PRIVATE
SwiftSystem::SystemPackage)

install(TARGETS Subprocess
EXPORT SwiftSubprocessTargets
ARCHIVE DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
emit_swift_interface(Subprocess)
install_swift_interface(Subprocess)
3 changes: 3 additions & 0 deletions Sources/_SubprocessCShims/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ add_library(_SubprocessCShims STATIC
process_shims.c)
target_include_directories(_SubprocessCShims PUBLIC
include)

install(TARGETS _SubprocessCShims
EXPORT SwiftSubprocessTargets)
51 changes: 51 additions & 0 deletions cmake/modules/EmitSwiftInterface.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
##
##===----------------------------------------------------------------------===##

function(emit_swift_interface target)
# Generate the target-variant binary swift module when performing zippered
# build
#
# Clean this up once CMake has nested swiftmodules in the build directory:
# https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10664
# https://cmake.org/cmake/help/git-stage/policy/CMP0195.html

# We can't expand the Swift_MODULE_NAME target property in a generator
# expression or it will fail saying that the target doesn't exist.
get_target_property(module_name ${target} Swift_MODULE_NAME)
if(NOT module_name)
set(module_name ${target})
endif()

# Account for an existing swiftmodule file generated with the previous logic
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule"
AND NOT IS_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule")
message(STATUS "Removing regular file ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule to support nested swiftmodule generation")
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule")
endif()

target_compile_options(${target} PRIVATE
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-module-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule>")
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule"
DEPENDS ${target})
target_sources(${target}
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule>)

# Generate textual swift interfaces is library-evolution is enabled
if(${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION)
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:-emit-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftinterface>
$<$<COMPILE_LANGUAGE:Swift>:-emit-private-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.private.swiftinterface>)
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:-library-level$<SEMICOLON>api>
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend$<SEMICOLON>-require-explicit-availability=ignore>)
endif()
endfunction()
23 changes: 23 additions & 0 deletions cmake/modules/InstallSwiftInterface.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
##
##===----------------------------------------------------------------------===##

# Install the generated swift interface files for the target.
function(install_swift_interface target)
# Swiftmodules are already in the directory structure
get_target_property(module_name ${target} Swift_MODULE_NAME)
if(NOT module_name)
set(module_name ${target})
endif()

install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule"
DESTINATION "${${PROJECT_NAME}_INSTALL_SWIFTMODULEDIR}"
COMPONENT SwiftSubprocess_development)
endfunction()
43 changes: 43 additions & 0 deletions cmake/modules/PlatformInfo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
##
##===----------------------------------------------------------------------===##

# TODO: This logic should migrate to CMake once CMake supports installing swiftmodules
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
if(CMAKE_Swift_COMPILER_TARGET)
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
endif()
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
message(CONFIGURE_LOG "Swift target info: ${module_triple_command}\n"
"${target_info_json}")

if(NOT ${PROJECT_NAME}_MODULE_TRIPLE)
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
set(${PROJECT_NAME}_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used for installed swift{doc,module,interface} files")
mark_as_advanced(${PROJECT_NAME}_MODULE_TRIPLE)

message(CONFIGURE_LOG "Swift module triple: ${module_triple}")
endif()

if(NOT ${PROJECT_NAME}_PLATFORM_SUBDIR)
string(JSON platform GET "${target_info_json}" "target" "platform")
set(${PROJECT_NAME}_PLATFORM_SUBDIR "${platform}" CACHE STRING "Platform name used for installed swift{doc,module,interface} files")
mark_as_advanced(${PROJECT_NAME}_PLATFORM_SUBDIR)

message(CONFIGURE_LOG "Swift platform: ${platform}")
endif()

if(NOT ${PROJECT_NAME}_ARCH_SUBDIR)
string(JSON arch GET "${target_info_json}" "target" "arch")
set(${PROJECT_NAME}_ARCH_SUBDIR "${arch}" CACHE STRING "Architecture used for setting the architecture subdirectory")
mark_as_advanced(${PROJECT_NAME}_ARCH_SUBDIR)

message(CONFIGURE_LOG "Swift Arch: ${arch}")
endif()