Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ctest/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::template::{CTestTemplate, RustTestTemplate};
use crate::translator::translate_primitive_type;
use crate::{
Const, Field, MapInput, Parameter, Result, Static, Struct, TranslationError, Type, Union,
VolatileItemKind, expand,
VolatileItemKind, expand, get_build_target,
};

/// A function that takes a mappable input and returns its mapping as `Some`, otherwise
Expand Down Expand Up @@ -961,7 +961,7 @@ impl TestGenerator {
crate_path: impl AsRef<Path>,
output_file_path: impl AsRef<Path>,
) -> Result<PathBuf, GenerationError> {
let expanded = expand(&crate_path, &self.cfg).map_err(|e| {
let expanded = expand(&crate_path, &self.cfg, get_build_target(self)?).map_err(|e| {
GenerationError::MacroExpansion(crate_path.as_ref().to_path_buf(), e.to_string())
})?;
let ast = syn::parse_file(&expanded)
Expand Down
21 changes: 21 additions & 0 deletions ctest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ mod runner;
mod template;
mod translator;

use std::env;

pub use ast::{Abi, Const, Field, Fn, Parameter, Static, Struct, Type, Union};
pub use generator::TestGenerator;
pub use macro_expansion::expand;
pub use runner::{__compile_test, __run_test, generate_test};
pub use translator::TranslationError;

use crate::generator::GenerationError;

/// A possible error that can be encountered in our library.
pub type Error = Box<dyn std::error::Error>;
/// A type alias for `std::result::Result` that defaults to our error type.
Expand Down Expand Up @@ -74,6 +78,23 @@ pub(crate) enum MapInput<'a> {
UnionFieldType(&'a Union, &'a Field),
}

/// Search for the target to build for, specified manually or through an environment variable.
///
/// This function will check the following places for the target name:
/// - TestGenerator.target
/// - TARGET environment variable.
/// - TARGET_PLATFORM environment variable.
fn get_build_target(generator: &TestGenerator) -> Result<String, GenerationError> {
generator
.target
.clone()
.or_else(|| env::var("TARGET").ok())
.or_else(|| env::var("TARGET_PLATFORM").ok())
.ok_or(GenerationError::EnvVarNotFound(
"TARGET, TARGET_PLATFORM".to_string(),
))
}

/* The From impls make it easier to write code in the test templates. */

impl<'a> From<&'a Const> for MapInput<'a> {
Expand Down
10 changes: 9 additions & 1 deletion ctest/src/macro_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use std::process::Command;
use crate::{EDITION, Result};

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

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

if !target.is_empty() {
cmd.arg("--target").arg(target);
}

// `libc` uses non standard cfg flags as well, which have to be manually expanded.
for (k, v) in cfg {
match v {
Expand Down
14 changes: 2 additions & 12 deletions ctest/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;

use crate::generator::GenerationError;
use crate::{EDITION, Result, TestGenerator};
use crate::{EDITION, Result, TestGenerator, get_build_target};

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

// Search for the target and host to build for if specified manually
// (generator.target, generator.host),
// via build script (TARGET, HOST), or for internal testing (TARGET_PLATFORM, HOST_PLATFORM).
let target = generator
.target
.clone()
.or_else(|| env::var("TARGET").ok())
.or_else(|| env::var("TARGET_PLATFORM").ok())
.ok_or(GenerationError::EnvVarNotFound(
"TARGET, TARGET_PLATFORM".to_string(),
))?;
let target = get_build_target(generator)?;
let host = env::var("HOST")
.or_else(|_| env::var("HOST_PLATFORM"))
.map_err(|_| GenerationError::EnvVarNotFound("HOST, HOST_PLATFORM".to_string()))?;
Expand Down
Loading