Skip to content

Commit eb3e4bc

Browse files
compnerdhyp
andauthored
FoundationMacros: use cross-compilation to build for host (#714) (#849)
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 1b79d86 commit eb3e4bc

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ set(BUILD_TESTING NO)
5555
set(COLLECTIONS_SINGLE_MODULE YES)
5656
set(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE YES)
5757

58+
set(SwiftFoundation_MACRO "" CACHE STRING "Path to Foundation macro plugin")
59+
5860
# Make sure our dependencies exists
5961
include(FetchContent)
6062
if (_SwiftFoundationICU_SourceDIR)

Sources/CMakeLists.txt

-6
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/FoundationEssentials/CMakeLists.txt

+3-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ add_subdirectory(String)
4848
add_subdirectory(TimeZone)
4949
add_subdirectory(URL)
5050

51-
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
52-
# Depend on FoundationMacros
53-
add_dependencies(FoundationEssentials FoundationMacros)
54-
target_compile_options(FoundationEssentials PRIVATE -plugin-path ${CMAKE_BINARY_DIR}/lib)
51+
if(SwiftFoundation_MACRO)
52+
target_compile_options(FoundationEssentials PRIVATE
53+
"SHELL:-plugin-path ${SwiftFoundation_MACRO}")
5554
endif()
5655

5756
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)

Sources/FoundationMacros/CMakeLists.txt

+35-18
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,45 @@
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+
1529
# SwiftSyntax Dependency
16-
include(FetchContent)
17-
find_package(SwiftSyntax)
30+
find_package(SwiftSyntax QUIET)
1831
if(NOT SwiftSyntax_FOUND)
32+
include(FetchContent)
33+
1934
# If building at desk, check out and link against the SwiftSyntax repo's targets
2035
FetchContent_Declare(SwiftSyntax
2136
GIT_REPOSITORY https://github.com/swiftlang/swift-syntax.git
2237
GIT_TAG 4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c) # 600.0.0-prerelease-2024-06-12
2338
FetchContent_MakeAvailable(SwiftSyntax)
39+
else()
40+
message(STATUS "Using swift-syntax from ${SwiftSyntax_DIR}")
2441
endif()
2542

2643
add_library(FoundationMacros SHARED
2744
FoundationMacros.swift
2845
PredicateMacro.swift)
29-
46+
3047
target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)
3148

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-
)
49+
target_compile_options(FoundationMacros PRIVATE -parse-as-library)
50+
target_compile_options(FoundationMacros PRIVATE
51+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>"
52+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
53+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>")
3854

3955
target_link_libraries(FoundationMacros PUBLIC
4056
SwiftSyntax::SwiftSyntax
@@ -43,18 +59,19 @@ target_link_libraries(FoundationMacros PUBLIC
4359
SwiftSyntax::SwiftSyntaxBuilder
4460
)
4561

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}
4762
set_target_properties(FoundationMacros PROPERTIES
63+
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
64+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
65+
PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
66+
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
67+
68+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/lib
69+
70+
# The macro is installed into lib/swift/host/plugins, but needs to load
71+
# libraries from lib/swift/host and lib/swift/${SWIFT_SYSTEM_NAME}
4872
INSTALL_RPATH "$ORIGIN/../../../swift/${SWIFT_SYSTEM_NAME}:$ORIGIN/.."
4973
INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
5074

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-
5775
install(TARGETS FoundationMacros
58-
ARCHIVE DESTINATION lib/swift/host/plugins
5976
LIBRARY DESTINATION lib/swift/host/plugins
60-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
77+
RUNTIME DESTINATION bin)

0 commit comments

Comments
 (0)