Skip to content

Commit 92bbd29

Browse files
committed
Enable conditionally building macros
1 parent c548fae commit 92bbd29

File tree

2 files changed

+87
-36
lines changed

2 files changed

+87
-36
lines changed

Package.swift

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import PackageDescription
33
import CompilerPluginSupport
44
import class Foundation.ProcessInfo
55

6+
// get environment variables
7+
let environment = ProcessInfo.processInfo.environment
8+
let dynamicLibrary = environment["SWIFT_BUILD_DYNAMIC_LIBRARY"] == "1"
9+
let enableMacros = environment["SWIFTPM_ENABLE_MACROS"] != "0"
10+
let buildDocs = environment["BUILDING_FOR_DOCUMENTATION_GENERATION"] == "1"
11+
612
// force building as dynamic library
7-
let dynamicLibrary = ProcessInfo.processInfo.environment["SWIFT_BUILD_DYNAMIC_LIBRARY"] != nil
813
let libraryType: PackageDescription.Product.Library.LibraryType? = dynamicLibrary ? .dynamic : nil
914

1015
let package = Package(
@@ -22,37 +27,89 @@ let package = Package(
2227
targets: [
2328
"CoreModel"
2429
]
25-
),
26-
.library(
27-
name: "CoreDataModel",
28-
type: libraryType,
29-
targets: [
30-
"CoreDataModel"
31-
]
32-
)
33-
],
34-
dependencies: [
35-
.package(
36-
url: "https://github.com/swiftlang/swift-syntax.git",
37-
from: "600.0.1"
3830
)
3931
],
4032
targets: [
4133
.target(
42-
name: "CoreModel",
43-
dependencies: [
44-
"CoreModelMacros"
45-
]
34+
name: "CoreModel"
4635
),
47-
.target(
48-
name: "CoreDataModel",
36+
.testTarget(
37+
name: "CoreModelTests",
4938
dependencies: [
5039
"CoreModel"
51-
],
52-
swiftSettings: [
53-
.swiftLanguageMode(.v5)
5440
]
55-
),
41+
)
42+
]
43+
)
44+
45+
#if canImport(Darwin)
46+
package.products.append(
47+
.library(
48+
name: "CoreDataModel",
49+
type: libraryType,
50+
targets: [
51+
"CoreDataModel"
52+
]
53+
)
54+
)
55+
package.targets.insert(
56+
.target(
57+
name: "CoreDataModel",
58+
dependencies: [
59+
"CoreModel"
60+
],
61+
swiftSettings: [
62+
.swiftLanguageMode(.v5)
63+
]
64+
),
65+
at: 1
66+
)
67+
package.targets[package.targets.count - 1] = .testTarget(
68+
name: "CoreModelTests",
69+
dependencies: [
70+
"CoreModel",
71+
.byName(
72+
name: "CoreDataModel",
73+
condition: .when(platforms: [
74+
.macOS,
75+
.iOS,
76+
.macCatalyst,
77+
.watchOS,
78+
.tvOS,
79+
.visionOS
80+
])
81+
)
82+
]
83+
)
84+
#endif
85+
86+
// SwiftPM plugins
87+
if buildDocs {
88+
package.dependencies += [
89+
.package(
90+
url: "https://github.com/swiftlang/swift-docc-plugin.git",
91+
from: "1.4.5"
92+
)
93+
]
94+
}
95+
96+
if enableMacros {
97+
let version: Version
98+
#if swift(>=6.1)
99+
version = "601.0.1"
100+
#else
101+
version = "600.0.1"
102+
#endif
103+
package.targets[0].swiftSettings = [
104+
.define("SWIFTPM_ENABLE_MACROS")
105+
]
106+
package.dependencies += [
107+
.package(
108+
url: "https://github.com/swiftlang/swift-syntax.git",
109+
from: version
110+
)
111+
]
112+
package.targets += [
56113
.macro(
57114
name: "CoreModelMacros",
58115
dependencies: [
@@ -65,17 +122,9 @@ let package = Package(
65122
package: "swift-syntax"
66123
)
67124
]
68-
),
69-
.testTarget(
70-
name: "CoreModelTests",
71-
dependencies: [
72-
"CoreModel",
73-
"CoreModelMacros",
74-
.byName(
75-
name: "CoreDataModel",
76-
condition: .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])
77-
)
78-
]
79125
)
80126
]
81-
)
127+
package.targets[0].dependencies += [
128+
"CoreModelMacros"
129+
]
130+
}

Sources/CoreModel/Macros.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Alsey Coleman Miller on 6/17/25.
66
//
77

8+
#if !hasFeature(Embedded) && SWIFTPM_ENABLE_MACROS
89
@attached(member, names: arbitrary)
910
@attached(extension, conformances: CoreModel.Entity)
1011
public macro Entity(_ name: String? = nil) = #externalMacro(
@@ -23,3 +24,4 @@ public macro Relationship<T: CoreModel.Entity>(destination: T.Type, inverse: T.C
2324
module: "CoreModelMacros",
2425
type: "RelationshipMacro"
2526
)
27+
#endif

0 commit comments

Comments
 (0)