Skip to content

Fix conversion from PartialTargetTriple to TargetTriple #3467

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

Closed
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
16 changes: 6 additions & 10 deletions src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,12 @@ impl TargetTriple {
impl std::convert::TryFrom<PartialTargetTriple> for TargetTriple {
type Error = &'static str;
fn try_from(value: PartialTargetTriple) -> std::result::Result<Self, Self::Error> {
if value.arch.is_some() && value.os.is_some() && value.env.is_some() {
Ok(Self(format!(
"{}-{}-{}",
value.arch.unwrap(),
value.os.unwrap(),
value.env.unwrap()
)))
} else {
Err("Incomplete / bad target triple")
}
let arch = value.arch.ok_or("Incomplete / bad target triple")?;
let os = value.os.ok_or("Incomplete / bad target triple")?;
Ok(Self(match value.env {
Some(env) => format!("{}-{}-{}", arch, os, env),
None => format!("{}-{}", arch, os),
}))
}
}

Expand Down
122 changes: 107 additions & 15 deletions src/dist/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,138 @@ use regex::Regex;
// the PartialTargetTriple.

static LIST_ARCHS: &[&str] = &[
"i386",
"i586",
"i686",
"x86_64",
"aarch64",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to check, where did you source these lists of current arches, OSes and envs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied all the targets from the Platform Support section of the rustc book and created the lists manually. I'm not sure if these lists already exist somewhere, so I created them manually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, maybe add a docstring linking to a section in the book for each of these constants?

"arm",
"armebv7r",
"armv5te",
"armv7",
"armv7a",
"armv7r",
"armv7s",
"aarch64",
"asmjs",
"avr",
"bpfeb",
"bpfel",
"hexagon",
"i386",
"i586",
"i686",
"loongarch64",
"mips",
"mipsel",
"mips64",
"mips64el",
"mipsel",
"mipsisa32r6",
"mipsisa32r6el",
"msp430",
"nvptx64",
"powerpc",
"powerpc64",
"powerpc64le",
"riscv32gc",
"riscv32i",
"riscv32im",
"riscv32imac",
"riscv32imc",
"riscv64gc",
"riscv64imac",
"s390x",
"loongarch64",
"sparc",
"sparc64",
"sparcv9",
"thumbv4t",
"thumbv5te",
"thumbv6m",
"thumbv7a",
"thumbv7em",
"thumbv7m",
"thumbv7neon",
"thumbv8m.base",
"thumbv8m.main",
"wasm32",
"wasm64",
"x86_64",
"x86_64h",
];
static LIST_OSES: &[&str] = &[
"pc-windows",
"unknown-linux",
"apple-darwin",
"unknown-netbsd",
"apple-ios",
"apple-tvos",
"apple-watchos",
"esp-espidf",
"fortanix-unknown",
"fuchsia",
"ibm-aix",
"kmc-solid_asp3",
"linux",
"nintendo-3ds",
"nintendo-switch",
"none",
"nvidia-cuda",
"openwrt-linux",
"pc-nto",
"pc-solaris",
"pc-windows",
"rumprun-netbsd",
"sony-psp",
"sony-psx",
"sony-vita",
"sun-solaris",
"unknown-dragonfly",
"unknown-emscripten",
"unknown-freebsd",
"unknown-fuchsia",
"unknown-gnu",
"unknown-haiku",
"unknown-hermit",
"unknown-illumos",
"unknown-l4re",
"unknown-linux",
"unknown-netbsd",
"unknown-none",
"unknown-nto",
"unknown-openbsd",
"unknown-redox",
"unknown-uefi",
"unknown-unknown",
"unknown-xous",
"uwp-windows",
"wasi",
"wrs-vxworks",
];
static LIST_ENVS: &[&str] = &[
"android",
"androideabi",
"atmega328",
"eabi",
"eabihf",
"elf",
"freestanding",
"gnu",
"gnux32",
"msvc",
"gnu_ilp32",
"gnuabi64",
"gnueabi",
"gnueabihf",
"gnuabi64",
"androideabi",
"android",
"gnullvm",
"gnuspe",
"gnux32",
"macabi",
"msvc",
"musl",
"muslabi64",
"musleabi",
"musleabihf",
"newlibeabihf",
"ohos",
"qnx700",
"qnx710",
"sgx",
"sim",
"softfloat",
"spe",
"uclibc",
"uclibceabi",
"uclibceabihf",
];

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
64 changes: 62 additions & 2 deletions tests/suite/cli_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,27 @@ fn add_component() {
});
}

#[test]
fn add_component_by_target_triple() {
test(&|config| {
config.with_scenario(Scenario::SimpleV2, &|config| {
config.expect_ok(&["rustup", "default", "stable"]);
config.expect_ok(&[
"rustup",
"component",
"add",
&format!("rust-std-{}", clitools::CROSS_ARCH1),
]);
let path = format!(
"toolchains/stable-{}/lib/rustlib/{}/lib/libstd.rlib",
this_host_triple(),
clitools::CROSS_ARCH1
);
assert!(config.rustupdir.has(path));
})
});
}

#[test]
fn remove_component() {
test(&|config| {
Expand All @@ -1383,22 +1404,61 @@ fn remove_component() {
});
}

#[test]
fn remove_component_by_target_triple() {
let component_with_triple = format!("rust-std-{}", clitools::CROSS_ARCH1);
test(&|config| {
config.with_scenario(Scenario::SimpleV2, &|config| {
config.expect_ok(&["rustup", "default", "stable"]);
config.expect_ok(&["rustup", "component", "add", &component_with_triple]);
let path = PathBuf::from(format!(
"toolchains/stable-{}/lib/rustlib/{}/lib/libstd.rlib",
this_host_triple(),
clitools::CROSS_ARCH1
));
assert!(config.rustupdir.has(&path));
config.expect_ok(&["rustup", "component", "remove", &component_with_triple]);
assert!(!config.rustupdir.has(path.parent().unwrap()));
})
});
}

#[test]
fn add_remove_multiple_components() {
let files = [
"lib/rustlib/src/rust-src/foo.rs".to_owned(),
format!("lib/rustlib/{}/analysis/libfoo.json", this_host_triple()),
format!("lib/rustlib/{}/lib/libstd.rlib", clitools::CROSS_ARCH1),
format!("lib/rustlib/{}/lib/libstd.rlib", clitools::CROSS_ARCH2),
];
let component_with_triple1 = format!("rust-std-{}", clitools::CROSS_ARCH1);
let component_with_triple2 = format!("rust-std-{}", clitools::CROSS_ARCH2);

test(&|config| {
config.with_scenario(Scenario::SimpleV2, &|config| {
config.expect_ok(&["rustup", "default", "nightly"]);
config.expect_ok(&["rustup", "component", "add", "rust-src", "rust-analysis"]);
config.expect_ok(&[
"rustup",
"component",
"add",
"rust-src",
"rust-analysis",
&component_with_triple1,
&component_with_triple2,
]);
for file in &files {
let path = format!("toolchains/nightly-{}/{}", this_host_triple(), file);
assert!(config.rustupdir.has(&path));
}
config.expect_ok(&["rustup", "component", "remove", "rust-src", "rust-analysis"]);
config.expect_ok(&[
"rustup",
"component",
"remove",
"rust-src",
"rust-analysis",
&component_with_triple1,
&component_with_triple2,
]);
for file in &files {
let path = PathBuf::from(format!(
"toolchains/nightly-{}/{}",
Expand Down