From fccf8d318d67bd4f3b74b3b8711162794de5232e Mon Sep 17 00:00:00 2001 From: ftheirs Date: Thu, 14 Mar 2024 17:09:25 -0300 Subject: [PATCH] fix Rust compilation flags --- .gitignore | 2 + CMakeLists.txt | 29 +++++++++-- app/Makefile | 21 ++++---- app/rust/.cargo/config | 13 +++++ app/rust/Cargo.lock | 114 +++++++++++++++++++++++++++++++++++++++++ app/rust/Cargo.toml | 17 ++++-- 6 files changed, 178 insertions(+), 18 deletions(-) create mode 100644 app/rust/.cargo/config create mode 100644 app/rust/Cargo.lock diff --git a/.gitignore b/.gitignore index 6dd1d06..5d3399a 100644 --- a/.gitignore +++ b/.gitignore @@ -76,3 +76,5 @@ tests_zemu/yarn.lock tests_tools/target fuzz-*.log /scan-build +app/rust/.cargo/registry +app/rust/.cargo/.package-cache* diff --git a/CMakeLists.txt b/CMakeLists.txt index 24cfe61..3b89d3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,9 +138,32 @@ target_include_directories(app_lib PUBLIC ) ############################################################## -## Rust library +## Rust library for CPP tests set(RUST_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/app/rust") -set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/aarch64-apple-darwin/release") + +# Determine the Rust target triple based on the host system +if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") + if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64") + set(RUST_TARGET_TRIPLE "aarch64-unknown-linux-gnu") + elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") + set(RUST_TARGET_TRIPLE "x86_64-unknown-linux-gnu") + else() + message(FATAL_ERROR "Unsupported processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}") + endif() +elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") + if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "arm64") + set(RUST_TARGET_TRIPLE "aarch64-apple-darwin") + elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") + set(RUST_TARGET_TRIPLE "x86_64-apple-darwin") + else() + message(FATAL_ERROR "Unsupported processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}") + endif() +else() + message(FATAL_ERROR "Unsupported host system: ${CMAKE_HOST_SYSTEM_NAME}") +endif() + +# Use debug mode for debugging tests +set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/${RUST_TARGET_TRIPLE}/debug") # Custom target for the Rust library add_custom_target(RustLibClean @@ -148,7 +171,7 @@ add_custom_target(RustLibClean WORKING_DIRECTORY ${RUST_LIB_DIR} ) add_custom_target(RustLibBuild - COMMAND cargo build --release --target=aarch64-apple-darwin + COMMAND cargo build --target ${RUST_TARGET_TRIPLE} --features cpp_tests WORKING_DIRECTORY ${RUST_LIB_DIR} DEPENDS RustLibClean ) diff --git a/app/Makefile b/app/Makefile index 9bd4f26..7fa6601 100755 --- a/app/Makefile +++ b/app/Makefile @@ -1,6 +1,6 @@ #******************************************************************************* # Ledger App -# (c) 2018 - 2023 Zondax AG +# (c) 2018 - 2024 Zondax AG # (c) 2017 Ledger # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -70,23 +70,24 @@ ifndef ICONNAME $(error ICONNAME is not set) endif - include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.platform -CFLAGS += -Wvla -APP_CUSTOM_LINK_DEPENDENCIES = rust -LDLIBS += -Lrust/target/thumbv6m-none-eabi/release -lrslib -APP_SOURCE_PATH += $(CURDIR)/rust/include - -# Include blake2s implementation from deps since it's not supported by the SDK -APP_SOURCE_PATH += $(CURDIR)/../deps/blake2/ref/blake2s-ref.c +# Add SDK BLAKE2b DEFINES += HAVE_HASH HAVE_BLAKE2 INCLUDES_PATH += $(BOLOS_SDK)/lib_cxng/src +# Building Rust +LDFLAGS += -z muldefs +LDLIBS += -L$(MY_DIR)rust/target/$(RUST_TARGET)/release -lrslib + +APP_SOURCE_PATH += $(CURDIR)/rust/include +APP_CUSTOM_LINK_DEPENDENCIES = rust +RUST_TARGET:=thumbv6m-none-eabi + .PHONY: rust rust: - cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target thumbv6m-none-eabi --release + cd rust && RUSTC_BOOTSTRAP=1 CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target $(RUST_TARGET) --release # Before linking, we need to be sure rust lib is there bin/app.elf: rust diff --git a/app/rust/.cargo/config b/app/rust/.cargo/config new file mode 100644 index 0000000..90cae2f --- /dev/null +++ b/app/rust/.cargo/config @@ -0,0 +1,13 @@ +[build] + +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +rustflags = [ + "--emit", "asm", + "-C", "relocation-model=ropi", + "-C", "link-arg=-nostartfiles", + "-C", "link-arg=-Tlink.ld", + "-C", "inline-threshold=0" +] +[unstable] +build-std=["core"] +build-std-features=["panic_immediate_abort"] diff --git a/app/rust/Cargo.lock b/app/rust/Cargo.lock new file mode 100644 index 0000000..6858d7f --- /dev/null +++ b/app/rust/Cargo.lock @@ -0,0 +1,114 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "bls12_381" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc6d6292be3a19e6379786dac800f551e5865a5bb51ebbe3064ab80433f403" +dependencies = [ + "ff", + "rand_core", + "subtle", +] + +[[package]] +name = "ff" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" +dependencies = [ + "rand_core", + "subtle", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "group" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +dependencies = [ + "ff", + "rand_core", + "subtle", +] + +[[package]] +name = "jubjub" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8499f7a74008aafbecb2a2e608a3e13e4dd3e84df198b604451efe93f2de6e61" +dependencies = [ + "bitvec", + "bls12_381", + "ff", + "group", + "rand_core", + "subtle", +] + +[[package]] +name = "panic-halt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812" + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rslib" +version = "0.0.1" +dependencies = [ + "jubjub", + "panic-halt", +] + +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] diff --git a/app/rust/Cargo.toml b/app/rust/Cargo.toml index fceab5f..2ce3b04 100644 --- a/app/rust/Cargo.toml +++ b/app/rust/Cargo.toml @@ -2,8 +2,9 @@ authors = ["Zondax AG "] name = "rslib" version = "0.0.1" -edition = "2018" +edition = "2021" readme = "README.md" +resolver = "2" [lib] name = "rslib" @@ -16,13 +17,19 @@ jubjub = { version = "0.10.0", default-features = false } panic-halt = "0.2.0" [profile.release] +lto = false +codegen-units = 1 +debug = false +opt-level = "z" + +[profile.dev] lto = "fat" codegen-units = 1 debug=true opt-level = "s" panic = "abort" -[profile.dev] -panic = "abort" -debug=true - +[features] +default = [] +# use when compiling this crate as a lib for the cpp_tests suite +cpp_tests = []