Skip to content

Commit

Permalink
Update version of cc crate to v1.0.97
Browse files Browse the repository at this point in the history
Reason:

In order to build the Windows version of the Rust toolchain for the Android platform, the following patch to the cc is crate is required to avoid incorrectly determining that we are building with the Android NDK: rust-lang/cc-rs@57853c4

This patch is present in version 1.0.80 and newer versions of the cc crate. The rustc source distribution currently has 3 different versions of cc in the vendor directory, only one of which has the necessary fix.

We (the Android Rust toolchain) are currently maintaining local patches to upgrade the cc crate dependency versions, which we would like to upstream.

Furthermore, beyond the specific reason, the cc crate in bootstrap is currently pinned at an old version due to problems in the past when trying to update it. It is worthwhile to figure out and resolve these problems so we can keep the dependency up-to-date.

Other fixes:

As of cc v1.0.78, object files are prefixed with a 16-character hash.
Update src/bootstrap/src/core/build_steps/llvm.rs to account for this to
avoid failures when building libunwind and libcrt. Note that while the hash
prefix was introduced in v1.0.78, in order to determine the names of the
object files without scanning the directory, we rely on the compile_intermediates
method, which was introduced in cc v1.0.86

As of cc v1.0.86, compilation on MacOS uses the -mmacosx-version-min flag.
A long-standing bug in the CMake rules for compiler-rt causes compilation
to fail when this flag is specified. So we add a workaround to suppress this
flag.

Updating to cc v1.0.91 and newer requires fixes to bootstrap unit tests.
The unit tests use targets named "A", "B", etc., which fail a validation
check introduced in 1.0.91 of the cc crate.
  • Loading branch information
jfgoog committed May 6, 2024
1 parent 25e3949 commit 615b485
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/bootstrap/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ dependencies = [

[[package]]
name = "cc"
version = "1.0.73"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"

[[package]]
name = "cfg-if"
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test = false
# Most of the time updating these dependencies requires modifications to the
# bootstrap codebase(e.g., https://github.com/rust-lang/rust/issues/124565);
# otherwise, some targets will fail. That's why these dependencies are explicitly pinned.
cc = "=1.0.73"
cc = "=1.0.97"
cmake = "=0.1.48"

build_helper = { path = "../tools/build_helper" }
Expand Down
59 changes: 47 additions & 12 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use std::sync::OnceLock;
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
use crate::core::config::{Config, TargetSelection};
use crate::utils::channel;
use crate::utils::helpers::{self, exe, get_clang_cl_resource_dir, output, t, up_to_date};
use crate::utils::helpers::{
self, exe, get_clang_cl_resource_dir, output, t, unhashed_basename, up_to_date,
};
use crate::{generate_smart_stamp_hash, CLang, GitRepo, Kind};

use build_helper::ci::CiEnv;
Expand Down Expand Up @@ -506,7 +508,7 @@ impl Step for Llvm {
cfg.define("LLVM_VERSION_SUFFIX", suffix);
}

configure_cmake(builder, target, &mut cfg, true, ldflags, &[]);
configure_cmake(builder, target, &mut cfg, true, ldflags, &[], &[]);
configure_llvm(builder, target, &mut cfg);

for (key, val) in &builder.config.llvm_build_config {
Expand Down Expand Up @@ -596,6 +598,7 @@ fn configure_cmake(
use_compiler_launcher: bool,
mut ldflags: LdFlags,
extra_compiler_flags: &[&str],
suppressed_compiler_flag_prefixes: &[&str],
) {
// Do not print installation messages for up-to-date files.
// LLVM and LLD builds can produce a lot of those and hit CI limits on log size.
Expand Down Expand Up @@ -729,7 +732,17 @@ fn configure_cmake(
}

cfg.build_arg("-j").build_arg(builder.jobs().to_string());
let mut cflags: OsString = builder.cflags(target, GitRepo::Llvm, CLang::C).join(" ").into();
let mut cflags: OsString = builder
.cflags(target, GitRepo::Llvm, CLang::C)
.into_iter()
.filter(|flag| {
!suppressed_compiler_flag_prefixes
.iter()
.any(|suppressed_prefix| flag.starts_with(suppressed_prefix))
})
.collect::<Vec<String>>()
.join(" ")
.into();
if let Some(ref s) = builder.config.llvm_cflags {
cflags.push(" ");
cflags.push(s);
Expand All @@ -742,7 +755,17 @@ fn configure_cmake(
cflags.push(&format!(" {flag}"));
}
cfg.define("CMAKE_C_FLAGS", cflags);
let mut cxxflags: OsString = builder.cflags(target, GitRepo::Llvm, CLang::Cxx).join(" ").into();
let mut cxxflags: OsString = builder
.cflags(target, GitRepo::Llvm, CLang::Cxx)
.into_iter()
.filter(|flag| {
!suppressed_compiler_flag_prefixes
.iter()
.any(|suppressed_prefix| flag.starts_with(suppressed_prefix))
})
.collect::<Vec<String>>()
.join(" ")
.into();
if let Some(ref s) = builder.config.llvm_cxxflags {
cxxflags.push(" ");
cxxflags.push(s);
Expand Down Expand Up @@ -921,7 +944,7 @@ impl Step for Lld {
ldflags.push_all("-Wl,-rpath,'$ORIGIN/../../../'");
}

configure_cmake(builder, target, &mut cfg, true, ldflags, &[]);
configure_cmake(builder, target, &mut cfg, true, ldflags, &[], &[]);
configure_llvm(builder, target, &mut cfg);

// Re-use the same flags as llvm to control the level of debug information
Expand Down Expand Up @@ -1022,13 +1045,20 @@ impl Step for Sanitizers {
let use_compiler_launcher = !self.target.contains("apple-darwin");
let extra_compiler_flags: &[&str] =
if self.target.contains("apple") { &["-fembed-bitcode=off"] } else { &[] };
// Since v1.0.86, the cc crate adds -mmacosx-version-min to the default
// flags on MacOS. A long-standing bug in the CMake rules for compiler-rt
// causes architecture detection to be skipped when this flag is present,
// and compilation fails. https://github.com/llvm/llvm-project/issues/88780
let suppressed_compiler_flag_prefixes: &[&str] =
if self.target.contains("apple-darwin") { &["-mmacosx-version-min="] } else { &[] };
configure_cmake(
builder,
self.target,
&mut cfg,
use_compiler_launcher,
LdFlags::default(),
extra_compiler_flags,
suppressed_compiler_flag_prefixes,
);

t!(fs::create_dir_all(&out_dir));
Expand Down Expand Up @@ -1190,7 +1220,7 @@ impl Step for CrtBeginEnd {

let crtbegin_src = builder.src.join("src/llvm-project/compiler-rt/lib/builtins/crtbegin.c");
let crtend_src = builder.src.join("src/llvm-project/compiler-rt/lib/builtins/crtend.c");
if up_to_date(&crtbegin_src, &out_dir.join("crtbegin.o"))
if up_to_date(&crtbegin_src, &out_dir.join("crtbeginS.o"))
&& up_to_date(&crtend_src, &out_dir.join("crtendS.o"))
{
return out_dir;
Expand Down Expand Up @@ -1222,10 +1252,15 @@ impl Step for CrtBeginEnd {
.define("CRT_HAS_INITFINI_ARRAY", None)
.define("EH_USE_FRAME_REGISTRY", None);

cfg.compile("crt");
let objs = cfg.compile_intermediates();
assert_eq!(objs.len(), 2);
for obj in objs {
let base_name = unhashed_basename(&obj);
assert!(base_name == "crtbegin" || base_name == "crtend");
t!(fs::copy(&obj, out_dir.join(format!("{}S.o", base_name))));
t!(fs::rename(&obj, out_dir.join(format!("{}.o", base_name))));
}

t!(fs::copy(out_dir.join("crtbegin.o"), out_dir.join("crtbeginS.o")));
t!(fs::copy(out_dir.join("crtend.o"), out_dir.join("crtendS.o")));
out_dir
}
}
Expand Down Expand Up @@ -1372,9 +1407,9 @@ impl Step for Libunwind {
for entry in fs::read_dir(&out_dir).unwrap() {
let file = entry.unwrap().path().canonicalize().unwrap();
if file.is_file() && file.extension() == Some(OsStr::new("o")) {
// file name starts with "Unwind-EHABI", "Unwind-seh" or "libunwind"
let file_name = file.file_name().unwrap().to_str().expect("UTF-8 file name");
if cpp_sources.iter().any(|f| file_name.starts_with(&f[..f.len() - 4])) {
// Object file name without the hash prefix is "Unwind-EHABI", "Unwind-seh" or "libunwind".
let base_name = unhashed_basename(&file);
if cpp_sources.iter().any(|f| *base_name == f[..f.len() - 4]) {
cc_cfg.object(&file);
count += 1;
}
Expand Down
Loading

0 comments on commit 615b485

Please sign in to comment.