Skip to content

Commit dce6e3c

Browse files
committed
ctest: pass target to macro expansion
This also fixes missing `reg32` struct bugs on 32-bit FreeBSD.
1 parent 586ccc1 commit dce6e3c

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

Cargo.lock

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

ctest/src/generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::template::{CTestTemplate, RustTestTemplate};
1414
use crate::translator::translate_primitive_type;
1515
use crate::{
1616
Const, Field, MapInput, Parameter, Result, Static, Struct, TranslationError, Type, Union,
17-
VolatileItemKind, expand,
17+
VolatileItemKind, expand, get_build_target,
1818
};
1919

2020
/// A function that takes a mappable input and returns its mapping as `Some`, otherwise
@@ -961,7 +961,7 @@ impl TestGenerator {
961961
crate_path: impl AsRef<Path>,
962962
output_file_path: impl AsRef<Path>,
963963
) -> Result<PathBuf, GenerationError> {
964-
let expanded = expand(&crate_path, &self.cfg).map_err(|e| {
964+
let expanded = expand(&crate_path, &self.cfg, get_build_target(self)?).map_err(|e| {
965965
GenerationError::MacroExpansion(crate_path.as_ref().to_path_buf(), e.to_string())
966966
})?;
967967
let ast = syn::parse_file(&expanded)

ctest/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ mod runner;
2020
mod template;
2121
mod translator;
2222

23+
use std::env;
24+
2325
pub use ast::{Abi, Const, Field, Fn, Parameter, Static, Struct, Type, Union};
2426
pub use generator::TestGenerator;
2527
pub use macro_expansion::expand;
2628
pub use runner::{__compile_test, __run_test, generate_test};
2729
pub use translator::TranslationError;
2830

31+
use crate::generator::GenerationError;
32+
2933
/// A possible error that can be encountered in our library.
3034
pub type Error = Box<dyn std::error::Error>;
3135
/// A type alias for `std::result::Result` that defaults to our error type.
@@ -74,6 +78,23 @@ pub(crate) enum MapInput<'a> {
7478
UnionFieldType(&'a Union, &'a Field),
7579
}
7680

81+
/// Search for the target to build for, specified manually or through an environment variable.
82+
///
83+
/// This function will check the following places for the target name:
84+
/// - TestGenerator.target
85+
/// - TARGET environment variable.
86+
/// - TARGET_PLATFORM environment variable.
87+
fn get_build_target(generator: &TestGenerator) -> Result<String, GenerationError> {
88+
Ok(generator
89+
.target
90+
.clone()
91+
.or_else(|| env::var("TARGET").ok())
92+
.or_else(|| env::var("TARGET_PLATFORM").ok())
93+
.ok_or(GenerationError::EnvVarNotFound(
94+
"TARGET, TARGET_PLATFORM".to_string(),
95+
))?)
96+
}
97+
7798
/* The From impls make it easier to write code in the test templates. */
7899

79100
impl<'a> From<&'a Const> for MapInput<'a> {

ctest/src/macro_expansion.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ use std::process::Command;
66
use crate::{EDITION, Result};
77

88
/// Use rustc to expand all macros and pretty print the crate into a single file.
9-
pub fn expand<P: AsRef<Path>>(crate_path: P, cfg: &[(String, Option<String>)]) -> Result<String> {
9+
pub fn expand<P: AsRef<Path>>(
10+
crate_path: P,
11+
cfg: &[(String, Option<String>)],
12+
target: String,
13+
) -> Result<String> {
1014
let rustc = env::var("RUSTC").unwrap_or_else(|_| String::from("rustc"));
1115

1216
let mut cmd = Command::new(rustc);
@@ -16,6 +20,10 @@ pub fn expand<P: AsRef<Path>>(crate_path: P, cfg: &[(String, Option<String>)]) -
1620
.arg(EDITION) // By default, -Zunpretty=expanded uses 2015 edition.
1721
.arg(canonicalize(crate_path)?);
1822

23+
if !target.is_empty() {
24+
cmd.arg("--target").arg(target);
25+
}
26+
1927
// `libc` uses non standard cfg flags as well, which have to be manually expanded.
2028
for (k, v) in cfg {
2129
match v {

ctest/src/runner.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
77
use std::process::Command;
88

99
use crate::generator::GenerationError;
10-
use crate::{EDITION, Result, TestGenerator};
10+
use crate::{EDITION, Result, TestGenerator, get_build_target};
1111

1212
/// Generate all tests for the given crate and output the Rust side to a file.
1313
#[doc(hidden)]
@@ -18,17 +18,7 @@ pub fn generate_test(
1818
) -> Result<PathBuf, GenerationError> {
1919
let output_file_path = generator.generate_files(crate_path, output_file_path)?;
2020

21-
// Search for the target and host to build for if specified manually
22-
// (generator.target, generator.host),
23-
// via build script (TARGET, HOST), or for internal testing (TARGET_PLATFORM, HOST_PLATFORM).
24-
let target = generator
25-
.target
26-
.clone()
27-
.or_else(|| env::var("TARGET").ok())
28-
.or_else(|| env::var("TARGET_PLATFORM").ok())
29-
.ok_or(GenerationError::EnvVarNotFound(
30-
"TARGET, TARGET_PLATFORM".to_string(),
31-
))?;
21+
let target = get_build_target(&generator)?;
3222
let host = env::var("HOST")
3323
.or_else(|_| env::var("HOST_PLATFORM"))
3424
.map_err(|_| GenerationError::EnvVarNotFound("HOST, HOST_PLATFORM".to_string()))?;

0 commit comments

Comments
 (0)