From e9620145e2a31fbd8f7da390ca00acdd37811ecc Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Sat, 31 Aug 2024 11:42:25 +0200 Subject: [PATCH 1/3] rust: Output warning so that it is visible without `-vv` Using `-vv` with cargo (cargo build -vv) does output the stderr/stdout of build scripts. But printing "cargo::warning" will emit the message without the flags. Since rust 1.77, the syntax `cargo::` is preferred over the old `cargo:`. --- src/rust/bitbox02/build.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/rust/bitbox02/build.rs b/src/rust/bitbox02/build.rs index 49aca2f9a..34d244717 100644 --- a/src/rust/bitbox02/build.rs +++ b/src/rust/bitbox02/build.rs @@ -2,27 +2,28 @@ fn main() { #[cfg(feature = "testing")] { if let Ok(cmake_dir) = std::env::var("CMAKE_CURRENT_BINARY_DIR") { - println!("cargo:rustc-link-search={}/../lib", cmake_dir); + println!("cargo::rustc-link-search={}/../lib", cmake_dir); // c and rust code merged :O - println!("cargo:rustc-link-lib=bitbox_merged"); + println!("cargo::rustc-link-lib=bitbox_merged"); println!( - "cargo:rerun-if-changed={}/../lib/libbitbox_merged.a", + "cargo::rerun-if-changed={}/../lib/libbitbox_merged.a", cmake_dir ); + println!("cargo::rerun-if-changed=build.rs"); // external libs - println!("cargo:rustc-link-lib=wallycore"); - println!("cargo:rustc-link-lib=secp256k1"); - println!("cargo:rustc-link-lib=ctaes"); - println!("cargo:rustc-link-lib=fatfs"); - println!("cargo:rustc-link-lib=sd-mock"); + println!("cargo::rustc-link-lib=wallycore"); + println!("cargo::rustc-link-lib=secp256k1"); + println!("cargo::rustc-link-lib=ctaes"); + println!("cargo::rustc-link-lib=fatfs"); + println!("cargo::rustc-link-lib=sd-mock"); // system libs - println!("cargo:rustc-link-lib=cmocka"); + println!("cargo::rustc-link-lib=cmocka"); } else { // This is useful in case project is built by tool that doesn't need to link the final // target, like rust-analyzer and clippy. - eprintln!("Missing env variable CMAKE_CURRENT_BINARY_DIR, linking will fail"); + println!("cargo::warning=Missing env variable CMAKE_CURRENT_BINARY_DIR, linking will fail"); } } } From 8a59a76265063ec907e1d94cbff24d1f8166f4bf Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Mon, 2 Sep 2024 11:47:10 +0200 Subject: [PATCH 2/3] rust: Don't stop compilation when missing env var --- src/rust/bitbox02-rust/src/version.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/rust/bitbox02-rust/src/version.rs b/src/rust/bitbox02-rust/src/version.rs index a177312d1..5e776b498 100644 --- a/src/rust/bitbox02-rust/src/version.rs +++ b/src/rust/bitbox02-rust/src/version.rs @@ -13,13 +13,12 @@ // limitations under the License. /// Firmware version, short format, e.g. "v9.12.0". -// This is evaluated at compile time, if the env var is missing or not set, there will be a compiler error. +// We don't want this to be a hard error during development so that rust tools are happy. pub static FIRMWARE_VERSION_SHORT: &str = { - let version = env!("FIRMWARE_VERSION_SHORT"); - // Need explicit check as the env var could be set to an empty string accidentally (env!() only - // panics if it is not set at all). - if version.is_empty() { - panic!("FIRMWARE_VERSION_SHORT is not set"); + let version = option_env!("FIRMWARE_VERSION_SHORT"); + if let Some(version) = version { + version + } else { + "" } - version }; From 91683a11fff1401e3abdd009b8d052c99bebf09e Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Mon, 2 Sep 2024 11:47:37 +0200 Subject: [PATCH 3/3] rust: Make cargo warn when missing env variable Adding a build script impacts the metadata of the crate which means that it also impacts all the mangled function names and eventually has a minor impact on the final binary. All rust symbols end with a hash of the crate metadata. --- src/rust/bitbox02-rust/build.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/rust/bitbox02-rust/build.rs diff --git a/src/rust/bitbox02-rust/build.rs b/src/rust/bitbox02-rust/build.rs new file mode 100644 index 000000000..d473698d0 --- /dev/null +++ b/src/rust/bitbox02-rust/build.rs @@ -0,0 +1,26 @@ +// Copyright 2024 Shift Crypto AG +// +// 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. + +// Emit a warning if FIRMWARE_VERSION_SHORT isn't set. We don't want this to be a hard error during +// development so that rust tools are happy. +fn main() { + let version = option_env!("FIRMWARE_VERSION_SHORT"); + if let Some(version) = version { + if version.is_empty() { + println!("cargo::warning=FIRMWARE_VERSION_SHORT is empty"); + } + } else { + println!("cargo::warning=FIRMWARE_VERSION_SHORT is not set"); + } +}