Skip to content

Commit

Permalink
Extend triple target to everything distributed by forc (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfiedotwtf committed Oct 4, 2024
1 parent 2abac56 commit 2ba1c68
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 43 deletions.
19 changes: 10 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ members = ["component", "ci/build-channel", "ci/compare-versions"]

[dev-dependencies]
chrono = "0.4.33"
regex = "1.11"
strip-ansi-escapes = "0.2.0"

[lints.clippy]
Expand Down
135 changes: 101 additions & 34 deletions src/target_triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,107 @@ impl TargetTriple {
}

pub fn from_component(component: &str) -> Result<Self> {
match Component::from_name(component).map(|c| c.name)?.as_str() {
component::FORC => {
let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};
let architecture = match std::env::consts::ARCH {
"aarch64" => "arm64",
"x86_64" => "amd64",
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

Ok(Self(format!("{os}_{architecture}")))
}
_ => {
let architecture = match std::env::consts::ARCH {
"aarch64" | "x86_64" => std::env::consts::ARCH,
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

let vendor = match std::env::consts::OS {
"macos" => "apple",
_ => "unknown",
};

let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux-gnu",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};

Ok(Self(format!("{architecture}-{vendor}-{os}")))
}
if Component::is_distributed_by_forc(component) {
let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};
let architecture = match std::env::consts::ARCH {
"aarch64" => "arm64",
"x86_64" => "amd64",
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

Ok(Self(format!("{os}_{architecture}")))
} else {
let architecture = match std::env::consts::ARCH {
"aarch64" | "x86_64" => std::env::consts::ARCH,
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

let vendor = match std::env::consts::OS {
"macos" => "apple",
_ => "unknown",
};

let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux-gnu",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};

Ok(Self(format!("{architecture}-{vendor}-{os}")))
}
}
}

#[cfg(test)]
mod test_from_component {
use super::*;
use component::{Component, Components};
use regex::Regex;

#[test]
fn forc() {
let target_triple = TargetTriple::from_component("forc").unwrap();
let expected_triple = Regex::new("^(darwin|linux)_(arm64|amd64)$").unwrap();
assert!(expected_triple.is_match(&target_triple.0));
}

#[test]
fn publishables() {
for publishable in Components::collect_publishables().unwrap() {
let component = Component::resolve_from_name(&publishable.name).unwrap();
let target_triple = TargetTriple::from_component(&component.name).unwrap();

let expected_triple_regex = if publishable.name == "forc" {
"^(darwin|linux)_(arm64|amd64)$"
} else {
"^(aarch64|x86_64)-(apple|unknown)-(darwin|linux-gnu)$"
};


let expected_triple = Regex::new(expected_triple_regex).unwrap();
assert!(expected_triple.is_match(&target_triple.0));
}
}

#[test]
fn plugins() {
let forc = Component::from_name("forc").unwrap();

for plugin in Components::collect_plugins().unwrap() {
let component = Component::resolve_from_name(&plugin.name).unwrap();
let target_triple = TargetTriple::from_component(&component.name).unwrap();

let expected_triple_regex = if Component::is_in_same_distribution(&forc, &component) {
"^(darwin|linux)_(arm64|amd64)$"
} else {
"^(aarch64|x86_64)-(apple|unknown)-(darwin|linux-gnu)$"
};

let expected_triple = Regex::new(expected_triple_regex).unwrap();
assert!(expected_triple.is_match(&target_triple.0));
}
}

#[test]
fn executables() {
let forc = Component::from_name("forc").unwrap();

for executable in Components::collect_plugin_executables().unwrap() {
let component = Component::resolve_from_name(&executable).unwrap();
let target_triple = TargetTriple::from_component(&component.name).unwrap();

let expected_triple_regex = if Component::is_in_same_distribution(&forc, &component) {
"^(darwin|linux)_(arm64|amd64)$"
} else {
"^(aarch64|x86_64)-(apple|unknown)-(darwin|linux-gnu)$"
};

let expected_triple = Regex::new(expected_triple_regex).unwrap();
assert!(expected_triple.is_match(&target_triple.0));
}
}
}

0 comments on commit 2ba1c68

Please sign in to comment.