Skip to content

Commit 0e50a04

Browse files
authored
Support resources in ObjC++ code (#3828)
Resources weren't supported in ObjC++ code so far, but there wasn't a good reason for that. rdar://83058216
1 parent 22e64d4 commit 0e50a04

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

Fixtures/Resources/Simple/Package.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ let package = Package(
1717
.copy("foo.txt"),
1818
]
1919
),
20+
21+
.target(
22+
name: "CPPResource",
23+
resources: [
24+
.copy("foo.txt"),
25+
]
26+
),
2027
]
2128
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <Foundation/Foundation.h>
2+
3+
int main(int argc, const char * argv[]) {
4+
@autoreleasepool {
5+
NSBundle *bundle = SWIFTPM_MODULE_BUNDLE;
6+
NSString *foo = [bundle pathForResource:@"foo" ofType:@"txt"];
7+
NSData *data = [NSFileManager.defaultManager contentsAtPath:foo];
8+
NSString *contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
9+
printf("%s", contents.UTF8String);
10+
}
11+
return 0;
12+
}

Sources/Build/BuildPlan.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,17 @@ public final class ClangTargetBuildDescription {
471471
headerFileStream <<< """
472472
#import <Foundation/Foundation.h>
473473
474+
#if __cplusplus
475+
extern "C" {
476+
#endif
477+
474478
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE(void);
475479
476480
#define SWIFTPM_MODULE_BUNDLE \(target.c99name)_SWIFTPM_MODULE_BUNDLE()
481+
482+
#if __cplusplus
483+
}
484+
#endif
477485
"""
478486
let headerFile = derivedSources.root.appending(component: "resource_bundle_accessor.h")
479487
self.resourceAccessorHeaderFile = headerFile

Sources/PackageModel/Sources.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct Sources: Codable {
4646
guard let ext = $0.extension else {
4747
return false
4848
}
49-
return ext == SupportedLanguageExtension.m.rawValue
49+
return ext == SupportedLanguageExtension.m.rawValue || ext == SupportedLanguageExtension.mm.rawValue
5050
})
5151
}
5252
}

Tests/FunctionalTests/ResourcesTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ResourcesTests: XCTestCase {
2121
// Objective-C module requires macOS
2222
#if os(macOS)
2323
executables.append("SeaResource")
24+
executables.append("CPPResource")
2425
#endif
2526

2627
for execName in executables {

0 commit comments

Comments
 (0)