Skip to content

Commit 8a7ca15

Browse files
committed
WIP add test logs
1 parent 3450480 commit 8a7ca15

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ jobs:
105105
rustup target add x86_64-unknown-linux-musl
106106
cargo generate-lockfile && ./ci/run-docker.sh ${{ matrix.target }}
107107
108+
- name: Print test logs if available
109+
run: if [ -f "target/test-log.txt" ]; then cat target/test-log.txt; fi
110+
shell: bash
111+
108112
clippy:
109113
name: Clippy
110114
runs-on: ubuntu-24.04

configure.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Config {
88
pub manifest_dir: PathBuf,
99
pub out_dir: PathBuf,
1010
pub opt_level: u8,
11+
pub cargo_features: Vec<String>,
1112
pub target_arch: String,
1213
pub target_env: String,
1314
pub target_family: Option<String>,
@@ -22,11 +23,16 @@ impl Config {
2223
let target_features = env::var("CARGO_CFG_TARGET_FEATURE")
2324
.map(|feats| feats.split(',').map(ToOwned::to_owned).collect())
2425
.unwrap_or_default();
26+
let cargo_features = env::vars()
27+
.filter_map(|(name, _value)| name.strip_prefix("CARGO_FEATURE_").map(ToOwned::to_owned))
28+
.map(|s| s.to_lowercase().replace("_", "-"))
29+
.collect();
2530

2631
Self {
2732
manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()),
2833
out_dir: PathBuf::from(env::var("OUT_DIR").unwrap()),
2934
opt_level: env::var("OPT_LEVEL").unwrap().parse().unwrap(),
35+
cargo_features,
3036
target_arch: env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
3137
target_env: env::var("CARGO_CFG_TARGET_ENV").unwrap(),
3238
target_family: env::var("CARGO_CFG_TARGET_FAMILY").ok(),
@@ -45,6 +51,7 @@ pub fn emit_libm_config(cfg: &Config) {
4551
emit_arch_cfg();
4652
emit_optimization_cfg(cfg);
4753
emit_cfg_shorthands(cfg);
54+
emit_cfg_env(cfg);
4855
emit_f16_f128_cfg(cfg);
4956
}
5057

@@ -53,6 +60,7 @@ pub fn emit_libm_config(cfg: &Config) {
5360
pub fn emit_test_config(cfg: &Config) {
5461
emit_optimization_cfg(cfg);
5562
emit_cfg_shorthands(cfg);
63+
emit_cfg_env(cfg);
5664
emit_f16_f128_cfg(cfg);
5765
}
5866

@@ -97,6 +105,13 @@ fn emit_cfg_shorthands(cfg: &Config) {
97105
}
98106
}
99107

108+
/// Reemit config that we make use of for test logging.
109+
fn emit_cfg_env(cfg: &Config) {
110+
println!("cargo:rustc-env=CFG_CARGO_FEATURES={:?}", cfg.cargo_features);
111+
println!("cargo:rustc-env=CFG_OPT_LEVEL={}", cfg.opt_level);
112+
println!("cargo:rustc-env=CFG_TARGET_FEATURES={:?}", cfg.target_features);
113+
}
114+
100115
/// Configure whether or not `f16` and `f128` support should be enabled.
101116
fn emit_f16_f128_cfg(cfg: &Config) {
102117
println!("cargo:rustc-check-cfg=cfg(f16_enabled)");

crates/libm-test/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ mod precision;
1111
mod run_cfg;
1212
mod test_traits;
1313

14+
use std::env;
15+
use std::fs::File;
16+
use std::io::Write;
17+
use std::path::PathBuf;
18+
use std::sync::LazyLock;
19+
use std::time::SystemTime;
20+
1421
pub use f8_impl::f8;
1522
pub use libm::support::{Float, Int, IntTy, MinInt};
1623
pub use num::{FloatExt, logspace};
@@ -43,3 +50,46 @@ pub const fn ci() -> bool {
4350
Some(_) => true,
4451
}
4552
}
53+
54+
/// Print a string to stderr and additionally log it to `target/test-log.txt`. This is useful for
55+
/// saving output that would otherwise be consumed by the test harness.
56+
pub fn test_log(s: &str) {
57+
static OUTPUT: LazyLock<Option<File>> = LazyLock::new(|| {
58+
let target_dir = match env::var("CARGO_TARGET_DIR") {
59+
Ok(s) => PathBuf::from(s),
60+
Err(_) => {
61+
let Ok(x) = env::var("CARGO_MANIFEST_DIR") else {
62+
return None;
63+
};
64+
65+
PathBuf::from(x).parent().unwrap().parent().unwrap().join("target")
66+
}
67+
};
68+
let outfile = target_dir.join("test-log.txt");
69+
70+
let mut f = File::options()
71+
.create(true)
72+
.append(true)
73+
.open(outfile)
74+
.expect("failed to open logfile");
75+
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
76+
77+
writeln!(f, "\n\nTest run at {}", now.as_secs()).unwrap();
78+
writeln!(f, "arch: {}", env::consts::ARCH).unwrap();
79+
writeln!(f, "os: {}", env::consts::OS).unwrap();
80+
writeln!(f, "bits: {}", usize::BITS).unwrap();
81+
writeln!(f, "emulated: {}", emulated()).unwrap();
82+
writeln!(f, "ci: {}", ci()).unwrap();
83+
writeln!(f, "opt level: {}", env!("CFG_OPT_LEVEL")).unwrap();
84+
writeln!(f, "target features: {}", env!("CFG_TARGET_FEATURES")).unwrap();
85+
writeln!(f, "cargo features: {}", env!("CFG_CARGO_FEATURES")).unwrap();
86+
87+
Some(f)
88+
});
89+
90+
eprintln!("{s}");
91+
92+
if let Some(mut f) = OUTPUT.as_ref() {
93+
writeln!(f, "{s}").unwrap();
94+
}
95+
}

crates/libm-test/src/run_cfg.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::env;
44
use std::sync::LazyLock;
55

6-
use crate::{BaseName, FloatTy, Identifier};
6+
use crate::{BaseName, FloatTy, Identifier, test_log};
77

88
pub const EXTENSIVE_ENV: &str = "LIBM_EXTENSIVE_TESTS";
99

@@ -98,6 +98,7 @@ static EXTENSIVE: LazyLock<Vec<Identifier>> = LazyLock::new(|| {
9898
});
9999

100100
/// Information about the function to be tested.
101+
#[derive(Debug)]
101102
struct TestEnv {
102103
/// Tests should be reduced because the platform is slow. E.g. 32-bit or emulated.
103104
slow_platform: bool,
@@ -195,6 +196,10 @@ pub fn get_iterations(ctx: &CheckCtx, test_ty: GeneratorKind, _argnum: usize) ->
195196

196197
let ntests = if test_ty == primary_test { primary_test_iter } else { secondary_test_iter };
197198

198-
eprintln!("running {ntests:?} tests for {test_ty:?} `{}`", ctx.fn_ident);
199+
test_log(&format!(
200+
"running {ntests:?} tests for {test_ty:?} {basis:?} {fn_ident} ",
201+
basis = ctx.basis,
202+
fn_ident = ctx.fn_ident
203+
));
199204
TestAction::Iterations(ntests)
200205
}

0 commit comments

Comments
 (0)