Skip to content

Commit

Permalink
Merge #518
Browse files Browse the repository at this point in the history
518: `build.rs`: Obtain version from macro; simplify r=Urhengulas a=Urhengulas

Fixes #517.

Co-authored-by: Johann Hemmann <johann.hemmann@code.berlin>
  • Loading branch information
bors[bot] and Urhengulas authored Jul 19, 2021
2 parents 9a23d2d + 8ee41d7 commit 7d5aaed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ jobs:
- stable
- nightly
runs-on: ubuntu-latest
env:
QEMU_RUN_IGNORE_VERSION: 1
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand Down
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ bitflags = "1"
rustc_version = "0.4"
trybuild = "1.0.42"

[build-dependencies]
git-version = "0.3"
semver = "1.0"

[workspace]
members = [
".",
Expand All @@ -49,10 +53,7 @@ lto = "fat"
opt-level = 3
overflow-checks = false

[build-dependencies]
semver = "1.0"

[package.metadata.docs.rs]
features = ["alloc"]
rustdoc-args = [ "--cfg=docsrs" ]
rustdoc-args = ["--cfg=docsrs"]
targets = ["thumbv6m-none-eabi", "thumbv7em-none-eabihf"]
76 changes: 35 additions & 41 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
use semver::Version;
use std::{env, error::Error, fs, path::Path, path::PathBuf, process::Command};
use std::{env, error::Error, fs, path::PathBuf};

use git_version::git_version;

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 {
// `--match v*` matches defmt tags, e.g. `v0.2.3`, but ignores related crates, e.g. `defmt-decoder-v0.2.2`
const GIT_DESCRIBE: &str = git_version!(args = ["--long", "--match", "v*"], cargo_prefix = "");

let version = match semver::Version::parse(GIT_DESCRIBE) {
// if parsing fails it is a git version
Err(_) => extract_git_hash(GIT_DESCRIBE).to_string(),
// if success it is semver
Ok(semver) => match semver.major {
// minor is breaking when major = 0
format!("{}.{}", semver.major, semver.minor)
} else {
0 => format!("{}.{}", semver.major, semver.minor),
// ignore minor, patch, pre and build
semver.major.to_string()
}
_ => semver.major.to_string(),
},
};

// Load linker-script and insert version
let mut linker_script = fs::read_to_string("defmt.x.in")?;
linker_script = linker_script.replace("$DEFMT_VERSION", version.trim());

// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var("OUT_DIR")?);
fs::write(out.join("defmt.x"), linker_script)?;
println!("cargo:rustc-link-search={}", out.display());
let target = env::var("TARGET")?;

// `"atomic-cas": false` in `--print target-spec-json`
// last updated: rust 1.48.0
match &target[..] {
if matches!(
&*env::var("TARGET")?,
"avr-gnu-base"
| "msp430-none-elf"
| "riscv32i-unknown-none-elf"
| "riscv32imc-unknown-none-elf"
| "thumbv4t-none-eabi"
| "thumbv6m-none-eabi" => {
println!("cargo:rustc-cfg=no_cas");
}
_ => {}
| "msp430-none-elf"
| "riscv32i-unknown-none-elf"
| "riscv32imc-unknown-none-elf"
| "thumbv4t-none-eabi"
| "thumbv6m-none-eabi"
) {
println!("cargo:rustc-cfg=no_cas");
}

Ok(())
}

/// Extract git hash from a `git describe` statement
fn extract_git_hash(git_describe: &str) -> &str {
git_describe.split("-").nth(2).unwrap()
}

0 comments on commit 7d5aaed

Please sign in to comment.