Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use std::{
use toml::Value;
use walkdir::WalkDir;

#[path = "version_env.rs"]
mod version_env;

// The following license text that should be present at the beginning of every source file.
const EXPECTED_LICENSE_TEXT: &[u8] = include_bytes!(".resources/license_header");

Expand Down Expand Up @@ -312,6 +315,8 @@ fn main() {
// Check if the tokio_console feature is correctly enabled.
check_tokio_console_flags();

// Register the release version for runtime version reporting.
version_env::emit_version_env();
// Register build-time information.
built::write_built_file().expect("Failed to acquire build-time information");
}
21 changes: 21 additions & 0 deletions cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkOS library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[path = "../version_env.rs"]
mod version_env;

fn main() {
version_env::emit_version_env();
}
21 changes: 19 additions & 2 deletions cli/src/helpers/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Updater {
.repo_owner(Self::SNARKOS_REPO_OWNER)
.repo_name(Self::SNARKOS_REPO_NAME)
.bin_name(Self::SNARKOS_BIN_NAME)
.current_version(env!("CARGO_PKG_VERSION"))
.current_version(env!("SNARKOS_VERSION"))
.show_download_progress(show_output)
.no_confirm(true)
.show_output(show_output);
Expand All @@ -66,7 +66,7 @@ impl Updater {
.repo_owner(Self::SNARKOS_REPO_OWNER)
.repo_name(Self::SNARKOS_REPO_NAME)
.bin_name(Self::SNARKOS_BIN_NAME)
.current_version(env!("CARGO_PKG_VERSION"))
.current_version(env!("SNARKOS_VERSION"))
.build()?;

let current_version = updater.current_version();
Expand All @@ -92,6 +92,23 @@ impl Updater {
}
}

#[cfg(test)]
mod tests {
use std::{fs, path::Path};

#[test]
fn snarkos_version_matches_release_version() {
let release_version = Path::new(env!("CARGO_MANIFEST_DIR"))
.ancestors()
.map(|dir| dir.join(".cargo").join("release-version"))
.find(|path| path.is_file())
.map(|path| fs::read_to_string(path).unwrap())
.unwrap();

assert_eq!(env!("SNARKOS_VERSION"), release_version.trim().trim_start_matches('v'));
}
}

#[derive(Debug, Error)]
pub enum UpdaterError {
#[error("{}: {}", _0, _1)]
Expand Down
21 changes: 21 additions & 0 deletions node/cdn/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkOS library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[path = "../../version_env.rs"]
mod version_env;

fn main() {
version_env::emit_version_env();
}
2 changes: 1 addition & 1 deletion node/cdn/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub const CDN_BASE_URL: &str = "https://cdn.provable.com/v0/blocks";

/// Returns the user-agent string for CDN requests.
const fn cdn_user_agent() -> &'static str {
concat!("snarkos/", env!("CARGO_PKG_VERSION"))
concat!("snarkos/", env!("SNARKOS_VERSION"))
}

/// Updates the metrics during CDN sync.
Expand Down
4 changes: 4 additions & 0 deletions node/metrics/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[path = "../../version_env.rs"]
mod version_env;

fn main() {
version_env::emit_version_env();
built::write_built_file().expect("Failed to acquire build-time information");
Comment thread
vicsn marked this conversation as resolved.
}
2 changes: 1 addition & 1 deletion node/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod built_info {
/// The resulting metric will show as:
/// snarkos_build_info{version="4.2.1",git_commit="abc123",git_branch="main",features="cuda,metrics"} 1
pub fn set_build_info() {
let version = built_info::PKG_VERSION;
let version = env!("SNARKOS_VERSION");
let git_commit = built_info::GIT_COMMIT_HASH.unwrap_or("unknown");
let git_branch = built_info::GIT_HEAD_REF.unwrap_or("unknown");
let features = built_info::FEATURES_LOWERCASE_STR.replace(' ', "");
Expand Down
4 changes: 4 additions & 0 deletions node/rest/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[path = "../../version_env.rs"]
mod version_env;

fn main() {
version_env::emit_version_env();
built::write_built_file().expect("Failed to acquire build-time information");
Comment thread
vicsn marked this conversation as resolved.
}
2 changes: 1 addition & 1 deletion node/rest/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl VersionInfo {
let consensus_heights: Vec<u32> = N::CONSENSUS_VERSION_HEIGHTS().iter().map(|(_, height)| *height).collect();

VERSION_INFO.get_or_init(|| VersionInfo {
version: built_info::PKG_VERSION.to_string(),
version: env!("SNARKOS_VERSION").to_string(),
git_commit: built_info::GIT_COMMIT_HASH.unwrap_or("unknown").to_string(),
git_branch: built_info::GIT_HEAD_REF.unwrap_or("unknown").to_string(),
latest_consensus_version: latest_num,
Expand Down
3 changes: 2 additions & 1 deletion snarkos/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ fn check_for_version() {
if let Some(first_arg) = env::args().nth(1)
&& ["--version", "-V"].contains(&&*first_arg)
{
let version = env!("SNARKOS_VERSION");
let branch = GIT_HEAD_REF.unwrap_or("unknown_branch");
let commit = GIT_COMMIT_HASH.unwrap_or("unknown_commit");
let mut features = FEATURES_LOWERCASE_STR.to_owned();
features.retain(|c| c != ' ');

print_info!("snarkos {branch} {commit} features=[{features}]");
print_info!("snarkos {version} {branch} {commit} features=[{features}]");

exit(0);
}
Expand Down
48 changes: 48 additions & 0 deletions version_env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkOS library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{
env,
fs,
path::{Path, PathBuf},
};

pub fn emit_version_env() {
let release_version = find_release_version_path().unwrap_or_else(|| {
panic!(
"Failed to locate '.cargo/release-version' starting from '{}'",
env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| "<unknown manifest dir>".to_string())
)
});

println!("cargo:rerun-if-changed={}", release_version.display());

let version = fs::read_to_string(&release_version)
.unwrap_or_else(|err| panic!("Failed to read '{}': {err}", release_version.display()));
let version = version.trim();
let version = version.strip_prefix('v').unwrap_or(version);
assert!(!version.is_empty(), "'{}' did not contain a version", release_version.display());

println!("cargo:rustc-env=SNARKOS_VERSION={version}");
}

fn find_release_version_path() -> Option<PathBuf> {
let manifest_dir = env::var_os("CARGO_MANIFEST_DIR")?;

Path::new(&manifest_dir)
.ancestors()
.map(|dir| dir.join(".cargo").join("release-version"))
.find(|path| path.is_file())
}