Skip to content

Commit

Permalink
Specify wire version in source, instead of getting it from cargo/git.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Jul 18, 2021
1 parent af44cd9 commit f8b8265
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 139 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ lto = "fat"
opt-level = 3
overflow-checks = false

[build-dependencies]
semver = "1.0"

[package.metadata.docs.rs]
features = ["alloc"]
rustdoc-args = [ "--cfg=docsrs" ]
Expand Down
35 changes: 2 additions & 33 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
use semver::Version;
use std::{env, error::Error, fs, path::Path, path::PathBuf, process::Command};
use std::{env, error::Error, fs, path::PathBuf};

fn main() -> Result<(), Box<dyn Error>> {
// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var("OUT_DIR")?);
let mut linker_script = fs::read_to_string("defmt.x.in")?;
let hash = Command::new("git")
.args(&["rev-parse", "HEAD"])
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
});
let version = if let Some(hash) = hash {
hash
} else {
assert!(
!Path::new(".git").exists(),
"you need to install the `git` command line tool to use the git version of `defmt`"
);

// no git info -> assume crates.io
let semver = Version::parse(&std::env::var("CARGO_PKG_VERSION")?)?;
if semver.major == 0 {
// minor is breaking when major = 0
format!("{}.{}", semver.major, semver.minor)
} else {
// ignore minor, patch, pre and build
semver.major.to_string()
}
};
linker_script = linker_script.replace("$DEFMT_VERSION", version.trim());
let linker_script = fs::read_to_string("defmt.x.in")?;
fs::write(out.join("defmt.x"), linker_script)?;
println!("cargo:rustc-link-search={}", out.display());
let target = env::var("TARGET")?;
Expand Down
3 changes: 0 additions & 3 deletions decoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ object = { version = "0.25", default-features = false, features = ["read_core",
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[build-dependencies]
semver = "1.0"

[features]
# WARNING: API and wire format subject to change.
unstable = []
Expand Down
51 changes: 0 additions & 51 deletions decoder/build.rs

This file was deleted.

46 changes: 3 additions & 43 deletions decoder/src/elf2table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,52 +201,12 @@ pub fn parse_impl(elf: &[u8], check_version: bool) -> Result<Option<Table>, anyh

/// Checks if the version encoded in the symbol table is compatible with this version of the `decoder` crate
fn check_version(version: &str) -> Result<(), String> {
enum Kind {
/// `1` or `0.1`
Semver,
/// commit hash `e739d0ac703dfa629a159be329e8c62a1c3ed206`
Git,
}

impl Kind {
fn of(version: &str) -> Kind {
if version.contains('.') || version.parse::<u64>().is_ok() {
Kind::Semver
} else {
Kind::Git
}
}
}

if version != DEFMT_VERSION {
let mut msg = format!(
"defmt version mismatch: firmware is using {}, `probe-run` supports {}\nsuggestion: ",
version, DEFMT_VERSION
let msg = format!(
"defmt wire format version mismatch: firmware is using {}, `probe-run` supports {}\nsuggestion: `cargo install` a different version of `probe-run` that supports defmt {}",
version, DEFMT_VERSION, DEFMT_VERSION
);

let git_sem = "migrate your firmware to a crates.io version of defmt (check https://https://defmt.ferrous-systems.com) OR `cargo install` a _git_ version of `probe-run`: `cargo install --git https://github.com/knurling-rs/probe-run --branch main`";
let sem_git = "`cargo install` a non-git version of `probe-run`: `cargo install probe-run`";
let sem_sem = &*format!(
"`cargo install` a different non-git version of `probe-run` that supports defmt {}",
version,
);
let git_git = &*format!(
"pin _all_ `defmt` related dependencies to revision {0}; modify Cargo.toml files as shown below\n
[dependencies]
defmt = {{ git = \"https://github.com/knurling-rs/defmt\", rev = \"{0}\" }}
defmt-rtt = {{ git = \"https://github.com/knurling-rs/defmt\", rev = \"{0}\" }}
# ONLY pin this dependency if you are using the `print-defmt` feature
panic-probe = {{ git = \"https://github.com/knurling-rs/defmt\", features = [\"print-defmt\"], rev = \"{0}\" }}",
DEFMT_VERSION
);

match (Kind::of(version), Kind::of(DEFMT_VERSION)) {
(Kind::Git, Kind::Git) => msg.push_str(git_git),
(Kind::Git, Kind::Semver) => msg.push_str(git_sem),
(Kind::Semver, Kind::Git) => msg.push_str(sem_git),
(Kind::Semver, Kind::Semver) => msg.push_str(sem_sem),
}

return Err(msg);
}

Expand Down
3 changes: 1 addition & 2 deletions decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#![cfg_attr(docsrs, doc(cfg(unstable)))]
#![doc(html_logo_url = "https://knurling.ferrous-systems.com/knurling_logo_light_text.svg")]

// load DEFMT_VERSION
include!(concat!(env!("OUT_DIR"), "/version.rs"));
pub const DEFMT_VERSION: &str = "3";

mod decoder;
mod elf2table;
Expand Down
4 changes: 0 additions & 4 deletions defmt.x.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ SECTIONS
/* Symbols that aren't referenced by the program and */
/* should be placed at the end of the section */
KEEP(*(.defmt.end .defmt.end.*));

/* $DEFMT_VERSION may contain special chars, so we quote the symbol name */
/* Note that the quotes actually become part of the symbol name though! */
"_defmt_version_ = $DEFMT_VERSION" = 1;
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
extern crate alloc;

// This must be in the root lib.rs, otherwise it doesn't appear in the final binary.

#[used]
#[cfg_attr(target_os = "macos", link_section = ".defmt,end.VERSION")]
#[cfg_attr(not(target_os = "macos"), link_section = ".defmt.end")]
#[export_name = "_defmt_version_ = 3"]
static DEFMT_VERSION: u8 = 0;

#[used]
#[cfg_attr(target_os = "macos", link_section = ".defmt,end.ENCODING")]
#[cfg_attr(not(target_os = "macos"), link_section = ".defmt.end")]
Expand Down

0 comments on commit f8b8265

Please sign in to comment.