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

feat: add windows, macos, linux and unix to targets #832

Merged
merged 4 commits into from
Feb 23, 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
12 changes: 9 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,25 @@ The target table is currently implemented for the following sub-tables:

The target table is defined using `[target.PLATFORM.SUB-TABLE]`.
E.g `[target.linux-64.dependencies]`
The platform can be any of the target [platforms](#platforms) but must also be defined there.

The platform can be any of:

- `win`, `osx`, `linux` or `unix` (`unix` matches `linux` and `osx`)
- or any of the (more) specific [target platforms](#platforms), e.g. `linux-64`, `osx-arm64`

The sub-table can be any of the specified above.

To make it a bit more clear, let's look at an example below.
Currently, pixi combines the top level tables like `dependencies` with the target-specific ones into a single set.
Which, in the case of dependencies, can both add or overwrite dependencies.
In the example below, we have `cmake` being used for all targets but on `osx-64` a different version of python will be selected.
In the example below, we have `cmake` being used for all targets but on `osx-64` or `osx-arm64` a different version of python will be selected.

```toml
[dependencies]
cmake = "3.26.4"
python = "3.10"

[target.osx-64.dependencies]
[target.osx.dependencies]
python = "3.11"
```

Expand Down
25 changes: 22 additions & 3 deletions src/project/manifest/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ impl Target {
pub enum TargetSelector {
// Platform specific configuration
Platform(Platform),
Unix,
Linux,
Win,
MacOs,
// TODO: Add minijinja coolness here.
}

Expand All @@ -138,6 +142,10 @@ impl TargetSelector {
pub fn matches(&self, platform: Platform) -> bool {
match self {
TargetSelector::Platform(p) => p == &platform,
TargetSelector::Linux => platform.is_linux(),
TargetSelector::Unix => platform.is_unix(),
TargetSelector::Win => platform.is_windows(),
TargetSelector::MacOs => platform.is_osx(),
}
}
}
Expand All @@ -146,6 +154,10 @@ impl ToString for TargetSelector {
fn to_string(&self) -> String {
match self {
TargetSelector::Platform(p) => p.to_string(),
TargetSelector::Linux => "linux".to_string(),
TargetSelector::Unix => "unix".to_string(),
TargetSelector::Win => "win".to_string(),
TargetSelector::MacOs => "osx".to_string(),
}
}
}
Expand All @@ -161,9 +173,16 @@ impl<'de> Deserialize<'de> for TargetSelector {
where
D: Deserializer<'de>,
{
Ok(TargetSelector::Platform(Platform::deserialize(
deserializer,
)?))
let s = String::deserialize(deserializer)?;
match s.as_str() {
"linux" => Ok(TargetSelector::Linux),
"unix" => Ok(TargetSelector::Unix),
"win" => Ok(TargetSelector::Win),
"osx" => Ok(TargetSelector::MacOs),
_ => Platform::from_str(&s)
.map(TargetSelector::Platform)
.map_err(serde::de::Error::custom),
}
}
}

Expand Down
53 changes: 50 additions & 3 deletions src/project/manifest/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
consts,
project::manifest::{Feature, ProjectManifest, TargetSelector},
};
use itertools::Itertools;
use miette::{IntoDiagnostic, LabeledSpan, NamedSource, Report, WrapErr};
use rattler_conda_types::Platform;
use std::collections::HashSet;
Expand All @@ -27,7 +28,51 @@ impl ProjectManifest {
return Err(create_unsupported_platform_report(
source,
feature.targets.source_loc(target_sel).unwrap_or_default(),
p,
&[p],
feature,
));
}
}
TargetSelector::Linux => {
if !platforms.as_ref().iter().any(|p| p.is_linux()) {
return Err(create_unsupported_platform_report(
source,
feature.targets.source_loc(target_sel).unwrap_or_default(),
&[
&Platform::Linux64,
&Platform::LinuxAarch64,
&Platform::LinuxPpc64le,
],
feature,
));
}
}
TargetSelector::MacOs => {
if !platforms.as_ref().iter().any(|p| p.is_osx()) {
return Err(create_unsupported_platform_report(
source,
feature.targets.source_loc(target_sel).unwrap_or_default(),
&[&Platform::OsxArm64, &Platform::Osx64],
feature,
));
}
}
TargetSelector::Win => {
if !platforms.as_ref().iter().any(|p| p.is_windows()) {
return Err(create_unsupported_platform_report(
source,
feature.targets.source_loc(target_sel).unwrap_or_default(),
&[&Platform::Win64, &Platform::WinArm64],
feature,
));
}
}
TargetSelector::Unix => {
if !platforms.as_ref().iter().any(|p| p.is_unix()) {
return Err(create_unsupported_platform_report(
source,
feature.targets.source_loc(target_sel).unwrap_or_default(),
&[&Platform::Linux64],
feature,
));
}
Expand Down Expand Up @@ -150,16 +195,18 @@ impl ProjectManifest {
fn create_unsupported_platform_report(
source: NamedSource<String>,
span: Range<usize>,
platform: &Platform,
platform: &[&Platform],
feature: &Feature,
) -> Report {
let platform = platform.iter().map(|p| p.to_string()).join(", ");

miette::miette!(
labels = vec![LabeledSpan::at(
span,
format!("'{}' is not a supported platform", platform)
)],
help = format!(
"Add '{platform}' to the `{}` array of the {} manifest.",
"Add any of '{platform}' to the `{}` array of the {} manifest.",
consts::PROJECT_MANIFEST,
if feature.platforms.is_some() {
format!(
Expand Down
34 changes: 34 additions & 0 deletions tests/pixi_tomls/many_targets.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[project]
name = "pixi"
version = "0.14.0"
description = "Package management made easy!"
authors = ["Wolf Vollprecht <wolf@prefix.dev>", "Bas Zalmstra <bas@prefix.dev>", "Tim de Jager <tim@prefix.dev>", "Ruben Arts <ruben@prefix.dev>"]
channels = ["conda-forge"]
platforms = ["linux-64", "win-64", "osx-64", "osx-arm64"]

[dependencies]
all = "*"

[target.linux-64.dependencies]
linux = "*"

[target.win-64.dependencies]
win = "*"

[target.osx-64.dependencies]
osx_64 = "*"

[target.osx-arm64.dependencies]
osx_arm64 = "*"

[target.linux.dependencies]
all_linux = "*"

[target.win.dependencies]
all_win = "*"

[target.osx.dependencies]
all_macos = "*"

[target.unix.dependencies]
all_unix = "*"
23 changes: 22 additions & 1 deletion tests/project_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
mod common;

use std::path::PathBuf;

use crate::{common::package_database::PackageDatabase, common::PixiControl};
use rattler_conda_types::{Channel, ChannelConfig};
use insta::assert_debug_snapshot;
use pixi::Project;
use rattler_conda_types::{Channel, ChannelConfig, Platform};
use tempfile::TempDir;
use url::Url;

Expand Down Expand Up @@ -45,3 +49,20 @@ async fn add_channel() {
.unwrap();
assert!(project.channels().contains(&local_channel));
}

#[tokio::test]
async fn parse_project() {
fn dependency_names(project: &Project, platform: Platform) -> Vec<String> {
project
.dependencies(None, Some(platform))
.iter()
.map(|dep| dep.0.as_normalized().to_string())
.collect()
}

let pixi_toml = include_str!("./pixi_tomls/many_targets.toml");
let project = Project::from_str(&PathBuf::from("./many"), pixi_toml).unwrap();
assert_debug_snapshot!(dependency_names(&project, Platform::Linux64));
assert_debug_snapshot!(dependency_names(&project, Platform::OsxArm64));
assert_debug_snapshot!(dependency_names(&project, Platform::Win64));
}
10 changes: 10 additions & 0 deletions tests/snapshots/project_tests__parse_project-2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/project_tests.rs
expression: "dependency_names(&project, Platform::OsxArm64)"
---
[
"all",
"osx_arm64",
"all_macos",
"all_unix",
]
9 changes: 9 additions & 0 deletions tests/snapshots/project_tests__parse_project-3.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/project_tests.rs
expression: "dependency_names(&project, Platform::Win64)"
---
[
"all",
"win",
"all_win",
]
10 changes: 10 additions & 0 deletions tests/snapshots/project_tests__parse_project.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/project_tests.rs
expression: "dependency_names(&project, Platform::Linux64)"
---
[
"all",
"linux",
"all_linux",
"all_unix",
]
Loading