Skip to content

Switch pc-windows-gnu targets from GCC to Clang #92502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -366,25 +366,25 @@ jobs:
env:
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu"
SCRIPT: make ci-mingw-subset-1
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
os: windows-latest-xl
- name: i686-mingw-2
env:
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu"
SCRIPT: make ci-mingw-subset-2
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
os: windows-latest-xl
- name: x86_64-mingw-1
env:
SCRIPT: make ci-mingw-subset-1
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu --enable-profiler"
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
os: windows-latest-xl
- name: x86_64-mingw-2
env:
SCRIPT: make ci-mingw-subset-2
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu --enable-profiler"
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
os: windows-latest-xl
- name: dist-x86_64-msvc
env:
@@ -409,14 +409,14 @@ jobs:
env:
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu --enable-full-tools --enable-profiler"
SCRIPT: python x.py dist
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
DIST_REQUIRE_ALL_TOOLS: 1
os: windows-latest-xl
- name: dist-x86_64-mingw
env:
SCRIPT: python x.py dist
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu --enable-full-tools --enable-profiler"
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
DIST_REQUIRE_ALL_TOOLS: 1
os: windows-latest-xl
- name: dist-x86_64-msvc-alt
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/i686_pc_windows_gnu.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub fn target() -> Target {
.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".to_string(), "i386pe".to_string()]);
base.max_atomic_width = Some(64);
base.frame_pointer = FramePointer::Always; // Required for backtraces
base.linker = Some("i686-w64-mingw32-gcc".to_string());
base.linker = Some("i686-w64-mingw32-clang".to_string());

// Mark all dynamic libraries and executables as compatible with the larger 4GiB address
// space available to x86 Windows binaries on x86_64.
30 changes: 4 additions & 26 deletions compiler/rustc_target/src/spec/windows_gnu_base.rs
Original file line number Diff line number Diff line change
@@ -16,34 +16,13 @@ pub fn opts() -> TargetOptions {
],
);

let mut late_link_args = LinkArgs::new();
let mut late_link_args_dynamic = LinkArgs::new();
let mut late_link_args_static = LinkArgs::new();
// Order of `late_link_args*` was found through trial and error to work with various
// mingw-w64 versions (not tested on the CI). It's expected to change from time to time.
let mingw_libs = vec![
"-lmsvcrt".to_string(),
"-lmingwex".to_string(),
"-lmingw32".to_string(),
"-lgcc".to_string(), // alas, mingw* libraries above depend on libgcc
// mingw's msvcrt is a weird hybrid import library and static library.
// And it seems that the linker fails to use import symbols from msvcrt
// that are required from functions in msvcrt in certain cases. For example
// `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
// The library is purposely listed twice to fix that.
//
// See https://github.com/rust-lang/rust/pull/47483 for some more details.
"-lmsvcrt".to_string(),
"-luser32".to_string(),
"-lkernel32".to_string(),
];
late_link_args.insert(LinkerFlavor::Gcc, mingw_libs.clone());
late_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), mingw_libs);
let dynamic_unwind_libs = vec![
// If any of our crates are dynamically linked then we need to use
// the shared libgcc_s-dw2-1.dll. This is required to support
// unwinding across DLL boundaries.
"-lgcc_s".to_string(),
"-l:libunwind.dll.a".to_string(),
];
late_link_args_dynamic.insert(LinkerFlavor::Gcc, dynamic_unwind_libs.clone());
late_link_args_dynamic.insert(LinkerFlavor::Lld(LldFlavor::Ld), dynamic_unwind_libs);
@@ -53,8 +32,7 @@ pub fn opts() -> TargetOptions {
// binaries to be redistributed without the libgcc_s-dw2-1.dll
// dependency, but unfortunately break unwinding across DLL
// boundaries when unwinding across FFI boundaries.
"-lgcc_eh".to_string(),
"-l:libpthread.a".to_string(),
"-l:libunwind.a".to_string(),
];
late_link_args_static.insert(LinkerFlavor::Gcc, static_unwind_libs.clone());
late_link_args_static.insert(LinkerFlavor::Lld(LldFlavor::Ld), static_unwind_libs);
@@ -64,7 +42,8 @@ pub fn opts() -> TargetOptions {
env: "gnu".to_string(),
vendor: "pc".to_string(),
// FIXME(#13846) this should be enabled for windows
function_sections: false,
function_sections: true,
no_default_libraries: false,
linker: Some("gcc".to_string()),
dynamic_linking: true,
executables: true,
@@ -80,7 +59,6 @@ pub fn opts() -> TargetOptions {
pre_link_objects_fallback: crt_objects::pre_mingw_fallback(),
post_link_objects_fallback: crt_objects::post_mingw_fallback(),
crt_objects_fallback: Some(CrtObjectsFallback::Mingw),
late_link_args,
late_link_args_dynamic,
late_link_args_static,
abi_return_struct_as_int: true,
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/x86_64_pc_windows_gnu.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ pub fn target() -> Target {
base.pre_link_args
.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".to_string(), "i386pep".to_string()]);
base.max_atomic_width = Some(64);
base.linker = Some("x86_64-w64-mingw32-gcc".to_string());
base.linker = Some("x86_64-w64-mingw32-clang".to_string());

Target {
llvm_target: "x86_64-pc-windows-gnu".to_string(),
9 changes: 9 additions & 0 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
@@ -470,6 +470,15 @@ def download_toolchain(self, stage0=True, rustc_channel=None):
with output(self.rustc_stamp(stage0)) as rust_stamp:
rust_stamp.write(key)

# FIXME remove this when the Clang Windows GNU toolchain has been published
if (os.environ.get('GCC_LIBS_HACK')):
gcc_libs_hack = os.environ.get('GCC_LIBS_HACK')
gcc_libs_hack_dest = '{}/rustlib/{}/lib'.format(lib_dir, self.build)
shutil.copy(gcc_libs_hack + '/libgcc.a', gcc_libs_hack_dest)
shutil.copy(gcc_libs_hack + '/libgcc_eh.a', gcc_libs_hack_dest)
shutil.copy(gcc_libs_hack + '/libgcc_s.a', gcc_libs_hack_dest)
shutil.copy(gcc_libs_hack + '/libstdc++.a', gcc_libs_hack_dest)

if self.rustfmt() and self.rustfmt().startswith(bin_root) and (
not os.path.exists(self.rustfmt())
or self.program_out_of_date(
24 changes: 11 additions & 13 deletions src/bootstrap/dist.rs
Original file line number Diff line number Diff line change
@@ -160,31 +160,29 @@ fn make_win_dist(
}

let compiler = if target == "i686-pc-windows-gnu" {
"i686-w64-mingw32-gcc.exe"
"i686-w64-mingw32-clang.exe"
} else if target == "x86_64-pc-windows-gnu" {
"x86_64-w64-mingw32-gcc.exe"
"x86_64-w64-mingw32-clang.exe"
} else if target == "aarch64-pc-windows-gnu" {
"aarch64-w64-mingw32-clang.exe"
} else {
"gcc.exe"
"clang.exe"
};
let target_tools = [compiler, "ld.exe", "dlltool.exe", "libwinpthread-1.dll"];
let mut rustc_dlls = vec!["libwinpthread-1.dll"];
if target.starts_with("i686-") {
rustc_dlls.push("libgcc_s_dw2-1.dll");
} else {
rustc_dlls.push("libgcc_s_seh-1.dll");
}

rustc_dlls.push("libunwind.dll");
rustc_dlls.push("libc++.dll");

let target_libs = [
//MinGW libs
"libgcc.a",
"libgcc_eh.a",
"libgcc_s.a",
"libunwind.a",
"libunwind.dll.a",
"libm.a",
"libmingw32.a",
"libmingwex.a",
"libstdc++.a",
"libc++.a",
"libiconv.a",
"libmoldname.a",
"libpthread.a",
//Windows import libs
"libadvapi32.a",
10 changes: 5 additions & 5 deletions src/bootstrap/native.rs
Original file line number Diff line number Diff line change
@@ -163,10 +163,10 @@ impl Step for Llvm {
}
};

let llvm_exp_targets = match builder.config.llvm_experimental_targets {
Some(ref s) => s,
None => "AVR;M68k",
};
//let llvm_exp_targets = match builder.config.llvm_experimental_targets {
// Some(ref s) => s,
// None => "AVR;M68k",
//};

let assertions = if builder.config.llvm_assertions { "ON" } else { "OFF" };
let plugins = if builder.config.llvm_plugins { "ON" } else { "OFF" };
@@ -177,7 +177,7 @@ impl Step for Llvm {
.define("LLVM_ENABLE_ASSERTIONS", assertions)
.define("LLVM_ENABLE_PLUGINS", plugins)
.define("LLVM_TARGETS_TO_BUILD", llvm_targets)
.define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets)
// .define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing the LLVM build to fail due to duplicate symbols - I'm not sure how to fix that so I've disabled the experimental targets for now

[2451/2844] Building CXX object tools/llvm-config/CMakeFiles/llvm-config.dir/llvm-config.cpp.obj
[2452/2844] Linking CXX executable bin\llvm-config.exe
[2453/2844] Linking CXX executable bin\llvm-ar.exe
[2454/2844] Generating ../../bin/llvm-ranlib.exe
[2455/2844] Generating ../../bin/llvm-lib.exe
[2456/2844] Generating ../../bin/llvm-dlltool.exe
[2457/2844] Building CXX object tools/lto/CMakeFiles/LTO.dir/lto.cpp.obj
[2458/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/FindBugs.cpp.obj
[2459/2844] Building CXX object tools/llvm-lto/CMakeFiles/llvm-lto.dir/llvm-lto.cpp.obj
[2460/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/Miscompilation.cpp.obj
[2461/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/ExtractFunction.cpp.obj
[2462/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/CrashDebugger.cpp.obj
[2463/2844] Building Options.inc...
[2464/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/bugpoint.cpp.obj
[2465/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/BugDriver.cpp.obj
[2466/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/OptimizerDriver.cpp.obj
[2467/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/ToolRunner.cpp.obj
[2468/2844] Building CXX object tools/llvm-profdata/CMakeFiles/llvm-profdata.dir/llvm-profdata.cpp.obj
[2469/2844] Linking CXX executable bin\llvm-profdata.exe
[2470/2844] Building CXX object tools/dsymutil/CMakeFiles/dsymutil.dir/CFBundle.cpp.obj
[2471/2844] Building CXX object tools/bugpoint/CMakeFiles/bugpoint.dir/ExecutionDriver.cpp.obj
[2472/2844] Linking CXX executable bin\bugpoint.exe
FAILED: bin/bugpoint.exe
cmd.exe /C "cd . && C:\msys64\clang64\bin\g++.exe -ffunction-sections -fdata-sections -m64 -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -ffunction-sections -fdata-sections -O3 -DNDEBUG -Wl,--stack,16777216 @CMakeFiles\bugpoint.rsp -o bin\bugpoint.exe -Wl,--out-implib,lib\libbugpoint.dll.a -Wl,--major-image-version,0,--minor-image-version,0  && cd ."
ld.lld: error: duplicate symbol: vtable for llvm::FormalArgHandler
>>> defined at libLLVMPowerPCCodeGen.a(PPCCallLowering.cpp.obj)
>>> defined at libLLVMM68kCodeGen.a(M68kCallLowering.cpp.obj)
g++: error: linker command failed with exit code 1 (use -v to see invocation)
[2473/2844] Building CXX object tools/lli/CMakeFiles/lli.dir/ExecutionUtils.cpp.obj
[2474/2844] Building CXX object tools/dsymutil/CMakeFiles/dsymutil.dir/BinaryHolder.cpp.obj
[2475/2844] Building CXX object tools/dsymutil/CMakeFiles/dsymutil.dir/MachODebugMapParser.cpp.obj
[2476/2844] Building CXX object tools/llc/CMakeFiles/llc.dir/llc.cpp.obj
[2477/2844] Building CXX object tools/dsymutil/CMakeFiles/dsymutil.dir/dsymutil.cpp.obj
[2478/2844] Building CXX object tools/lli/CMakeFiles/lli.dir/lli.cpp.obj
[2479/2844] Building CXX object lib/Passes/CMakeFiles/LLVMPasses.dir/PassBuilder.cpp.obj
ninja: build stopped: subcommand failed.
thread 'main' panicked at '
command did not execute successfully, got: exit code: 1

build script failed, must exit now', C:\Users\Dennis\.cargo\registry\src\github.com-1ecc6299db9ec823\cmake-0.1.44\src\lib.rs:885:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
        finished in 1092.867 seconds
Build completed unsuccessfully in 0:18:14

.define("LLVM_INCLUDE_EXAMPLES", "OFF")
.define("LLVM_INCLUDE_DOCS", "OFF")
.define("LLVM_INCLUDE_BENCHMARKS", "OFF")
12 changes: 6 additions & 6 deletions src/ci/github-actions/ci.yml
Original file line number Diff line number Diff line change
@@ -577,28 +577,28 @@ jobs:
env:
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu
SCRIPT: make ci-mingw-subset-1
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
<<: *job-windows-xl

- name: i686-mingw-2
env:
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu
SCRIPT: make ci-mingw-subset-2
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
<<: *job-windows-xl

- name: x86_64-mingw-1
env:
SCRIPT: make ci-mingw-subset-1
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-gnu --enable-profiler
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
<<: *job-windows-xl

- name: x86_64-mingw-2
env:
SCRIPT: make ci-mingw-subset-2
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-gnu --enable-profiler
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
<<: *job-windows-xl

- name: dist-x86_64-msvc
@@ -644,15 +644,15 @@ jobs:
env:
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu --enable-full-tools --enable-profiler
SCRIPT: python x.py dist
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
DIST_REQUIRE_ALL_TOOLS: 1
<<: *job-windows-xl

- name: dist-x86_64-mingw
env:
SCRIPT: python x.py dist
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-gnu --enable-full-tools --enable-profiler
CUSTOM_MINGW: 1
CUSTOM_MINGW: 0
DIST_REQUIRE_ALL_TOOLS: 1
<<: *job-windows-xl

16 changes: 12 additions & 4 deletions src/ci/scripts/install-mingw.sh
Original file line number Diff line number Diff line change
@@ -57,10 +57,18 @@ if isWindows; then
esac

if [[ "${CUSTOM_MINGW-0}" -ne 1 ]]; then
pacman -S --noconfirm --needed mingw-w64-$arch-toolchain mingw-w64-$arch-cmake \
mingw-w64-$arch-gcc \
mingw-w64-$arch-python # the python package is actually for python3
ciCommandAddPath "$(ciCheckoutPath)/msys2/mingw${bits}/bin"
# FIXME remove the GCC_LIBS_HACK when the Clang Windows GNU toolchain has been published
export GCC_LIBS_HACK="$(cygpath -am missing-libs-hack)"
echo "GCC_LIBS_HACK path is ${GCC_LIBS_HACK}"
mkdir -p "${GCC_LIBS_HACK}"
cp "$(cygpath -u $(clang -print-libgcc-file-name))" "${GCC_LIBS_HACK}/libgcc.a"
cp "/clang64/lib/libunwind.a" "${GCC_LIBS_HACK}/libgcc_eh.a"
cp "/clang64/lib/libunwind.dll.a" "${GCC_LIBS_HACK}/libgcc_s.a"
export RUSTFLAGS_BOOTSTRAP="-C link-arg=-L$(cygpath -am missing-libs-hack)"
pacman -S --noconfirm --needed mingw-w64-clang-$arch-toolchain mingw-w64-clang-$arch-cmake \
Copy link
Author

@dennisameling dennisameling Jan 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the change from mingw-w64 to mingw-w64-clang packages here - these are the key enablers for Clang support. I'm not sure how your custom mingw toolchain should be updated so I switched CI to CUSTOM_MINGW: 0 for now

mingw-w64-clang-$arch-ninja \
mingw-w64-clang-$arch-python # the python package is actually for python3
ciCommandAddPath "$(ciCheckoutPath)/msys2/clang${bits}/bin"
else
mingw_dir="mingw${bits}"