diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index ef235c43f..451d59e3b 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -12,6 +12,31 @@ ## ##===----------------------------------------------------------------------===## +if(NOT SwiftFoudation_MACRO) + include(ExternalProject) + # The macros are required for building Swift-Foundation. Build them for the + # build machine. + ExternalProject_Add(FoundationMacros + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/FoundationMacros" + INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/FoundationMacros" + CMAKE_ARGS + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_Swift_COMPILER=${CMAKE_Swift_COMPILER} + -DCMAKE_INSTALL_PREFIX= + -DSwiftFoundation_BUILD_EXECUTABLE_MACROS=YES) + + ExternalProject_Get_Property(FoundationMacros INSTALL_DIR) + add_library(FoundationBuildMacros INTERFACE) + add_dependencies(FoundationBuildMacros FoundationMacros) + + if(CMAKE_HOST_WIN32) + set(_SwiftFoundation_HOST_EXECUTABLE_SUFFIX .exe) + endif() + + target_compile_options(FoundationBuildMacros INTERFACE + "$<$:SHELL:-load-plugin-executable ${INSTALL_DIR}/bin/FoundationMacros${_SwiftFoundation_HOST_EXECUTABLE_SUFFIX}#FoundationMacros>") +endif() + add_subdirectory(_FoundationCShims) add_subdirectory(FoundationEssentials) add_subdirectory(FoundationInternationalization) diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt index 235e258a4..9cbcb222b 100644 --- a/Sources/FoundationEssentials/CMakeLists.txt +++ b/Sources/FoundationEssentials/CMakeLists.txt @@ -55,21 +55,7 @@ if(SwiftFoundation_MACRO) target_compile_options(FoundationEssentials PRIVATE "SHELL:-plugin-path ${SwiftFoundation_MACRO}") else() - message(STATUS "SwiftFoundation_MACRO not provided, building Foundation macros locally for host") - # No path to Foundation macros was provided, so we must build it ourselves - set(FoundationMacros_BuildLocalExecutable YES) - FetchContent_Declare(FoundationMacros - SOURCE_DIR ${CMAKE_SOURCE_DIR}/Sources/FoundationMacros) - FetchContent_MakeAvailable(FoundationMacros) - add_dependencies(FoundationEssentials FoundationMacros) - get_target_property(MacroDIR FoundationMacros RUNTIME_OUTPUT_DIRECTORY) - if(CMAKE_SYSTEM_NAME STREQUAL Windows) - set(MacroExecutable "${MacroDIR}/FoundationMacros.exe#FoundationMacros") - else() - set(MacroExecutable "${MacroDIR}/FoundationMacros#FoundationMacros") - endif() - target_compile_options(FoundationEssentials PUBLIC - "SHELL:$<$:-load-plugin-executable ${MacroExecutable}>") + target_link_libraries(FoundationEssentials PRIVATE FoundationBuildMacros) endif() if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android) diff --git a/Sources/FoundationMacros/CMakeLists.txt b/Sources/FoundationMacros/CMakeLists.txt index 31925224c..e8e7f50cc 100644 --- a/Sources/FoundationMacros/CMakeLists.txt +++ b/Sources/FoundationMacros/CMakeLists.txt @@ -26,6 +26,9 @@ endif() project(FoundationMacros LANGUAGES Swift) +option(SwiftFoundation_BUILD_EXECUTABLE_MACROS + "Build the FoundationMacros as an executable" NO) + # SwiftSyntax Dependency find_package(SwiftSyntax QUIET) if(NOT SwiftSyntax_FOUND) @@ -41,13 +44,13 @@ else() message(STATUS "SwiftSyntax_DIR provided, using swift-syntax from ${SwiftSyntax_DIR}") endif() -if(NOT FoundationMacros_BuildLocalExecutable) - add_library(FoundationMacros SHARED) - target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY) -else() +if(SwiftFoundation_BUILD_EXECUTABLE_MACROS) add_executable(FoundationMacros) target_link_libraries(FoundationMacros PUBLIC SwiftSyntax::SwiftCompilerPlugin) +else() + add_library(FoundationMacros SHARED) + target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY) endif() # Parse the module as a library, even if it's an executable, because it uses an `@main` type to define its entry point. @@ -82,8 +85,6 @@ set_target_properties(FoundationMacros PROPERTIES INSTALL_RPATH "$ORIGIN/../../../swift/${SWIFT_SYSTEM_NAME}:$ORIGIN/.." INSTALL_REMOVE_ENVIRONMENT_RPATH ON) -if(NOT FoundationMacros_BuildLocalExecutable) - install(TARGETS FoundationMacros - LIBRARY DESTINATION lib/swift/host/plugins - RUNTIME DESTINATION bin) -endif() +install(TARGETS FoundationMacros + LIBRARY DESTINATION lib/swift/host/plugins + RUNTIME DESTINATION bin)