Skip to content

Commit ed0dd0f

Browse files
compnerdhyp
andcommitted
FoundationMacros: use cross-compilation to build for host
Use `ExternalProject` to switch FoundationMacros to cross-compilation. This allows us to build the macros for the right OS/architecture when cross-compiling Foundation for other environments. Co-authored-by: Alex Lorenz <[email protected]>
1 parent 4440638 commit ed0dd0f

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ set(BUILD_TESTING NO)
4545
set(COLLECTIONS_SINGLE_MODULE YES)
4646
set(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE YES)
4747

48+
set(SwiftFoundation_MACRO "" CACHE STRING "Path to Foundation macro plugin")
49+
4850
# Make sure our dependencies exists
4951
include(FetchContent)
5052
if (_SwiftFoundationICU_SourceDIR)

Sources/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,5 @@
1313
##===----------------------------------------------------------------------===##
1414

1515
add_subdirectory(_FoundationCShims)
16-
17-
# Disable the macro build on Windows until we can correctly build it for the host architecture
18-
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
19-
add_subdirectory(FoundationMacros)
20-
endif()
21-
2216
add_subdirectory(FoundationEssentials)
2317
add_subdirectory(FoundationInternationalization)

Sources/FoundationMacros/CMakeLists.txt

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,47 @@
1212
##
1313
##===----------------------------------------------------------------------===##
1414

15+
cmake_minimum_required(VERSION 3.22)
16+
17+
if(POLICY CMP0156)
18+
# Deduplicate linked libraries where appropriate
19+
cmake_policy(SET CMP0156 NEW)
20+
endif()
21+
if(POLICY CMP0157)
22+
# New Swift build model: improved incremental build performance and LSP support
23+
cmake_policy(SET CMP0157 NEW)
24+
endif()
25+
26+
project(FoundationMacros
27+
LANGUAGES Swift)
28+
29+
include(GNUInstallDirs)
30+
1531
# SwiftSyntax Dependency
16-
include(FetchContent)
17-
find_package(SwiftSyntax)
32+
find_package(SwiftSyntax QUIET)
1833
if(NOT SwiftSyntax_FOUND)
34+
include(FetchContent)
35+
1936
# If building at desk, check out and link against the SwiftSyntax repo's targets
2037
FetchContent_Declare(SwiftSyntax
2138
GIT_REPOSITORY https://github.com/swiftlang/swift-syntax.git
2239
GIT_TAG 4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c) # 600.0.0-prerelease-2024-06-12
2340
FetchContent_MakeAvailable(SwiftSyntax)
41+
else()
42+
message(STATUS "Using swift-syntax from ${SwiftSyntax_DIR}")
2443
endif()
2544

2645
add_library(FoundationMacros SHARED
2746
FoundationMacros.swift
2847
PredicateMacro.swift)
29-
48+
3049
target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)
3150

32-
set_target_properties(FoundationMacros
33-
PROPERTIES
34-
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
35-
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
36-
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/lib
37-
)
51+
target_compile_options(FoundationMacros PRIVATE -parse-as-library)
52+
target_compile_options(FoundationMacros PRIVATE
53+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>"
54+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
55+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>")
3856

3957
target_link_libraries(FoundationMacros PUBLIC
4058
SwiftSyntax::SwiftSyntax
@@ -43,18 +61,19 @@ target_link_libraries(FoundationMacros PUBLIC
4361
SwiftSyntax::SwiftSyntaxBuilder
4462
)
4563

46-
# The macro is installed into lib/swift/host/plugins, but needs to load libraries from lib/swift/host and lib/swift/${SWIFT_SYSTEM_NAME}
4764
set_target_properties(FoundationMacros PROPERTIES
65+
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
66+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
67+
PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
68+
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
69+
70+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/lib
71+
72+
# The macro is installed into lib/swift/host/plugins, but needs to load
73+
# libraries from lib/swift/host and lib/swift/${SWIFT_SYSTEM_NAME}
4874
INSTALL_RPATH "$ORIGIN/../../../swift/${SWIFT_SYSTEM_NAME}:$ORIGIN/.."
4975
INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
5076

51-
target_compile_options(FoundationMacros PRIVATE -parse-as-library)
52-
target_compile_options(FoundationMacros PRIVATE
53-
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>"
54-
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
55-
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>")
56-
5777
install(TARGETS FoundationMacros
58-
ARCHIVE DESTINATION lib/swift/host/plugins
5978
LIBRARY DESTINATION lib/swift/host/plugins
6079
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

0 commit comments

Comments
 (0)