Skip to content

Automatically build FoundationMacros for local CMake builds #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macros should build through ExternalProject. If we are cross-compiling Foundation, these macros will get built for the machine that we are cross-compiling to instead of the machine running the build, and therefore will not be usable.

SOURCE_DIR ${CMAKE_SOURCE_DIR}/Sources/FoundationMacros)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't CMAKE_SOURCE_DIR be the swift-corelibs-foundation when this project is added as an external project from there? Would ${CMAKE_CURRENT_SOURCE_DIR}/../FoundationMacros be more correct here?

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()