Skip to content

Commit

Permalink
Automatically build FoundationMacros for local CMake builds (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmschonfeld authored Aug 19, 2024
1 parent 586c460 commit fd4788a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,22 @@ set(SwiftFoundation_MACRO "" CACHE STRING "Path to Foundation macro plugin")
# Make sure our dependencies exists
include(FetchContent)
if (_SwiftFoundationICU_SourceDIR)
message(STATUS "_SwiftFoundationICU_SourceDIR provided, using swift-foundation-icu checkout at ${_SwiftFoundationICU_SourceDIR}")
FetchContent_Declare(SwiftFoundationICU
SOURCE_DIR ${_SwiftFoundationICU_SourceDIR})
else()
message(STATUS "_SwiftFoundationICU_SourceDIR not provided, checking out local copy of swift-foundation-icu")
FetchContent_Declare(SwiftFoundationICU
GIT_REPOSITORY https://github.com/apple/swift-foundation-icu.git
GIT_TAG 0.0.9)
endif()

if (_SwiftCollections_SourceDIR)
message(STATUS "_SwiftCollections_SourceDIR provided, using swift-foundation-icu checkout at ${_SwiftCollections_SourceDIR}")
FetchContent_Declare(SwiftCollections
SOURCE_DIR ${_SwiftCollections_SourceDIR})
else()
message(STATUS "_SwiftCollections_SourceDIR not provided, checking out local copy of swift-collections")
FetchContent_Declare(SwiftCollections
GIT_REPOSITORY https://github.com/apple/swift-collections.git
GIT_TAG 1.1.2)
Expand Down
22 changes: 20 additions & 2 deletions Sources/FoundationEssentials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,26 @@ add_subdirectory(TimeZone)
add_subdirectory(URL)

if(SwiftFoundation_MACRO)
target_compile_options(FoundationEssentials PRIVATE
"SHELL:-plugin-path ${SwiftFoundation_MACRO}")
message(STATUS "SwiftFoundation_MACRO provided, using macros in ${SwiftFoundation_MACRO}")
# A path to Foundation macros was provided, so we use that path
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:$<$<COMPILE_LANGUAGE:Swift>:-load-plugin-executable ${MacroExecutable}>")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
Expand Down
28 changes: 20 additions & 8 deletions Sources/FoundationMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ project(FoundationMacros
# SwiftSyntax Dependency
find_package(SwiftSyntax QUIET)
if(NOT SwiftSyntax_FOUND)
message(STATUS "SwiftSyntax_DIR not provided, checking out local copy of swift-syntax")
include(FetchContent)

# If building at desk, check out and link against the SwiftSyntax repo's targets
Expand All @@ -37,16 +38,25 @@ if(NOT SwiftSyntax_FOUND)
GIT_TAG 4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c) # 600.0.0-prerelease-2024-06-12
FetchContent_MakeAvailable(SwiftSyntax)
else()
message(STATUS "Using swift-syntax from ${SwiftSyntax_DIR}")
message(STATUS "SwiftSyntax_DIR provided, using swift-syntax from ${SwiftSyntax_DIR}")
endif()

add_library(FoundationMacros SHARED
if(NOT FoundationMacros_BuildLocalExecutable)
add_library(FoundationMacros SHARED)
target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)
else()
add_executable(FoundationMacros)
target_link_libraries(FoundationMacros PUBLIC
SwiftSyntax::SwiftCompilerPlugin)
endif()

# Parse the module as a library, even if it's an executable, because it uses an `@main` type to define its entry point.
target_compile_options(FoundationMacros PRIVATE -parse-as-library)

target_sources(FoundationMacros PRIVATE
FoundationMacros.swift
PredicateMacro.swift)

target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)

target_compile_options(FoundationMacros PRIVATE -parse-as-library)
target_compile_options(FoundationMacros PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
Expand All @@ -72,6 +82,8 @@ set_target_properties(FoundationMacros PROPERTIES
INSTALL_RPATH "$ORIGIN/../../../swift/${SWIFT_SYSTEM_NAME}:$ORIGIN/.."
INSTALL_REMOVE_ENVIRONMENT_RPATH ON)

install(TARGETS FoundationMacros
LIBRARY DESTINATION lib/swift/host/plugins
RUNTIME DESTINATION bin)
if(NOT FoundationMacros_BuildLocalExecutable)
install(TARGETS FoundationMacros
LIBRARY DESTINATION lib/swift/host/plugins
RUNTIME DESTINATION bin)
endif()

0 comments on commit fd4788a

Please sign in to comment.