Skip to content

Commit

Permalink
Implement build sysroot with cargo_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nilehmann committed Jan 3, 2025
1 parent 1f0ae50 commit dd12781
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target
.liquid
*.dot
log
sysroot/
3 changes: 3 additions & 0 deletions lib/flux-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ flux-attrs = { path = "../flux-attrs", version = "0.1.0" }

[lints]
workspace = true

[package.metadata.flux]
enabled = true
3 changes: 3 additions & 0 deletions lib/flux-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub use flux_attrs::*;

#[sig(fn(bool[true]) )]
pub fn assert(_: bool) {}
23 changes: 0 additions & 23 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
#![feature(register_tool)]
use std::path::PathBuf;

pub const FLUX_SYSROOT: &str = "FLUX_SYSROOT";
pub const FLUX_FULL_COMPILATION: &str = "FLUX_FULL_COMPILATION";

pub fn find_flux_path() -> PathBuf {
let executable_name = if cfg!(windows) { "rustc-flux.exe" } else { "rustc-flux" };
find_file_in_target_dir(executable_name)
}

/// Rustc flags to pass Flux when running tests
pub fn default_rustc_flags() -> Vec<String> {
vec!["--crate-type=rlib".to_string(), "--edition=2021".to_string()]
}

fn find_file_in_target_dir(file: &str) -> PathBuf {
let target_directory = if cfg!(debug_assertions) { "debug" } else { "release" };
let local_path: PathBuf = ["target", target_directory, file].into_iter().collect();
if local_path.exists() {
return local_path;
}
let workspace_path: PathBuf = ["..", "target", target_directory, file]
.into_iter()
.collect();
if workspace_path.exists() {
return workspace_path;
}
panic!("Could not find {file}");
}
56 changes: 45 additions & 11 deletions tests/tests/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,53 @@ use std::{env, path::PathBuf};

use compiletest_rs::{common::Mode, Config};
use itertools::Itertools;
use tests::{default_rustc_flags, find_flux_path, FLUX_FULL_COMPILATION, FLUX_SYSROOT};

fn config() -> Config {
let bless = env::args().any(|arg| arg == "--bless");
let filters = env::args()
.tuple_windows()
.filter_map(|(arg, val)| if arg == "--test-args" { Some(val) } else { None })
.collect_vec();
Config { rustc_path: find_flux_path(), filters, bless, ..Config::default() }
use tests::{default_rustc_flags, FLUX_FULL_COMPILATION, FLUX_SYSROOT};

#[derive(Debug)]
struct Args {
filters: Vec<String>,
flux: PathBuf,
sysroot: PathBuf,
}

impl Args {
fn parse() -> Args {
let mut filters = vec![];
let mut sysroot = None;
let mut flux = None;
for (arg, val) in env::args().tuple_windows() {
match &arg[..] {
"--filter" => {
filters.push(val);
}
"--flux" => {
if flux.is_some() {
panic!("option '--flux' given more than once");
}
flux = Some(val);
}
"--sysroot" => {
if sysroot.is_some() {
panic!("option '--sysroot' given more than once");
}
sysroot = Some(val);
}
_ => {}
}
}
let Some(flux) = flux else {
panic!("option '--flux' must be provided");
};
let Some(sysroot) = sysroot else {
panic!("option '--sysroot' must be provided");
};
Args { filters, flux: PathBuf::from(flux), sysroot: PathBuf::from(sysroot) }
}
}

fn test_runner(_: &[&()]) {
let mut config = config().tempdir();
let args = Args::parse();
let mut config = Config { rustc_path: args.flux, filters: args.filters, ..Config::default() };

let mut rustc_flags = default_rustc_flags();

Expand All @@ -32,7 +66,7 @@ fn test_runner(_: &[&()]) {

// Force full compilation to make sure we generate artifacts when annotating tests with `@aux-build`
env::set_var(FLUX_FULL_COMPILATION, "1");
env::set_var(FLUX_SYSROOT, config.rustc_path.parent().unwrap());
env::set_var(FLUX_SYSROOT, &args.sysroot);

let path: PathBuf = ["tests", "pos"].iter().collect();
if path.exists() {
Expand Down
Loading

0 comments on commit dd12781

Please sign in to comment.