Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8a9c602

Browse files
compnerdhyp
andcommittedAug 8, 2024
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 8a9c602

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed
 

‎Sources/CMakeLists.txt

+20-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,27 @@
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_Swift_FLAGS="${_FoundationMacrosSwiftFlags} ${CMAKE_HOST_Swift_FLAGS}"
26+
INSTALL_COMMAND "")
27+
ExternalProject_Get_Property(FoundationMacros BINARY_DIR)
28+
if(CMAKE_HOST_WIN32)
29+
set(_SwiftFoundation_PredicateMacro "${BINARY_DIR}/FoundationMacros.dll#PredicateMacro")
30+
set(_SwiftFoundation_ExpressionMacro "${BINARY_DIR}/FoundationMacros.dll#ExpressionMacro")
31+
else()
32+
set(_SwiftFoundation_PredicateMacro "${BINARY_DIR}/FoundationMacros#PredicateMacro")
33+
set(_SwiftFoundation_ExpressionMacro "${BINARY_DIR}/FoundationMacros#ExpressionMacro")
2034
endif()
2135

36+
add_subdirectory(_FoundationCShims)
2237
add_subdirectory(FoundationEssentials)
2338
add_subdirectory(FoundationInternationalization)

‎Sources/FoundationMacros/CMakeLists.txt

+34-16
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,17 +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
5876
ARCHIVE DESTINATION lib/swift/host/plugins
5977
LIBRARY DESTINATION lib/swift/host/plugins

0 commit comments

Comments
 (0)
Please sign in to comment.