2021-06-12 10:19:31 +00:00
#[ cfg(feature = " test " ) ]
use std ::ffi ::OsStr ;
2021-03-23 12:14:33 +00:00
use std ::{ env , path ::PathBuf , process ::Command } ;
fn main ( ) {
2024-09-25 09:53:27 +00:00
println! ( " cargo:rustc-env=BUILD_DIR= {} " , build_dir ( ) ) ;
2022-06-08 08:31:23 +00:00
#[ cfg(feature = " micropython " ) ]
2021-03-23 12:14:33 +00:00
generate_qstr_bindings ( ) ;
2022-06-08 08:31:23 +00:00
#[ cfg(feature = " micropython " ) ]
2021-03-23 12:14:33 +00:00
generate_micropython_bindings ( ) ;
2022-03-05 17:50:49 +00:00
generate_trezorhal_bindings ( ) ;
2024-01-22 12:44:02 +00:00
#[ cfg(feature = " crypto " ) ]
generate_crypto_bindings ( ) ;
2021-06-12 10:19:31 +00:00
#[ cfg(feature = " test " ) ]
link_core_objects ( ) ;
2021-03-23 12:14:33 +00:00
}
2024-08-08 15:18:32 +00:00
fn build_dir ( ) -> String {
2024-09-25 09:53:27 +00:00
let build_dir_str = env ::var ( " BUILD_DIR " ) . unwrap_or ( String ::from ( " ../../build/unix " ) ) ;
PathBuf ::from ( build_dir_str )
. canonicalize ( )
. unwrap ( )
. to_str ( )
. unwrap ( )
. to_string ( )
2024-08-08 15:18:32 +00:00
}
2024-05-17 10:12:35 +00:00
const DEFAULT_BINDGEN_MACROS_COMMON : & [ & str ] = & [
2024-11-15 09:07:01 +00:00
" -I../projects/unix " ,
2024-05-17 10:12:35 +00:00
" -I../../build/unix " ,
" -I../../vendor/micropython/ports/unix " ,
" -I../../../crypto " ,
" -I../../../storage " ,
" -I../../vendor/micropython " ,
" -I../../vendor/micropython/lib/uzlib " ,
2024-11-15 09:07:01 +00:00
" -I../rtl/inc " ,
" -I../gfx/inc " ,
" -I../io/button/inc " ,
" -I../io/display/inc " ,
" -I../io/haptic/inc " ,
" -I../io/touch/inc " ,
" -I../io/rgb_led/inc " ,
" -I../io/usb/inc " ,
" -I../sec/entropy/inc " ,
" -I../sys/time/inc " ,
" -I../util/flash/inc " ,
" -I../util/translations/inc " ,
2024-05-17 10:12:35 +00:00
" -I../models " ,
" - DTREZOR_EMULATOR " ,
2024-11-15 09:07:01 +00:00
" -DUSE_BUTTON " ,
" -DUSE_TOUCH " ,
" -DUSE_HAPTIC " ,
" -DUSE_RGB_LED " ,
2024-05-17 10:12:35 +00:00
] ;
#[ cfg(feature = " model_tt " ) ]
const DEFAULT_BINDGEN_MACROS_T2T1 : & [ & str ] = & [
" -DSTM32F427 " ,
" -DTREZOR_MODEL_T " ,
" -DFLASH_BIT_ACCESS=1 " ,
" -DFLASH_BLOCK_WORDS=1 " ,
2024-04-14 19:16:50 +00:00
" -DTREZOR_BOARD= \" T2T1/boards/t2t1-unix.h \" " ,
2024-05-17 10:12:35 +00:00
] ;
#[ cfg(not(feature = " model_tt " )) ]
const DEFAULT_BINDGEN_MACROS_T2T1 : & [ & str ] = & [ ] ;
#[ cfg(feature = " model_tr " ) ]
const DEFAULT_BINDGEN_MACROS_T2B1 : & [ & str ] = & [
" -DSTM32F427 " ,
" -DTREZOR_MODEL_R " ,
" -DFLASH_BIT_ACCESS=1 " ,
" -DFLASH_BLOCK_WORDS=1 " ,
2024-04-14 19:16:50 +00:00
" -DTREZOR_BOARD= \" T2B1/boards/t2b1-unix.h \" " ,
2024-05-17 10:12:35 +00:00
] ;
#[ cfg(not(feature = " model_tr " )) ]
const DEFAULT_BINDGEN_MACROS_T2B1 : & [ & str ] = & [ ] ;
2024-05-31 09:21:11 +00:00
#[ cfg(feature = " model_mercury " ) ]
const DEFAULT_BINDGEN_MACROS_T3T1 : & [ & str ] = & [
" -DSTM32U5 " ,
" -DTREZOR_MODEL_T3T1 " ,
" -DFLASH_BIT_ACCESS=0 " ,
" -DFLASH_BLOCK_WORDS=4 " ,
" -DTREZOR_BOARD= \" T3T1/boards/t3t1-unix.h \" " ,
] ;
#[ cfg(not(feature = " model_mercury " )) ]
const DEFAULT_BINDGEN_MACROS_T3T1 : & [ & str ] = & [ ] ;
2024-05-17 10:12:35 +00:00
fn add_bindgen_macros < ' a > ( clang_args : & mut Vec < & ' a str > , envvar : Option < & ' a str > ) {
let default_macros = DEFAULT_BINDGEN_MACROS_COMMON
. iter ( )
. chain ( DEFAULT_BINDGEN_MACROS_T2T1 )
2024-05-31 09:21:11 +00:00
. chain ( DEFAULT_BINDGEN_MACROS_T2B1 )
. chain ( DEFAULT_BINDGEN_MACROS_T3T1 ) ;
2024-05-17 10:12:35 +00:00
match envvar {
Some ( envvar ) = > clang_args . extend ( envvar . split ( ',' ) ) ,
None = > clang_args . extend ( default_macros ) ,
}
}
2023-12-27 22:29:22 +00:00
2021-03-23 12:14:33 +00:00
/// Generates Rust module that exports QSTR constants used in firmware.
2022-06-08 08:31:23 +00:00
#[ cfg(feature = " micropython " ) ]
2021-03-23 12:14:33 +00:00
fn generate_qstr_bindings ( ) {
let out_path = env ::var ( " OUT_DIR " ) . unwrap ( ) ;
// Tell cargo to invalidate the built crate whenever the header changes.
println! ( " cargo:rerun-if-changed=qstr.h " ) ;
2024-02-06 13:41:20 +00:00
let dest_file = PathBuf ::from ( out_path ) . join ( " qstr.rs " ) ;
2021-03-23 12:14:33 +00:00
bindgen ::Builder ::default ( )
. header ( " qstr.h " )
// Build the Qstr enum as a newtype so we can define method on it.
2024-05-06 10:54:48 +00:00
. default_enum_style ( bindgen ::EnumVariation ::NewType {
is_bitfield : false ,
is_global : false ,
} )
2021-03-23 12:14:33 +00:00
// Pass in correct include paths.
2024-08-08 15:18:32 +00:00
. clang_args ( & [ " -I " , & build_dir ( ) ] )
2021-03-23 12:14:33 +00:00
// Customize the standard types.
. use_core ( )
. ctypes_prefix ( " cty " )
. size_t_is_usize ( true )
// Tell cargo to invalidate the built crate whenever any of the
// included header files change.
. parse_callbacks ( Box ::new ( bindgen ::CargoCallbacks ) )
. generate ( )
. expect ( " Unable to generate Rust QSTR bindings " )
2024-02-06 13:41:20 +00:00
. write_to_file ( & dest_file )
2021-03-23 12:14:33 +00:00
. unwrap ( ) ;
2024-02-06 13:41:20 +00:00
// rewrite the file to change internal representation of the qstr newtype
let qstr_generated = std ::fs ::read_to_string ( & dest_file ) . unwrap ( ) ;
let qstr_modified = qstr_generated . replace (
" pub struct Qstr(pub cty::c_uint); " ,
" pub struct Qstr(pub usize); " ,
) ;
assert_ne! ( qstr_generated , qstr_modified , " Failed to rewrite type of Qstr in qstr.rs file. \n This indicates that the generated file has changed. Please update the rewriting code. " ) ;
std ::fs ::write ( & dest_file , qstr_modified ) . unwrap ( ) ;
2021-03-23 12:14:33 +00:00
}
2022-03-05 17:50:49 +00:00
fn prepare_bindings ( ) -> bindgen ::Builder {
let mut bindings = bindgen ::Builder ::default ( ) ;
2024-08-08 15:18:32 +00:00
let build_dir_include = format! ( " -I {} " , build_dir ( ) ) ;
2024-04-12 20:39:30 +00:00
let mut clang_args : Vec < & str > = Vec ::new ( ) ;
2024-05-17 10:12:35 +00:00
let bindgen_macros_env = env ::var ( " BINDGEN_MACROS " ) . ok ( ) ;
add_bindgen_macros ( & mut clang_args , bindgen_macros_env . as_deref ( ) ) ;
2022-03-05 17:50:49 +00:00
2024-11-13 10:04:53 +00:00
#[ cfg(feature = " framebuffer " ) ]
2024-04-16 13:53:13 +00:00
{
2024-11-13 10:04:53 +00:00
bindings = bindings . clang_args ( & [ " -DFRAMEBUFFER " ] ) ;
2024-04-16 13:53:13 +00:00
}
2024-08-08 15:18:32 +00:00
clang_args . push ( & build_dir_include ) ;
2022-03-05 17:50:49 +00:00
// Pass in correct include paths and defines.
if is_firmware ( ) {
2023-06-27 22:10:46 +00:00
clang_args . push ( " -nostdinc " ) ;
2024-04-12 20:39:30 +00:00
2022-03-05 17:50:49 +00:00
// Append gcc-arm-none-eabi's include paths.
let cc_output = Command ::new ( " arm-none-eabi-gcc " )
. arg ( " -E " )
. arg ( " -Wp,-v " )
. arg ( " - " )
. output ( )
. expect ( " arm-none-eabi-gcc failed to execute " ) ;
if ! cc_output . status . success ( ) {
panic! ( " arm-none-eabi-gcc failed " ) ;
}
let include_paths =
String ::from_utf8 ( cc_output . stderr ) . expect ( " arm-none-eabi-gcc returned invalid output " ) ;
let include_args = include_paths
. lines ( )
. skip_while ( | s | ! s . contains ( " search starts here: " ) )
. take_while ( | s | ! s . contains ( " End of search list. " ) )
. filter ( | s | s . starts_with ( ' ' ) )
. map ( | s | format! ( " -I {} " , s . trim ( ) ) ) ;
bindings = bindings . clang_args ( include_args ) ;
}
2024-04-12 20:39:30 +00:00
bindings = bindings . clang_args ( & clang_args ) ;
2022-03-05 17:50:49 +00:00
bindings
// Customize the standard types.
. use_core ( )
. ctypes_prefix ( " cty " )
. size_t_is_usize ( true )
// Disable the layout tests. They spew out a lot of code-style bindings, and are not too
// relevant for our use-case.
. layout_tests ( false )
// Tell cargo to invalidate the built crate whenever any of the
// included header files change.
. parse_callbacks ( Box ::new ( bindgen ::CargoCallbacks ) )
}
2022-06-08 08:31:23 +00:00
#[ cfg(feature = " micropython " ) ]
2021-03-23 12:14:33 +00:00
fn generate_micropython_bindings ( ) {
let out_path = env ::var ( " OUT_DIR " ) . unwrap ( ) ;
// Tell cargo to invalidate the built crate whenever the header changes.
println! ( " cargo:rerun-if-changed=micropython.h " ) ;
2022-03-05 17:50:49 +00:00
let bindings = prepare_bindings ( )
2021-03-23 12:14:33 +00:00
. header ( " micropython.h " )
// obj
. new_type_alias ( " mp_obj_t " )
. allowlist_type ( " mp_obj_type_t " )
. allowlist_type ( " mp_obj_base_t " )
. allowlist_function ( " mp_obj_new_int " )
. allowlist_function ( " mp_obj_new_int_from_ll " )
. allowlist_function ( " mp_obj_new_int_from_ull " )
. allowlist_function ( " mp_obj_new_int_from_uint " )
. allowlist_function ( " mp_obj_new_bytes " )
. allowlist_function ( " mp_obj_new_str " )
2022-02-19 12:49:04 +00:00
. allowlist_function ( " mp_obj_new_tuple " )
2024-02-06 09:57:12 +00:00
. allowlist_function ( " mp_obj_new_attrtuple " )
2021-03-23 12:14:33 +00:00
. allowlist_function ( " mp_obj_get_int_maybe " )
. allowlist_function ( " mp_obj_is_true " )
. allowlist_function ( " mp_call_function_n_kw " )
. allowlist_function ( " trezor_obj_get_ll_checked " )
2021-09-09 14:53:05 +00:00
. allowlist_function ( " trezor_obj_str_from_rom_text " )
2021-03-23 12:14:33 +00:00
// buffer
. allowlist_function ( " mp_get_buffer " )
. allowlist_var ( " MP_BUFFER_READ " )
. allowlist_var ( " MP_BUFFER_WRITE " )
2022-03-15 11:32:28 +00:00
. allowlist_var ( " mp_type_str " )
2022-10-19 16:06:13 +00:00
. allowlist_var ( " mp_type_bytes " )
. allowlist_var ( " mp_type_bytearray " )
. allowlist_var ( " mp_type_memoryview " )
2021-03-23 12:14:33 +00:00
// dict
. allowlist_type ( " mp_obj_dict_t " )
. allowlist_function ( " mp_obj_new_dict " )
. allowlist_var ( " mp_type_dict " )
// fun
. allowlist_type ( " mp_obj_fun_builtin_fixed_t " )
2023-02-21 11:30:24 +00:00
. allowlist_var ( " mp_type_fun_builtin_0 " )
2021-03-23 12:14:33 +00:00
. allowlist_var ( " mp_type_fun_builtin_1 " )
. allowlist_var ( " mp_type_fun_builtin_2 " )
. allowlist_var ( " mp_type_fun_builtin_3 " )
2021-11-03 17:38:17 +00:00
. allowlist_type ( " mp_obj_fun_builtin_var_t " )
. allowlist_var ( " mp_type_fun_builtin_var " )
2021-03-23 12:14:33 +00:00
// gc
. allowlist_function ( " gc_alloc " )
2024-06-27 19:07:37 +00:00
. allowlist_function ( " gc_free " )
2024-05-29 08:47:27 +00:00
. allowlist_var ( " GC_ALLOC_FLAG_HAS_FINALISER " )
2021-03-23 12:14:33 +00:00
// iter
. allowlist_type ( " mp_obj_iter_buf_t " )
. allowlist_function ( " mp_getiter " )
. allowlist_function ( " mp_iternext " )
// list
. allowlist_type ( " mp_obj_list_t " )
. allowlist_function ( " mp_obj_new_list " )
. allowlist_function ( " mp_obj_list_append " )
2022-11-21 15:08:09 +00:00
. allowlist_function ( " mp_obj_list_get " )
2022-04-20 14:52:10 +00:00
. allowlist_function ( " mp_obj_list_set_len " )
2021-03-23 12:14:33 +00:00
. allowlist_var ( " mp_type_list " )
// map
. allowlist_type ( " mp_map_elem_t " )
. allowlist_function ( " mp_map_init " )
. allowlist_function ( " mp_map_init_fixed_table " )
. allowlist_function ( " mp_map_lookup " )
2021-09-09 14:53:05 +00:00
// exceptions
. allowlist_function ( " nlr_jump " )
. allowlist_function ( " mp_obj_new_exception " )
. allowlist_function ( " mp_obj_new_exception_args " )
2021-06-22 17:47:03 +00:00
. allowlist_function ( " trezor_obj_call_protected " )
2021-09-09 14:53:05 +00:00
. allowlist_var ( " mp_type_AttributeError " )
2024-01-15 09:44:25 +00:00
. allowlist_var ( " mp_type_EOFError " )
2022-11-22 11:25:42 +00:00
. allowlist_var ( " mp_type_IndexError " )
2021-09-09 14:53:05 +00:00
. allowlist_var ( " mp_type_KeyError " )
. allowlist_var ( " mp_type_MemoryError " )
. allowlist_var ( " mp_type_OverflowError " )
. allowlist_var ( " mp_type_ValueError " )
. allowlist_var ( " mp_type_TypeError " )
2021-10-24 09:29:44 +00:00
// time
. allowlist_function ( " mp_hal_ticks_ms " )
2021-11-26 18:40:35 +00:00
. allowlist_function ( " mp_hal_delay_ms " )
2022-07-20 09:18:37 +00:00
// debug
. allowlist_function ( " mp_print_strn " )
. allowlist_var ( " mp_plat_print " )
2021-03-23 12:14:33 +00:00
// typ
2022-01-26 18:04:02 +00:00
. allowlist_var ( " mp_type_type " )
// module
. allowlist_type ( " mp_obj_module_t " )
2022-03-05 17:50:49 +00:00
. allowlist_var ( " mp_type_module " )
2023-06-20 09:51:40 +00:00
// qstr
. allowlist_function ( " qstr_data " )
2024-01-17 14:40:19 +00:00
// tuple
. allowlist_type ( " mp_obj_tuple_t " )
2022-03-05 17:50:49 +00:00
// `ffi::mp_map_t` type is not allowed to be `Clone` or `Copy` because we tie it
// to the data lifetimes with the `MapRef` type, see `src/micropython/map.rs`.
// TODO: We should disable `Clone` and `Copy` for all types and only allow-list
// the specific cases we require.
. no_copy ( " _mp_map_t " ) ;
2021-03-23 12:14:33 +00:00
2022-03-05 17:50:49 +00:00
// Write the bindings to a file in the OUR_DIR.
bindings
. generate ( )
. expect ( " Unable to generate bindings " )
. write_to_file ( PathBuf ::from ( out_path ) . join ( " micropython.rs " ) )
. unwrap ( ) ;
}
2021-03-23 12:14:33 +00:00
2022-03-05 17:50:49 +00:00
fn generate_trezorhal_bindings ( ) {
let out_path = env ::var ( " OUT_DIR " ) . unwrap ( ) ;
2021-03-23 12:14:33 +00:00
2022-03-05 17:50:49 +00:00
// Tell cargo to invalidate the built crate whenever the header changes.
println! ( " cargo:rerun-if-changed=trezorhal.h " ) ;
2021-03-23 12:14:33 +00:00
2022-03-05 17:50:49 +00:00
let bindings = prepare_bindings ( )
. header ( " trezorhal.h " )
2023-09-29 10:42:39 +00:00
// model
. allowlist_var ( " MODEL_INTERNAL_NAME " )
. allowlist_var ( " MODEL_FULL_NAME " )
2024-08-08 15:12:57 +00:00
// entropy
. allowlist_var ( " HW_ENTROPY_LEN " )
. allowlist_function ( " entropy_get " )
2022-03-05 17:50:49 +00:00
// secbool
. allowlist_type ( " secbool " )
. must_use_type ( " secbool " )
. allowlist_var ( " sectrue " )
. allowlist_var ( " secfalse " )
2022-04-01 09:05:19 +00:00
// flash
. allowlist_function ( " flash_init " )
2022-03-05 17:50:49 +00:00
// storage
. allowlist_var ( " EXTERNAL_SALT_SIZE " )
. allowlist_function ( " storage_init " )
. allowlist_function ( " storage_wipe " )
. allowlist_function ( " storage_is_unlocked " )
. allowlist_function ( " storage_lock " )
. allowlist_function ( " storage_unlock " )
. allowlist_function ( " storage_has_pin " )
. allowlist_function ( " storage_get_pin_rem " )
. allowlist_function ( " storage_change_pin " )
2022-04-01 09:05:19 +00:00
. allowlist_function ( " storage_ensure_not_wipe_code " )
2022-03-05 17:50:49 +00:00
. allowlist_function ( " storage_has " )
. allowlist_function ( " storage_get " )
. allowlist_function ( " storage_set " )
. allowlist_function ( " storage_delete " )
. allowlist_function ( " storage_set_counter " )
. allowlist_function ( " storage_next_counter " )
2023-08-11 15:57:32 +00:00
. allowlist_function ( " translations_read " )
. allowlist_function ( " translations_write " )
. allowlist_function ( " translations_erase " )
. allowlist_function ( " translations_area_bytesize " )
2022-03-05 17:50:49 +00:00
// display
. allowlist_function ( " display_refresh " )
2024-11-13 10:04:53 +00:00
. allowlist_function ( " display_set_backlight " )
. allowlist_function ( " display_get_backlight " )
. allowlist_function ( " display_wait_for_sync " )
2023-04-06 21:19:39 +00:00
. allowlist_var ( " DISPLAY_RESX " )
. allowlist_var ( " DISPLAY_RESY " )
2024-04-16 13:53:13 +00:00
. allowlist_type ( " display_fb_info_t " )
. allowlist_function ( " display_get_frame_buffer " )
. allowlist_function ( " display_fill " )
. allowlist_function ( " display_copy_rgb565 " )
// gfx_bitblt
. allowlist_type ( " gfx_bitblt_t " )
. allowlist_function ( " gfx_rgb565_fill " )
. allowlist_function ( " gfx_rgb565_copy_mono4 " )
. allowlist_function ( " gfx_rgb565_copy_rgb565 " )
. allowlist_function ( " gfx_rgb565_blend_mono4 " )
2024-05-11 09:10:26 +00:00
. allowlist_function ( " gfx_rgb565_blend_mono8 " )
2024-04-16 13:53:13 +00:00
. allowlist_function ( " gfx_rgba8888_fill " )
. allowlist_function ( " gfx_rgba8888_copy_mono4 " )
. allowlist_function ( " gfx_rgba8888_copy_rgb565 " )
. allowlist_function ( " gfx_rgba8888_copy_rgba8888 " )
. allowlist_function ( " gfx_rgba8888_blend_mono4 " )
2024-05-11 09:10:26 +00:00
. allowlist_function ( " gfx_rgba8888_blend_mono8 " )
2024-04-16 13:53:13 +00:00
. allowlist_function ( " gfx_mono8_fill " )
. allowlist_function ( " gfx_mono8_copy_mono1p " )
. allowlist_function ( " gfx_mono8_copy_mono4 " )
. allowlist_function ( " gfx_mono8_blend_mono1p " )
. allowlist_function ( " gfx_mono8_blend_mono4 " )
2024-11-15 09:07:01 +00:00
. allowlist_function ( " gfx_bitblt_wait " )
2022-08-18 13:12:02 +00:00
// fonts
. allowlist_function ( " font_height " )
2022-08-16 14:51:10 +00:00
. allowlist_function ( " font_max_height " )
. allowlist_function ( " font_baseline " )
2022-08-18 13:12:02 +00:00
. allowlist_function ( " font_get_glyph " )
2024-01-17 14:29:49 +00:00
. allowlist_function ( " font_text_width " )
2022-06-14 11:29:29 +00:00
// uzlib
. allowlist_function ( " uzlib_uncompress_init " )
. allowlist_function ( " uzlib_uncompress " )
2022-03-05 17:50:49 +00:00
// bip39
. allowlist_function ( " mnemonic_word_completion_mask " )
. allowlist_var ( " BIP39_WORDLIST_ENGLISH " )
. allowlist_var ( " BIP39_WORD_COUNT " )
// slip39
. allowlist_function ( " slip39_word_completion_mask " )
. allowlist_function ( " button_sequence_to_word " )
2023-05-04 12:14:58 +00:00
. allowlist_var ( " SLIP39_WORDLIST " )
. allowlist_var ( " SLIP39_WORD_COUNT " )
2022-03-05 17:50:49 +00:00
// random
2022-06-08 08:31:23 +00:00
. allowlist_function ( " random_uniform " )
2022-05-31 07:31:32 +00:00
// rgb led
. allowlist_function ( " rgb_led_set_color " )
2024-07-12 09:35:49 +00:00
// systick
. allowlist_function ( " systick_delay_ms " )
. allowlist_function ( " systick_ms " )
2024-01-17 14:29:49 +00:00
// toif
. allowlist_type ( " toif_format_t " )
2022-10-13 11:44:31 +00:00
//usb
2022-05-19 08:26:00 +00:00
. allowlist_function ( " usb_configured " )
// touch
2024-06-03 18:01:49 +00:00
. allowlist_function ( " touch_get_event " )
2022-05-19 08:26:00 +00:00
// button
2024-10-01 08:46:07 +00:00
. allowlist_type ( " button_t " )
2024-11-06 10:22:24 +00:00
. allowlist_function ( " button_get_event " )
2023-08-24 09:30:29 +00:00
// haptic
. allowlist_type ( " haptic_effect_t " )
2024-04-29 08:37:00 +00:00
. allowlist_function ( " haptic_play " )
2024-05-02 07:29:05 +00:00
. allowlist_function ( " haptic_play_custom " ) ;
2022-05-19 08:26:00 +00:00
2022-03-05 17:50:49 +00:00
// Write the bindings to a file in the OUR_DIR.
2021-03-23 12:14:33 +00:00
bindings
. generate ( )
2022-03-05 17:50:49 +00:00
. expect ( " Unable to generate bindings " )
. write_to_file ( PathBuf ::from ( out_path ) . join ( " trezorhal.rs " ) )
2021-03-23 12:14:33 +00:00
. unwrap ( ) ;
}
2021-05-19 10:07:58 +00:00
2024-01-22 12:44:02 +00:00
fn generate_crypto_bindings ( ) {
let out_path = env ::var ( " OUT_DIR " ) . unwrap ( ) ;
// Tell cargo to invalidate the built crate whenever the header changes.
println! ( " cargo:rerun-if-changed=crypto.h " ) ;
let bindings = prepare_bindings ( )
. header ( " crypto.h " )
// ed25519
. allowlist_type ( " ed25519_signature " )
. allowlist_type ( " ed25519_public_key " )
. allowlist_function ( " ed25519_cosi_combine_publickeys " )
// incorrect signature from bindgen, see crypto::ed25519:ffi_override
//.allowlist_function("ed25519_sign_open")
// sha256
. allowlist_var ( " SHA256_DIGEST_LENGTH " )
. allowlist_type ( " SHA256_CTX " )
. no_copy ( " SHA256_CTX " )
. allowlist_function ( " sha256_Init " )
. allowlist_function ( " sha256_Update " )
. allowlist_function ( " sha256_Final " ) ;
// Write the bindings to a file in the OUR_DIR.
bindings
. clang_arg ( " -fgnuc-version=0 " ) // avoid weirdness with ed25519.h CONST definition
. generate ( )
. expect ( " Unable to generate bindings " )
. write_to_file ( PathBuf ::from ( out_path ) . join ( " crypto.rs " ) )
. unwrap ( ) ;
}
2021-05-19 10:07:58 +00:00
fn is_firmware ( ) -> bool {
let target = env ::var ( " TARGET " ) . unwrap ( ) ;
2023-06-28 08:51:30 +00:00
target . starts_with ( " thumbv7 " ) | | target . starts_with ( " thumbv8 " )
2021-05-19 10:07:58 +00:00
}
2021-06-12 10:19:31 +00:00
#[ cfg(feature = " test " ) ]
fn link_core_objects ( ) {
let crate_path = env ::var ( " CARGO_MANIFEST_DIR " ) . unwrap ( ) ;
let build_path = format! ( " {} /../../build/unix " , crate_path ) ;
2022-01-26 16:49:48 +00:00
// List of object filenames to ignore in the `embed` directory
2021-06-12 10:19:31 +00:00
let embed_blocklist = [ OsStr ::new ( " main_main.o " ) ] ;
2021-06-15 07:54:20 +00:00
// Collect all objects that the `core` library uses, and link it in. We have to
// make sure to avoid the object with the `_main` symbol, so we don't get any
// duplicates.
2021-06-12 10:19:31 +00:00
let mut cc = cc ::Build ::new ( ) ;
for obj in glob ::glob ( & format! ( " {} /embed/**/*.o " , build_path ) ) . unwrap ( ) {
let obj = obj . unwrap ( ) ;
if embed_blocklist . contains ( & obj . file_name ( ) . unwrap ( ) ) {
// Ignore.
} else {
cc . object ( obj ) ;
}
}
2021-10-22 18:05:21 +00:00
2021-06-12 10:19:31 +00:00
for obj in glob ::glob ( & format! ( " {} /vendor/**/*.o " , build_path ) ) . unwrap ( ) {
2021-10-22 18:05:21 +00:00
let obj = obj . unwrap ( ) ;
2022-01-26 16:49:48 +00:00
cc . object ( obj ) ;
2021-06-12 10:19:31 +00:00
}
2022-04-27 09:57:45 +00:00
// Add frozen modules, if present.
for obj in glob ::glob ( & format! ( " {} /*.o " , build_path ) ) . unwrap ( ) {
cc . object ( obj . unwrap ( ) ) ;
}
2021-06-15 07:54:20 +00:00
// Compile all the objects into a static library and link it in automatically.
2021-06-12 10:19:31 +00:00
cc . compile ( " core_lib " ) ;
println! ( " cargo:rustc-link-lib=SDL2 " ) ;
println! ( " cargo:rustc-link-lib=SDL2_image " ) ;
}