swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.66k stars 10.38k forks source link

Building Swift fails on linux with module redefinition errors #62429

Open keith opened 1 year ago

keith commented 1 year ago

When building the Swift compiler on Linux with:

LDFLAGS="-Xlinker -z -Xlinker nostart-stop-gc" CC=/usr/bin/clang CXX=/usr/bin/clang++ ./swift/utils/build-script --release-debuginfo --sccache --llvm-max-parallel-lto-link-jobs 1 --llvm-cmake-options='-DLLVM_ENABLE_LLD=' --extra-cmake-options="-DSWIFT_USE_LINKER= -DLLVM_USE_LINKER=/home/ksmiley/.bin/ld.lld" --reconfigure --bootstrapping off

While also having swiftc 5.7.1 in my $PATH, I end up getting these build failures:

/opt/swift/swift-5.7.1/usr/lib/swift/dispatch/module.modulemap:8:8: error: redefinition of module 'DispatchIntrospection'
module DispatchIntrospection [system] [extern_c] {
       ^
/home/ksmiley/dev/oss-swift/swift-corelibs-libdispatch/dispatch/module.modulemap:8:8: note: previously defined here
module DispatchIntrospection [system] [extern_c] {
       ^
/opt/swift/swift-5.7.1/usr/lib/swift/dispatch/module.modulemap:13:8: error: redefinition of module 'CDispatch'
module CDispatch [system] [extern_c] {
       ^
/home/ksmiley/dev/oss-swift/swift-corelibs-libdispatch/dispatch/module.modulemap:13:8: note: previously defined here
module CDispatch [system] [extern_c] {
       ^
ninja: build stopped: subcommand failed.

Dispatch seems to be the first module that fails, but there are many errors with the same issue. Eventually I also see:

/home/ksmiley/dev/oss-swift/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/lib/swift/host/SwiftDiagnostics.swiftmodule/x86_64-unknown-linux-gnu.private.swiftinterface:5:8: error: failed to build module 'SwiftSyntax' for importation due to the errors above; the textual interface may be broken by project issues or a compiler bug
import SwiftSyntax
       ^
/home/ksmiley/dev/oss-swift/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/lib/swift/host/SwiftParser.swiftmodule/x86_64-unknown-linux-gnu.private.swiftinterface:5:8: error: failed to build module 'SwiftDiagnostics' for importation due to the errors above; the textual interface may be broken by project issues or a compiler bug
import SwiftDiagnostics
       ^
/opt/swift/swift-5.7.1/usr/lib/swift/dispatch/module.modulemap:1:8: error: redefinition of module 'Dispatch'
module Dispatch {
       ^
/home/ksmiley/dev/oss-swift/swift-corelibs-libdispatch/dispatch/module.modulemap:1:8: note: previously defined here
module Dispatch {
       ^

But I believe the message is correct that the issue is the other duplicate modules.

My impression was that building with an existing Swift compiler installation was expected (or maybe even required), but I am attempting to build without these in my $PATH assuming that if it still works that way it won't be able to find these modulemaps, so maybe I'll side step this.

Steps to reproduce

Environment

keith commented 1 year ago

might have been fixed by passing -skip-early-swift-driver --skip-early-swiftsyntax from the docs (must have changed since my last build ~a month ago since I just re-ran my previous command)

finagolfin commented 1 year ago

I've seen this on Android too, with the prebuilt Swift toolchain package colliding with a trunk toolchain I'm building. The Linux CI doesn't have a prebuilt Swift toolchain, so it doesn't build those early Swift tools, but we'll need to fix this for when building the Swift compiler with a prebuilt Swift compiler becomes mandatory, so keep this open.

redsun82 commented 1 year ago

I am just encountering this since the update to swift 5.8. Unfortunately I do require bootstrapping. Any workaround for this?

finagolfin commented 1 year ago

This has nothing to do with bootstrapping: it should work if you don't have a prebuilt toolchain in your path, as the linux CI doesn't, or if you pass the two build flags Keith mentioned.

kateinoigakukun commented 1 year ago

Just for record, dispatch/module.modulemap is no longer placed under source directory of swift-corelibs-libdispatch after https://github.com/apple/swift-corelibs-libdispatch/pull/785, so I just needed to clean the source directory by git clean -f to repair the build failure.

finagolfin commented 1 year ago

@keith, still seeing this or can we close? I haven't seen this in a while, and the linux CI builds with a prebuilt Swift 5.8 toolchain now.

keith commented 1 year ago

not sure