Skip to content

Commit f5fa395

Browse files
committed
Rework the location of the macro definitions for observation and adjust the build scripts to produce a plugin
1 parent 0c03788 commit f5fa395

File tree

8 files changed

+185
-48
lines changed

8 files changed

+185
-48
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,3 +981,70 @@ function(set_swift_llvm_is_available name)
981981
target_compile_definitions(${name} PRIVATE
982982
$<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:SWIFT_LLVM_SUPPORT_IS_AVAILABLE>)
983983
endfunction()
984+
985+
986+
987+
# Add a new host library with the given name.
988+
function(add_swift_host_macro_library name)
989+
set(ASHL_SOURCES ${ARGN})
990+
991+
# Create the library target.
992+
add_library(${name} ${ASHL_SOURCES})
993+
994+
# Add this to the list of exported targets.
995+
set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS ${name})
996+
997+
# Determine where Swift modules will be built and installed.
998+
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
999+
set(module_base "${module_dir}/${name}.swiftmodule")
1000+
set(module_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftmodule")
1001+
set(module_interface_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftinterface")
1002+
set(module_sourceinfo_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftsourceinfo")
1003+
1004+
# Add a custom target to create the module directory.
1005+
add_custom_command(
1006+
TARGET ${name}
1007+
PRE_BUILD
1008+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base}
1009+
COMMENT "Generating module directory for ${name}")
1010+
1011+
# Install the Swift module into the appropriate location.
1012+
set_target_properties(${name}
1013+
PROPERTIES Swift_MODULE_DIRECTORY ${module_dir}
1014+
)
1015+
1016+
# Configure the emission of the Swift module files.
1017+
target_compile_options("${name}" PRIVATE
1018+
$<$<COMPILE_LANGUAGE:Swift>:
1019+
-module-name;${name};
1020+
-enable-library-evolution;
1021+
-emit-module-path;${module_file};
1022+
-emit-module-source-info-path;${module_sourceinfo_file};
1023+
-emit-module-interface-path;${module_interface_file}
1024+
>)
1025+
1026+
# NOTE: workaround for CMake not setting up include flags yet
1027+
set_target_properties(${name} PROPERTIES
1028+
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
1029+
)
1030+
1031+
set_target_properties(${name} PROPERTIES
1032+
BUILD_WITH_INSTALL_RPATH YES
1033+
)
1034+
1035+
# Install this target
1036+
install(TARGETS ${name}
1037+
EXPORT SwiftSyntaxTargets
1038+
ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
1039+
LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
1040+
RUNTIME DESTINATION bin
1041+
)
1042+
1043+
# Install the module files.
1044+
install(
1045+
DIRECTORY ${module_base}
1046+
DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
1047+
FILES_MATCHING PATTERN "*.swiftinterface"
1048+
)
1049+
endfunction()
1050+

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_subdirectory(Immediate)
3535
add_subdirectory(IRGen)
3636
add_subdirectory(LLVMPasses)
3737
add_subdirectory(Localization)
38+
add_subdirectory(Macros)
3839
add_subdirectory(Markup)
3940
add_subdirectory(Migrator)
4041
add_subdirectory(Option)

lib/Macros/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#===--- CMakeLists.txt - Macro support libraries ------------------------===#
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
#===----------------------------------------------------------------------===#
12+
13+
add_subdirectory(Sources/ObservationMacros)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#===--- CMakeLists.txt - Observation macros library ----------------------===#
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
#===----------------------------------------------------------------------===#
12+
13+
## TODO: Turn this into a function for other macros to use
14+
set(SWIFT_HOST_LIBRARIES_SUBDIRECTORY "swift/host")
15+
16+
set(module_name "ObservationMacros")
17+
set(library_name "${module_name}")
18+
19+
add_library("${library_name}" SHARED
20+
ObservableMacro.swift)
21+
22+
set_target_properties(${library_name}
23+
PROPERTIES
24+
PREFIX ""
25+
SUFFIX ".lib"
26+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}"
27+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}"
28+
)
29+
30+
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
31+
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
32+
endif()
33+
34+
# Determine the Swift module path
35+
set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE})
36+
set(module_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
37+
set(module_base "${module_dir}/${module_name}.swiftmodule")
38+
set(module_file "${module_base}/${module_triple}.swiftmodule")
39+
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
40+
set(module_doc_file "${module_base}/${module_triple}.swiftdoc")
41+
42+
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
43+
MACCATALYST_BUILD_FLAVOR ""
44+
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
45+
46+
# Add a custom target to create the module directory and remove any old
47+
# compiled module file.
48+
add_custom_command_target(
49+
create_module_dirs_dependency_target
50+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base}
51+
COMMAND "${CMAKE_COMMAND}" -E rm -f "${module_file}"
52+
OUTPUT ${module_base}
53+
COMMENT "Generating module directory for ${module_name}")
54+
add_dependencies(${library_name} ${create_module_dirs_dependency_target})
55+
56+
# Build the module with library evolution enabled and install into the
57+
# appropriate locations.
58+
target_compile_options("${library_name}" PRIVATE
59+
$<$<COMPILE_LANGUAGE:Swift>:
60+
-module-name;${module_name};
61+
-enable-library-evolution;
62+
-emit-module-interface-path;
63+
"${module_interface_file}" ;
64+
-I ${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY};
65+
-Xlinker -L${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY};
66+
-target;${target}>)
67+
68+
69+
swift_install_in_component(TARGETS "${library_name}"
70+
RUNTIME
71+
DESTINATION "bin"
72+
COMPONENT compiler
73+
FRAMEWORK
74+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}"
75+
COMPONENT compiler
76+
LIBRARY
77+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}"
78+
COMPONENT compiler
79+
ARCHIVE
80+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}"
81+
COMPONENT compiler)
82+
83+
swift_install_in_component(DIRECTORY "${module_base}"
84+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}"
85+
COMPONENT compiler)
86+
87+
# set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${library_name})

stdlib/public/Observation/Sources/ObservationMacros/ObservableMacro.swift renamed to lib/Macros/Sources/ObservationMacros/ObservableMacro.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import SwiftSyntax
22
import SwiftSyntaxMacros
33

4+
@_implementationOnly import SwiftDiagnostics
5+
@_implementationOnly import SwiftOperators
6+
@_implementationOnly import SwiftSyntaxBuilder
7+
48
private extension DeclSyntaxProtocol {
59
var isObservableStoredProperty: Bool {
610
guard let property = self.as(VariableDeclSyntax.self),
@@ -27,7 +31,7 @@ public struct ObservableMacro: MemberMacro, MemberAttributeMacro {
2731

2832
let parentName = identified.identifier
2933

30-
let registrar: DeclSyntax =
34+
let registrar: DeclSyntax =
3135
"""
3236
let _registrar = ObservationRegistrar<\(parentName)>()
3337
"""
@@ -74,12 +78,17 @@ public struct ObservableMacro: MemberMacro, MemberAttributeMacro {
7478
}
7579

7680
// MARK: - MemberAttributeMacro
77-
public static func expansion(
81+
82+
public static func expansion<
83+
Declaration: DeclGroupSyntax,
84+
MemberDeclaration: DeclSyntaxProtocol,
85+
Context: MacroExpansionContext
86+
>(
7887
of node: AttributeSyntax,
79-
attachedTo declaration: some DeclGroupSyntax,
80-
providingAttributesFor member: DeclSyntax,
81-
in context: some MacroExpansionContext
82-
) throws -> [SwiftSyntax.AttributeSyntax] {
88+
attachedTo declaration: Declaration,
89+
providingAttributesFor member: MemberDeclaration,
90+
in context: Context
91+
) throws -> [AttributeSyntax] {
8392
guard member.isObservableStoredProperty else {
8493
return []
8594
}

stdlib/public/Observation/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@
1111
#===----------------------------------------------------------------------===#
1212

1313
add_subdirectory(Sources/Observation)
14-
add_subdirectory(Sources/ObservationMacros)

stdlib/public/Observation/Sources/Observation/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212

1313
set(SWIFT_OBSERVATION_SWIFT_FLAGS)
1414

15-
set(SWIFT_HOST_LIBRARIES_SUBDIRECTORY "swift/host")
16-
1715
list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include)
1816

1917
list(APPEND SWIFT_OBSERVATION_SWIFT_FLAGS
2018
"-enable-experimental-feature" "Macros"
2119
"-load-plugin-library"
22-
${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY})
20+
${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}/ObservationMacros.lib)
2321

2422
add_swift_target_library(swiftObservation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
2523
KeyPaths.swift
@@ -37,7 +35,7 @@ add_swift_target_library(swiftObservation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS
3735
SWIFT_COMPILE_FLAGS
3836
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
3937
${SWIFT_OBSERVATION_SWIFT_FLAGS}
40-
SWIFT_MODULE_DEPENDS _Concurrency ObservationMacros
38+
SWIFT_MODULE_DEPENDS _Concurrency
4139
INSTALL_IN_COMPONENT stdlib
4240

4341
MACCATALYST_BUILD_FLAVOR "zippered"

stdlib/public/Observation/Sources/ObservationMacros/CMakeLists.txt

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)