From 704ec0576cda7c4ba786036670de34ebe1bfabb4 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Thu, 10 Nov 2022 14:09:18 -0500 Subject: [PATCH 1/5] Drop build-std from template The UEFI targets are now tier 2, so we don't need to use build-std anymore. --- template/.cargo/config.toml | 2 -- template/README.md | 1 - template/rust-toolchain.toml | 2 +- 3 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 template/.cargo/config.toml diff --git a/template/.cargo/config.toml b/template/.cargo/config.toml deleted file mode 100644 index 7317206c5..000000000 --- a/template/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[unstable] -build-std = ["core", "alloc"] diff --git a/template/README.md b/template/README.md index 4a3b1c417..dc75c6ac9 100644 --- a/template/README.md +++ b/template/README.md @@ -8,7 +8,6 @@ how to build and run a UEFI application developed using `uefi-rs`. ## File structure -- [`.cargo/config`](./.cargo/config) sets some `build-std` options. - [`rust-toolchain.toml`](rust-toolchain.toml) sets the nightly channel. - [`Cargo.toml`](./Cargo.toml) shows the necessary dependencies. - [`src/main.rs`](./src/main.rs) has a minimal entry point that diff --git a/template/rust-toolchain.toml b/template/rust-toolchain.toml index f70d22540..a96790bb0 100644 --- a/template/rust-toolchain.toml +++ b/template/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src"] +targets = ["aarch64-unknown-uefi", "i686-unknown-uefi", "x86_64-unknown-uefi"] From 42ed2cedb4935940b2501f2bc280bcacdc5e0c77 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Thu, 10 Nov 2022 15:06:16 -0500 Subject: [PATCH 2/5] xtask: Make build-std optional If the target being built is installed via rustup, skip adding the build-std arguments. We still need build-std for our current minimum-supported nightly CI, but for nightly-2022-11-10 and later the UEFI targets are tier 2 and can be installed via rustup. --- xtask/src/cargo.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/xtask/src/cargo.rs b/xtask/src/cargo.rs index a689c6a17..2a9d3f99b 100644 --- a/xtask/src/cargo.rs +++ b/xtask/src/cargo.rs @@ -160,6 +160,19 @@ pub fn fix_nested_cargo_env(cmd: &mut Command) { cmd.env("PATH", sanitized_path(orig_path)); } +/// Check if the three UEFI targets are installed via rustup (only +/// supported since nightly-2022-11-10). +fn is_target_installed(target: &str) -> Result { + let output = Command::new("rustup") + .args(["target", "list", "--installed"]) + .output()?; + if !output.status.success() { + bail!("failed to get installed targets"); + } + let stdout = String::from_utf8(output.stdout)?; + Ok(stdout.lines().any(|x| x == target)) +} + #[derive(Debug)] pub struct Cargo { pub action: CargoAction, @@ -218,7 +231,14 @@ impl Cargo { } if let Some(target) = self.target { - cmd.args(["--target", target.as_triple(), "-Zbuild-std=core,alloc"]); + cmd.args(["--target", target.as_triple()]); + + // If the target is not installed, use build-std. Keep this + // around until our minimum-supported nightly version is at + // least 2022-11-10. + if !is_target_installed(target.as_triple())? { + cmd.args(["-Zbuild-std=core,alloc"]); + } } if self.packages.is_empty() { From 8d150d6a25a8369f48b8b8e6e765780a2c8338bd Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Thu, 10 Nov 2022 15:08:26 -0500 Subject: [PATCH 3/5] Add UEFI targets to rust-toolchain.toml These targets are now available via rustup as of nightly-2022-11-10. --- rust-toolchain.toml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index bb0480f10..a96790bb0 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,6 +1,3 @@ [toolchain] channel = "nightly" - -# Install the `rust-src` component so that `-Zbuild-std` works. This in -# addition to the components included in the default profile. -components = ["rust-src"] +targets = ["aarch64-unknown-uefi", "i686-unknown-uefi", "x86_64-unknown-uefi"] From e964518bc36c2fb3a867701d95e320bc71274b8a Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Thu, 10 Nov 2022 15:13:48 -0500 Subject: [PATCH 4/5] book: Remove references to build-std As of nightly-2022-11-10 the UEFI targets are tier 2 and available via rustup, so build-std isn't needed anymore. --- book/src/tutorial/building.md | 44 +++++++---------------------------- 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/book/src/tutorial/building.md b/book/src/tutorial/building.md index 9130a9dda..dcc76d30e 100644 --- a/book/src/tutorial/building.md +++ b/book/src/tutorial/building.md @@ -3,8 +3,7 @@ ## Nightly toolchain Rust's nightly toolchain is currently required because uefi-rs uses some -unstable features. The [`build-std`] feature we use to build the -standard libraries is also unstable. +unstable features. The easiest way to set this up is using a [rustup toolchain file]. In the root of your repository, add `rust-toolchain.toml`: @@ -12,51 +11,24 @@ the root of your repository, add `rust-toolchain.toml`: ```toml [toolchain] channel = "nightly" -components = ["rust-src"] +targets = ["x86_64-unknown-uefi"] ``` +Here we have specified the `x86_64-unknown-uefi` target; there are also +`i686-unknown-uefi` and `aarch64-unknown-uefi` targets available. + Note that nightly releases can sometimes break, so you might opt to pin -to a specific release. For example, `channel = "nightly-2022-09-01"`. +to a specific release. For example, `channel = "nightly-2022-11-10"`. ## Build the application Run this command to build the application: ```sh -cargo build --target x86_64-unknown-uefi \ - -Zbuild-std=core,alloc +cargo build --target x86_64-unknown-uefi ``` This will produce an x86-64 executable: `target/x86_64-unknown-uefi/debug/my-uefi-app.efi`. -## Simplifying the build command - -The above build command is verbose and not easy to remember. With a bit -of configuration we can simplify it a lot. - -Create a `.cargo` directory in the root of the project: - -```sh -mkdir .cargo -``` - -Create `.cargo/config.toml` with these contents: - -```toml -[build] -target = "x86_64-unknown-uefi" - -[unstable] -build-std = ["core", "alloc"] -``` - -Now you can build much more simply: - -```sh -cargo build -``` - -[`build-std`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std -[`rust-toolchain.toml`]: https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file -[rustup toolchain file]: https://rust-lang.github.io/rustup/concepts/toolchains.html +[rustup toolchain file]: https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file From 7b8a188dbad4da181b41de38e9817cb5cbf7e326 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Thu, 10 Nov 2022 15:28:29 -0500 Subject: [PATCH 5/5] Update rust-toolchain.toml section in BUILDING.md --- BUILDING.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/BUILDING.md b/BUILDING.md index 6819adba9..5d85847ab 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -26,7 +26,9 @@ outside of the `uefi-rs` repo. ```toml [toolchain] channel = "nightly" - components = ["rust-src"] + + # Install the x86_64 UEFI target; aarch64 and i686 are also available. + targets = ["x86_64-unknown-uefi"] ``` - Build the crate: