Skip to main content

hyperlight_wasm/
build_info.rs

1/*
2Copyright 2024 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use std::sync::Once;
18
19use log::info;
20// LOG_ONCE is used to log information about the crate version once
21static LOG_ONCE: Once = Once::new();
22
23// The `built` crate is used in build.rs to generate a `built.rs` file that contains
24// information about the build environment.
25//
26// In addition build.rs appends information about the hyperlight-wasm-runtime binary to the `built.rs` file
27//
28// Collectively that information is used to populate the `BuildInfo` struct.
29//
30include!(concat!(env!("OUT_DIR"), "/built.rs"));
31
32/// The build information for the hyperlight-wasm crate
33pub struct BuildInfo {
34    /// The date and time the hyperlight-wasm-runtime was built
35    pub wasm_runtime_created: &'static str,
36    /// The size of the hyperlight-wasm-runtime binary
37    pub wasm_runtime_size: &'static str,
38    /// The blake3 hash of the hyperlight-wasm-runtime binary
39    pub wasm_runtime_blake3_hash: &'static str,
40    /// The version of wasmtime being used by hyperlight-wasm
41    pub wasm_runtime_wasmtime_version: &'static str,
42    /// The name of the package
43    pub package_name: &'static str,
44    /// The version of the package
45    pub package_version: &'static str,
46    /// The features enabled for the package
47    pub features: Vec<&'static str>,
48    /// The target triple for the build
49    pub target: &'static str,
50    /// The optimization level for the build
51    pub opt_level: &'static str,
52    /// The profile for the build
53    pub profile: &'static str,
54    /// Whether the build was in debug mode
55    pub debug: bool,
56    /// The path to rustc used for the build
57    pub rustc: &'static str,
58    /// The date and time the package was built
59    pub built_time_utc: &'static str,
60    /// The CI platform used for the build
61    pub ci_platform: Option<&'static str>,
62    /// The git commit hash for the build
63    pub git_commit_hash: Option<&'static str>,
64    /// The git head ref for the build
65    pub git_head_ref: Option<&'static str>,
66    /// The git version for the build
67    pub git_version: Option<&'static str>,
68    /// Whether the git repo was dirty when the package was built
69    pub git_dirty: bool,
70}
71
72impl Default for BuildInfo {
73    fn default() -> Self {
74        let mut features: Vec<&str> = Vec::new();
75
76        for feature in FEATURES {
77            features.push(feature);
78        }
79
80        Self {
81            wasm_runtime_created: WASM_RUNTIME_CREATED,
82            wasm_runtime_size: WASM_RUNTIME_SIZE,
83            wasm_runtime_blake3_hash: WASM_RUNTIME_BLAKE3_HASH,
84            wasm_runtime_wasmtime_version: WASM_RUNTIME_WASMTIME_VERSION,
85            package_name: PKG_NAME,
86            package_version: PKG_VERSION,
87            features,
88            target: TARGET,
89            opt_level: OPT_LEVEL,
90            profile: PROFILE,
91            debug: DEBUG,
92            rustc: RUSTC,
93            built_time_utc: BUILT_TIME_UTC,
94            ci_platform: CI_PLATFORM,
95            git_commit_hash: GIT_COMMIT_HASH,
96            git_head_ref: GIT_HEAD_REF,
97            git_version: GIT_VERSION,
98            git_dirty: GIT_DIRTY.unwrap_or(false),
99        }
100    }
101}
102
103impl BuildInfo {
104    /// Get the build information
105    pub fn get() -> Self {
106        Self::default()
107    }
108    pub(crate) fn log() {
109        Self::default().log_build_details();
110    }
111    fn log_build_details(&self) {
112        LOG_ONCE.call_once(|| {
113            info!("{}", self);
114        });
115    }
116    /// Get the version of wasmtime being used by hyperlight-wasm
117    pub(crate) fn get_wasmtime_version() -> &'static str {
118        WASM_RUNTIME_WASMTIME_VERSION
119    }
120}
121
122impl std::fmt::Display for BuildInfo {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        writeln!(
125            f,
126            "hyperlight-wasm-runtime created at: {}",
127            self.wasm_runtime_created
128        )?;
129        writeln!(
130            f,
131            "hyperlight-wasm-runtime size: {}",
132            self.wasm_runtime_size
133        )?;
134        writeln!(
135            f,
136            "hyperlight-wasm-runtime hash: {}",
137            self.wasm_runtime_blake3_hash
138        )?;
139        writeln!(
140            f,
141            "hyperlight-wasm-runtime wasmtime version: {}",
142            self.wasm_runtime_wasmtime_version
143        )?;
144        writeln!(f, "Package name: {}", self.package_name)?;
145        writeln!(f, "Package version: {}", self.package_version)?;
146        writeln!(f, "Package features: {:#?}", self.features)?;
147        writeln!(f, "Target triple: {}", self.target)?;
148        writeln!(f, "Optimization level: {}", self.opt_level)?;
149        writeln!(f, "Profile: {}", self.profile)?;
150        writeln!(f, "Debug: {}", self.debug)?;
151        writeln!(f, "Rustc: {}", self.rustc)?;
152        writeln!(f, "Built at: {}", self.built_time_utc)?;
153        writeln!(f, "CI platform: {:?}", self.ci_platform.unwrap_or("None"))?;
154        writeln!(
155            f,
156            "Git commit hash: {:?}",
157            self.git_commit_hash.unwrap_or("None")
158        )?;
159        writeln!(f, "Git head ref: {:?}", self.git_head_ref.unwrap_or("None"))?;
160        writeln!(f, "Git version: {:?}", self.git_version.unwrap_or("None"))?;
161        if self.git_dirty {
162            writeln!(f, "Repo had uncommitted changes when built")?;
163        }
164        Ok(())
165    }
166}