Skip to content

Commit

Permalink
cmake: Deduce PRODUCT_ macros from PLATFORM_ and EDITION_.
Browse files Browse the repository at this point in the history
Instead of #defininig all the possible PRODUCT_ macros, it
makes more sense to enforce the definition of the (limited)
PLATFORM_ and EDITION_ macros, and extract the relevant definitions
of PRODUCT_* from there. This makes it easier to enforce that the macros
are defined consistently, and allows further separation of code that is
dependent on the platform it will run on, but not on the edition (e.g.
the touch code depends on the PLATFORM_*, but it doesn't matter whether
it will run on a -multi or on a -btconly device).
  • Loading branch information
conte91 committed Dec 17, 2019
1 parent a5a1ae6 commit 77df799
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 67 deletions.
39 changes: 19 additions & 20 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ set(DBB-FIRMWARE-UI-SOURCES
${CMAKE_SOURCE_DIR}/src/ui/components/show_logo.c
${CMAKE_SOURCE_DIR}/src/ui/components/waiting.c
${CMAKE_SOURCE_DIR}/src/ui/screen_process.c
${CMAKE_SOURCE_DIR}/src/ui/ugui/ugui.c
${CMAKE_SOURCE_DIR}/src/touch/gestures.c
)
set(DBB-FIRMWARE-UI-SOURCES ${DBB-FIRMWARE-UI-SOURCES} PARENT_SCOPE)
Expand Down Expand Up @@ -133,7 +132,6 @@ set(DBB-BOOTLOADER-SOURCES
${CMAKE_SOURCE_DIR}/src/bootloader/mpu_regions.c
${CMAKE_SOURCE_DIR}/src/queue.c
${CMAKE_SOURCE_DIR}/src/usb/usb_processing.c
${CMAKE_SOURCE_DIR}/src/ui/ugui/ugui.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_9X9.c
${CMAKE_SOURCE_DIR}/src/hardfault.c
${CMAKE_SOURCE_DIR}/src/ui/components/ui_images.c
Expand All @@ -149,6 +147,7 @@ set(DRIVER-SOURCES
${CMAKE_SOURCE_DIR}/src/platform/platform_init.c
${CMAKE_SOURCE_DIR}/src/qtouch/qtouch.c
${CMAKE_SOURCE_DIR}/src/ui/oled/oled.c
${CMAKE_SOURCE_DIR}/src/ui/ugui/ugui.c
)
set(DRIVER-SOURCES ${DRIVER-SOURCES} PARENT_SCOPE)

Expand Down Expand Up @@ -520,25 +519,25 @@ if(CMAKE_CROSSCOMPILING)
target_sources(${bootloader}.elf PRIVATE ${BITBOXBASE-ONLY-SOURCES})
endforeach(bootloader)

target_compile_definitions(bootloader.elf PRIVATE PRODUCT_BITBOX_MULTI)
target_compile_definitions(bootloader-development.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE)
target_compile_definitions(bootloader-semihosting.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE SEMIHOSTING)
target_compile_definitions(bootloader-development-locked.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE BOOTLOADER_PRODUCTION)
target_compile_definitions(bootloader.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1")
target_compile_definitions(bootloader-development.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" BOOTLOADER_DEVDEVICE)
target_compile_definitions(bootloader-semihosting.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" BOOTLOADER_DEVDEVICE SEMIHOSTING)
target_compile_definitions(bootloader-development-locked.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" BOOTLOADER_DEVDEVICE BOOTLOADER_PRODUCTION)
set_property(TARGET bootloader-development-locked.elf PROPERTY EXCLUDE_FROM_ALL ON)


target_compile_definitions(bootloader-production.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_PRODUCTION)
target_compile_definitions(bootloader-production.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" BOOTLOADER_PRODUCTION)
set_property(TARGET bootloader-production.elf PROPERTY EXCLUDE_FROM_ALL ON)

target_compile_definitions(bootloader-btc.elf PRIVATE PRODUCT_BITBOX_BTCONLY)
target_compile_definitions(bootloader-btc-development.elf PRIVATE PRODUCT_BITBOX_BTCONLY BOOTLOADER_DEVDEVICE)
target_compile_definitions(bootloader-btc-production.elf PRIVATE PRODUCT_BITBOX_BTCONLY BOOTLOADER_PRODUCTION)
target_compile_definitions(bootloader-btc.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_BTCONLY=1")
target_compile_definitions(bootloader-btc-development.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_BTCONLY=1" BOOTLOADER_DEVDEVICE)
target_compile_definitions(bootloader-btc-production.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_BTCONLY=1" BOOTLOADER_PRODUCTION)
set_property(TARGET bootloader-btc-production.elf PROPERTY EXCLUDE_FROM_ALL ON)

target_compile_definitions(bootloader-bitboxbase.elf PRIVATE PRODUCT_BITBOX_BASE)
target_compile_definitions(bootloader-bitboxbase-development.elf PRIVATE BOOTLOADER_DEVDEVICE PRODUCT_BITBOX_BASE)
target_compile_definitions(bootloader-bitboxbase-semihosting.elf PRIVATE BOOTLOADER_DEVDEVICE PRODUCT_BITBOX_BASE SEMIHOSTING)
target_compile_definitions(bootloader-bitboxbase-production.elf PRIVATE BOOTLOADER_PRODUCTION PRODUCT_BITBOX_BASE)
target_compile_definitions(bootloader-bitboxbase.elf PRIVATE "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1")
target_compile_definitions(bootloader-bitboxbase-development.elf PRIVATE BOOTLOADER_DEVDEVICE "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1")
target_compile_definitions(bootloader-bitboxbase-semihosting.elf PRIVATE BOOTLOADER_DEVDEVICE "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1" SEMIHOSTING)
target_compile_definitions(bootloader-bitboxbase-production.elf PRIVATE BOOTLOADER_PRODUCTION "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1")
set_property(TARGET bootloader-bitboxbase-production.elf PROPERTY EXCLUDE_FROM_ALL ON)

set(FIRMWARES
Expand Down Expand Up @@ -609,7 +608,7 @@ if(CMAKE_CROSSCOMPILING)
endforeach(firmware)

target_sources(firmware.elf PRIVATE firmware.c)
target_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_ETH=1" "APP_U2F=1")
target_compile_definitions(firmware.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" "APP_ETH=1" "APP_U2F=1")
target_sources(firmware.elf PRIVATE ${FIRMWARE-U2F-SOURCES})
target_link_libraries(firmware.elf PRIVATE bitbox02-platform)
target_link_libraries(firmware.elf PRIVATE app_btc-multi)
Expand All @@ -620,21 +619,21 @@ if(CMAKE_CROSSCOMPILING)
target_link_libraries(firmware-semihosting.elf PRIVATE app_btc-multi)

# Select an implementation of the system calls that can communicate with the debugger
target_compile_definitions(firmware-semihosting.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_ETH=1" "APP_U2F=1")
target_compile_definitions(firmware-semihosting.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" "APP_ETH=1" "APP_U2F=1")
target_compile_definitions(firmware-semihosting.elf PRIVATE SEMIHOSTING)

target_sources(firmware-btc.elf PRIVATE firmware.c)
target_compile_definitions(firmware-btc.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_ETH=0" "APP_U2F=0")
target_compile_definitions(firmware-btc.elf PRIVATE "PLATFORM_BITBOX02=1" "EDITION_BTCONLY=1" "APP_ETH=0" "APP_U2F=0")
target_link_libraries(firmware-btc.elf PRIVATE bitbox02-platform)
target_link_libraries(firmware-btc.elf PRIVATE app_btc-btc)

target_sources(firmware-bitboxbase.elf PRIVATE bitboxbase/bitboxbase.c)
target_compile_definitions(firmware-bitboxbase.elf PRIVATE PRODUCT_BITBOX_BASE "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_compile_definitions(firmware-bitboxbase.elf PRIVATE "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1" "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_sources(firmware-bitboxbase.elf PRIVATE ${BITBOXBASE-FIRMWARE-SOURCES})
target_link_libraries(firmware-bitboxbase.elf PRIVATE bitboxbase-platform)

target_sources(firmware-bitboxbase-semihosting.elf PRIVATE bitboxbase/bitboxbase.c)
target_compile_definitions(firmware-bitboxbase-semihosting.elf PRIVATE PRODUCT_BITBOX_BASE SEMIHOSTING "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_compile_definitions(firmware-bitboxbase-semihosting.elf PRIVATE "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1" SEMIHOSTING "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_sources(firmware-bitboxbase-semihosting.elf PRIVATE ${BITBOXBASE-FIRMWARE-SOURCES})
target_link_libraries(firmware-bitboxbase-semihosting.elf PRIVATE bitboxbase-platform)

Expand All @@ -646,7 +645,7 @@ if(CMAKE_CROSSCOMPILING)
target_link_libraries(factory-setup.elf PRIVATE bitbox02-platform)

target_sources(factory-setup-bitboxbase.elf PRIVATE factorysetup.c)
target_compile_definitions(factory-setup-bitboxbase.elf PRIVATE PRODUCT_BITBOXBASE_FACTORYSETUP "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_compile_definitions(factory-setup-bitboxbase.elf PRIVATE "PLATFORM_BITBOXBASE=1" "EDITION_FACTORYSETUP=1" "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_link_libraries(factory-setup-bitboxbase.elf PRIVATE bitboxbase-platform)

foreach(name ${BOOTLOADERS} ${FIRMWARES})
Expand Down
76 changes: 59 additions & 17 deletions src/platform/platform_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,109 @@
#ifndef _PLATFORM_CONFIG_H
#define _PLATFORM_CONFIG_H

// Force the PRODUCT_ defines to be 0 or 1, so they can be used safely without risk of typos.
#if !defined(PLATFORM_BITBOX02)
#define PLATFORM_BITBOX02 0
#elif PLATFORM_BITBOX02 != 0 && PLATFORM_BITBOX02 != 1
#error "Invalid value for PLATFORM_BITBOX02."
#endif

#if !defined(PLATFORM_BITBOXBASE)
#define PLATFORM_BITBOXBASE 0
#elif PLATFORM_BITBOXBASE != 0 && PLATFORM_BITBOXBASE != 1
#error "Invalid value for PLATFORM_BITBOXBASE."
#endif

#if !defined(EDITION_STANDARD)
#define EDITION_STANDARD 0
#elif EDITION_STANDARD != 0 && EDITION_STANDARD != 1
#error "Invalid value for EDITION_STANDARD."
#endif
#if !defined(EDITION_BTCONLY)
#define EDITION_BTCONLY 0
#elif EDITION_BTCONLY != 0 && EDITION_BTCONLY != 1
#error "Invalid value for EDITION_BTCONLY."
#endif
#if !defined(EDITION_FACTORYSETUP)
#define EDITION_FACTORYSETUP 0
#elif EDITION_FACTORYSETUP != 0 && EDITION_FACTORYSETUP != 1
#error "Invalid value for EDITION_FACTORYSETUP."
#endif

#if PLATFORM_BITBOX02 == 1

#if EDITION_STANDARD == 1
#define PRODUCT_BITBOX_MULTI 1
#elif EDITION_BTCONLY == 1
#define PRODUCT_BITBOX_BTCONLY 1
#elif EDITION_FACTORYSETUP == 1
#define PRODUCT_BITBOX02_FACTORYSETUP 1
#else
#error "Invalid EDITION value."
#endif

#elif PLATFORM_BITBOXBASE == 1

#if EDITION_STANDARD == 1
#define PRODUCT_BITBOX_BASE 1
#elif EDITION_FACTORYSETUP == 1
#define PRODUCT_BITBOXBASE_FACTORYSETUP 1
#else
#error "Invalid EDITION value."
#endif

#else
#error "Invalid PRODUCT value."
#endif

#if !defined(PRODUCT_BITBOX_MULTI)
#define PRODUCT_BITBOX_MULTI 0
#elif PRODUCT_BITBOX_MULTI != 1
#error "invalid product value"
#error "invalid PRODUCT_BITBOX_MULTI value"
#endif

#if !defined(PRODUCT_BITBOX_BTCONLY)
#define PRODUCT_BITBOX_BTCONLY 0
#elif PRODUCT_BITBOX_BTCONLY != 1
#error "invalid product value"
#error "invalid PRODUCT_BITBOX_BTCONLY value"
#endif

#if !defined(PRODUCT_BITBOX_BASE)
#define PRODUCT_BITBOX_BASE 0
#elif PRODUCT_BITBOX_BASE != 1
#error "invalid product value"
#error "invalid PRODUCT_BITBOX_BASE value"
#endif

#if !defined(PRODUCT_BITBOX02_FACTORYSETUP)
#define PRODUCT_BITBOX02_FACTORYSETUP 0
#elif PRODUCT_BITBOX02_FACTORYSETUP != 1
#error "invald product value"
#error "invald PRODUCT_BITBOX02_FACTORYSETUP value"
#endif

#if !defined(PRODUCT_BITBOXBASE_FACTORYSETUP)
#define PRODUCT_BITBOXBASE_FACTORYSETUP 0
#elif PRODUCT_BITBOXBASE_FACTORYSETUP != 1
#error "invald product value"
#error "invald PRODUCT_BITBOXBASE_FACTORYSETUP value"
#endif


// Derive other useful definitions from the product.

#if PRODUCT_BITBOX_MULTI == 1
#define PLATFORM_BITBOX02 1
#define PLATFORM_BITBOXBASE 0
#define FACTORYSETUP 0
#endif

#if PRODUCT_BITBOX_BTCONLY == 1
#define PLATFORM_BITBOX02 1
#define PLATFORM_BITBOXBASE 0
#define FACTORYSETUP 0
#endif

#if PRODUCT_BITBOX_BASE == 1
#define PLATFORM_BITBOX02 0
#define PLATFORM_BITBOXBASE 1
#define FACTORYSETUP 0
#endif

#if PRODUCT_BITBOX02_FACTORYSETUP == 1
#define PLATFORM_BITBOX02 1
#define PLATFORM_BITBOXBASE 0
#define FACTORYSETUP 1
#endif

#if PRODUCT_BITBOXBASE_FACTORYSETUP == 1
#define PLATFORM_BITBOX02 0
#define PLATFORM_BITBOXBASE 1
#define FACTORYSETUP 1
#endif

Expand Down
4 changes: 2 additions & 2 deletions test/device-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ target_link_libraries(bitbox02-test-platform
)

# Always enable semihosting for the test firmwares
target_compile_definitions(bitbox02-test-platform PUBLIC PRODUCT_BITBOX_MULTI SEMIHOSTING)
target_compile_definitions(bitbox02-test-platform PUBLIC "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" SEMIHOSTING)
target_link_libraries(bitbox02-test-platform PUBLIC --specs=nano.specs --specs=rdimon.specs)
target_compile_options(bitbox02-test-platform PUBLIC --specs=nano.specs --specs=rdimon.specs)

Expand Down Expand Up @@ -193,7 +193,7 @@ target_link_libraries(bitboxbase-test-platform
add_dependencies(bitboxbase-test-platform rust-cbindgen)

# Always enable semihosting for the test firmwares
target_compile_definitions(bitboxbase-test-platform PUBLIC PRODUCT_BITBOX_BASE SEMIHOSTING)
target_compile_definitions(bitboxbase-test-platform PUBLIC "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1" SEMIHOSTING)
target_link_libraries(bitboxbase-test-platform PUBLIC --specs=nano.specs --specs=rdimon.specs)
target_compile_options(bitboxbase-test-platform PUBLIC --specs=nano.specs --specs=rdimon.specs)

Expand Down
37 changes: 9 additions & 28 deletions test/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,23 @@

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter -Wno-missing-prototypes -Wno-missing-declarations -Wno-implicit-function-declaration -Wno-bad-function-cast")


set(DBB-FILTERED-SOURCES
${DBB-FIRMWARE-COMMON-SOURCES}
${DBB-FIRMWARE-UI-COMMON-SOURCES}
${DBB-FIRMWARE-UI-SOURCES}
${DBB-FIRMWARE-PROTOBUF-SOURCES}
${FIRMWARE-U2F-SOURCES}
${DBB-FIRMWARE-USB-SOURCES}
${DBB-FIRMWARE-USART-SOURCES}
)

set(IGNORE_SOURCES
"src/screen.c"
"src/ui/ugui/ugui.c"
"src/memory/nvmctrl.c"
"src/memory/smarteeprom.c"
)

# Exclude some files which depends on the hardware.
foreach(SOURCEFILE ${IGNORE_SOURCES})
list(FILTER DBB-FILTERED-SOURCES EXCLUDE REGEX ".*/${SOURCEFILE}$")
endforeach()

message("FILTERED SOURCES: ${DBB-FILTERED-SOURCES}")

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/tools/nanopb/extra)
find_package(Nanopb REQUIRED)

add_library(bitbox
STATIC
${DBB-FIRMWARE-COMMON-SOURCES}
${DBB-FIRMWARE-COMPONENT-SOURCES}
${DBB-FIRMWARE-WORKFLOW-SOURCES}
${APP-BTC-SOURCES}
${DBB-FILTERED-SOURCES}
${DBB-FIRMWARE-UI-COMMON-SOURCES}
${DBB-FIRMWARE-UI-SOURCES}
${DBB-FIRMWARE-PROTOBUF-SOURCES}
${DBB-FIRMWARE-USB-SOURCES}
${DBB-FIRMWARE-USART-SOURCES}
${CTAES-SOURCES}
${ETHEREUM-SOURCES}
${FIRMWARE-U2F-SOURCES}
# The usb.c file mixes together hardware and testing code... This should be split.
${CMAKE_SOURCE_DIR}/src/usb/usb.c
${CMAKE_SOURCE_DIR}/src/usb/usb_frame.c
Expand Down Expand Up @@ -94,7 +75,7 @@ set_source_files_properties(
target_include_directories(bitbox SYSTEM PUBLIC ${NANOPB_INCLUDE_DIRS})
target_include_directories(bitbox PRIVATE ${CMAKE_SOURCE_DIR}/external/FatFs/source)
target_compile_definitions(bitbox PUBLIC "PB_NO_PACKED_STRUCTS=1" "PB_FIELD_16BIT=1")
target_compile_definitions(bitbox PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
target_compile_definitions(bitbox PUBLIC "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
add_dependencies(bitbox generate-protobufs)

# Since noiseprotocol and wallycore are external projects we need to specify the dependency
Expand Down Expand Up @@ -146,7 +127,7 @@ target_include_directories(
${INCLUDES}
${CMAKE_BINARY_DIR}/src
)
target_compile_definitions(u2f-util PUBLIC "TESTING" PRODUCT_BITBOX_MULTI "APP_U2F=1" "APP_BTC=1" "APP_LTC=1" "APP_ETH=1")
target_compile_definitions(u2f-util PUBLIC "TESTING" "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1" "APP_U2F=1" "APP_BTC=1" "APP_LTC=1" "APP_ETH=1")
target_compile_definitions(u2f-util PUBLIC "USE_KECCAK")


Expand Down

0 comments on commit 77df799

Please sign in to comment.