From 283cc25302e638d70e694262fef7ecf75b268ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Liebau?= Date: Sun, 12 Feb 2023 11:40:52 +0100 Subject: [PATCH] Changes needed to prepare crate to be compatible with crate2nix. In https://github.com/stackabletech/operator-templating/pull/212 we are planning to add a more developer friendly workflow for building and deploying artifacts to Kubernetes during development. This workflow uses crate2nix, which uses rustc directly and does not provide all environment variables required by the built crate we use (see https://github.com/kolloch/crate2nix/issues/158). The content of these PR are the fixes needed to work around those limitations. --- rust/operator-binary/build.rs | 12 +++++++++++- rust/operator-binary/src/stackable-hdfs-operator.rs | 11 ++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/rust/operator-binary/build.rs b/rust/operator-binary/build.rs index d8f91cb9..ed36fcf0 100644 --- a/rust/operator-binary/build.rs +++ b/rust/operator-binary/build.rs @@ -1,3 +1,13 @@ +use std::path::PathBuf; + fn main() { - built::write_built_file().expect("Failed to acquire build-time information"); + let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR is required")); + built::write_built_file_with_opts( + // built's env module depends on a whole bunch of variables that crate2nix doesn't provide + // so we grab the specific env variables that we care about out ourselves instead. + built::Options::default().set_env(false), + "Cargo.toml".as_ref(), + &out_dir.join("built.rs"), + ) + .unwrap(); } diff --git a/rust/operator-binary/src/stackable-hdfs-operator.rs b/rust/operator-binary/src/stackable-hdfs-operator.rs index b3a08c69..6c9fe1ab 100644 --- a/rust/operator-binary/src/stackable-hdfs-operator.rs +++ b/rust/operator-binary/src/stackable-hdfs-operator.rs @@ -1,4 +1,4 @@ -use clap::Parser; +use clap::{crate_description, crate_version, Parser}; use stackable_hdfs_crd::constants::APP_NAME; use stackable_hdfs_crd::HdfsCluster; use stackable_hdfs_operator::OPERATOR_NAME; @@ -10,10 +10,11 @@ use stackable_operator::{ mod built_info { // The file has been placed there by the build script. include!(concat!(env!("OUT_DIR"), "/built.rs")); + pub const TARGET_PLATFORM: Option<&str> = option_env!("TARGET"); } #[derive(clap::Parser)] -#[clap(about = built_info::PKG_DESCRIPTION, author = stackable_operator::cli::AUTHOR)] +#[clap(about, author)] struct Opts { #[clap(subcommand)] cmd: Command, @@ -36,10 +37,10 @@ async fn main() -> anyhow::Result<()> { ); stackable_operator::utils::print_startup_string( - built_info::PKG_DESCRIPTION, - built_info::PKG_VERSION, + crate_description!(), + crate_version!(), built_info::GIT_VERSION, - built_info::TARGET, + built_info::TARGET_PLATFORM.unwrap_or("unknown target"), built_info::BUILT_TIME_UTC, built_info::RUSTC_VERSION, );