Skip to content

Commit ad05377

Browse files
committed
target-spec compiletest: adjust to new target spec
1 parent b7c5076 commit ad05377

File tree

3 files changed

+130
-133
lines changed

3 files changed

+130
-133
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/compiletests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use-compiled-tools = ["rustc_codegen_spirv/use-compiled-tools"]
1616
[dependencies]
1717
compiletest = { version = "0.11.2", package = "compiletest_rs" }
1818
rustc_codegen_spirv = { workspace = true }
19+
rustc_codegen_spirv-types = { workspace = true }
1920
clap = { version = "4", features = ["derive"] }
2021
itertools = "0.14.0"
2122

tests/compiletests/src/main.rs

Lines changed: 128 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::Parser;
22
use itertools::Itertools as _;
3-
use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
3+
use rustc_codegen_spirv_types::{Target, TargetSpecVersion, query_rustc_version};
4+
use std::ffi::OsString;
45
use std::{
56
env, io,
67
path::{Path, PathBuf},
@@ -28,12 +29,6 @@ impl Opt {
2829
}
2930
}
3031

31-
const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-";
32-
33-
fn target_spec_json(target: &str) -> String {
34-
format!("{TARGET_SPEC_DIR_PATH}/{target}.json")
35-
}
36-
3732
#[derive(Copy, Clone)]
3833
enum DepKind {
3934
SpirvLib,
@@ -48,9 +43,9 @@ impl DepKind {
4843
}
4944
}
5045

51-
fn target_dir_suffix(self, target: &str) -> String {
46+
fn target_dir_suffix(self, target: &Target) -> String {
5247
match self {
53-
Self::SpirvLib => format!("{target}/debug/deps"),
48+
Self::SpirvLib => format!("{}/debug/deps", target.target()),
5449
Self::ProcMacro => "debug/deps".into(),
5550
}
5651
}
@@ -161,8 +156,8 @@ impl Runner {
161156

162157
println!("Testing env: {stage_id}\n");
163158

164-
let target = format!("{SPIRV_TARGET_PREFIX}{env}");
165-
let libs = build_deps(&self.deps_target_dir, &self.codegen_backend_path, &target);
159+
let target = Target::from_env(env).unwrap();
160+
let libs = self.build_deps(&target);
166161
let mut flags = test_rustc_flags(
167162
&self.codegen_backend_path,
168163
&libs,
@@ -181,7 +176,7 @@ impl Runner {
181176
stage_id,
182177
target_rustcflags: Some(flags),
183178
mode: mode.parse().expect("Invalid mode"),
184-
target: target_spec_json(&target),
179+
target: self.target_spec_json(&target).into_string().unwrap(),
185180
src_base: self.tests_dir.join(mode),
186181
build_base: self.compiletest_build_dir.clone(),
187182
bless: self.opt.bless,
@@ -194,140 +189,140 @@ impl Runner {
194189
compiletest::run_tests(&config);
195190
}
196191
}
197-
}
198-
199-
/// Runs the processes needed to build `spirv-std` & other deps.
200-
fn build_deps(deps_target_dir: &Path, codegen_backend_path: &Path, target: &str) -> TestDeps {
201-
// Build compiletests-deps-helper
202-
std::process::Command::new("cargo")
203-
.args([
204-
"build",
205-
"-p",
206-
"compiletests-deps-helper",
207-
"-Zbuild-std=core",
208-
"-Zbuild-std-features=compiler-builtins-mem",
209-
&*format!("--target={}", target_spec_json(target)),
210-
])
211-
.arg("--target-dir")
212-
.arg(deps_target_dir)
213-
.env("RUSTFLAGS", rust_flags(codegen_backend_path))
214-
.stderr(std::process::Stdio::inherit())
215-
.stdout(std::process::Stdio::inherit())
216-
.status()
217-
.and_then(map_status_to_result)
218-
.unwrap();
219-
220-
let compiler_builtins = find_lib(
221-
deps_target_dir,
222-
"compiler_builtins",
223-
DepKind::SpirvLib,
224-
target,
225-
);
226-
let core = find_lib(deps_target_dir, "core", DepKind::SpirvLib, target);
227-
let spirv_std = find_lib(deps_target_dir, "spirv_std", DepKind::SpirvLib, target);
228-
let glam = find_lib(deps_target_dir, "glam", DepKind::SpirvLib, target);
229-
let spirv_std_macros = find_lib(
230-
deps_target_dir,
231-
"spirv_std_macros",
232-
DepKind::ProcMacro,
233-
target,
234-
);
235192

236-
let all_libs = [
237-
&compiler_builtins,
238-
&core,
239-
&spirv_std,
240-
&glam,
241-
&spirv_std_macros,
242-
];
243-
if all_libs.iter().any(|r| r.is_err()) {
244-
// FIXME(eddyb) `missing_count` should always be `0` anyway.
245-
// FIXME(eddyb) use `--message-format=json-render-diagnostics` to
246-
// avoid caring about duplicates (or search within files at all).
247-
let missing_count = all_libs
248-
.iter()
249-
.filter(|r| matches!(r, Err(FindLibError::Missing)))
250-
.count();
251-
let duplicate_count = all_libs
252-
.iter()
253-
.filter(|r| matches!(r, Err(FindLibError::Duplicate)))
254-
.count();
255-
eprintln!(
256-
"warning: cleaning deps ({missing_count} missing libs, {duplicate_count} duplicated libs)"
257-
);
258-
clean_deps(deps_target_dir);
259-
build_deps(deps_target_dir, codegen_backend_path, target)
260-
} else {
261-
TestDeps {
262-
core: core.ok().unwrap(),
263-
glam: glam.ok().unwrap(),
264-
compiler_builtins: compiler_builtins.ok().unwrap(),
265-
spirv_std: spirv_std.ok().unwrap(),
266-
spirv_std_macros: spirv_std_macros.ok().unwrap(),
193+
/// Runs the processes needed to build `spirv-std` & other deps.
194+
fn build_deps(&self, target: &Target) -> TestDeps {
195+
// Build compiletests-deps-helper
196+
std::process::Command::new("cargo")
197+
.args([
198+
"build",
199+
"-p",
200+
"compiletests-deps-helper",
201+
"-Zbuild-std=core",
202+
"-Zbuild-std-features=compiler-builtins-mem",
203+
"--target",
204+
])
205+
.arg(self.target_spec_json(target))
206+
.arg("--target-dir")
207+
.arg(&self.deps_target_dir)
208+
.env("RUSTFLAGS", rust_flags(&self.codegen_backend_path))
209+
.stderr(std::process::Stdio::inherit())
210+
.stdout(std::process::Stdio::inherit())
211+
.status()
212+
.and_then(map_status_to_result)
213+
.unwrap();
214+
215+
let compiler_builtins = self.find_lib("compiler_builtins", DepKind::SpirvLib, target);
216+
let core = self.find_lib("core", DepKind::SpirvLib, target);
217+
let spirv_std = self.find_lib("spirv_std", DepKind::SpirvLib, target);
218+
let glam = self.find_lib("glam", DepKind::SpirvLib, target);
219+
let spirv_std_macros = self.find_lib("spirv_std_macros", DepKind::ProcMacro, target);
220+
221+
let all_libs = [
222+
&compiler_builtins,
223+
&core,
224+
&spirv_std,
225+
&glam,
226+
&spirv_std_macros,
227+
];
228+
if all_libs.iter().any(|r| r.is_err()) {
229+
// FIXME(eddyb) `missing_count` should always be `0` anyway.
230+
// FIXME(eddyb) use `--message-format=json-render-diagnostics` to
231+
// avoid caring about duplicates (or search within files at all).
232+
let missing_count = all_libs
233+
.iter()
234+
.filter(|r| matches!(r, Err(FindLibError::Missing)))
235+
.count();
236+
let duplicate_count = all_libs
237+
.iter()
238+
.filter(|r| matches!(r, Err(FindLibError::Duplicate)))
239+
.count();
240+
eprintln!(
241+
"warning: cleaning deps ({missing_count} missing libs, {duplicate_count} duplicated libs)"
242+
);
243+
self.clean_deps();
244+
self.build_deps(target)
245+
} else {
246+
TestDeps {
247+
core: core.ok().unwrap(),
248+
glam: glam.ok().unwrap(),
249+
compiler_builtins: compiler_builtins.ok().unwrap(),
250+
spirv_std: spirv_std.ok().unwrap(),
251+
spirv_std_macros: spirv_std_macros.ok().unwrap(),
252+
}
267253
}
268254
}
269-
}
270255

271-
fn clean_deps(deps_target_dir: &Path) {
272-
std::process::Command::new("cargo")
273-
.arg("clean")
274-
.arg("--target-dir")
275-
.arg(deps_target_dir)
276-
.stderr(std::process::Stdio::inherit())
277-
.stdout(std::process::Stdio::inherit())
278-
.status()
279-
.and_then(map_status_to_result)
280-
.unwrap();
256+
fn clean_deps(&self) {
257+
std::process::Command::new("cargo")
258+
.arg("clean")
259+
.arg("--target-dir")
260+
.arg(&self.deps_target_dir)
261+
.stderr(std::process::Stdio::inherit())
262+
.stdout(std::process::Stdio::inherit())
263+
.status()
264+
.and_then(map_status_to_result)
265+
.unwrap();
266+
}
267+
268+
fn target_spec_json(&self, target: &Target) -> OsString {
269+
let rustc_version = query_rustc_version(None).unwrap();
270+
TargetSpecVersion::target_arg(rustc_version, target, &self.deps_target_dir).unwrap()
271+
}
281272
}
282273

283274
enum FindLibError {
284275
Missing,
285276
Duplicate,
286277
}
287278

288-
/// Attempt find the rlib that matches `base`, if multiple rlibs are found then
289-
/// a clean build is required and `Err(FindLibError::Duplicate)` is returned.
290-
fn find_lib(
291-
deps_target_dir: &Path,
292-
base: impl AsRef<Path>,
293-
dep_kind: DepKind,
294-
target: &str,
295-
) -> Result<PathBuf, FindLibError> {
296-
let base = base.as_ref();
297-
let (expected_prefix, expected_extension) = dep_kind.prefix_and_extension();
298-
let expected_name = format!("{}{}", expected_prefix, base.display());
299-
300-
let dir = deps_target_dir.join(dep_kind.target_dir_suffix(target));
301-
302-
std::fs::read_dir(dir)
303-
.unwrap()
304-
.map(|entry| entry.unwrap().path())
305-
.filter(move |path| {
306-
let name = {
307-
let name = path.file_stem();
308-
if name.is_none() {
309-
return false;
310-
}
311-
name.unwrap()
312-
};
313-
314-
let name_matches = name.to_str().unwrap().starts_with(&expected_name)
279+
impl Runner {
280+
/// Attempt find the rlib that matches `base`, if multiple rlibs are found then
281+
/// a clean build is required and `Err(FindLibError::Duplicate)` is returned.
282+
fn find_lib(
283+
&self,
284+
base: impl AsRef<Path>,
285+
dep_kind: DepKind,
286+
target: &Target,
287+
) -> Result<PathBuf, FindLibError> {
288+
let base = base.as_ref();
289+
let (expected_prefix, expected_extension) = dep_kind.prefix_and_extension();
290+
let expected_name = format!("{}{}", expected_prefix, base.display());
291+
292+
let dir = self
293+
.deps_target_dir
294+
.join(dep_kind.target_dir_suffix(target));
295+
296+
std::fs::read_dir(dir)
297+
.unwrap()
298+
.map(|entry| entry.unwrap().path())
299+
.filter(move |path| {
300+
let name = {
301+
let name = path.file_stem();
302+
if name.is_none() {
303+
return false;
304+
}
305+
name.unwrap()
306+
};
307+
308+
let name_matches = name.to_str().unwrap().starts_with(&expected_name)
315309
&& name.len() == expected_name.len() + 17 // we expect our name, '-', and then 16 hexadecimal digits
316310
&& ends_with_dash_hash(name.to_str().unwrap());
317-
let extension_matches = path
318-
.extension()
319-
.is_some_and(|ext| ext == expected_extension);
320-
321-
name_matches && extension_matches
322-
})
323-
.exactly_one()
324-
.map_err(|mut iter| {
325-
if iter.next().is_none() {
326-
FindLibError::Missing
327-
} else {
328-
FindLibError::Duplicate
329-
}
330-
})
311+
let extension_matches = path
312+
.extension()
313+
.is_some_and(|ext| ext == expected_extension);
314+
315+
name_matches && extension_matches
316+
})
317+
.exactly_one()
318+
.map_err(|mut iter| {
319+
if iter.next().is_none() {
320+
FindLibError::Missing
321+
} else {
322+
FindLibError::Duplicate
323+
}
324+
})
325+
}
331326
}
332327

333328
/// Returns whether this string ends with a dash ('-'), followed by 16 lowercase hexadecimal characters

0 commit comments

Comments
 (0)