1+ # Copyright 2018 Shift Cryptosecurity AG
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+
15+
16+ #-----------------------------------------------------------------------------
17+ # Build bitbox lib to use in tests
18+
19+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter -Wno-missing-prototypes -Wno-missing-declarations -Wno-implicit-function-declaration -Wno-bad-function-cast" )
20+
21+ set (DBB-FILTERED-SOURCES
22+ ${DBB-FIRMWARE-SOURCES }
23+ ${DBB-FIRMWARE-UI-SOURCES }
24+ ${FIRMWARE-U2F-SOURCES }
25+ ${DBB-FIRMWARE-USB-SOURCES }
26+ )
27+
28+ set (IGNORE_SOURCES
29+ "src/screen.c"
30+ "src/memory/nvmctrl.c"
31+ "src/memory/smarteeprom.c"
32+ "src/memory.mpu.c"
33+ )
34+ #f9b3ceaa91e
35+
36+ # Exclude some files which depends on the hardware.
37+ foreach (SOURCEFILE ${IGNORE_SOURCES} )
38+ list (FILTER DBB-FILTERED-SOURCES EXCLUDE REGEX ".*/${SOURCEFILE} $" )
39+ endforeach ()
40+
41+ message ("FILTERED SOURCES: ${DBB-FILTERED-SOURCES}" )
42+
43+ add_library (sd-mock-simulator
44+ STATIC
45+ framework /mock_diskio.c
46+ )
47+ target_include_directories (
48+ sd-mock-simulator
49+ PUBLIC
50+ $<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES >
51+ )
52+
53+
54+ # We create a CMake "object library" to track all the compiled sources so that
55+ # they can be reused between a normal library and the manually crafted "merged"
56+ # library.
57+ # See https://cmake.org/cmake/help/v3.10/command/add_library.html#object-libraries
58+ add_library (bitbox_objects-simulator
59+ OBJECT
60+ ${DBB-FILTERED-SOURCES }
61+ ${CTAES-SOURCES }
62+ ${ETHEREUM-SOURCES }
63+ framework /mock_cipher.c
64+ framework /mock_memory.c
65+ framework /mock_screen.c
66+ framework /mock_smarteeprom.c
67+ framework /mock_securechip.c
68+ )
69+
70+ add_library (bitbox-simulator
71+ STATIC
72+ $<TARGET_OBJECTS:bitbox_objects-simulator>
73+ )
74+
75+ # Here we create the "merged" library, which starts with the c-lib created by
76+ # cargo in the "bitbox02-rust-c" project. That project produces a c-lib without
77+ # mangled symbols so that it is easier to use from C. We then extend that
78+ # library with all the object files declared in "bitbox_objects-simulator", all the code
79+ # we want to be able to call from tests.
80+ # By linking to "bitbox_merged-simulator" in the rust unit tests we get access to all our
81+ # code. (That linkage is done by cargo using a "build.rs" script in the
82+ # bitbox02 rust project.)
83+ add_custom_command (
84+ OUTPUT ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} /libbitbox_merged-simulator.a
85+ DEPENDS c-unit-tests_rust_c bitbox_objects-simulator $<TARGET_OBJECTS:bitbox_objects-simulator>
86+ COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_PROPERTY:c-unit-tests_rust_c,IMPORTED_LOCATION > ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} /libbitbox_merged-simulator.a
87+ COMMAND ar q ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} /libbitbox_merged-simulator.a $<TARGET_OBJECTS:bitbox_objects-simulator>
88+ VERBATIM
89+ COMMAND_EXPAND_LISTS
90+ )
91+
92+ add_custom_target (bitbox_merged-simulator DEPENDS ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} /libbitbox_merged-simulator.a)
93+
94+ target_include_directories (
95+ bitbox_objects-simulator
96+ SYSTEM PUBLIC
97+ ${SYSTEMINCLUDES}
98+ ${CMAKE_SOURCE_DIR} /external
99+ ${CMAKE_SOURCE_DIR} /external/ctaes
100+ ${CMAKE_SOURCE_DIR} /external/fatfs/source
101+ $<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES >
102+ $<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES >
103+ $<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES >
104+ )
105+ target_include_directories (
106+ bitbox_objects-simulator
107+ PUBLIC
108+ ${INCLUDES}
109+ ${CMAKE_CURRENT_SOURCE_DIR} /framework /includes
110+ ${CMAKE_CURRENT_SOURCE_DIR}
111+ ${CMAKE_BINARY_DIR} /src
112+ )
113+
114+ target_include_directories (
115+ bitbox-simulator
116+ SYSTEM PUBLIC
117+ ${SYSTEMINCLUDES}
118+ ${CMAKE_SOURCE_DIR} /external
119+ ${CMAKE_SOURCE_DIR} /external/ctaes
120+ ${CMAKE_SOURCE_DIR} /external/fatfs/source
121+ $<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES >
122+ $<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES >
123+ $<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES >
124+ )
125+
126+ target_include_directories (
127+ bitbox-simulator
128+ PUBLIC
129+ ${INCLUDES}
130+ ${CMAKE_CURRENT_SOURCE_DIR} /framework /includes
131+ ${CMAKE_CURRENT_SOURCE_DIR}
132+ ${CMAKE_BINARY_DIR} /src
133+ )
134+
135+ add_dependencies (bitbox_objects-simulator
136+ rust-cbindgen
137+ wallycore
138+ secp256k1
139+ ctaes
140+ fatfs
141+ sd-mock-simulator
142+ )
143+ target_link_libraries (bitbox-simulator PRIVATE ${LIBBITBOX02_RUST} "-lm" )
144+
145+ target_compile_definitions (bitbox_objects-simulator PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1" )
146+ target_compile_definitions (bitbox_objects-simulator PUBLIC TESTING)
147+
148+ # Since wallycore is an external projects we need to specify the dependency
149+ add_dependencies (bitbox_objects-simulator libwally-core)
150+
151+ target_compile_definitions (bitbox-simulator PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1" )
152+ target_compile_definitions (bitbox-simulator PUBLIC TESTING)
153+
154+ target_link_libraries (bitbox-simulator
155+ PUBLIC
156+ secp256k1
157+ PRIVATE
158+ wallycore
159+ ctaes
160+ fatfs
161+ sd-mock-simulator
162+ )
163+
164+ if (SANITIZE_ADDRESS)
165+ target_compile_options (bitbox_objects-simulator PUBLIC "-fsanitize=address" )
166+ target_compile_options (bitbox-simulator PUBLIC "-fsanitize=address" )
167+ endif ()
168+ if (SANTIZE_UNDEFINED)
169+ target_compile_options (bitbox_objects-simulator PUBLIC "-fsanitize=undefined" )
170+ target_compile_options (bitbox-simulator PUBLIC "-fsanitize=undefined" )
171+ endif ()
172+ if (COVERAGE)
173+ target_link_libraries (bitbox-simulator PUBLIC "--coverage" )
174+ target_compile_options (bitbox_objects-simulator PUBLIC "--coverage" )
175+ target_compile_options (bitbox-simulator PUBLIC "--coverage" )
176+ endif ()
177+
178+ #-----------------------------------------------------------------------------
179+ # Simulator
180+
181+ add_executable (simulator simulator.c framework /eh_personality.c)
182+ # asan must be first library in linking order
183+ target_link_libraries (simulator PRIVATE
184+ $<$<BOOL :${SANITIZE_ADDRESS} >:asan>
185+ $<$<BOOL :${SANITIZE_UNDEFINED} >:-fsanitize=undefined>
186+ -Wl,--start -group
187+ c-unit-tests_rust_c
188+ bitbox-simulator
189+ -Wl,--end -group
190+ ""
191+ )
0 commit comments