Skip to content

Commit c3d422a

Browse files
committed
Add support for building bootloaders using -Zbuild-std
Bootloaders can opt-in to the new functionality by setting a `package.metadata.bootloader.build-std` key with a comma-separated string of sysroot crates that should be built. For example, a setting of `build-std = "core"` results in the build command `cargo build -Zbuild-std=core`. If no such key is set, bootimage continues to compile the bootloader using the command `cargo xbuild`.
1 parent 57983ec commit c3d422a

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/builder/bootloader.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct BuildConfig {
1414
target_dir: PathBuf,
1515
kernel_bin_path: PathBuf,
1616
kernel_manifest_path: PathBuf,
17+
build_std: Option<String>,
1718
}
1819

1920
impl BuildConfig {
@@ -54,6 +55,26 @@ impl BuildConfig {
5455
.into(),
5556
)
5657
})?;
58+
let build_std = {
59+
let key = metadata
60+
.and_then(|t| t.get("bootloader"))
61+
.and_then(|t| t.get("build-std"));
62+
if let Some(key) = key {
63+
Some(
64+
key.as_str()
65+
.ok_or_else(|| {
66+
BootloaderError::BootloaderInvalid(
67+
"A non-string `package.metadata.bootloader.build-std` key found in \
68+
Cargo.toml of bootloader"
69+
.into(),
70+
)
71+
})?
72+
.into(),
73+
)
74+
} else {
75+
None
76+
}
77+
};
5778

5879
let binary_feature = cargo_toml
5980
.get("features")
@@ -90,14 +111,19 @@ impl BuildConfig {
90111
target_dir,
91112
kernel_manifest_path: kernel_pkg.manifest_path.clone(),
92113
kernel_bin_path: kernel_bin_path.to_owned(),
114+
build_std,
93115
})
94116
}
95117

96118
/// Creates the cargo build command for building the bootloader.
97119
pub fn build_command(&self) -> Command {
98120
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_owned());
99121
let mut cmd = Command::new(&cargo);
100-
cmd.arg("xbuild");
122+
if let Some(build_std) = &self.build_std {
123+
cmd.arg("build").arg(&format!("-Zbuild-std={}", build_std));
124+
} else {
125+
cmd.arg("xbuild");
126+
}
101127
cmd.arg("--manifest-path");
102128
cmd.arg(&self.manifest_path);
103129
cmd.arg("--bin").arg(&self.bootloader_name);

0 commit comments

Comments
 (0)