Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support setting host_platform explicitly #1087

Merged
merged 4 commits into from
Oct 7, 2024
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
9 changes: 3 additions & 6 deletions src/env_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,7 @@ pub fn vars(output: &Output, build_state: &str) -> HashMap<String, String> {
} else {
insert!(vars, "CONDA_BUILD_CROSS_COMPILATION", "0");
}
insert!(
vars,
"SUBDIR",
output.build_configuration.target_platform.to_string()
);
insert!(vars, "SUBDIR", output.target_platform().to_string());
insert!(
vars,
"build_platform",
Expand All @@ -315,8 +311,9 @@ pub fn vars(output: &Output, build_state: &str) -> HashMap<String, String> {
insert!(
vars,
"target_platform",
output.build_configuration.target_platform.to_string()
output.target_platform().to_string()
);
insert!(vars, "host_platform", output.host_platform().to_string());
insert!(vars, "CONDA_BUILD_STATE", build_state);

vars.extend(language_vars(output));
Expand Down
27 changes: 23 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,35 @@ pub async fn get_build_output(

let recipe_text = fs::read_to_string(recipe_path).into_diagnostic()?;

if args.target_platform == Platform::NoArch || args.build_platform == Platform::NoArch {
if args.target_platform == Some(Platform::NoArch) || args.build_platform == Platform::NoArch {
return Err(miette::miette!(
"target-platform / build-platform cannot be `noarch` - that should be defined in the recipe"
));
}

let mut host_platform = args.host_platform;
// If target_platform is not set, we default to the host platform
let target_platform = args.target_platform.unwrap_or(host_platform);
// If target_platform is set and host_platform is not, then we default host_platform to the target_platform
if let Some(target_platform) = args.target_platform {
// Check if `host_platform` is set by looking at the args (not ideal)
let host_platform_set = std::env::args().any(|arg| arg.starts_with("--host-platform"));
if !host_platform_set {
host_platform = target_platform
}
}

tracing::debug!(
"Platforms: build: {}, host: {}, target: {}",
args.build_platform,
host_platform,
target_platform
);

let selector_config = SelectorConfig {
// We ignore noarch here
target_platform: args.target_platform,
host_platform: args.target_platform,
target_platform,
host_platform,
hash: None,
build_platform: args.build_platform,
variant: BTreeMap::new(),
Expand Down Expand Up @@ -292,7 +311,7 @@ pub async fn get_build_output(
recipe,
build_configuration: BuildConfiguration {
target_platform: discovered_output.target_platform,
host_platform: args.target_platform,
host_platform,
build_platform: args.build_platform,
hash,
variant: discovered_output.used_vars.clone(),
Expand Down
7 changes: 6 additions & 1 deletion src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,13 @@ pub struct BuildOpts {
pub build_platform: Platform,

/// The target platform for the build.
#[arg(long)]
pub target_platform: Option<Platform>,

/// The host platform for the build. If set, it will be used to determine
/// also the target_platform (as long as it is not noarch).
#[arg(long, default_value_t = Platform::current())]
pub target_platform: Platform,
pub host_platform: Platform,

/// Add a channel to search for dependencies in.
#[arg(short = 'c', long, default_value = "conda-forge")]
Expand Down
29 changes: 26 additions & 3 deletions src/recipe/jinja.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{collections::BTreeMap, str::FromStr};

use minijinja::value::{from_args, Kwargs, Object};
use minijinja::{Environment, Value};
use rattler_conda_types::{PackageName, ParseStrictness, Platform, Version, VersionSpec};
use rattler_conda_types::{Arch, PackageName, ParseStrictness, Platform, Version, VersionSpec};

use crate::render::pin::PinArgs;
pub use crate::render::pin::{Pin, PinExpression};
Expand Down Expand Up @@ -361,9 +361,19 @@ fn default_filters(env: &mut Environment) {
env.add_filter("unique", minijinja::filters::unique);
}

fn parse_platform(platform: &str) -> Result<Platform, minijinja::Error> {
Platform::from_str(platform).map_err(|e| {
minijinja::Error::new(
minijinja::ErrorKind::InvalidOperation,
format!("Invalid platform: {e}"),
)
})
}

fn set_jinja(config: &SelectorConfig) -> minijinja::Environment<'static> {
let SelectorConfig {
target_platform,
host_platform,
build_platform,
variant,
experimental,
Expand Down Expand Up @@ -428,8 +438,7 @@ fn set_jinja(config: &SelectorConfig) -> minijinja::Environment<'static> {

let variant_clone = variant.clone();
env.add_function("cdt", move |package_name: String| {
use rattler_conda_types::Arch;
let arch = build_platform.arch().or_else(|| target_platform.arch());
let arch = host_platform.arch().or_else(|| build_platform.arch());
let arch_str = arch.map(|arch| format!("{arch}"));

let cdt_arch = if let Some(s) = variant_clone.get("cdt_arch") {
Expand Down Expand Up @@ -489,6 +498,20 @@ fn set_jinja(config: &SelectorConfig) -> minijinja::Environment<'static> {
jinja_pin_function(name, kwargs, InternalRepr::PinCompatible)
});

// Add the is_... functions
env.add_function("is_linux", |platform: &str| {
Ok(parse_platform(platform)?.is_linux())
});
env.add_function("is_osx", |platform: &str| {
Ok(parse_platform(platform)?.is_osx())
});
env.add_function("is_windows", |platform: &str| {
Ok(parse_platform(platform)?.is_windows())
});
env.add_function("is_unix", |platform: &str| {
Ok(parse_platform(platform)?.is_unix())
});

env.add_function("load_from_file", move |path: String| {
if !experimental {
return Err(minijinja::Error::new(
Expand Down
5 changes: 5 additions & 0 deletions src/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl SelectorConfig {
Value::from_safe_string(self.target_platform.to_string()),
);

context.insert(
"host_platform".to_string(),
Value::from_safe_string(self.host_platform.to_string()),
);

if let Some(platform) = self.host_platform.only_platform() {
context.insert(
platform.to_string(),
Expand Down
Loading