Skip to content

Commit

Permalink
Make bootloader and partition table finding work on Rust >=1.77 and e…
Browse files Browse the repository at this point in the history
…sp-idf-sys >=0.34.0

The package ID format was officially documented as an opaque identifier
and the only thing we were allowed to do with them is look them up in
the cargo metadata.  Rust 1.77 switched the package ID field to use the
publicly documented "Package ID Specification" format instead, which
means that the old logic no longer works.

But we also cannot currently reliably look up the new pkgids in the
cargo metadata due to them not being given for indirect dependencies, so
instead we fall back to getting the bootloader and partition table from
the top of the target directory, where they've been copied over since
`esp-idf-sys` 0.34.0.

Signed-off-by: Johannes Löthberg <[email protected]>
  • Loading branch information
kyrias committed Apr 29, 2024
1 parent 15991b5 commit 8ee9bc3
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
process::{exit, Command, ExitStatus, Stdio},
};

use cargo_metadata::Message;
use cargo_metadata::{Message, MetadataCommand};
use clap::{Args, CommandFactory, Parser, Subcommand};
use espflash::{
cli::{
Expand Down Expand Up @@ -469,6 +469,15 @@ fn build(
Message::BuildScriptExecuted(script)
if script.package_id.repr.starts_with("esp-idf-sys") =>
{
// This logic only works on Rust versions <1.77 as after that the package ID format
// changed, which is fine because these IDs were explicitly documented as being
// opaque identifiers.
//
// We cannot reliably look up the new IDs in the cargo metadata however
// unless https://github.com/rust-lang/cargo/issues/7289 is implemented, because
// otherwise this would fail in case the crate doesn't directly depend on
// `esp-idf-sys`.

// If the `esp-idf-sys` package is being used, attempt to use the bootloader and
// partition table compiled by `embuild` instead.
let build_path = PathBuf::from(script.out_dir).join("build");
Expand Down Expand Up @@ -505,6 +514,33 @@ fn build(
}
}

// The logic parsing the `cargo build` output currently cannot reliably work for cargo versions
// >= 1.76, so if the above logic failed we'll try to get the files from the target directory
// directly, which is supported since `esp-idf-sys` 0.34.0.
{
let target_dir = MetadataCommand::new()
.exec()
.into_diagnostic()?
.target_directory
.into_std_path_buf()
.join(target)
.join(if build_options.release {
"release"
} else {
"debug"
});
let bl_path = target_dir.join("bootloader.bin");
let pt_path = target_dir.join("partition-table.bin");

if bl_path.exists() && bl_path.is_file() {
bootloader_path = Some(bl_path);
}

if pt_path.exists() && pt_path.is_file() {
partition_table_path = Some(pt_path);
}
}

// Check if the command succeeded, otherwise return an error. Any error messages
// occurring during the build are shown above, when the compiler messages are
// rendered.
Expand Down

0 comments on commit 8ee9bc3

Please sign in to comment.