Skip to content

Commit

Permalink
build: add an API with which to retrieve build metadata [WPB-11001]
Browse files Browse the repository at this point in the history
This data is stringly typed due to passing through the environment
between being generated and being incorporated into the program.
No attempt is made to parse it in order to keep things simple;
the expected use case for this data is just log information anyway.

This adds more information that was strictly requested by the PR,
in the assumption that this information is negligibly cheap to store and
can only help with reducing parsing demand.
  • Loading branch information
coriolinus committed Nov 15, 2024
1 parent a155e40 commit 028bd62
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 11 deletions.
153 changes: 143 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions crypto-ffi/src/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,44 @@ pub fn version() -> String {
VERSION.to_string()
}

#[derive(uniffi::Record)]
/// Metadata describing the conditions of the build of this software.
pub struct BuildMetadata {
/// Build Timestamp
pub timestamp: String,
/// Whether this build was in Debug mode (true) or Release mode (false)
pub cargo_debug: String,
/// Features enabled for this build
pub cargo_features: String,
/// Optimization level
pub opt_level: String,
/// Build target triple
pub target_triple: String,
/// Git branch
pub git_branch: String,
/// Output of `git describe`
pub git_describe: String,
/// Hash of current git commit
pub git_sha: String,
/// `true` when the source code differed from the commit at the most recent git hash
pub git_dirty: String,
}

#[uniffi::export]
pub fn build_metadata() -> BuildMetadata {
BuildMetadata {
timestamp: core_crypto::BUILD_METADATA.timestamp.to_string(),
cargo_debug: core_crypto::BUILD_METADATA.cargo_debug.to_string(),
cargo_features: core_crypto::BUILD_METADATA.cargo_features.to_string(),
opt_level: core_crypto::BUILD_METADATA.opt_level.to_string(),
target_triple: core_crypto::BUILD_METADATA.target_triple.to_string(),
git_branch: core_crypto::BUILD_METADATA.git_branch.to_string(),
git_describe: core_crypto::BUILD_METADATA.git_describe.to_string(),
git_sha: core_crypto::BUILD_METADATA.git_sha.to_string(),
git_dirty: core_crypto::BUILD_METADATA.git_dirty.to_string(),
}
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum CoreCryptoError {
#[error(transparent)]
Expand Down
3 changes: 3 additions & 0 deletions crypto-ffi/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ use wasm_bindgen_futures::future_to_promise;
#[allow(dead_code)]
pub(super) const VERSION: &str = env!("CARGO_PKG_VERSION");

#[allow(dead_code)]
pub(super) use core_crypto::BUILD_METADATA;

Check failure on line 42 in crypto-ffi/src/wasm/mod.rs

View workflow job for this annotation

GitHub Actions / check

unused import: `core_crypto::BUILD_METADATA`

Check failure on line 42 in crypto-ffi/src/wasm/mod.rs

View workflow job for this annotation

GitHub Actions / build-and-test-wasm

unused import: `core_crypto::BUILD_METADATA`

// This is intended to hotfix this import:
// ❯ wasmer inspect bindings/js/wasm/core-crypto-ffi_bg.wasm | grep env
// "env"."__stack_chk_fail": [] -> []
Expand Down
7 changes: 7 additions & 0 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,10 @@ harness = false
[[bench]]
name = "mls_proteus"
harness = false

[build-dependencies]
anyhow = "1.0.93"
# this is appropriate if we always and only build in environments where `git`
# is available on the command line. If not, we should replace this with
# `vergen-gix` or `vergen-git2`.
vergen-gitcl = { version = "1.0.1", features = ["build", "cargo"] }
29 changes: 28 additions & 1 deletion crypto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

fn main() {
use vergen_gitcl::{BuildBuilder, CargoBuilder, Emitter, GitclBuilder};

fn main() -> anyhow::Result<()> {
// Target aliases
#[cfg(target_os = "ios")]
println!("cargo:rustc-cfg=ios");
Expand All @@ -28,4 +30,29 @@ fn main() {
if let Ok(profile) = std::env::var("PROFILE") {
println!("cargo:rustc-cfg=build=\"{profile}\"");
}

// collect a bunch of build/git information and emit it into the build environment,
// from whence we can extract it and make it public
let build = BuildBuilder::default().build_timestamp(true).build()?;
let cargo = CargoBuilder::default()
.debug(true)
.features(true)
.opt_level(true)
.target_triple(true)
.build()?;
let git = GitclBuilder::default()
.branch(true)
.sha(false)
.dirty(false)
.describe(true, true, Some("v*"))
.build()?;

Emitter::default()
.fail_on_error()
.add_instructions(&build)?
.add_instructions(&cargo)?
.add_instructions(&git)?
.emit()?;

Ok(())
}
Loading

0 comments on commit 028bd62

Please sign in to comment.