From 3b8ad9e8c5305bcb23a22afffd78877727afe7b1 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 19 Aug 2024 10:03:12 -0400 Subject: [PATCH 1/5] Add web5_proc_macros with git_sha proc macro --- Cargo.toml | 2 +- crates/web5/Cargo.toml | 2 +- crates/web5/src/lib.rs | 15 +++++---------- crates/web5_proc_macros/Cargo.toml | 15 +++++++++++++++ crates/web5_proc_macros/src/lib.rs | 23 +++++++++++++++++++++++ 5 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 crates/web5_proc_macros/Cargo.toml create mode 100644 crates/web5_proc_macros/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index f7b9cef4..49169577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = [ "crates/web5", "crates/web5_cli", "bindings/web5_uniffi", - "bindings/web5_uniffi_wrapper", + "bindings/web5_uniffi_wrapper", "crates/web5_proc_macros", ] default-members = [ "crates/web5", diff --git a/crates/web5/Cargo.toml b/crates/web5/Cargo.toml index 505837ae..ec80bddb 100644 --- a/crates/web5/Cargo.toml +++ b/crates/web5/Cargo.toml @@ -15,7 +15,6 @@ josekit = "0.8.6" jsonpath-rust = "0.5.1" jsonschema = { version = "0.18.0", default-features = false } k256 = { version = "0.13.3", features = ["ecdsa", "jwk"] } -lazy_static = "1.5.0" tokio = "1.38.0" rand = { workspace = true } regex = "1.10.4" @@ -27,6 +26,7 @@ simple-dns = "0.7.0" thiserror = { workspace = true } url = "2.5.0" uuid = { workspace = true } +web5_proc_macros = { path = "../web5_proc_macros" } zbase32 = "0.1.2" [dev-dependencies] diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 0d5d96f8..b99e479d 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -8,19 +8,14 @@ pub mod errors; pub mod json; pub mod rfc3339; -#[cfg(test)] -mod test_vectors; #[cfg(test)] mod test_helpers; - -pub const GIT_COMMIT_HASH: &str = env!("WEB5_GIT_COMMIT_HASH"); +#[cfg(test)] +mod test_vectors; // TODO: https://github.com/TBD54566975/web5-rs/issues/287 #[allow(dead_code)] -static LOG_LEVEL: LazyLock> = LazyLock::new(|| { - // Default log level if the environment variable is not set - env::var("LOG_LEVEL").ok() -}); +static LOG_LEVEL: LazyLock> = LazyLock::new(|| env::var("WEB5_SDK_LOG_LEVEL").ok()); pub(crate) mod logging { #[macro_export] @@ -28,7 +23,7 @@ pub(crate) mod logging { ($msg:literal $(, $arg:tt)*) => { if let Some(ref level) = *$crate::LOG_LEVEL { if level == "DEBUG" { - println!("[DEBUG] {}:{}", env!("WEB5_GIT_COMMIT_HASH"), format!($msg, $($arg)*)); + println!("[DEBUG] {}:{}", web5_proc_macros::git_sha!(), format!($msg, $($arg)*)); } } }; @@ -36,7 +31,7 @@ pub(crate) mod logging { if let Some(ref level) = *$crate::LOG_LEVEL { if level == "DEBUG" { let msg = $closure(); - println!("[DEBUG] {}:{}", env!("WEB5_GIT_COMMIT_HASH"), msg); + println!("[DEBUG] {}:{}", web5_proc_macros::git_sha!(), msg); } } }; diff --git a/crates/web5_proc_macros/Cargo.toml b/crates/web5_proc_macros/Cargo.toml new file mode 100644 index 00000000..07c8bb5c --- /dev/null +++ b/crates/web5_proc_macros/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "web5_proc_macros" +version = "0.1.0" +edition = "2021" +homepage.workspace = true +repository.workspace = true +license-file.workspace = true + +[dependencies] +proc-macro2 = "1.0.86" +quote = "1.0.36" +syn = "2.0.75" + +[lib] +proc-macro = true \ No newline at end of file diff --git a/crates/web5_proc_macros/src/lib.rs b/crates/web5_proc_macros/src/lib.rs new file mode 100644 index 00000000..2f112930 --- /dev/null +++ b/crates/web5_proc_macros/src/lib.rs @@ -0,0 +1,23 @@ +extern crate proc_macro; +use proc_macro::TokenStream; +use quote::quote; +use std::process::Command; + +#[proc_macro] +pub fn git_sha(_input: TokenStream) -> TokenStream { + let output = Command::new("git") + .args(&["rev-parse", "HEAD"]) + .output() + .expect("Failed to execute git command"); + + let git_hash = String::from_utf8(output.stdout) + .expect("Invalid UTF-8 sequence") + .trim() + .to_string(); + + let expanded = quote! { + #git_hash + }; + + TokenStream::from(expanded) +} From 1ccf916e14c1cbbfdb32ccd30b47845d7a072a53 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 19 Aug 2024 10:31:18 -0400 Subject: [PATCH 2/5] Use short SHA, case insensitive log level, remove kt code --- bound/kt/pom.xml | 23 ------------------- .../main/kotlin/web5/sdk/rust/SystemTarget.kt | 11 +-------- crates/web5/build.rs | 18 --------------- crates/web5/src/lib.rs | 14 +++++------ crates/web5_proc_macros/src/lib.rs | 2 +- 5 files changed, 9 insertions(+), 59 deletions(-) delete mode 100644 crates/web5/build.rs diff --git a/bound/kt/pom.xml b/bound/kt/pom.xml index 1c16b415..c271b81a 100644 --- a/bound/kt/pom.xml +++ b/bound/kt/pom.xml @@ -539,29 +539,6 @@ ${kotlin.jvm.target} - - org.codehaus.mojo - exec-maven-plugin - 3.0.0 - - - get-git-commit-id - - exec - - initialize - - git - - rev-parse - --short - HEAD - - ${project.build.directory}/git-commit-id.txt - - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt index a26bcd74..a31fb046 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt @@ -3,19 +3,10 @@ package web5.sdk.rust import java.io.File internal val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase() -internal val gitCommitHash = run { - val commitFile = File("target/git-commit-id.txt") - if (commitFile.exists()) { - commitFile.readText().trim() - } else { - println("Git commit hash not found.") - "" - } -} internal fun log(message: String) { if (logLevel == "debug") { - println("web5 sdk SystemArchitecture $gitCommitHash: $message") + println("web5 sdk SystemArchitecture $message") } } diff --git a/crates/web5/build.rs b/crates/web5/build.rs deleted file mode 100644 index 9d322b71..00000000 --- a/crates/web5/build.rs +++ /dev/null @@ -1,18 +0,0 @@ -use std::process::Command; - -fn main() { - // Execute the `git rev-parse HEAD` command to get the current commit hash - let output = Command::new("git") - .args(["rev-parse", "HEAD"]) - .output() - .expect("Failed to execute git command"); - - // Convert the output to a string - let git_hash = String::from_utf8(output.stdout).expect("Invalid UTF-8 sequence"); - - // Remove the newline character from the commit hash - let git_hash_trimmed = git_hash.trim(); - - // Pass the commit hash to the compiler as an environment variable - println!("cargo:rustc-env=WEB5_GIT_COMMIT_HASH={}", git_hash_trimmed); -} diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index b99e479d..110606ad 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -22,17 +22,17 @@ pub(crate) mod logging { macro_rules! log_dbg { ($msg:literal $(, $arg:tt)*) => { if let Some(ref level) = *$crate::LOG_LEVEL { - if level == "DEBUG" { - println!("[DEBUG] {}:{}", web5_proc_macros::git_sha!(), format!($msg, $($arg)*)); - } + if level.to_lowercase() == "debug" { + println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), format!($msg, $($arg)*)); + } } }; ($closure:expr) => { if let Some(ref level) = *$crate::LOG_LEVEL { - if level == "DEBUG" { - let msg = $closure(); - println!("[DEBUG] {}:{}", web5_proc_macros::git_sha!(), msg); - } + if level.to_lowercase() == "debug" { + let msg = $closure(); + println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), msg); + } } }; } diff --git a/crates/web5_proc_macros/src/lib.rs b/crates/web5_proc_macros/src/lib.rs index 2f112930..9695595d 100644 --- a/crates/web5_proc_macros/src/lib.rs +++ b/crates/web5_proc_macros/src/lib.rs @@ -6,7 +6,7 @@ use std::process::Command; #[proc_macro] pub fn git_sha(_input: TokenStream) -> TokenStream { let output = Command::new("git") - .args(&["rev-parse", "HEAD"]) + .args(&["rev-parse", "--short", "HEAD"]) .output() .expect("Failed to execute git command"); From e29e9e6ed2dc58d8dff90f3fef926fb4b0e30da0 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 19 Aug 2024 10:32:57 -0400 Subject: [PATCH 3/5] PR review --- Cargo.toml | 3 ++- crates/web5/src/lib.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 49169577..02d47ae4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,8 +2,9 @@ members = [ "crates/web5", "crates/web5_cli", + "crates/web5_proc_macros", "bindings/web5_uniffi", - "bindings/web5_uniffi_wrapper", "crates/web5_proc_macros", + "bindings/web5_uniffi_wrapper", ] default-members = [ "crates/web5", diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 110606ad..6088c1bd 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -22,17 +22,17 @@ pub(crate) mod logging { macro_rules! log_dbg { ($msg:literal $(, $arg:tt)*) => { if let Some(ref level) = *$crate::LOG_LEVEL { - if level.to_lowercase() == "debug" { - println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), format!($msg, $($arg)*)); - } + if level.to_lowercase() == "debug" { + println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), format!($msg, $($arg)*)); + } } }; ($closure:expr) => { if let Some(ref level) = *$crate::LOG_LEVEL { - if level.to_lowercase() == "debug" { - let msg = $closure(); - println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), msg); - } + if level.to_lowercase() == "debug" { + let msg = $closure(); + println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), msg); + } } }; } From ec2b0b3087af740cb636dc2c138d40becf7e0399 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 19 Aug 2024 10:38:52 -0400 Subject: [PATCH 4/5] Fix linting, fix linux builds --- Cargo.toml | 2 +- .../web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile | 1 + .../web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile | 1 + crates/web5_proc_macros/src/lib.rs | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 02d47ae4..513443f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = [ "crates/web5_cli", "crates/web5_proc_macros", "bindings/web5_uniffi", - "bindings/web5_uniffi_wrapper", + "bindings/web5_uniffi_wrapper", ] default-members = [ "crates/web5", diff --git a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile index aa8a30a6..202b6873 100644 --- a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile +++ b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile @@ -19,6 +19,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper COPY bindings/web5_uniffi ./bindings/web5_uniffi COPY crates/web5 ./crates/web5 COPY crates/web5_cli ./crates/web5_cli +COPY crates/web5_proc_macros ./crates/web5_proc_macros # Execute the build RUN cargo build --release --package web5_uniffi diff --git a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile index 8a2df5a0..6c2bc3d2 100644 --- a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile +++ b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile @@ -24,6 +24,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper COPY bindings/web5_uniffi ./bindings/web5_uniffi COPY crates/web5 ./crates/web5 COPY crates/web5_cli ./crates/web5_cli +COPY crates/web5_proc_macros ./crates/web5_proc_macros # Build the static lib (override the lib type) RUN sed -i 's/crate-type = \["cdylib"\]/crate-type = \["staticlib"\]/' bindings/web5_uniffi/Cargo.toml diff --git a/crates/web5_proc_macros/src/lib.rs b/crates/web5_proc_macros/src/lib.rs index 9695595d..f555c8e2 100644 --- a/crates/web5_proc_macros/src/lib.rs +++ b/crates/web5_proc_macros/src/lib.rs @@ -6,7 +6,7 @@ use std::process::Command; #[proc_macro] pub fn git_sha(_input: TokenStream) -> TokenStream { let output = Command::new("git") - .args(&["rev-parse", "--short", "HEAD"]) + .args(["rev-parse", "--short", "HEAD"]) .output() .expect("Failed to execute git command"); From 2fa3639221037f23d9856d5366fd8eabb99309a3 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 19 Aug 2024 10:42:50 -0400 Subject: [PATCH 5/5] Fix CLI build --- crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile | 1 + crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile b/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile index 62b68e23..bc7822f6 100644 --- a/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile +++ b/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile @@ -19,6 +19,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper COPY bindings/web5_uniffi ./bindings/web5_uniffi COPY crates/web5 ./crates/web5 COPY crates/web5_cli ./crates/web5_cli +COPY crates/web5_proc_macros ./crates/web5_proc_macros # Execute the build RUN cargo build --release --package web5_cli diff --git a/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile b/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile index 011cb040..14c6f2bf 100644 --- a/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile +++ b/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile @@ -24,6 +24,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper COPY bindings/web5_uniffi ./bindings/web5_uniffi COPY crates/web5 ./crates/web5 COPY crates/web5_cli ./crates/web5_cli +COPY crates/web5_proc_macros ./crates/web5_proc_macros RUN cargo build --release --package web5_cli