Skip to content

Commit b3d17fa

Browse files
Linux packages fail to build with LLVM 18 and Apple 2023 versions of clang (#7241)
Newer versions of clang add builtin modules for its C Standard Library headers. This creates problems* when building with a single module map that tries to absorb all of the OS/SDK dependencies, which is what all non-Apple platforms currently do in SwiftPM. This works for Swift targets because the Swift compiler injects module maps via vfs for non-Apple platforms. However, clang doesn't do anything similar for C-based targets, and so they fail to build. For now, don't build with modules for any SwiftPM C-based targets on non-Apple platforms. \* Some problems are module cycles and headers being absorbed into unexpected modules. clang modules generally expect that OS/SDK headers are covered by module maps, and _not_ absorbed into whatever module happens to include them first. rdar://120716498
1 parent 6e2f76c commit b3d17fa

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,15 @@ public final class ClangTargetBuildDescription {
239239
}
240240

241241
// Enable Clang module flags, if appropriate.
242-
let enableModules: Bool
243242
let triple = self.buildParameters.triple
244-
if toolsVersion < .v5_8 {
245-
// For version < 5.8, we enable them except in these cases:
246-
// 1. on Darwin when compiling for C++, because C++ modules are disabled on Apple-built Clang releases
247-
// 2. on Windows when compiling for any language, because of issues with the Windows SDK
248-
// 3. on Android when compiling for any language, because of issues with the Android SDK
249-
enableModules = !(triple.isDarwin() && isCXX) && !triple.isWindows() && !triple.isAndroid()
250-
} else {
251-
// For version >= 5.8, we disable them when compiling for C++ regardless of platforms, see:
252-
// https://github.com/llvm/llvm-project/issues/55980 for clang frontend crash when module
253-
// enabled for C++ on c++17 standard and above.
254-
enableModules = !isCXX && !triple.isWindows() && !triple.isAndroid()
255-
}
256-
243+
// Swift is able to use modules on non-Darwin platforms because it injects its own module maps
244+
// via vfs. However, nothing does that for C based compilation, and so non-Darwin platforms can't
245+
// support clang modules.
246+
// Note that if modules get enabled for other platforms later, we'll need to verify that
247+
// https://github.com/llvm/llvm-project/issues/55980 (crash on C++17 and later) is fixed, or don't
248+
// enable modules in the affected modes.
249+
let enableModules = triple.isDarwin()
257250
if enableModules {
258-
// Using modules currently conflicts with the Windows and Android SDKs.
259251
args += ["-fmodules", "-fmodule-name=" + target.c99name]
260252
}
261253

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,11 +1353,11 @@ final class BuildPlanTests: XCTestCase {
13531353
args += ["-target", defaultTargetTriple]
13541354
args += ["-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1"]
13551355
args += ["-fblocks"]
1356-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
1356+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
13571357
args += ["-fmodules", "-fmodule-name=extlib"]
13581358
#endif
13591359
args += ["-I", ExtPkg.appending(components: "Sources", "extlib", "include").pathString]
1360-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
1360+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
13611361
args += ["-fmodules-cache-path=\(buildPath.appending(components: "ModuleCache"))"]
13621362
#endif
13631363

@@ -1382,7 +1382,7 @@ final class BuildPlanTests: XCTestCase {
13821382

13831383
args += ["-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1"]
13841384
args += ["-fblocks"]
1385-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
1385+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
13861386
args += ["-fmodules", "-fmodule-name=exe"]
13871387
#endif
13881388
args += [
@@ -1392,7 +1392,7 @@ final class BuildPlanTests: XCTestCase {
13921392
"-I", ExtPkg.appending(components: "Sources", "extlib", "include").pathString,
13931393
"-fmodule-map-file=\(buildPath.appending(components: "extlib.build", "module.modulemap"))",
13941394
]
1395-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
1395+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
13961396
args += ["-fmodules-cache-path=\(buildPath.appending(components: "ModuleCache"))"]
13971397
#endif
13981398
args += [hostTriple.isWindows() ? "-gdwarf" : "-g"]
@@ -1710,11 +1710,11 @@ final class BuildPlanTests: XCTestCase {
17101710

17111711
args += ["-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1"]
17121712
args += ["-fblocks"]
1713-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
1713+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
17141714
args += ["-fmodules", "-fmodule-name=lib"]
17151715
#endif
17161716
args += ["-I", Pkg.appending(components: "Sources", "lib", "include").pathString]
1717-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
1717+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
17181718
args += ["-fmodules-cache-path=\(buildPath.appending(components: "ModuleCache"))"]
17191719
#endif
17201720
args += [hostTriple.isWindows() ? "-gdwarf" : "-g"]
@@ -2866,11 +2866,11 @@ final class BuildPlanTests: XCTestCase {
28662866
var expectedExeBasicArgs = triple.isDarwin() ? ["-fobjc-arc"] : []
28672867
expectedExeBasicArgs += ["-target", defaultTargetTriple]
28682868
expectedExeBasicArgs += ["-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1", "-fblocks"]
2869-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
2869+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
28702870
expectedExeBasicArgs += ["-fmodules", "-fmodule-name=exe"]
28712871
#endif
28722872
expectedExeBasicArgs += ["-I", Pkg.appending(components: "Sources", "exe", "include").pathString]
2873-
#if !os(Windows) // FIXME(5473) - modules flags on Windows dropped
2873+
#if os(macOS) // FIXME(5473) - support modules on non-Apple platforms
28742874
expectedExeBasicArgs += ["-fmodules-cache-path=\(buildPath.appending(components: "ModuleCache"))"]
28752875
#endif
28762876

@@ -2889,7 +2889,7 @@ final class BuildPlanTests: XCTestCase {
28892889
var expectedLibBasicArgs = triple.isDarwin() ? ["-fobjc-arc"] : []
28902890
expectedLibBasicArgs += ["-target", defaultTargetTriple]
28912891
expectedLibBasicArgs += ["-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1", "-fblocks"]
2892-
let shouldHaveModules = !(triple.isDarwin() || triple.isWindows() || triple.isAndroid())
2892+
let shouldHaveModules = triple.isDarwin()
28932893
if shouldHaveModules {
28942894
expectedLibBasicArgs += ["-fmodules", "-fmodule-name=lib"]
28952895
}
@@ -3455,9 +3455,8 @@ final class BuildPlanTests: XCTestCase {
34553455
let args = [
34563456
"-target", "wasm32-unknown-wasi",
34573457
"-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1",
3458-
"-fblocks", "-fmodules", "-fmodule-name=lib",
3458+
"-fblocks",
34593459
"-I", Pkg.appending(components: "Sources", "lib", "include").pathString,
3460-
"-fmodules-cache-path=\(buildPath.appending(components: "ModuleCache"))",
34613460
"-g",
34623461
]
34633462
XCTAssertEqual(try lib.basicArguments(isCXX: false), args)

0 commit comments

Comments
 (0)