Skip to content

Commit 0045b31

Browse files
committed
Exclude C/C++-only targets from resource bundle accessor generation
1 parent a94b27b commit 0045b31

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Sources/SWBCore/SpecImplementations/Tools/SwiftCompiler.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,6 +3855,20 @@ public extension BuildPhaseWithBuildFiles {
38553855
}
38563856
return containsFiles(ofType: swiftFileType, referenceLookupContext, specLookupContext, scope, filePathResolver)
38573857
}
3858+
3859+
/// Checks if the build phase contains any ObjC/ObjC++ source files.
3860+
func containsObjCSources(_ referenceLookupContext: any ReferenceLookupContext, _ specLookupContext: any SpecLookupContext, _ scope: MacroEvaluationScope, _ filePathResolver: FilePathResolver) -> Bool {
3861+
let objCFileType = specLookupContext.lookupFileType(identifier: "sourcecode.c.objc")
3862+
let objCPlusPlusFileType = specLookupContext.lookupFileType(identifier: "sourcecode.cpp.objcpp")
3863+
3864+
for fileType in [objCFileType, objCPlusPlusFileType] {
3865+
guard let fileType else { continue }
3866+
if containsFiles(ofType: fileType, referenceLookupContext, specLookupContext, scope, filePathResolver) {
3867+
return true
3868+
}
3869+
}
3870+
return false
3871+
}
38583872
}
38593873

38603874
struct SwiftOutputFileMap: Codable {

Sources/SWBTaskConstruction/TaskProducers/BuildPhaseTaskProducers/SourcesTaskProducer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,8 +1852,10 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
18521852
// we don't need to worry about targets with mixed languages.
18531853
if buildPhase.containsSwiftSources(workspace, context, scope, context.filePathResolver) {
18541854
return await generatePackageTargetBundleAccessorForSwift(scope, bundleName: bundleName)
1855-
} else {
1855+
} else if buildPhase.containsObjCSources(workspace, context, scope, context.filePathResolver) {
18561856
return await generatePackageTargetBundleAccessorForObjC(scope, bundleName: bundleName)
1857+
} else {
1858+
return nil
18571859
}
18581860
}
18591861

Tests/SWBTaskConstructionTests/PackageProductConstructionTests.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ fileprivate struct PackageProductConstructionTests: CoreBasedTests {
932932
children: [
933933
TestFile("main.swift"),
934934
TestFile("main.m"),
935+
TestFile("main.c"),
935936
TestFile("Assets.xcassets"),
936937
]),
937938
buildConfigurations: [
@@ -947,7 +948,7 @@ fileprivate struct PackageProductConstructionTests: CoreBasedTests {
947948
targets: [
948949
TestAggregateTarget(
949950
"ALL",
950-
dependencies: ["tool", "objctool",
951+
dependencies: ["tool", "objctool", "ctool",
951952
"tool_without_resource_bundle_without_catalog",
952953
"tool_without_resource_bundle_with_catalog"]
953954
),
@@ -984,6 +985,21 @@ fileprivate struct PackageProductConstructionTests: CoreBasedTests {
984985
],
985986
dependencies: ["mallory"]
986987
),
988+
TestStandardTarget(
989+
"ctool", type: .commandLineTool,
990+
buildConfigurations: [
991+
TestBuildConfiguration("Debug", buildSettings: [
992+
"PRODUCT_NAME": "$(TARGET_NAME)",
993+
"USE_HEADERMAP": "NO",
994+
"DEFINES_MODULE": "YES",
995+
"PACKAGE_RESOURCE_BUNDLE_NAME": "tool_resources",
996+
]),
997+
],
998+
buildPhases: [
999+
TestSourcesBuildPhase(["main.c"]),
1000+
],
1001+
dependencies: ["mallory"]
1002+
),
9871003
TestStandardTarget(
9881004
"mallory", type: .bundle,
9891005
buildConfigurations: [
@@ -1078,6 +1094,15 @@ fileprivate struct PackageProductConstructionTests: CoreBasedTests {
10781094
}
10791095
}
10801096

1097+
results.checkTarget("ctool") { target in
1098+
results.checkNoTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("resource_bundle_accessor.m"))
1099+
results.checkNoTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("resource_bundle_accessor.h"))
1100+
1101+
results.checkTask(.matchTarget(target), .matchRuleType("CompileC"), .matchRuleItemBasename("main.c")) { task in
1102+
task.checkCommandLineDoesNotContain("-include")
1103+
}
1104+
}
1105+
10811106
results.checkTarget("mallory") { target in
10821107
results.checkWriteAuxiliaryFileTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("resource_bundle_accessor.swift")) { task, contents in
10831108
XCTAssertMatch(contents.unsafeStringValue, .contains("static let module: Bundle"))

0 commit comments

Comments
 (0)