Skip to content

Commit affe149

Browse files
authored
Merge pull request #1293 from NickeZ/nickez/rust-not-panic
Generate rust bindings in rust build scripts
2 parents bf7b135 + fd91160 commit affe149

File tree

3 files changed

+248
-165
lines changed

3 files changed

+248
-165
lines changed

src/CMakeLists.txt

Lines changed: 6 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@ set(BOOTLOADER-SOURCES
188188
${DRIVER-SOURCES}
189189
)
190190

191-
# rust-bindgen to generate rust declarations from c-headers.
192-
# The compiled binary to avoid recompiling bindgen on every clean build
193-
find_program(BINDGEN bindgen)
194191
# cbindgen is used to generate the c-headers from rust code.
195192
find_program(CBINDGEN cbindgen)
196193
# cargo is the rust build system and dependency manager
@@ -270,156 +267,6 @@ if(NOT CMAKE_CROSSCOMPILING)
270267
list(APPEND RUST_INCLUDES -I${CMAKE_SOURCE_DIR}/test/unit-test/framework/includes)
271268
endif()
272269

273-
if(CMAKE_CROSSCOMPILING)
274-
set(RUST_BINDGEN_FLAGS -D__SAMD51J20A__ --target=thumbv7em-none-eabi -mcpu=cortex-m4 -mthumb -mfloat-abi=soft --sysroot=${CMAKE_SYSROOT}
275-
# APP_ vars active when compiling C code to be used in Rust. It
276-
# is okay to activate all of them here - Rust's 'app-' features
277-
# control usage/compilation in Rust.
278-
-DAPP_U2F=1 # needed to wrap securechip_u2f_counter_set() in Rust.
279-
)
280-
else()
281-
set(RUST_BINDGEN_FLAGS -DTESTING=1)
282-
endif()
283-
284-
add_custom_target(rust-bindgen
285-
# Generate rust bindings
286-
COMMAND
287-
${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/rust
288-
COMMAND
289-
${BINDGEN}
290-
--output ${CMAKE_CURRENT_BINARY_DIR}/rust/bindings.rs.out
291-
--use-core
292-
--with-derive-default
293-
--ctypes-prefix util::c_types
294-
--allowlist-function bip32_derive_xpub
295-
--allowlist-function localtime
296-
--allowlist-function wally_free_string
297-
--allowlist-function mock_memory_factoryreset
298-
--allowlist-function memory_setup
299-
--allowlist-function memory_is_initialized
300-
--allowlist-function memory_set_initialized
301-
--allowlist-function memory_is_seeded
302-
--allowlist-function memory_is_mnemonic_passphrase_enabled
303-
--allowlist-function memory_get_attestation_pubkey_and_certificate
304-
--allowlist-function memory_get_attestation_bootloader_hash
305-
--allowlist-function memory_bootloader_hash
306-
--allowlist-function memory_get_noise_static_private_key
307-
--allowlist-function memory_check_noise_remote_static_pubkey
308-
--allowlist-function memory_add_noise_remote_static_pubkey
309-
--allowlist-function memory_get_device_name
310-
--allowlist-function memory_set_device_name
311-
--allowlist-function memory_set_mnemonic_passphrase_enabled
312-
--allowlist-var MEMORY_MULTISIG_NAME_MAX_LEN
313-
--allowlist-function memory_set_seed_birthdate
314-
--allowlist-function memory_get_seed_birthdate
315-
--allowlist-function memory_multisig_get_by_hash
316-
--allowlist-function memory_multisig_set_by_hash
317-
--allowlist-function smarteeprom_bb02_config
318-
--allowlist-function bitbox02_smarteeprom_init
319-
--rustified-enum memory_result_t
320-
--allowlist-var MEMORY_DEVICE_NAME_MAX_LEN
321-
--allowlist-function securechip_attestation_sign
322-
--allowlist-function securechip_monotonic_increments_remaining
323-
--allowlist-function securechip_u2f_counter_set
324-
--allowlist-function securechip_model
325-
--rustified-enum securechip_model_t
326-
--allowlist-var KEYSTORE_MAX_SEED_LENGTH
327-
--allowlist-function keystore_is_locked
328-
--allowlist-function keystore_unlock
329-
--allowlist-function keystore_unlock_bip39
330-
--allowlist-function keystore_lock
331-
--allowlist-function keystore_create_and_store_seed
332-
--allowlist-function keystore_copy_seed
333-
--allowlist-function keystore_get_bip39_mnemonic
334-
--allowlist-function keystore_get_bip39_word
335-
--allowlist-function keystore_get_ed25519_seed
336-
--allowlist-function keystore_bip85_bip39
337-
--allowlist-function keystore_bip85_ln
338-
--allowlist-function keystore_secp256k1_compressed_to_uncompressed
339-
--allowlist-function keystore_secp256k1_nonce_commit
340-
--allowlist-function keystore_secp256k1_sign
341-
--allowlist-function keystore_secp256k1_schnorr_bip86_sign
342-
--allowlist-function keystore_bip39_mnemonic_to_seed
343-
--allowlist-function keystore_mock_unlocked
344-
--allowlist-var EC_PUBLIC_KEY_UNCOMPRESSED_LEN
345-
--allowlist-var EC_PUBLIC_KEY_LEN
346-
--allowlist-function keystore_encode_xpub_at_keypath
347-
--allowlist-function keystore_encrypt_and_store_seed
348-
--allowlist-var XPUB_ENCODED_LEN
349-
--allowlist-var BIP32_SERIALIZED_LEN
350-
--allowlist-function lock_animation_start
351-
--allowlist-function lock_animation_stop
352-
--allowlist-function delay_us
353-
--rustified-enum keystore_error_t
354-
--rustified-enum keystore_secp256k1_pubkey_format
355-
--allowlist-function keystore_secp256k1_schnorr_bip86_pubkey
356-
--allowlist-function util_format_datetime
357-
--allowlist-type buffer_t
358-
--allowlist-function delay_ms
359-
--allowlist-function UG_PutString
360-
--allowlist-function UG_FontSelect
361-
--allowlist-function UG_ClearBuffer
362-
--allowlist-function UG_SendBuffer
363-
--allowlist-function screen_print_debug
364-
--allowlist-function ui_screen_stack_push
365-
--allowlist-function ui_screen_stack_pop
366-
--allowlist-function ui_screen_stack_pop_all
367-
--allowlist-function screen_saver_disable
368-
--allowlist-function screen_saver_enable
369-
--allowlist-function screen_process
370-
--allowlist-function label_create
371-
--allowlist-function confirm_create
372-
--allowlist-function status_create
373-
--allowlist-function sdcard_create
374-
--allowlist-function menu_create
375-
--allowlist-function trinary_choice_create
376-
--rustified-enum trinary_choice_t
377-
--allowlist-var BASE58_CHECKSUM_LEN
378-
--allowlist-function random_32_bytes_mcu
379-
--allowlist-function random_mock_reset
380-
--allowlist-type component_t
381-
--allowlist-type confirm_params_t
382-
--allowlist-var MAX_LABEL_SIZE
383-
--allowlist-var font_font_a_9X9
384-
--allowlist-var font_font_a_11X10
385-
--allowlist-var font_monogram_5X9
386-
--allowlist-var font_password_11X12
387-
--allowlist-type trinary_input_string_params_t
388-
--allowlist-var INPUT_STRING_MAX_SIZE
389-
--allowlist-function trinary_input_string_create
390-
--allowlist-function trinary_input_string_set_input
391-
--allowlist-function confirm_transaction_address_create
392-
--allowlist-function confirm_transaction_fee_create
393-
--allowlist-function progress_create
394-
--allowlist-function progress_set
395-
--allowlist-function empty_create
396-
--allowlist-function reset_reset
397-
--allowlist-function sd_card_inserted
398-
--allowlist-function sd_format
399-
--allowlist-function sd_list_subdir
400-
--allowlist-function sd_erase_file_in_subdir
401-
--allowlist-function sd_load_bin
402-
--allowlist-function sd_write_bin
403-
--allowlist-var SD_MAX_FILE_SIZE
404-
--allowlist-function sd_free_list
405-
--allowlist-var BIP39_WORDLIST_LEN
406-
--rustified-enum simple_type_t
407-
--rustified-enum multisig_script_type_t
408-
--rustified-enum output_type_t
409-
--allowlist-var MAX_VARINT_SIZE
410-
--allowlist-var MAX_PK_SCRIPT_SIZE
411-
--allowlist-function reboot
412-
--allowlist-function secp256k1_ecdsa_anti_exfil_host_commit
413-
--allowlist-function wally_get_secp_context
414-
--allowlist-function wally_hash160
415-
--allowlist-function wally_sha512
416-
--allowlist-function printf
417-
${CMAKE_CURRENT_SOURCE_DIR}/rust/bitbox02-sys/wrapper.h --
418-
-DPB_NO_PACKED_STRUCTS=1 -DPB_FIELD_16BIT=1 -fshort-enums ${RUST_BINDGEN_FLAGS} ${RUST_INCLUDES}
419-
COMMAND
420-
${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/rust/bindings.rs.out ${CMAKE_CURRENT_BINARY_DIR}/rust/bindings.rs
421-
)
422-
423270

424271
# Test rust crates that contain business logic. Avoid testing crates that depend on hardware.
425272
if(NOT CMAKE_CROSSCOMPILING)
@@ -429,6 +276,7 @@ if(NOT CMAKE_CROSSCOMPILING)
429276
add_custom_target(rust-test
430277
COMMAND
431278
${CMAKE_COMMAND} -E env
279+
CMAKE_SYSROOT=${CMAKE_SYSROOT}
432280
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
433281
FIRMWARE_VERSION_SHORT=${FIRMWARE_VERSION}
434282
# only one test thread because of unsafe concurrent access to `SafeData`, `mock_sd()` and `mock_memory()`. Using mutexes instead leads to mutex poisoning and very messy output in case of a unit test failure.
@@ -443,6 +291,7 @@ if(NOT CMAKE_CROSSCOMPILING)
443291
${CARGO} clean --target-dir ${RUST_BINARY_DIR}
444292
COMMAND
445293
${CMAKE_COMMAND} -E env
294+
CMAKE_SYSROOT=${CMAKE_SYSROOT}
446295
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
447296
FIRMWARE_VERSION_SHORT=${FIRMWARE_VERSION}
448297
${CARGO} clippy
@@ -465,7 +314,7 @@ if(NOT CMAKE_CROSSCOMPILING)
465314
-A clippy::forget_non_drop
466315
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust/
467316
)
468-
add_dependencies(rust-clippy rust-bindgen generate-protobufs)
317+
add_dependencies(rust-clippy generate-protobufs)
469318
endif()
470319

471320
# If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only.
@@ -510,6 +359,7 @@ foreach(type ${RUST_LIBS})
510359
OUTPUT ${lib} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a dummy
511360
COMMAND
512361
${CMAKE_COMMAND} -E env
362+
CMAKE_SYSROOT=${CMAKE_SYSROOT}
513363
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
514364
RUSTFLAGS=${RUSTFLAGS}
515365
FIRMWARE_VERSION_SHORT=${FIRMWARE_VERSION}
@@ -521,7 +371,6 @@ foreach(type ${RUST_LIBS})
521371
# DEPFILES are only supported with the Ninja build tool
522372
#DEPFILE ${RUST_BINARY_DIR}/feature-${type}/${RUST_TARGET_ARCH_DIR}/${RUST_PROFILE}/libbitbox02_rust_c.d
523373
WORKING_DIRECTORY ${LIBBITBOX02_RUST_SOURCE_DIR}
524-
DEPENDS rust-bindgen
525374
COMMENT "Building Rust library lib${type}_rust_c.a"
526375
)
527376
add_custom_target(${type}-rust-target DEPENDS ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a)
@@ -536,17 +385,14 @@ endforeach()
536385
if(CMAKE_CROSSCOMPILING)
537386
add_custom_target(rust-docs
538387
COMMAND
388+
CMAKE_SYSROOT=${CMAKE_SYSROOT}
539389
${CMAKE_COMMAND} -E env
540390
FIRMWARE_VERSION_SHORT=${FIRMWARE_VERSION}
541-
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
542-
SYSROOT=${CMAKE_SYSROOT}
543-
INCLUDES="${RUST_INCLUDES}"
544391
${CARGO} doc --document-private-items --target-dir ${CMAKE_BINARY_DIR}/docs-rust --target thumbv7em-none-eabi
545392
COMMAND
546393
${CMAKE_COMMAND} -E echo "See docs at file://${CMAKE_BINARY_DIR}/docs-rust/thumbv7em-none-eabi/doc/bitbox02_rust/index.html"
547-
WORKING_DIRECTORY ${LIBBITBOX02_RUST_SOURCE_DIR}
394+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust
548395
)
549-
add_dependencies(rust-docs rust-bindgen)
550396

551397
set(STACK_SIZE "0x10000" CACHE STRING "Specify stack size for bootloader/firmware")
552398
set(STACK_SIZE ${STACK_SIZE} PARENT_SCOPE)

0 commit comments

Comments
 (0)