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 4, 2019
1 parent ae91ec3 commit 436611b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 39 deletions.
36 changes: 18 additions & 18 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -515,25 +515,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 @@ -604,7 +604,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 @@ -615,21 +615,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_ETH=0" "APP_LTC=0" "APP_U2F=0")
target_compile_definitions(firmware-bitboxbase.elf PRIVATE "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1" "APP_BTC=0" "APP_ETH=0" "APP_LTC=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)
target_compile_definitions(firmware-bitboxbase-semihosting.elf PRIVATE "PLATFORM_BITBOXBASE=1" "EDITION_STANDARD=1" SEMIHOSTING)
target_sources(firmware-bitboxbase-semihosting.elf PRIVATE ${BITBOXBASE-FIRMWARE-SOURCES})
target_link_libraries(firmware-bitboxbase-semihosting.elf PRIVATE bitboxbase-platform)

Expand All @@ -641,7 +641,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)
target_compile_definitions(factory-setup-bitboxbase.elf PRIVATE "PLATFORM_BITBOXBASE=1" "EDITION_FACTORYSETUP=1")
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 @@ -189,7 +189,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
4 changes: 2 additions & 2 deletions test/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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 @@ -139,7 +139,7 @@ target_include_directories(
${INCLUDES}
${CMAKE_BINARY_DIR}/src
)
target_compile_definitions(u2f-util PUBLIC "TESTING" PRODUCT_BITBOX_MULTI)
target_compile_definitions(u2f-util PUBLIC "TESTING" "PLATFORM_BITBOX02=1" "EDITION_STANDARD=1")
target_compile_definitions(u2f-util PUBLIC "USE_KECCAK")


Expand Down

0 comments on commit 436611b

Please sign in to comment.