From f495a316640c09443ca42d08d4e6d2c2636b992e Mon Sep 17 00:00:00 2001 From: cepetr Date: Thu, 8 Aug 2024 17:18:32 +0200 Subject: [PATCH] refactor(core/build): remove hardcoded build path [no changelog] --- core/Makefile | 2 ++ core/SConscript.bootloader | 3 ++- core/SConscript.bootloader_emu | 3 ++- core/SConscript.firmware | 3 ++- core/SConscript.unix | 3 ++- core/embed/rust/build.rs | 21 +++++++++------------ core/embed/rust/src/protobuf/defs.rs | 17 +---------------- 7 files changed, 20 insertions(+), 32 deletions(-) diff --git a/core/Makefile b/core/Makefile index e6647bf5e..6bea0e20f 100644 --- a/core/Makefile +++ b/core/Makefile @@ -143,6 +143,7 @@ test: ## run unit tests cd tests ; ./run_tests.sh $(TESTOPTS) test_rust: ## run rs unit tests + export BUILD_DIR=$(abspath $(UNIX_BUILD_DIR)) ; \ cd embed/rust ; cargo test $(TESTOPTS) --target=$(RUST_TARGET) \ --no-default-features --features $(MODEL_FEATURE),test \ -- --test-threads=1 --nocapture @@ -212,6 +213,7 @@ pyright: python ../tools/pyright_tool.py clippy: + export BUILD_DIR=$(abspath $(UNIX_BUILD_DIR)) ; \ cd embed/rust ; cargo clippy $(TESTOPTS) --all-features --target=$(RUST_TARGET) ## code generation: diff --git a/core/SConscript.bootloader b/core/SConscript.bootloader index e5226cd31..8a1a0ce0e 100644 --- a/core/SConscript.bootloader +++ b/core/SConscript.bootloader @@ -283,8 +283,9 @@ def cargo_build(): ] bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) + build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) + return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) rust = env.Command( target=RUST_LIBPATH, diff --git a/core/SConscript.bootloader_emu b/core/SConscript.bootloader_emu index 9d0627cbc..c7483132a 100644 --- a/core/SConscript.bootloader_emu +++ b/core/SConscript.bootloader_emu @@ -300,8 +300,9 @@ def cargo_build(): ] bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) + build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} ' + ' '.join(cargo_opts) + return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} ' + ' '.join(cargo_opts) rust = env.Command( target=RUST_LIBPATH, diff --git a/core/SConscript.firmware b/core/SConscript.firmware index b2a6abcc8..1be1a4b5c 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -861,8 +861,9 @@ def cargo_build(): env.get('ENV')['TREZOR_MODEL'] = TREZOR_MODEL bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) + build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) + return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) rust = env.Command( target=RUST_LIBPATH, diff --git a/core/SConscript.unix b/core/SConscript.unix index 4539e800d..7e0897817 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -897,8 +897,9 @@ def cargo_build(): env.get('ENV')['TREZOR_MODEL'] = TREZOR_MODEL bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) + build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}" --target {TARGET}' + return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}" --target {TARGET}' rust = env.Command( target=RUST_LIBPATH, diff --git a/core/embed/rust/build.rs b/core/embed/rust/build.rs index 543cf5046..e1cdaaefb 100644 --- a/core/embed/rust/build.rs +++ b/core/embed/rust/build.rs @@ -14,6 +14,10 @@ fn main() { link_core_objects(); } +fn build_dir() -> String { + env::var("BUILD_DIR").expect("BUILD_DIR not defined") +} + const DEFAULT_BINDGEN_MACROS_COMMON: &[&str] = &[ "-I../unix", "-I../trezorhal/unix", @@ -94,14 +98,7 @@ fn generate_qstr_bindings() { is_global: false, }) // Pass in correct include paths. - .clang_args(&[ - "-I", - if is_firmware() { - "../../build/firmware" - } else { - "../../build/unix" - }, - ]) + .clang_args(&["-I", &build_dir()]) // Customize the standard types. .use_core() .ctypes_prefix("cty") @@ -127,6 +124,8 @@ fn generate_qstr_bindings() { fn prepare_bindings() -> bindgen::Builder { let mut bindings = bindgen::Builder::default(); + let build_dir_include = format!("-I{}", build_dir()); + let mut clang_args: Vec<&str> = Vec::new(); let bindgen_macros_env = env::var("BINDGEN_MACROS").ok(); @@ -142,12 +141,12 @@ fn prepare_bindings() -> bindgen::Builder { bindings = bindings.clang_args(["-DNEW_RENDERING"]); } + clang_args.push(&build_dir_include); + // Pass in correct include paths and defines. if is_firmware() { clang_args.push("-nostdinc"); - clang_args.push("-I../../build/firmware"); - // Append gcc-arm-none-eabi's include paths. let cc_output = Command::new("arm-none-eabi-gcc") .arg("-E") @@ -168,8 +167,6 @@ fn prepare_bindings() -> bindgen::Builder { .map(|s| format!("-I{}", s.trim())); bindings = bindings.clang_args(include_args); - } else { - clang_args.push("-I../../build/unix"); } bindings = bindings.clang_args(&clang_args); diff --git a/core/embed/rust/src/protobuf/defs.rs b/core/embed/rust/src/protobuf/defs.rs index cdae0d22f..acf416568 100644 --- a/core/embed/rust/src/protobuf/defs.rs +++ b/core/embed/rust/src/protobuf/defs.rs @@ -109,24 +109,9 @@ struct NameDef { msg_offset: u16, } -#[cfg(target_arch = "arm")] macro_rules! proto_def_path { ($filename:expr) => { - concat!( - env!("CARGO_MANIFEST_DIR"), - "/../../build/firmware/rust/", - $filename - ) - }; -} -#[cfg(not(target_arch = "arm"))] -macro_rules! proto_def_path { - ($filename:expr) => { - concat!( - env!("CARGO_MANIFEST_DIR"), - "/../../build/unix/rust/", - $filename - ) + concat!(env!("BUILD_DIR"), "/rust/", $filename) }; }