Skip to content

Commit d4aa632

Browse files
committed
Introduce new defines to enable #bundle in Swift Packages
1 parent 5da13be commit d4aa632

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Modules.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,18 @@ extension PackagePIFProjectBuilder {
596596
settings[.COREML_COMPILER_CONTAINER] = "swift-package"
597597
}
598598

599+
// Leave an explicit indicator regarding whether we are generating a Bundle.module accessor.
600+
// This will be read by the #bundle macro defined in Foundation.
601+
if !shouldGenerateBundleAccessor {
602+
// No resources, so explicitly indicate that.
603+
// #bundle will then produce an error about there being no resources.
604+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS].lazilyInitializeAndMutate(initialValue: ["$(inherited)"]) { $0.append("SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE") }
605+
} else if !(resourceBundleName?.isEmpty ?? true) {
606+
// We have an explicit resource bundle via Bundle.module.
607+
// #bundle should call into that.
608+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS].lazilyInitializeAndMutate(initialValue: ["$(inherited)"]) { $0.append("SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE") }
609+
} // else we won't set either of those and just let #bundle point to the same bundle as the source code.
610+
599611
if desiredModuleType == .macro {
600612
settings[.SWIFT_IMPLEMENTS_MACROS_FOR_MODULE_NAMES] = [sourceModule.c99name]
601613
}

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Products.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ extension PackagePIFProjectBuilder {
245245

246246
if result.shouldGenerateBundleAccessor {
247247
settings[.GENERATE_RESOURCE_ACCESSORS] = "YES"
248+
249+
// Do not set `SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE` here since it is just going to point to the same bundle as code.
250+
// #bundle can use its default implementation for that.
248251
}
249252
if result.shouldGenerateEmbedInCodeAccessor {
250253
settings[.GENERATE_EMBED_IN_CODE_ACCESSORS] = "YES"
@@ -264,6 +267,9 @@ extension PackagePIFProjectBuilder {
264267

265268
if result.shouldGenerateBundleAccessor {
266269
settings[.GENERATE_RESOURCE_ACCESSORS] = "YES"
270+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS].lazilyInitializeAndMutate(initialValue: ["$(inherited)"]) { $0.append("SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE") }
271+
} else {
272+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS].lazilyInitializeAndMutate(initialValue: ["$(inherited)"]) { $0.append("SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE") }
267273
}
268274
if result.shouldGenerateEmbedInCodeAccessor {
269275
settings[.GENERATE_EMBED_IN_CODE_ACCESSORS] = "YES"
@@ -305,6 +311,9 @@ extension PackagePIFProjectBuilder {
305311
settings[.GENERATE_RESOURCE_ACCESSORS] = "YES"
306312
settings[.GENERATE_EMBED_IN_CODE_ACCESSORS] = "NO"
307313

314+
// Do not set `SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE` here since it is just going to point to the same bundle as code.
315+
// #bundle can use its default implementation for that.
316+
308317
// If we did not create a resource bundle target,
309318
// we still need to add build tool commands for any generated files.
310319
addBuildToolCommands(

Sources/XCBuildSupport/PIFBuilder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
501501
if let resourceBundle = addResourceBundle(for: mainTarget, in: pifTarget) {
502502
settings[.PACKAGE_RESOURCE_BUNDLE_NAME] = resourceBundle
503503
settings[.GENERATE_RESOURCE_ACCESSORS] = "YES"
504+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS, default: ["$(inherited)"]].append("SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE")
505+
} else {
506+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS, default: ["$(inherited)"]].append("SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE")
504507
}
505508

506509
// For targets, we use the common build settings for both the "Debug" and the "Release" configurations (all
@@ -729,7 +732,10 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
729732
if let resourceBundle = addResourceBundle(for: target, in: pifTarget) {
730733
settings[.PACKAGE_RESOURCE_BUNDLE_NAME] = resourceBundle
731734
settings[.GENERATE_RESOURCE_ACCESSORS] = "YES"
735+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS, default: ["$(inherited)"]].append("SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE")
732736
impartedSettings[.EMBED_PACKAGE_RESOURCE_BUNDLE_NAMES, default: ["$(inherited)"]].append(resourceBundle)
737+
} else {
738+
settings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS, default: ["$(inherited)"]].append("SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE")
733739
}
734740

735741
// For targets, we use the common build settings for both the "Debug" and the "Release" configurations (all

0 commit comments

Comments
 (0)