Skip to content
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

Added support for target-specific config metadata #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ test-timeout = 300
test-no-reboot = true
```

## Target-specific configuration

Target-specific configuration can be done through `[package.metadata.<target-triple>.bootimage]`. The default architecture if none is specified is `x86_64`.

## License

Licensed under either of
Expand Down
16 changes: 14 additions & 2 deletions src/builder/disk_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ pub fn create_disk_image(
.tool(&llvm_tools::exe("llvm-objcopy"))
.ok_or(DiskImageError::LlvmObjcopyNotFound)?;

let format = match std::env::var("TARGET") {
Ok(target) => {
match target.split("-").next() {
Some("x86_64") => "elf64-x86-64",
Some("aarch64") => "elf64-aarch64",
_ => return Err(DiskImageError::TargetNotSupported { target })
}
},
Err(_) => "elf64-x86-64"
};

// convert bootloader to binary
let mut cmd = Command::new(objcopy);
cmd.arg("-I").arg("elf64-x86-64");
cmd.arg("-I").arg(format);
cmd.arg("-O").arg("binary");
cmd.arg("--binary-architecture=i386:x86-64");
// --binary-architecture is deprecated
//cmd.arg("--binary-architecture=i386:x86-64");
cmd.arg(bootloader_elf_path);
cmd.arg(output_bin_path);
let output = cmd.output().map_err(|err| DiskImageError::Io {
Expand Down
7 changes: 7 additions & 0 deletions src/builder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ pub enum DiskImageError {
#[error("Could not find `llvm-objcopy` in the `llvm-tools-preview` rustup component.")]
LlvmObjcopyNotFound,

/// The build target is not supported
#[error("The specified build target '{target}' is not supported. Supported architectures are: x86_64, aarch64")]
TargetNotSupported {
/// The specified build target (from env:TARGET)
target: String
},

/// The `llvm-objcopy` command failed
#[error("Failed to run `llvm-objcopy`: {}", String::from_utf8_lossy(.stderr))]
ObjcopyFailed {
Expand Down
43 changes: 33 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,41 @@ fn read_config_inner(manifest_path: &Path) -> Result<Config> {
.context("Failed to parse Cargo.toml")?
};

let metadata = cargo_toml
.get("package")
.and_then(|table| table.get("metadata"))
.and_then(|table| table.get("bootimage"));
let metadata = match metadata {
let target = std::env::var("TARGET");
let target_metadata = match target {
Ok(target) => {
let metadata = cargo_toml
.get("package")
.and_then(|table| table.get("metadata"))
.and_then(|table| table.get(target))
.and_then(|table| table.get("bootimage"));
match metadata {
Some(metadata) => metadata.as_table(),
None => None
}
},
Err(_) => None
};
let metadata = match target_metadata {
// have target-specific config
Some(meta) => meta,
// no target-specific config, fallback to default
None => {
return Ok(ConfigBuilder::default().into());
}
Some(metadata) => metadata
.as_table()
.ok_or_else(|| anyhow!("Bootimage configuration invalid: {:?}", metadata))?,
let metadata = cargo_toml
.get("package")
.and_then(|table| table.get("metadata"))
.and_then(|table| table.get("bootimage"));
match metadata {
None => {
return Ok(ConfigBuilder::default().into());
}
Some(metadata) => metadata
.as_table()
.ok_or_else(|| anyhow!("Bootimage configuration invalid: {:?}", metadata))?,
}
}
};


let mut config = ConfigBuilder::default();

Expand Down