From a332b7b782315c9408192d4c6ee4167fb81459e8 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 3 Jul 2024 08:37:19 -0700 Subject: [PATCH 1/2] 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. --- Sources/CMakeLists.txt | 25 ++++++++++++++++++++----- Sources/FoundationMacros/CMakeLists.txt | 17 ++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index f7f7ba156..fa411c6d1 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -12,12 +12,27 @@ ## ##===----------------------------------------------------------------------===## -add_subdirectory(_FoundationCShims) - -# Disable the macro build on Windows until we can correctly build it for the host architecture -if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows) - add_subdirectory(FoundationMacros) +include(ExternalProject) +if(CMAKE_HOST_WIN32) + set(_FoundationMacrosSwiftFlags -DCMAKE_Swift_FLAGS="-use-ld=lld") +endif() +ExternalProject_Add(FoundationMacros + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/FoundationMacros" + PREFIX "${CMAKE_BINARY_DIR}/_deps" + BINARY_DIR "macros" + CMAKE_ARGS + -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} + ${_FoundationMacrosSwiftFlags} + INSTALL_COMMAND "") +ExternalProject_Get_Property(FoundationMacros BINARY_DIR) +if(CMAKE_HOST_WIN32) + set(_SwiftFoundation_PredicateMacro "${BINARY_DIR}/FoundationMacros.exe#PredicateMacro") + set(_SwiftFoundation_ExpressionMacro "${BINARY_DIR}/FoundationMacros.exe#ExpressionMacro") +else() + set(_SwiftFoundation_PredicateMacro "${BINARY_DIR}/FoundationMacros#PredicateMacro") + set(_SwiftFoundation_ExpressionMacro "${BINARY_DIR}/FoundationMacros#ExpressionMacro") endif() +add_subdirectory(_FoundationCShims) add_subdirectory(FoundationEssentials) add_subdirectory(FoundationInternationalization) diff --git a/Sources/FoundationMacros/CMakeLists.txt b/Sources/FoundationMacros/CMakeLists.txt index bbc7bd628..239dd6c44 100644 --- a/Sources/FoundationMacros/CMakeLists.txt +++ b/Sources/FoundationMacros/CMakeLists.txt @@ -12,10 +12,25 @@ ## ##===----------------------------------------------------------------------===## +cmake_minimum_required(VERSION 3.22) + +if(POLICY CMP0156) + # Deduplicate linked libraries where appropriate + cmake_policy(SET CMP0156 NEW) +endif() +if(POLICY CMP0157) + # New Swift build model: improved incremental build performance and LSP support + cmake_policy(SET CMP0157 NEW) +endif() + +project(FoundationMacros + LANGUAGES Swift) + # SwiftSyntax Dependency -include(FetchContent) find_package(SwiftSyntax) if(NOT SwiftSyntax_FOUND) + include(FetchContent) + # If building at desk, check out and link against the SwiftSyntax repo's targets FetchContent_Declare(SwiftSyntax GIT_REPOSITORY https://github.com/swiftlang/swift-syntax.git From 7baf52b445cd9c8b22525ea47a85501300af5fbc Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Mon, 29 Jul 2024 12:26:29 -0700 Subject: [PATCH 2/2] [macros] pass in additional CMAKE_HOST_Swift_FLAGS when building foundation macros on a windows host, when such flags are present --- Sources/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index fa411c6d1..ad8ecf178 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -14,7 +14,11 @@ include(ExternalProject) if(CMAKE_HOST_WIN32) - set(_FoundationMacrosSwiftFlags -DCMAKE_Swift_FLAGS="-use-ld=lld") + set(_flags "-use-ld=lld") + if(DEFINED CMAKE_HOST_Swift_FLAGS) + set(_flags "${_flags} ${CMAKE_HOST_Swift_FLAGS}") + endif() + set(_FoundationMacrosSwiftFlags "-DCMAKE_Swift_FLAGS=${_flags}") endif() ExternalProject_Add(FoundationMacros SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/FoundationMacros"