Skip to content

Commit 890309d

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 890309d

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

Sources/CMakeLists.txt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,28 @@
1212
##
1313
##===----------------------------------------------------------------------===##
1414

15-
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)
15+
include(ExternalProject)
16+
if(CMAKE_HOST_WIN32)
17+
set(_FoundationMacrosSwiftFlags "-use-ld=lld")
18+
endif()
19+
ExternalProject_Add(FoundationMacros
20+
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/FoundationMacros"
21+
PREFIX "${CMAKE_BINARY_DIR}/_deps"
22+
BINARY_DIR "macros"
23+
CMAKE_ARGS
24+
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
25+
-DCMAKE_SYSTEM_NAME=${CMAKE_HOST_SYSTEM_NAME}
26+
"-DCMAKE_Swift_FLAGS=${_FoundationMacrosSwiftFlags} ${CMAKE_HOST_Swift_FLAGS}"
27+
INSTALL_COMMAND "")
28+
ExternalProject_Get_Property(FoundationMacros BINARY_DIR)
29+
if(CMAKE_HOST_WIN32)
30+
set(_SwiftFoundation_PredicateMacro "${BINARY_DIR}/FoundationMacros.dll#PredicateMacro")
31+
set(_SwiftFoundation_ExpressionMacro "${BINARY_DIR}/FoundationMacros.dll#ExpressionMacro")
32+
else()
33+
set(_SwiftFoundation_PredicateMacro "${BINARY_DIR}/FoundationMacros#PredicateMacro")
34+
set(_SwiftFoundation_ExpressionMacro "${BINARY_DIR}/FoundationMacros#ExpressionMacro")
2035
endif()
2136

37+
add_subdirectory(_FoundationCShims)
2238
add_subdirectory(FoundationEssentials)
2339
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)