From f185d07657c4c1070c563193b7ef7ccc81882d1a Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Fri, 10 Jan 2025 01:38:12 +0100 Subject: [PATCH] Make git tag a single source of truth for versions, available in compile-time --- .gitattributes | 1 + .github/workflows/ci.yml | 10 +++++---- .gitignore | 1 + Cargo.lock | 2 +- Cargo.toml | 2 +- build.rs | 37 +++++++++++++++++++++++++++++++ justfile | 47 ++++++++++++++++++++++++++++++++++++++++ src/frame/vulkan.rs | 12 +++++++--- src/main.rs | 5 +++++ 9 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 .gitattributes create mode 100644 build.rs create mode 100644 justfile diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..eedf9ff --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/build.rs export-subst diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e9c9e0b..825dcfa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,19 +7,21 @@ on: jobs: test: - name: make test + name: just test runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - uses: taiki-e/install-action@just - run: sudo apt-get update - run: sudo apt-get -y install v4l-utils libv4l-dev libudev-dev libvulkan-dev libdbus-1-dev - - run: make test + - run: just test lint: - name: make lint + name: just lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - uses: taiki-e/install-action@just - run: sudo apt-get update - run: sudo apt-get -y install v4l-utils libv4l-dev libudev-dev libvulkan-dev libdbus-1-dev - - run: make lint + - run: just lint diff --git a/.gitignore b/.gitignore index 8b84bc4..22599a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /dist /target +/vendor diff --git a/Cargo.lock b/Cargo.lock index 357c7ee..62bbdc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1515,7 +1515,7 @@ dependencies = [ [[package]] name = "wluma" -version = "4.6.1" +version = "0.0.0" dependencies = [ "anyhow", "ash", diff --git a/Cargo.toml b/Cargo.toml index 7b136c1..3f35a2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "wluma" -version = "4.6.1" authors = ["Maxim Baz", "Cyril Levis"] edition = "2021" +license = "ISC" [dependencies] ash = { version = "~0.38", features = ["linked"], default-features = false } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..6a50b80 --- /dev/null +++ b/build.rs @@ -0,0 +1,37 @@ +use std::process::Command; + +fn main() { + let version = match std::env::var("WLUMA_VERSION") { + Ok(v) => v, + Err(_) => { + let version = "$Format:%(describe)$"; // Replaced by git-archive. + let version = if version.starts_with('$') { + match Command::new("git").args(["describe", "--tags"]).output() { + Ok(o) if o.status.success() => { + String::from_utf8_lossy(&o.stdout).trim().to_string() + } + Ok(o) => panic!("git-describe exited non-zero: {}", o.status), + Err(err) => panic!("failed to execute git-describe: {err}"), + } + } else { + version.to_string() + }; + + let version = version.strip_prefix('v').unwrap_or(&version); + println!("cargo:rustc-env=WLUMA_VERSION={version}"); + version.to_string() + } + }; + + let parts = version + .split(|c: char| !c.is_ascii_digit()) + .collect::>(); + + if parts.len() < 3 { + panic!("Unable to parse 'major.minor.patch' from version: {version}"); + } + + println!("cargo:rustc-env=WLUMA_VERSION_MAJOR={}", parts[0]); + println!("cargo:rustc-env=WLUMA_VERSION_MINOR={}", parts[1]); + println!("cargo:rustc-env=WLUMA_VERSION_PATCH={}", parts[2]); +} diff --git a/justfile b/justfile new file mode 100644 index 0000000..ec9b779 --- /dev/null +++ b/justfile @@ -0,0 +1,47 @@ +default: release + +app := "wluma" +version := `git describe --tags 2>/dev/null || echo 0.0.0-unknown` +vendor-config := """ + [source.crates-io] + replace-with = "vendored-sources" + + [source.vendored-sources] + directory = "vendor" +""" + +release: clean vendor + mkdir -p dist + + git -c tar.tar.gz.command="gzip -cn" archive -o "dist/{{app}}-{{version}}.tar.gz" --format tar.gz --prefix "{{app}}-{{version}}/" "{{version}}" + + git -c tar.tar.gz.command="gzip -cn" archive -o "dist/{{app}}-{{version}}-vendored.tar.gz" --format tar.gz \ + `find vendor -type f -printf '--prefix={{app}}-{{version}}/%h/ --add-file=%p '` \ + --add-virtual-file '{{app}}-{{version}}/.cargo/config.toml:{{vendor-config}}' \ + --prefix "{{app}}-{{version}}/" "{{version}}" + + for file in dist/*; do \ + gpg --detach-sign --armor "$file"; \ + done + + rm -f "dist/{{app}}-{{version}}.tar.gz" + +run *args: + cargo run {{args}} + +build *args: + cargo build --locked {{args}} + +lint: + cargo fmt -- --check + cargo clippy -- -Dwarnings + +test: + cargo test --locked + +vendor: + cargo vendor vendor + +clean: + rm -rf dist + rm -rf vendor diff --git a/src/frame/vulkan.rs b/src/frame/vulkan.rs index 59126e1..3042ce4 100644 --- a/src/frame/vulkan.rs +++ b/src/frame/vulkan.rs @@ -8,7 +8,6 @@ use std::ffi::CString; use std::ops::Drop; use std::os::fd::{AsRawFd, FromRawFd, OwnedFd}; -const WLUMA_VERSION: u32 = vk::make_api_version(0, 4, 6, 1); const VULKAN_VERSION: u32 = vk::make_api_version(0, 1, 2, 0); const FINAL_MIP_LEVEL: u32 = 4; // Don't generate mipmaps beyond this level - GPU is doing too poor of a job averaging the colors @@ -37,11 +36,18 @@ pub struct Vulkan { impl Vulkan { pub fn new() -> Result> { let app_name = CString::new("wluma")?; + let app_version: u32 = vk::make_api_version( + 0, + env!("WLUMA_VERSION_MAJOR").parse()?, + env!("WLUMA_VERSION_MINOR").parse()?, + env!("WLUMA_VERSION_PATCH").parse()?, + ); + let app_info = vk::ApplicationInfo::default() .application_name(&app_name) - .application_version(WLUMA_VERSION) + .application_version(app_version) .engine_name(&app_name) - .engine_version(WLUMA_VERSION) + .engine_version(app_version) .api_version(VULKAN_VERSION); let instance_extensions = &[ diff --git a/src/main.rs b/src/main.rs index 998ea96..7a10e57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,9 @@ mod device_file; mod frame; mod predictor; +/// Current app version (determined at compile-time). +pub const VERSION: &str = env!("WLUMA_VERSION"); + fn main() { let panic_hook = std::panic::take_hook(); std::panic::set_hook(Box::new(move |panic_info| { @@ -20,6 +23,8 @@ fn main() { .parse_default_env() .init(); + log::debug!("== wluma v{} ==", VERSION); + let config = match config::load() { Ok(config) => config, Err(err) => panic!("Unable to load config: {}", err),