Skip to content

Commit

Permalink
fix Rust compilation flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Mar 14, 2024
1 parent 25eb6d7 commit fccf8d3
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ tests_zemu/yarn.lock
tests_tools/target
fuzz-*.log
/scan-build
app/rust/.cargo/registry
app/rust/.cargo/.package-cache*
29 changes: 26 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,40 @@ 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
COMMAND cargo clean
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
)
Expand Down
21 changes: 11 additions & 10 deletions app/Makefile
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/rust/.cargo/config
Original file line number Diff line number Diff line change
@@ -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"]
114 changes: 114 additions & 0 deletions app/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
authors = ["Zondax AG <[email protected]>"]
name = "rslib"
version = "0.0.1"
edition = "2018"
edition = "2021"
readme = "README.md"
resolver = "2"

[lib]
name = "rslib"
Expand All @@ -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 = []

0 comments on commit fccf8d3

Please sign in to comment.