Skip to content

Commit fd8ad12

Browse files
committed
Enable conditionally building macros
1 parent c548fae commit fd8ad12

File tree

2 files changed

+75
-37
lines changed

2 files changed

+75
-37
lines changed

Package.swift

Lines changed: 73 additions & 37 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,76 @@ 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: [
50-
"CoreModel"
51-
],
52-
swiftSettings: [
53-
.swiftLanguageMode(.v5)
39+
"CoreModel",
40+
.byName(
41+
name: "CoreDataModel",
42+
condition: .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])
43+
)
5444
]
55-
),
45+
)
46+
]
47+
)
48+
49+
#if canImport(Darwin)
50+
package.products.append(
51+
.library(
52+
name: "CoreDataModel",
53+
type: libraryType,
54+
targets: [
55+
"CoreDataModel"
56+
]
57+
)
58+
)
59+
package.targets.insert(
60+
.target(
61+
name: "CoreDataModel",
62+
dependencies: [
63+
"CoreModel"
64+
],
65+
swiftSettings: [
66+
.swiftLanguageMode(.v5)
67+
]
68+
),
69+
at: 1
70+
)
71+
#endif
72+
73+
// SwiftPM plugins
74+
if buildDocs {
75+
package.dependencies += [
76+
.package(
77+
url: "https://github.com/swiftlang/swift-docc-plugin.git",
78+
from: "1.4.5"
79+
)
80+
]
81+
}
82+
83+
if enableMacros {
84+
let version: Version
85+
#if swift(>=6.1)
86+
version = "601.0.1"
87+
#else
88+
version = "600.0.1"
89+
#endif
90+
package.targets[0].swiftSettings = [
91+
.define("SWIFTPM_ENABLE_MACROS")
92+
]
93+
package.dependencies += [
94+
.package(
95+
url: "https://github.com/swiftlang/swift-syntax.git",
96+
from: version
97+
)
98+
]
99+
package.targets += [
56100
.macro(
57101
name: "CoreModelMacros",
58102
dependencies: [
@@ -65,17 +109,9 @@ let package = Package(
65109
package: "swift-syntax"
66110
)
67111
]
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-
]
79112
)
80113
]
81-
)
114+
package.targets[0].dependencies += [
115+
"CoreModelMacros"
116+
]
117+
}

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)