Skip to content

Commit fd4788a

Browse files
authored
Automatically build FoundationMacros for local CMake builds (#844)
1 parent 586c460 commit fd4788a

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ set(SwiftFoundation_MACRO "" CACHE STRING "Path to Foundation macro plugin")
6060
# Make sure our dependencies exists
6161
include(FetchContent)
6262
if (_SwiftFoundationICU_SourceDIR)
63+
message(STATUS "_SwiftFoundationICU_SourceDIR provided, using swift-foundation-icu checkout at ${_SwiftFoundationICU_SourceDIR}")
6364
FetchContent_Declare(SwiftFoundationICU
6465
SOURCE_DIR ${_SwiftFoundationICU_SourceDIR})
6566
else()
67+
message(STATUS "_SwiftFoundationICU_SourceDIR not provided, checking out local copy of swift-foundation-icu")
6668
FetchContent_Declare(SwiftFoundationICU
6769
GIT_REPOSITORY https://github.com/apple/swift-foundation-icu.git
6870
GIT_TAG 0.0.9)
6971
endif()
7072

7173
if (_SwiftCollections_SourceDIR)
74+
message(STATUS "_SwiftCollections_SourceDIR provided, using swift-foundation-icu checkout at ${_SwiftCollections_SourceDIR}")
7275
FetchContent_Declare(SwiftCollections
7376
SOURCE_DIR ${_SwiftCollections_SourceDIR})
7477
else()
78+
message(STATUS "_SwiftCollections_SourceDIR not provided, checking out local copy of swift-collections")
7579
FetchContent_Declare(SwiftCollections
7680
GIT_REPOSITORY https://github.com/apple/swift-collections.git
7781
GIT_TAG 1.1.2)

Sources/FoundationEssentials/CMakeLists.txt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,26 @@ add_subdirectory(TimeZone)
5050
add_subdirectory(URL)
5151

5252
if(SwiftFoundation_MACRO)
53-
target_compile_options(FoundationEssentials PRIVATE
54-
"SHELL:-plugin-path ${SwiftFoundation_MACRO}")
53+
message(STATUS "SwiftFoundation_MACRO provided, using macros in ${SwiftFoundation_MACRO}")
54+
# A path to Foundation macros was provided, so we use that path
55+
target_compile_options(FoundationEssentials PRIVATE
56+
"SHELL:-plugin-path ${SwiftFoundation_MACRO}")
57+
else()
58+
message(STATUS "SwiftFoundation_MACRO not provided, building Foundation macros locally for host")
59+
# No path to Foundation macros was provided, so we must build it ourselves
60+
set(FoundationMacros_BuildLocalExecutable YES)
61+
FetchContent_Declare(FoundationMacros
62+
SOURCE_DIR ${CMAKE_SOURCE_DIR}/Sources/FoundationMacros)
63+
FetchContent_MakeAvailable(FoundationMacros)
64+
add_dependencies(FoundationEssentials FoundationMacros)
65+
get_target_property(MacroDIR FoundationMacros RUNTIME_OUTPUT_DIRECTORY)
66+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
67+
set(MacroExecutable "${MacroDIR}/FoundationMacros.exe#FoundationMacros")
68+
else()
69+
set(MacroExecutable "${MacroDIR}/FoundationMacros#FoundationMacros")
70+
endif()
71+
target_compile_options(FoundationEssentials PUBLIC
72+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-load-plugin-executable ${MacroExecutable}>")
5573
endif()
5674

5775
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)

Sources/FoundationMacros/CMakeLists.txt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ project(FoundationMacros
2929
# SwiftSyntax Dependency
3030
find_package(SwiftSyntax QUIET)
3131
if(NOT SwiftSyntax_FOUND)
32+
message(STATUS "SwiftSyntax_DIR not provided, checking out local copy of swift-syntax")
3233
include(FetchContent)
3334

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

43-
add_library(FoundationMacros SHARED
44+
if(NOT FoundationMacros_BuildLocalExecutable)
45+
add_library(FoundationMacros SHARED)
46+
target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)
47+
else()
48+
add_executable(FoundationMacros)
49+
target_link_libraries(FoundationMacros PUBLIC
50+
SwiftSyntax::SwiftCompilerPlugin)
51+
endif()
52+
53+
# Parse the module as a library, even if it's an executable, because it uses an `@main` type to define its entry point.
54+
target_compile_options(FoundationMacros PRIVATE -parse-as-library)
55+
56+
target_sources(FoundationMacros PRIVATE
4457
FoundationMacros.swift
4558
PredicateMacro.swift)
4659

47-
target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)
48-
49-
target_compile_options(FoundationMacros PRIVATE -parse-as-library)
5060
target_compile_options(FoundationMacros PRIVATE
5161
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>"
5262
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
@@ -72,6 +82,8 @@ set_target_properties(FoundationMacros PROPERTIES
7282
INSTALL_RPATH "$ORIGIN/../../../swift/${SWIFT_SYSTEM_NAME}:$ORIGIN/.."
7383
INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
7484

75-
install(TARGETS FoundationMacros
76-
LIBRARY DESTINATION lib/swift/host/plugins
77-
RUNTIME DESTINATION bin)
85+
if(NOT FoundationMacros_BuildLocalExecutable)
86+
install(TARGETS FoundationMacros
87+
LIBRARY DESTINATION lib/swift/host/plugins
88+
RUNTIME DESTINATION bin)
89+
endif()

0 commit comments

Comments
 (0)