From 5c4b03474e7bbcf71f4739f11d56c0cbb8c06890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Fri, 22 Nov 2024 09:51:43 +0100 Subject: [PATCH] add cargo features to the long version --- .github/workflows/pre-release.yml | 22 +++---- .github/workflows/release.yml | 20 +++---- Cargo.toml | 2 + build.rs | 95 ++++++++++++++++++++++--------- flake.nix | 4 +- src/cli.rs | 13 +++-- 6 files changed, 100 insertions(+), 56 deletions(-) diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 58f3e68b..4bbbf82b 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -10,21 +10,21 @@ jobs: fail-fast: false matrix: include: - - host: x86_64-linux + - os: ubuntu-latest + host: x86_64-linux target: x86_64-linux - os: ubuntu-latest - - host: x86_64-linux + - os: ubuntu-latest + host: x86_64-linux target: aarch64-linux - os: ubuntu-latest - - host: x86_64-linux + - os: ubuntu-latest + host: x86_64-linux target: x86_64-windows - os: ubuntu-latest - - host: x86_64-darwin + - os: macos-13 + host: x86_64-darwin target: x86_64-darwin - os: macos-13 - - host: aarch64-darwin + - os: macos-14 + host: aarch64-darwin target: aarch64-darwin - os: macos-14 steps: - name: Checkout code uses: actions/checkout@v4 @@ -45,7 +45,7 @@ jobs: nix build -L --expr " (builtins.getFlake \"git+file://${PWD}?shallow=1&rev=$(git rev-parse HEAD)\") .outputs.packages.${{ matrix.host }}.${{ matrix.target }}.overrideAttrs { - GIT_DESCRIBE = \"$(git describe --always)\"; + GIT_DESCRIBE = \"$(git describe --all --always --dirty)\"; }" nix run -L .#${{ matrix.target }} -- --version - name: Upload release artifacts diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 27d8f343..f94de2b8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,21 +29,21 @@ jobs: fail-fast: false matrix: include: - - host: x86_64-linux + - os: ubuntu-latest + host: x86_64-linux target: x86_64-linux - os: ubuntu-latest - - host: x86_64-linux + - os: ubuntu-latest + host: x86_64-linux target: aarch64-linux - os: ubuntu-latest - - host: x86_64-linux + - os: ubuntu-latest + host: x86_64-linux target: x86_64-windows - os: ubuntu-latest - - host: x86_64-darwin + - os: macos-13 + host: x86_64-darwin target: x86_64-darwin - os: macos-13 - - host: aarch64-darwin + - os: macos-14 + host: aarch64-darwin target: aarch64-darwin - os: macos-14 steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/Cargo.toml b/Cargo.toml index f286af8b..c22fa77e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,8 @@ pgp-native = ["email-lib/pgp-native", "mml-lib/pgp-native", "pimalaya-tui/pgp-na [build-dependencies] git2 = { version = "0.19", default-features = false } +serde = { version = "1", features = ["derive"] } +toml = "0.8" [dependencies] ariadne = "0.2" diff --git a/build.rs b/build.rs index 288a0b17..fbbd59a3 100644 --- a/build.rs +++ b/build.rs @@ -1,35 +1,76 @@ -use std::env; +use std::{collections::HashMap, env}; -use git2::Repository; +use git2::{DescribeOptions, Repository}; +use serde::Deserialize; fn main() { - let branch = if let Ok(describe) = env::var("GIT_DESCRIBE") { - describe - } else { - let repo = Repository::open(".").expect("should open git repository"); - let head = repo.head().expect("should get HEAD"); - head.shorthand() - .expect("should get branch name") - .to_string() - }; - println!("cargo::rustc-env=GIT_DESCRIBE={branch}"); + if let Ok(git) = Repository::open(".") { + if let None = maybe_forward_env("GIT_DESCRIBE") { + let mut opts = DescribeOptions::new(); + opts.describe_all(); + opts.show_commit_oid_as_fallback(true); - let rev = if let Ok(rev) = env::var("GIT_REV") { - rev - } else { - let repo = Repository::open(".").expect("should open git repository"); - let head = repo.head().expect("should get HEAD"); - let commit = head.peel_to_commit().expect("should get HEAD commit"); - commit.id().to_string() - }; - println!("cargo::rustc-env=GIT_REV={rev}"); + let description = git + .describe(&opts) + .expect("should describe git object") + .format(None) + .expect("should format git object description"); - let os = env::var("CARGO_CFG_TARGET_OS").expect("should get CARGO_CFG_TARGET_OS"); - println!("cargo::rustc-env=TARGET_OS={os}"); + println!("cargo::rustc-env=GIT_DESCRIBE={description}"); + }; - let env = env::var("CARGO_CFG_TARGET_ENV").expect("should get CARGO_CFG_TARGET_ENV"); - println!("cargo::rustc-env=TARGET_ENV={env}"); + if let None = maybe_forward_env("GIT_REV") { + let head = git.head().expect("should get git HEAD"); + let commit = head.peel_to_commit().expect("should get git HEAD commit"); + let rev = commit.id().to_string(); - let arch = env::var("CARGO_CFG_TARGET_ARCH").expect("should get CARGO_CFG_TARGET_ARCH"); - println!("cargo::rustc-env=TARGET_ARCH={arch}"); + println!("cargo::rustc-env=GIT_REV={rev}"); + }; + } + + let toml: CargoToml = + toml::from_str(include_str!("./Cargo.toml")).expect("should read Cargo.toml"); + + let mut features = String::new(); + + for (feature, _) in toml.features { + if feature == "default" { + continue; + } + + if feature_enabled(&feature) { + features.push(' '); + features.push_str(&format!("+{feature}")); + } + } + + println!("cargo::rustc-env=CARGO_FEATURES={features}"); + + forward_env("CARGO_CFG_TARGET_OS"); + forward_env("CARGO_CFG_TARGET_ENV"); + forward_env("CARGO_CFG_TARGET_ARCH"); +} + +#[derive(Deserialize)] +struct CargoToml { + features: HashMap>, +} + +fn feature_enabled(feature: &str) -> bool { + let feature = feature.replace('-', "_").to_uppercase(); + env::var(format!("CARGO_FEATURE_{feature}")).is_ok() +} + +fn maybe_forward_env(key: &str) -> Option { + match env::var(key) { + Err(_) => None, + Ok(val) => { + println!("cargo::rustc-env={key}={val}"); + Some(val) + } + } +} + +fn forward_env(key: &str) { + maybe_forward_env(key).expect(&format!("should get env {key}")); } diff --git a/flake.nix b/flake.nix index d8521844..beb13975 100644 --- a/flake.nix +++ b/flake.nix @@ -134,8 +134,8 @@ nativeBuildInputs = with pkgs; [ pkg-config ]; CARGO_BUILD_TARGET = targetConfig.rustTarget; CARGO_BUILD_RUSTFLAGS = [ "-Ctarget-feature=+crt-static" ]; - GIT_REV = self.rev or self.dirtyRev or "unknown-rev"; - GIT_DESCRIBE = "flake-" + self.shortRev or self.dirtyShortRev or "unknown"; + GIT_REV = self.rev or self.dirtyRev or "dirty"; + GIT_DESCRIBE = "flake-" + self.shortRev or self.dirtyShortRev or "dirty"; postInstall = '' export WINEPREFIX="$(mktemp -d)" diff --git a/src/cli.rs b/src/cli.rs index 6d365a98..5447eccf 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -78,15 +78,16 @@ impl Cli { pub const LONG_VERSION: &'static str = concat!( "v", env!("CARGO_PKG_VERSION"), - " on ", - env!("TARGET_OS"), + env!("CARGO_FEATURES"), + "\nbuild: ", + env!("CARGO_CFG_TARGET_OS"), " ", - env!("TARGET_ENV"), + env!("CARGO_CFG_TARGET_ENV"), " ", - env!("TARGET_ARCH"), - ", git ", + env!("CARGO_CFG_TARGET_ARCH"), + "\ngit: ", env!("GIT_DESCRIBE"), - " rev ", + ", rev ", env!("GIT_REV"), ); }