Skip to content

Commit 4e3ec4b

Browse files
committed
build-system: create usable debug build
Previously the debug build was to large to flash due to the optimization flag
1 parent 5ce4a18 commit 4e3ec4b

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

.ci/ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ make -j8 bootloader-btc-production
5959
make -j8 firmware
6060
make -j8 firmware-btc
6161
make -j8 factory-setup
62+
make -j8 firmware-debug
6263

6364
# Disallow some symbols in the final binary that we don't want.
6465
if arm-none-eabi-nm build/bin/firmware.elf | grep -q "float_to_decimal_common_shortest"; then

CMakeLists.txt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ endif()
233233

234234
# Optimize for size by default
235235
set(CMAKE_C_FLAGS_RELEASE "-Os -DNDEBUG")
236-
# Allow gdb extensions if available
237-
set(CMAKE_C_FLAGS_DEBUG "-Og -ggdb")
236+
# (-ggdb) Allow gdb extensions if available
237+
# Optimize debug build for size, optimizing for debug takes to much space.
238+
set(CMAKE_C_FLAGS_DEBUG "-Os -ggdb")
238239
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-Os -ggdb -DNDEBUG")
239240

240241
#-----------------------------------------------------------------------------
@@ -321,13 +322,15 @@ string(APPEND CMAKE_C_FLAGS " -Wundef -Wmissing-include-dirs")
321322
# Disable builtin warning
322323
string(APPEND CMAKE_C_FLAGS " -Wno-cast-function-type")
323324

324-
# Hardening
325-
string(APPEND CMAKE_C_FLAGS " -fstack-protector-all")
326-
if(CMAKE_CROSSCOMPILING)
327-
# Path to empty dummy libssp and libssp_shared. '-llibssp -llibssp_shared' is automatically added
328-
# with '-fstack-protector-all', but we don't need them as we have our own custom
329-
# `__stack_chk_fail`. See https://wiki.osdev.org/Stack_Smashing_Protector.
330-
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -L${CMAKE_CURRENT_SOURCE_DIR}/external/lib/ssp")
325+
# Enable stack protection on release builds
326+
if(NOT CMAKE_BUILD_TYPE STREQUAL "DEBUG")
327+
string(APPEND CMAKE_C_FLAGS " -fstack-protector-all")
328+
if(CMAKE_CROSSCOMPILING)
329+
# Path to empty dummy libssp and libssp_shared. '-llibssp -llibssp_shared' is automatically added
330+
# with '-fstack-protector-all', but we don't need them as we have our own custom
331+
# `__stack_chk_fail`. See https://wiki.osdev.org/Stack_Smashing_Protector.
332+
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -L${CMAKE_CURRENT_SOURCE_DIR}/external/lib/ssp")
333+
endif()
331334
endif()
332335

333336
# Disallow duplicate definitions, which is the default since GCC

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ build/Makefile:
2626
cd build && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake ..
2727
$(MAKE) -C py/bitbox02
2828

29+
build-debug/Makefile:
30+
mkdir -p build-debug
31+
cd build-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG ..
32+
$(MAKE) -C py/bitbox02
33+
2934
build-build/Makefile:
3035
mkdir -p build-build
3136
cd build-build && cmake .. -DCOVERAGE=ON -DSANITIZE_ADDRESS=$(SANITIZE) -DSANITIZE_UNDEFINED=$(SANITIZE)
@@ -41,6 +46,9 @@ build-build-rust-unit-tests/Makefile:
4146
# Directory for building for "host" machine according to gcc convention
4247
build: build/Makefile
4348

49+
# Directory for building debug build for "host" machine according to gcc convention
50+
build-debug: build-debug/Makefile
51+
4452
# Directory for building for "build" machine according to gcc convention
4553
build-build: build-build/Makefile
4654

@@ -50,10 +58,11 @@ build-build: build-build/Makefile
5058
build-build-rust-unit-tests: build-build-rust-unit-tests/Makefile
5159

5260
firmware: | build
53-
# Generate python bindings for protobuf for test scripts
5461
$(MAKE) -C build firmware.elf
5562
firmware-btc: | build
5663
$(MAKE) -C build firmware-btc.elf
64+
firmware-debug: | build-debug
65+
$(MAKE) -C build-debug firmware.elf
5766
bootloader: | build
5867
$(MAKE) -C build bootloader.elf
5968
bootloader-development: | build
@@ -128,4 +137,4 @@ prepare-tidy: | build build-build
128137
make -C build rust-cbindgen
129138
make -C build-build rust-cbindgen
130139
clean:
131-
rm -rf build build-build build-build-rust-unit-tests
140+
rm -rf build build-build build-debug build-build-rust-unit-tests

src/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ if(CMAKE_CROSSCOMPILING)
224224
set(RUST_TARGET_ARCH thumbv7em-none-eabi)
225225
set(RUST_TARGET_ARCH_DIR ${RUST_TARGET_ARCH})
226226
set(RUST_TARGET_ARCH_ARG --target ${RUST_TARGET_ARCH})
227-
set(RUST_CARGO_FLAGS ${RUST_CARGO_FLAGS} -Zbuild-std=core,alloc -Zbuild-std-features=panic_immediate_abort,optimize_for_size)
227+
if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
228+
set(RUST_CARGO_FLAGS ${RUST_CARGO_FLAGS} -Zbuild-std=core,alloc -Zbuild-std-features=optimize_for_size)
229+
else()
230+
set(RUST_CARGO_FLAGS ${RUST_CARGO_FLAGS} -Zbuild-std=core,alloc -Zbuild-std-features=panic_immediate_abort,optimize_for_size)
231+
endif()
228232
else()
229233
set(RUST_TARGET_ARCH_DIR .)
230234
endif()

src/rust/Cargo.toml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,27 @@ zeroize = "1.7.0"
4040
# This only affects the .elf output. Debug info is stripped from the final .bin.
4141
# Paths to source code can still appear in the final bin, as they are part of the panic!() output.
4242
debug = true
43-
43+
# Optmimize maximally for size, 'z' should produce even less code than 's'
4444
opt-level = 'z'
45+
# 1 gives smaller binaries (16 is default in release mode)
4546
codegen-units = 1
47+
# Abort on panics in release builds.
4648
panic = 'abort'
49+
# LTO gives smaller binaries due to cross-crate optimizations
4750
lto = true
4851

52+
# Mimic the release profile to save as much space as possible
4953
[profile.dev]
5054
opt-level = 'z'
55+
# Set lto="thin" to get faster builds
56+
lto = true
57+
# Enabling debug assertions will increase binary size
58+
debug-assertions = false
59+
# Enabling overflow checks will increase binary size
60+
overflow-checks = false
61+
# Set to maximally 256 to compile more in parallel
62+
codegen-units = 1
63+
# Set to 'abort' to save space
64+
panic = 'unwind'
65+
# Set to false to potentially reduce binary size
66+
incremental = true

0 commit comments

Comments
 (0)