Skip to content

Commit

Permalink
check host's libstdc++ version when using ci llvm
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed May 23, 2024
1 parent 3dc4fff commit 4c06ca3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
54 changes: 54 additions & 0 deletions src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::core::builder::{Builder, Cargo as CargoCommand, RunConfig, ShouldRun,
use crate::core::config::TargetSelection;
use crate::utils::channel::GitInfo;
use crate::utils::exec::BootstrapCommand;
use crate::utils::helpers::output;
use crate::utils::helpers::{add_dylib_path, exe, t};
use crate::Compiler;
use crate::Mode;
Expand Down Expand Up @@ -804,6 +805,59 @@ impl Step for LlvmBitcodeLinker {
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct LibcxxVersionTool {
pub target: TargetSelection,
}

#[derive(Debug, Clone)]
pub enum LibcxxVersion {
Gnu(usize),
#[allow(dead_code)]
Llvm(usize),
}

impl Step for LibcxxVersionTool {
type Output = LibcxxVersion;
const DEFAULT: bool = false;
const ONLY_HOSTS: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.never()
}

fn run(self, builder: &Builder<'_>) -> LibcxxVersion {
let out_dir = builder.out.join(self.target.to_string()).join("libcxx-version");
let _ = fs::remove_dir_all(&out_dir);
t!(fs::create_dir_all(&out_dir));

let compiler = builder.cxx(self.target).unwrap();
let mut cmd = Command::new(compiler);

let executable = out_dir.join("libcxx-version");
cmd.arg("-o").arg(&executable).arg(builder.src.join("src/tools/libcxx-version/main.cpp"));

builder.run_cmd(&mut cmd);

if !executable.exists() {
panic!("Something went wrong. {} is not present", executable.display());
}

let version_output = output(&mut Command::new(executable));

let version_str = version_output.split_once("version:").unwrap().1;
let version = version_str.trim().parse::<usize>().unwrap();

if version_output.starts_with("libstdc++") {
LibcxxVersion::Gnu(version)
} else if version_output.starts_with("libc++") {
LibcxxVersion::Llvm(version)
} else {
panic!("Coudln't recognize the standard library version.");
}
}
}

macro_rules! tool_extended {
(($sel:ident, $builder:ident),
$($name:ident,
Expand Down
46 changes: 42 additions & 4 deletions src/bootstrap/src/core/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use std::path::PathBuf;
use std::process::Command;
use walkdir::WalkDir;

use crate::builder::Kind;
use crate::core::build_steps::tool;
use crate::builder::{Kind, Builder};
use crate::core::config::Target;
use crate::utils::helpers::output;
use crate::Build;
Expand All @@ -35,6 +36,10 @@ const STAGE0_MISSING_TARGETS: &[&str] = &[
// just a dummy comment so the list doesn't get onelined
];

/// Minimum version threshold for libstdc++ required when using prebuilt LLVM
/// from CI (with`llvm.download-ci-llvm` option).
const LIBSTDCXX_MIN_VERSION_THRESHOLD: usize = 8;

impl Finder {
pub fn new() -> Self {
Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
Expand Down Expand Up @@ -99,6 +104,35 @@ pub fn check(build: &mut Build) {
cmd_finder.must_have("git");
}

// Ensure that a compatible version of libstdc++ is available on the system when using `llvm.download-ci-llvm`.
if !build.config.dry_run() && build.config.llvm_from_ci {
let builder = Builder::new(build);
let libcxx_version = builder.ensure(tool::LibcxxVersionTool { target: build.build });

match libcxx_version {
tool::LibcxxVersion::Gnu(version) => {
if LIBSTDCXX_MIN_VERSION_THRESHOLD > version {
eprintln!(
"\nYour system's libstdc++ version is too old for the `llvm.download-ci-llvm` option."
);
eprintln!("Current version detected: '{}'", version);
eprintln!("Minimum required version: '{}'", LIBSTDCXX_MIN_VERSION_THRESHOLD);
eprintln!(
"Consider upgrading libstdc++ or disabling the `llvm.download-ci-llvm` option."
);
crate::exit!(1);
}
}
tool::LibcxxVersion::Llvm(_) => {
eprintln!(
"\nYour system is using libc++, which is incompatible with the `llvm.download-ci-llvm` option."
);
eprintln!("Disable `llvm.download-ci-llvm` or switch to libstdc++.");
crate::exit!(1);
}
}
}

// We need cmake, but only if we're actually building LLVM or sanitizers.
let building_llvm = build
.hosts
Expand Down Expand Up @@ -199,11 +233,15 @@ than building it.
if !["A-A", "B-B", "C-C"].contains(&target_str.as_str()) {
let mut has_target = false;

let missing_targets_hashset: HashSet<_> = STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect();
let duplicated_targets: Vec<_> = stage0_supported_target_list.intersection(&missing_targets_hashset).collect();
let missing_targets_hashset: HashSet<_> =
STAGE0_MISSING_TARGETS.iter().map(|t| t.to_string()).collect();
let duplicated_targets: Vec<_> =
stage0_supported_target_list.intersection(&missing_targets_hashset).collect();

if !duplicated_targets.is_empty() {
println!("Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list.");
println!(
"Following targets supported from the stage0 compiler, please remove them from STAGE0_MISSING_TARGETS list."
);
for duplicated_target in duplicated_targets {
println!(" {duplicated_target}");
}
Expand Down

0 comments on commit 4c06ca3

Please sign in to comment.