Skip to content

Commit 97b92fd

Browse files
committed
Update CMake configuration, move post-build program size check to seperate post_build.cmake script
1 parent d970170 commit 97b92fd

File tree

4 files changed

+89
-63
lines changed

4 files changed

+89
-63
lines changed

Software/CMakeLists.txt

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ if (EXISTS ${picoVscode})
2323
include(${picoVscode})
2424
endif()
2525
# ====================================================================================
26-
2726
set(PICO_BOARD_HEADER_DIRS ${CMAKE_SOURCE_DIR})
2827
# Board type selection:
2928
#
30-
# Please select RP2040-Decoder-board.h for Rev >= 1.0 or RP2040-Decoder-board-legacy.h for Rev < 1.0
31-
set(PICO_BOARD RP2040-Decoder-board.h CACHE STRING "Board type")
29+
# Please select RP2040-Decoder-board-Rev-1_0 for Rev >= 1.0 or RP2040-Decoder-board-Rev-0_3 for Rev <= 0.3
30+
set(PICO_BOARD RP2040-Decoder-board-Rev-1_0)
3231

3332
# Pull in Raspberry Pi Pico SDK (must be before project)
3433
include(pico_sdk_import.cmake)
@@ -38,12 +37,13 @@ project(RP2040-Decoder C CXX ASM)
3837
# Initialise the Raspberry Pi Pico SDK
3938
pico_sdk_init()
4039

40+
# Manual compiler options, these generally override configuration set by CMAKE_BUILD_TYPE
4141
add_compile_options(-Wall
42-
-Ofast
43-
-g3
44-
-Wno-format # int != int32 _t as far as the compiler is concerned because gcc has int32_t as long int
45-
-Wno-unused-function # we have some for the docs that aren't called
46-
-Wno-maybe-uninitialized)
42+
# -O0 # Overrides optimization flags specified by CMAKE_BUILD_TYPE
43+
# -g3 # Overrides debug flags specified by CMAKE_BUILD_TYPE
44+
-Wno-format # unsigned int = uint32_t using gcc so don't warn about format
45+
-Wno-unused-function # Some defined functions used for debugging are never called or not used when logging is disabled
46+
)
4747

4848
# Add executable
4949
add_executable( RP2040-Decoder
@@ -55,8 +55,14 @@ add_executable( RP2040-Decoder
5555
shared.h )
5656

5757
pico_set_program_name(RP2040-Decoder "RP2040-Decoder")
58-
pico_set_program_version(RP2040-Decoder "1.0")
5958

59+
# Build type
60+
message("CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
61+
message("PICO_DEOPTIMIZED_DEBUG: ${PICO_DEOPTIMIZED_DEBUG}")
62+
if(NOT CMAKE_BUILD_TYPE)
63+
message(WARNING "CMAKE_BUILD_TYPE is not set. Defaulting to 'Release'")
64+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
65+
endif()
6066

6167
# Logging configuration:
6268
#
@@ -119,61 +125,16 @@ pico_add_extra_outputs(RP2040-Decoder)
119125
# Program size check
120126
#
121127
# Check for program size exceeding limits as CVs are stored at the end of flash, the program could theoretically overwrite this.
122-
# In practice the program size is usually only about 100KiB.
128+
# In practice the program size is usually does not exceed about 100KiB, the actual size is printed by CMake from arm-none-eabi-size.
123129
# Extract the directory of the C compiler and construct the path to the 'arm-none-eabi-size' tool
124130
get_filename_component(C_COMPILER_DIR ${CMAKE_C_COMPILER} DIRECTORY)
125131
set(SIZE_TOOL ${C_COMPILER_DIR}/arm-none-eabi-size)
126-
127-
# Add custom command to check the flash size after build
132+
set(BOARD_HEADER "${CMAKE_SOURCE_DIR}/${PICO_BOARD}.h")
133+
set(ELF_FILE "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.elf")
128134
add_custom_command(TARGET RP2040-Decoder POST_BUILD
129-
COMMAND ${CMAKE_COMMAND} -E echo "Checking flash size for $<TARGET_FILE:RP2040-Decoder>"
130-
COMMAND ${SIZE_TOOL} $<TARGET_FILE:RP2040-Decoder> > size_output.txt
131-
)
132-
133-
# Read the size output file
134-
file(READ "${CMAKE_BINARY_DIR}/size_output.txt" SIZE_OUTPUT)
135-
136-
# Print the content of SIZE_OUTPUT
137-
message(STATUS "arm-none-eabi-size output:\n${SIZE_OUTPUT}")
138-
139-
# Use regex to match all numbers
140-
string(REGEX MATCHALL "[0-9]+" MATCHED_VALUES "${SIZE_OUTPUT}")
141-
142-
# Get third element from list of matches, this corresponds with dec table value in "size_output.txt"
143-
list(GET MATCHED_VALUES 3 PROGRAM_SIZE_DEC)
144-
145-
# Check for success of program size extraction from "size_output.txt"
146-
if(PROGRAM_SIZE_DEC)
147-
message(STATUS "PROGRAM_SIZE_DEC: ${PROGRAM_SIZE_DEC} bytes")
148-
else()
149-
message(FATAL_ERROR "Could not extract program size from size_output.txt")
150-
endif()
151-
152-
153-
# Path to the header file that defines PICO_FLASH_SIZE_BYTES
154-
set(FLASH_CONFIG_HEADER ${CMAKE_SOURCE_DIR}/${PICO_BOARD}.h)
155-
# Read the contents of the header file into a variable
156-
file(READ ${FLASH_CONFIG_HEADER} HEADER_CONTENT)
157-
158-
# Extract the value of PICO_FLASH_SIZE_BYTES using a regular expression
159-
string(REGEX MATCHALL "#define PICO_FLASH_SIZE_BYTES [0-9]+" MACRO_MATCH "${HEADER_CONTENT}")
160-
string(REGEX MATCHALL "[0-9]+" PICO_FLASH_SIZE_BYTES "${MACRO_MATCH}")
161-
162-
163-
# Check for success of PICO_FLASH_SIZE_BYTES extraction from board header file
164-
if(PICO_FLASH_SIZE_BYTES)
165-
message(STATUS "PICO_FLASH_SIZE_BYTES: ${PICO_FLASH_SIZE_BYTES} bytes")
166-
else()
167-
message(FATAL_ERROR "Failed to find PICO_FLASH_SIZE_BYTES in the header file.")
168-
endif()
169-
170-
# Sector size is always 4096 bytes
171-
set(FLASH_SECTOR_SIZE 4096)
172-
message(STATUS "FLASH_SECTOR_SIZE: ${FLASH_SECTOR_SIZE} bytes")
173-
# Subtract flash sector size from flash size
174-
math(EXPR FLASH_TARGET_OFFSET "${PICO_FLASH_SIZE_BYTES} - ${FLASH_SECTOR_SIZE}")
175-
message(STATUS "FLASH_TARGET_OFFSET: ${FLASH_TARGET_OFFSET} bytes")
176-
177-
if(PROGRAM_SIZE_DEC GREATER FLASH_TARGET_OFFSET)
178-
message(FATAL_ERROR "Program size exceeds allowed flash size. Flash sector containing the CVs would be overwritten! Please reduce the size of the program.")
179-
endif()
135+
COMMAND ${CMAKE_COMMAND}
136+
-D BOARD_HEADER=${BOARD_HEADER}
137+
-D SIZE_TOOL=${SIZE_TOOL}
138+
-D ELF_FILE=${ELF_FILE}
139+
-P ${CMAKE_SOURCE_DIR}/post_build.cmake
140+
)

Software/post_build.cmake

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Check if the required arguments are provided
2+
if(NOT DEFINED BOARD_HEADER)
3+
message(FATAL_ERROR "BOARD_HEADER argument is missing")
4+
else()
5+
message(STATUS "BOARD_HEADER: ${BOARD_HEADER}")
6+
endif()
7+
8+
if(NOT DEFINED SIZE_TOOL)
9+
message(FATAL_ERROR "SIZE_TOOL argument is missing")
10+
else()
11+
message(STATUS "SIZE_TOOL: ${SIZE_TOOL}")
12+
endif()
13+
14+
if(NOT DEFINED ELF_FILE)
15+
message(FATAL_ERROR "ELF_FILE argument is missing")
16+
else()
17+
message(STATUS "ELF_FILE: ${ELF_FILE}")
18+
endif()
19+
20+
# Post build checks
21+
# Run the size tool and read the output
22+
execute_process(
23+
COMMAND ${SIZE_TOOL} "${ELF_FILE}"
24+
OUTPUT_VARIABLE SIZE_OUTPUT
25+
RESULT_VARIABLE SIZE_RESULT
26+
)
27+
28+
if(SIZE_RESULT)
29+
message(FATAL_ERROR "Failed to run arm-none-eabi-size on ${ELF_FILE}")
30+
else()
31+
# Print the content of SIZE_OUTPUT
32+
message(STATUS "arm-none-eabi-size output:\n${SIZE_OUTPUT}")
33+
endif()
34+
# Use regex to match all numbers
35+
string(REGEX MATCHALL "[0-9]+" MATCHED_VALUES "${SIZE_OUTPUT}")
36+
# Get third element from list of matches, this corresponds with dec table value in "size_output.txt"
37+
list(GET MATCHED_VALUES 3 PROGRAM_SIZE_DEC)
38+
# Check for success of program size extraction from "size_output.txt"
39+
if(PROGRAM_SIZE_DEC)
40+
message(STATUS "PROGRAM_SIZE_DEC: ${PROGRAM_SIZE_DEC} bytes")
41+
else()
42+
message(FATAL_ERROR "Could not extract program size from size_output.txt")
43+
endif()
44+
# Path to the header file that defines PICO_FLASH_SIZE_BYTES
45+
set(FLASH_CONFIG_HEADER ${BOARD_HEADER})
46+
# Read the contents of the header file into a variable
47+
file(READ ${FLASH_CONFIG_HEADER} HEADER_CONTENT)
48+
# Extract the value of PICO_FLASH_SIZE_BYTES using a regular expression
49+
string(REGEX MATCHALL "#define PICO_FLASH_SIZE_BYTES [0-9]+" MACRO_MATCH "${HEADER_CONTENT}")
50+
string(REGEX MATCHALL "[0-9]+" PICO_FLASH_SIZE_BYTES "${MACRO_MATCH}")
51+
# Check for success of PICO_FLASH_SIZE_BYTES extraction from board header file
52+
if(PICO_FLASH_SIZE_BYTES)
53+
message(STATUS "PICO_FLASH_SIZE_BYTES: ${PICO_FLASH_SIZE_BYTES} bytes")
54+
else()
55+
message(FATAL_ERROR "Failed to find PICO_FLASH_SIZE_BYTES in the header file.")
56+
endif()
57+
# Sector size is always 4096 bytes
58+
set(FLASH_SECTOR_SIZE 4096)
59+
message(STATUS "FLASH_SECTOR_SIZE: ${FLASH_SECTOR_SIZE} bytes")
60+
# Subtract flash sector size from flash size
61+
math(EXPR FLASH_TARGET_OFFSET "${PICO_FLASH_SIZE_BYTES} - ${FLASH_SECTOR_SIZE}")
62+
message(STATUS "FLASH_TARGET_OFFSET: ${FLASH_TARGET_OFFSET} bytes")
63+
if(PROGRAM_SIZE_DEC GREATER FLASH_TARGET_OFFSET)
64+
message(FATAL_ERROR "Program size exceeds allowed flash size. Flash sector containing the CVs would be overwritten! Please reduce the size of the program.")
65+
endif()

0 commit comments

Comments
 (0)