Skip to content

Commit 8e875b5

Browse files
authored
Add an option to make the bridging header internal (#816)
Add `SWIFT_BRIDGING_HEADER_IS_INTERNAL` option, which defaults to NO. When it is YES, use the new `-internal-import-bridging-header` instead of `-import-objc-header`. The new version treats the contents of the bridging header as if they came in through an internal import. Tracked by rdar://161172254.
1 parent fd3e9d2 commit 8e875b5

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

Sources/SWBCore/Settings/BuiltinMacros.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ public final class BuiltinMacros {
10571057
public static let SWIFT_WARNINGS_AS_WARNINGS_GROUPS = BuiltinMacros.declareStringListMacro("SWIFT_WARNINGS_AS_WARNINGS_GROUPS")
10581058
public static let SWIFT_WARNINGS_AS_ERRORS_GROUPS = BuiltinMacros.declareStringListMacro("SWIFT_WARNINGS_AS_ERRORS_GROUPS")
10591059
public static let SWIFT_OBJC_BRIDGING_HEADER = BuiltinMacros.declareStringMacro("SWIFT_OBJC_BRIDGING_HEADER")
1060+
public static let SWIFT_BRIDGING_HEADER_IS_INTERNAL = BuiltinMacros.declareBooleanMacro("SWIFT_BRIDGING_HEADER_IS_INTERNAL")
10601061
public static let SWIFT_OBJC_INTERFACE_HEADER_NAME = BuiltinMacros.declareStringMacro("SWIFT_OBJC_INTERFACE_HEADER_NAME")
10611062
public static let SWIFT_OBJC_INTERFACE_HEADER_DIR = BuiltinMacros.declareStringMacro("SWIFT_OBJC_INTERFACE_HEADER_DIR")
10621063
public static let SWIFT_OBJC_INTEROP_MODE = BuiltinMacros.declareStringMacro("SWIFT_OBJC_INTEROP_MODE")
@@ -2271,6 +2272,7 @@ public final class BuiltinMacros {
22712272
SWIFT_MODULE_ONLY_TVOS_DEPLOYMENT_TARGET,
22722273
SWIFT_MODULE_ONLY_WATCHOS_DEPLOYMENT_TARGET,
22732274
SWIFT_OBJC_BRIDGING_HEADER,
2275+
SWIFT_BRIDGING_HEADER_IS_INTERNAL,
22742276
SWIFT_OBJC_INTERFACE_HEADER_NAME,
22752277
SWIFT_OBJC_INTERFACE_HEADER_DIR,
22762278
SWIFT_OBJC_INTEROP_MODE,

Sources/SWBCore/SpecImplementations/Tools/SwiftCompiler.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,10 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
20972097
let objcBridgingHeaderPath = Path(cbc.scope.evaluate(BuiltinMacros.SWIFT_OBJC_BRIDGING_HEADER))
20982098
if !objcBridgingHeaderPath.isEmpty {
20992099
let objcBridgingHeaderNode = delegate.createNode(objcBridgingHeaderPath)
2100-
args += ["-import-objc-header", objcBridgingHeaderNode.path.normalize().str]
2100+
let flag = cbc.scope.evaluate(BuiltinMacros.SWIFT_BRIDGING_HEADER_IS_INTERNAL)
2101+
? "-internal-import-bridging-header"
2102+
: "-import-objc-header"
2103+
args += [flag, objcBridgingHeaderNode.path.normalize().str]
21012104
extraInputPaths.append(objcBridgingHeaderPath)
21022105
let precompsPath = cbc.scope.evaluate(BuiltinMacros.SHARED_PRECOMPS_DIR)
21032106
if !precompsPath.isEmpty,
@@ -3477,6 +3480,8 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
34773480
// We want the objc header in XOJIT mode so ignore in dynamic replacement mode
34783481
"-import-objc-header",
34793482

3483+
"-internal-import-bridging-header",
3484+
34803485
// Old setting only stripped in dynamic replacement mode for backward compatibility
34813486
"-clang-build-session-file",
34823487
] {

Sources/SWBUniversalPlatform/Specs/Swift.xcspec

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,16 @@
225225
Type = String;
226226
DefaultValue = "";
227227
Category = "General";
228-
DisplayName = "Objective-C Bridging Header";
229-
Description = "Path to the header defining the Objective-C interfaces to be exposed in Swift.";
228+
DisplayName = "Bridging Header";
229+
Description = "Path to the header defining the C interfaces to be exposed in Swift.";
230+
},
231+
{
232+
Name = "SWIFT_BRIDGING_HEADER_IS_INTERNAL";
233+
Type = Boolean;
234+
DefaultValue = "NO";
235+
Category = "General";
236+
DisplayName = "Bridging Header is Internal to the Module";
237+
Description = "When there is a bridging header, this setting indicates whether the contents of that bridging header should be imported as-if they came from an internal import. This is necessary when using bridging headers with Swift modules that are imported into other modules.";
230238
},
231239
{
232240
Name = "SWIFT_OBJC_INTERFACE_HEADER_NAME";

Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,8 +1748,9 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
17481748
}
17491749

17501750
/// Check handling of Swift bridging header.
1751-
@Test(.requireSDKs(.macOS))
1752-
func swiftBridgingHeader() async throws {
1751+
@Test(.requireSDKs(.macOS), arguments: [false, true])
1752+
func swiftBridgingHeader(isInternal: Bool) async throws {
1753+
let bridgingHeaderIsInternal = isInternal ? "YES" : "NO"
17531754
let testProject = try await TestProject(
17541755
"aProject",
17551756
groupTree: TestGroup(
@@ -1773,6 +1774,7 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
17731774
buildSettings: [
17741775
"SDKROOT": "macosx",
17751776
"SWIFT_OBJC_BRIDGING_HEADER": "swift-bridge-header.h",
1777+
"SWIFT_BRIDGING_HEADER_IS_INTERNAL": bridgingHeaderIsInternal,
17761778
"SWIFT_VERSION": swiftVersion,
17771779
]),
17781780
],
@@ -1797,8 +1799,9 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
17971799
results.checkTask(.matchTarget(target), .matchRuleType("SwiftDriver Compilation")) { task in
17981800
task.checkRuleInfo(["SwiftDriver Compilation", "FooApp", "normal", "x86_64", "com.apple.xcode.tools.swift.compiler"])
17991801

1802+
let expectedFlag = isInternal ? "-internal-import-bridging-header" : "-import-objc-header"
18001803
task.checkCommandLineContains([
1801-
"-import-objc-header", bridgeHeader.str])
1804+
expectedFlag, bridgeHeader.str])
18021805
task.checkInputs(contain: [.path(bridgeHeader.str)])
18031806
}
18041807
}

0 commit comments

Comments
 (0)