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: 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 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"] 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"] 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() {