Skip to content

Commit c96a621

Browse files
committed
Emit and search for the compatibility header in an include subdirectory
Emit the generated compatibility header and module map in a new `include` directory under the temporary build directory of Swift libraries. This directory can then be used as a header search path for the dependent clang targets. This replaces the previous header search strategy that used only the generated module map. This was incompatible with C source files and classic non-module textual includes. The use of a header search path should support both module imports via the module map as well as includes of the compatibility header.
1 parent a3f8115 commit c96a621

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,15 @@ public final class SwiftModuleBuildDescription {
890890
try self.fileSystem.writeFileContents(path, bytes: .init(encodingAsUTF8: content), atomically: true)
891891
}
892892

893+
/// Directory for the the compatibility header and module map generated for this target.
894+
/// The whole directory should be usable as a header search path.
895+
private var compatibilityHeaderDirectory: AbsolutePath {
896+
tempsPath.appending("include")
897+
}
898+
893899
/// Generates the module map for the Swift target and returns its path.
894900
private func generateModuleMap() throws -> AbsolutePath {
895-
let path = self.tempsPath.appending(component: moduleMapFilename)
901+
let path = self.compatibilityHeaderDirectory.appending(component: moduleMapFilename)
896902

897903
let bytes = ByteString(
898904
#"""
@@ -916,7 +922,7 @@ public final class SwiftModuleBuildDescription {
916922

917923
/// Returns the path to the ObjC compatibility header for this Swift target.
918924
var objCompatibilityHeaderPath: AbsolutePath {
919-
self.tempsPath.appending("\(self.target.name)-Swift.h")
925+
self.compatibilityHeaderDirectory.appending("\(self.target.name)-Swift.h")
920926
}
921927

922928
/// Returns the build flags from the declared build settings.

Sources/Build/BuildPlan/BuildPlan+Clang.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ extension BuildPlan {
3030
case is SwiftModule:
3131
if case let .swift(dependencyTargetDescription)? = description {
3232
if let moduleMap = dependencyTargetDescription.moduleMap {
33-
clangTarget.additionalFlags += ["-fmodule-map-file=\(moduleMap.pathString)"]
33+
// C languages clients should either import the module or include the compatibility header next to it.
34+
clangTarget.additionalFlags += ["-I", moduleMap.dirname]
3435
}
3536
}
3637

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5609,7 +5609,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
56095609
.anySequence,
56105610
"-emit-objc-header",
56115611
"-emit-objc-header-path",
5612-
"\(buildPath.appending(components: "Foo.build", "Foo-Swift.h"))",
5612+
"\(buildPath.appending(components: "Foo.build", "include", "Foo-Swift.h"))",
56135613
.anySequence,
56145614
]
56155615
)
@@ -5619,7 +5619,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
56195619
barTarget,
56205620
[
56215621
.anySequence,
5622-
"-fmodule-map-file=\(buildPath.appending(components: "Foo.build", "module.modulemap"))",
5622+
"-I", "\(buildPath.appending(components: "Foo.build", "include"))",
56235623
.anySequence,
56245624
]
56255625
)
@@ -5694,7 +5694,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
56945694
.anySequence,
56955695
"-emit-objc-header",
56965696
"-emit-objc-header-path",
5697-
"\(buildPath.appending(components: "Foo.build", "Foo-Swift.h"))",
5697+
"\(buildPath.appending(components: "Foo.build", "include", "Foo-Swift.h"))",
56985698
.anySequence,
56995699
]
57005700
)
@@ -5704,7 +5704,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
57045704
barTarget,
57055705
[
57065706
.anySequence,
5707-
"-fmodule-map-file=\(buildPath.appending(components: "Foo.build", "module.modulemap"))",
5707+
"-I", "\(buildPath.appending(components: "Foo.build", "include"))",
57085708
.anySequence,
57095709
]
57105710
)
@@ -5784,7 +5784,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
57845784
.anySequence,
57855785
"-emit-objc-header",
57865786
"-emit-objc-header-path",
5787-
"\(buildPath.appending(components: "Foo.build", "Foo-Swift.h"))",
5787+
"\(buildPath.appending(components: "Foo.build", "include", "Foo-Swift.h"))",
57885788
.anySequence,
57895789
]
57905790
)
@@ -5794,7 +5794,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
57945794
barTarget,
57955795
[
57965796
.anySequence,
5797-
"-fmodule-map-file=\(buildPath.appending(components: "Foo.build", "module.modulemap"))",
5797+
"-I", "\(buildPath.appending(components: "Foo.build", "include"))",
57985798
.anySequence,
57995799
]
58005800
)

Tests/BuildTests/ModuleAliasingBuildTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,19 +737,19 @@ final class ModuleAliasingBuildTests: XCTestCase {
737737
XCTAssertMatch(
738738
fooLoggingArgs,
739739
[.anySequence, "-emit-objc-header", "-emit-objc-header-path",
740-
"\(buildPath.appending(components: "FooLogging.build", "FooLogging-Swift.h"))",
740+
"\(buildPath.appending(components: "FooLogging.build", "include", "FooLogging-Swift.h"))",
741741
.anySequence]
742742
)
743743
XCTAssertMatch(
744744
barLoggingArgs,
745745
[.anySequence, "-emit-objc-header", "-emit-objc-header-path",
746-
"\(buildPath.appending(components: "BarLogging.build", "BarLogging-Swift.h"))",
746+
"\(buildPath.appending(components: "BarLogging.build", "include", "BarLogging-Swift.h"))",
747747
.anySequence]
748748
)
749749
XCTAssertMatch(
750750
loggingArgs,
751751
[.anySequence, "-emit-objc-header", "-emit-objc-header-path",
752-
"\(buildPath.appending(components: "Logging.build", "Logging-Swift.h"))",
752+
"\(buildPath.appending(components: "Logging.build", "include", "Logging-Swift.h"))",
753753
.anySequence]
754754
)
755755
}
@@ -843,13 +843,13 @@ final class ModuleAliasingBuildTests: XCTestCase {
843843
XCTAssertMatch(
844844
otherLoggingArgs,
845845
[.anySequence, "-emit-objc-header", "-emit-objc-header-path",
846-
"\(buildPath.appending(components: "OtherLogging.build", "OtherLogging-Swift.h"))",
846+
"\(buildPath.appending(components: "OtherLogging.build", "include", "OtherLogging-Swift.h"))",
847847
.anySequence]
848848
)
849849
XCTAssertMatch(
850850
loggingArgs,
851851
[.anySequence, "-emit-objc-header", "-emit-objc-header-path",
852-
"\(buildPath.appending(components: "Logging.build", "Logging-Swift.h"))",
852+
"\(buildPath.appending(components: "Logging.build", "include", "Logging-Swift.h"))",
853853
.anySequence]
854854
)
855855
}

0 commit comments

Comments
 (0)