Skip to content

Commit

Permalink
refactor: rework build and some dependencies (#153)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Aug 15, 2024
1 parent bfcf9cc commit 84897b9
Show file tree
Hide file tree
Showing 31 changed files with 910 additions and 602 deletions.
727 changes: 500 additions & 227 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ members = [
resolver = "2"

[workspace.package]
version = "5.7.0"
version = "5.8.0"
edition = "2021"
authors = ["tison <[email protected]>"]
readme = "README.md"
Expand All @@ -32,10 +32,14 @@ repository = "https://github.com/korandoru/hawkeye/"
rust-version = "1.76.0"

[workspace.dependencies]
anyhow = "1.0"
build-data = "0.2"
clap = { version = "4.5", features = ["derive", "string", "cargo"] }
const_format = { version = "0.2" }
hawkeye-fmt = { version = "5.7.0", path = "fmt" }
snafu = "0.8.2"
toml = "0.8.12"
tracing = "0.1.40"
log = { version = "0.4", features = ["kv_unstable_serde", "serde"] }
shadow-rs = "0.32"
toml = "0.8"

[workspace.metadata.release]
sign-tag = true
Expand Down
15 changes: 9 additions & 6 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ license.workspace = true
repository.workspace = true

[dependencies]
clap = { version = "4.5.3", features = ["derive", "string", "cargo"] }
hawkeye-fmt.workspace = true
snafu.workspace = true
tracing.workspace = true
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
anyhow = { workspace = true }
clap = { workspace = true }
const_format = { workspace = true }
hawkeye-fmt = { workspace = true }
log = { workspace = true }
env_logger = "0.11"
shadow-rs = { workspace = true }

[build-dependencies]
build-data = "0.1.5"
build-data = { workspace = true }
shadow-rs = { workspace = true }
89 changes: 28 additions & 61 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,36 @@
// Copyright 2024 - 2024, tison <[email protected]> and the HawkEye contributors
// SPDX-License-Identifier: Apache-2.0

use std::{borrow::Cow, sync::OnceLock};
use std::collections::BTreeSet;
use std::env;

const UNKNOWN: &str = "unknown";
use build_data::format_timestamp;
use build_data::get_source_time;
use shadow_rs::CARGO_METADATA;
use shadow_rs::CARGO_TREE;

pub struct BuildInfo {
pub branch: Cow<'static, str>,
pub commit: Cow<'static, str>,
pub commit_short: Cow<'static, str>,
pub dirty: Cow<'static, str>,
pub timestamp: Cow<'static, str>,

/// Rustc Version
pub rustc: Cow<'static, str>,
/// GreptimeDB Version
pub version: Cow<'static, str>,
}

static BUILD: OnceLock<BuildInfo> = OnceLock::new();

pub fn build_info() -> &'static BuildInfo {
BUILD.get_or_init(|| {
let branch = build_data::get_git_branch()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let commit = build_data::get_git_commit()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let commit_short = build_data::get_git_commit_short()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let dirty = build_data::get_git_dirty()
.map(|b| Cow::Owned(b.to_string()))
.unwrap_or(Cow::Borrowed(UNKNOWN));
let timestamp = build_data::get_source_time()
.map(|ts| Cow::Owned(build_data::format_timestamp(ts)))
.unwrap_or(Cow::Borrowed(UNKNOWN));
let rustc = build_data::get_rustc_version()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(UNKNOWN));
let version = Cow::Borrowed(env!("CARGO_PKG_VERSION"));

BuildInfo {
branch,
commit,
commit_short,
dirty,
timestamp,
rustc,
version,
}
})
}

fn main() {
let build_info = build_info();
println!("cargo:rustc-env=GIT_COMMIT={}", build_info.commit);
fn main() -> shadow_rs::SdResult<()> {
println!("cargo:rerun-if-changed=.git/refs/heads");
println!(
"cargo:rustc-env=GIT_COMMIT_SHORT={}",
build_info.commit_short
"cargo:rustc-env=SOURCE_TIMESTAMP={}",
if let Ok(t) = get_source_time() {
format_timestamp(t)
} else {
"".to_string()
}
);
println!("cargo:rustc-env=GIT_BRANCH={}", build_info.branch);
println!("cargo:rustc-env=GIT_DIRTY={}", build_info.dirty);
println!("cargo:rustc-env=GIT_DIRTY={}", build_info.dirty);
println!("cargo:rustc-env=RUSTC_VERSION={}", build_info.rustc);
println!("cargo:rustc-env=SOURCE_TIMESTAMP={}", build_info.timestamp);
build_data::set_BUILD_TIMESTAMP();

// The "CARGO_WORKSPACE_DIR" is set manually (not by Rust itself) in Cargo config file, to
// solve the problem where the "CARGO_MANIFEST_DIR" is not what we want when this repo is
// made as a submodule in another repo.
let src_path = env::var("CARGO_WORKSPACE_DIR").or_else(|_| env::var("CARGO_MANIFEST_DIR"))?;
let out_path = env::var("OUT_DIR")?;
let _ = shadow_rs::Shadow::build_with(
src_path,
out_path,
// exclude these two large constants that we don't need
BTreeSet::from([CARGO_METADATA, CARGO_TREE]),
)?;
Ok(())
}
48 changes: 13 additions & 35 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,25 @@
// Copyright 2024 - 2024, tison <[email protected]> and the HawkEye contributors
// SPDX-License-Identifier: Apache-2.0

use clap::{crate_description, FromArgMatches, Subcommand};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
use clap::crate_description;
use clap::FromArgMatches;
use clap::Subcommand;
use env_logger::Env;

use crate::cli::SubCommand;
use crate::subcommand::SubCommand;

pub mod cli;
pub mod subcommand;
pub mod version;

fn version() -> &'static str {
concat!(
"\nbranch: ",
env!("GIT_BRANCH"),
"\ncommit: ",
env!("GIT_COMMIT"),
"\ndirty: ",
env!("GIT_DIRTY"),
"\nversion: v",
env!("CARGO_PKG_VERSION"),
"\ntoolchain: ",
env!("RUSTC_VERSION"),
"\nbuild: ",
env!("SOURCE_TIMESTAMP"),
)
}

fn main() -> hawkeye_fmt::Result<()> {
tracing_subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();
fn main() {
env_logger::init_from_env(Env::new().default_filter_or("info"));

let cli = clap::Command::new("hawkeye")
let command = clap::Command::new("hawkeye")
.subcommand_required(true)
.version(version())
.version(version::version())
.about(crate_description!());
let cli = SubCommand::augment_subcommands(cli);
let args = cli.get_matches();
let command = SubCommand::augment_subcommands(command);
let args = command.get_matches();
match SubCommand::from_arg_matches(&args) {
Ok(cmd) => cmd.run(),
Err(e) => e.exit(),
Expand Down
Loading

0 comments on commit 84897b9

Please sign in to comment.