Skip to content

Commit 94e65f5

Browse files
authored
Merge pull request #1435 from NickeZ/nickez/fix-macos-unit-tests
build: osx support in build builds
2 parents 9048391 + f31c196 commit 94e65f5

File tree

7 files changed

+38
-24
lines changed

7 files changed

+38
-24
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ elseif(CCACHE_PROGRAM)
4444
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
4545
endif()
4646

47+
# This is ignored on platforms other than darwin. By default rust compiles for
48+
# 10.7 which doesn't link for us.
49+
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version")
50+
4751
project(bitbox02 C)
4852

4953
# nosys is set in arm.cmake so that `project(c)` above works. Remove it since it interferes with compile options

cmake/modules/FindCMocka.cmake

Lines changed: 0 additions & 13 deletions
This file was deleted.

external/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ set(LIBWALLY_CFLAGS "\
3232
-D_DEFAULT_SOURCE \
3333
-fno-strict-aliasing \
3434
")
35+
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
36+
string(APPEND LIBWALLY_CFLAGS " -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
37+
endif()
3538

3639
# Hide some warnings
3740
set(LIBWALLY_CFLAGS "${LIBWALLY_CFLAGS} -Wno-cast-qual -Wno-cast-align \

src/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ get_property(SECP256k1_INCLUDES TARGET secp256k1 PROPERTY INTERFACE_INCLUDE_DIRE
285285
foreach(include ${INCLUDES} ${SAMD51A_INCLUDES} ${ASF4_INCLUDES} ${ASF4_MIN_INCLUDES} ${CMSIS_INCLUDES} ${WALLY_INCLUDES} ${SECP256k1_INCLUDES})
286286
list(APPEND RUST_INCLUDES -I${include})
287287
endforeach()
288-
if(NOT CMAKE_CROSSCOMPILING)
288+
if(NOT (CMAKE_CROSSCOMPILING AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
289289
list(APPEND RUST_INCLUDES -I${CMAKE_SOURCE_DIR}/test/unit-test/framework/includes)
290290
endif()
291291

@@ -305,7 +305,10 @@ if(NOT CMAKE_CROSSCOMPILING)
305305
${CARGO} test $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:-v> --all-features --target-dir ${RUST_BINARY_DIR}/all-features ${RUST_CARGO_FLAGS} -- --nocapture --test-threads 1
306306
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust/
307307
)
308-
add_dependencies(rust-test bitbox_merged generate-protobufs)
308+
add_dependencies(rust-test generate-protobufs)
309+
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
310+
add_dependencies(rust-test bitbox_merged)
311+
endif()
309312

310313
add_custom_target(rust-clippy
311314
COMMAND

test/simulator/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ add_executable(simulator simulator.c framework/eh_personality.c)
179179
target_link_libraries(simulator PRIVATE
180180
$<$<BOOL:${SANITIZE_ADDRESS}>:asan>
181181
$<$<BOOL:${SANITIZE_UNDEFINED}>:-fsanitize=undefined>
182-
-Wl,--start-group
182+
$<$<NOT:$<PLATFORM_ID:Darwin>>:-Wl,--start-group>
183183
c-unit-tests_rust_c
184184
bitbox-simulator
185-
-Wl,--end-group
185+
$<$<NOT:$<PLATFORM_ID:Darwin>>:-Wl,--end-group>
186186
""
187187
)
188188

test/unit-test/CMakeLists.txt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# No linker for Mach-O supports the linker argument `--wrap`. Since we use
16+
# that, unit tests will never work on macos. Use linux/arm64 in docker instead.
17+
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
18+
message("No linker for Mach-O supports --wrap, will not generate unit-tests")
19+
return()
20+
endif()
21+
22+
# We use FindPkgConfig instead of FindPackage because it finds libraries in
23+
# both linux and macos
24+
find_package(PkgConfig REQUIRED)
25+
26+
# Unit testing uses CMocka
27+
pkg_check_modules(CMOCKA REQUIRED cmocka)
28+
# u2f tests with hardware uses hidapi-hidraw
29+
pkg_check_modules(HIDAPI REQUIRED hidapi-hidraw)
30+
31+
message("hidapi-hidraw " ${HIDAPI_INCLUDE_DIRS})
32+
1533

1634
#-----------------------------------------------------------------------------
1735
# Build bitbox lib to use in tests
@@ -115,6 +133,7 @@ target_include_directories(
115133
${CMAKE_CURRENT_SOURCE_DIR}/framework/includes
116134
${CMAKE_CURRENT_SOURCE_DIR}
117135
${CMAKE_BINARY_DIR}/src
136+
${CMOCKA_INCLUDE_DIRS}
118137
)
119138

120139
target_include_directories(
@@ -160,6 +179,7 @@ target_compile_definitions(bitbox PUBLIC TESTING _UNIT_TEST_)
160179
target_link_libraries(bitbox
161180
PUBLIC
162181
secp256k1
182+
${CMOCKA_LIBRARIES}
163183
PRIVATE
164184
wallycore
165185
ctaes
@@ -192,6 +212,7 @@ target_include_directories(
192212
u2f-util
193213
SYSTEM PUBLIC
194214
${CMAKE_CURRENT_SOURCE_DIR}
215+
${HIDAPI_INCLUDE_DIRS}
195216
)
196217
target_include_directories(
197218
u2f-util
@@ -202,6 +223,8 @@ target_include_directories(
202223
target_compile_definitions(u2f-util PUBLIC "TESTING" _UNIT_TEST_ PRODUCT_BITBOX_MULTI "APP_U2F=1" "APP_BTC=1" "APP_LTC=1" "APP_ETH=1")
203224
target_compile_definitions(u2f-util PUBLIC "USE_KECCAK")
204225

226+
target_link_libraries(u2f-util PUBLIC ${HIDAPI_LIBRARIES})
227+
205228

206229
#-----------------------------------------------------------------------------
207230
# Tests
@@ -239,8 +262,6 @@ set(TEST_LIST
239262
""
240263
)
241264

242-
find_package(CMocka REQUIRED)
243-
244265
list(LENGTH TEST_LIST TEST_LIST_LEN)
245266
math(EXPR TEST_LIST_LEN ${TEST_LIST_LEN}-1)
246267
foreach(I RANGE 0 ${TEST_LIST_LEN} 2)
@@ -257,7 +278,6 @@ foreach(I RANGE 0 ${TEST_LIST_LEN} 2)
257278
c-unit-tests_rust_c
258279
bitbox
259280
-Wl,--end-group
260-
${CMOCKA_LIBRARIES}
261281
${TEST_LINK_ARGS}
262282
)
263283
if(NOT ${TEST_NAME} STREQUAL "simulator")
@@ -286,7 +306,6 @@ foreach(TEST_NAME ${U2F_TESTS})
286306
bitbox
287307
-Wl,--end-group
288308
u2f-util
289-
${CMOCKA_LIBRARIES}
290309
)
291310
target_compile_definitions(${EXE} PRIVATE "TESTING")
292311
add_test(NAME test_${TEST_NAME} COMMAND ${EXE})
@@ -299,8 +318,6 @@ foreach(TEST_NAME ${U2F_TESTS})
299318
$<$<BOOL:${SANITIZE_ADDRESS}>:asan>
300319
$<$<BOOL:${SANITIZE_UNDEFINED}>:-fsanitize=undefined>
301320
u2f-util
302-
hidapi-hidraw
303-
${CMOCKA_LIBRARIES}
304321
)
305322
target_compile_definitions(${EXE} PRIVATE "TESTING" "WITH_HARDWARE")
306323
endforeach()

test/unit-test/u2f/u2f_util_t.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "usb/usb_frame.h"
2121

2222
#ifndef CONTINUOUS_INTEGRATION
23-
#include <hidapi/hidapi.h>
23+
#include <hidapi.h>
2424
#else
2525
typedef void hid_device;
2626
#endif

0 commit comments

Comments
 (0)