add cargo features to the long version

This commit is contained in:
Clément DOUIN
2024-11-22 09:51:43 +01:00
parent 14c77e4629
commit 5c4b03474e
6 changed files with 100 additions and 56 deletions
+11 -11
View File
@@ -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
+10 -10
View File
@@ -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
+2
View File
@@ -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"
+68 -27
View File
@@ -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<String, Vec<String>>,
}
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<String> {
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}"));
}
+2 -2
View File
@@ -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)"
+7 -6
View File
@@ -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"),
);
}