forked from prusa3d/Prusa-Firmware-MMU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
243 lines (203 loc) · 7.69 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
cmake_minimum_required(VERSION 3.19)
include(cmake/Utilities.cmake)
include(cmake/GetGitRevisionDescription.cmake)
include(cmake/ProjectVersion.cmake)
include(cmake/ReproducibleBuild.cmake)
project(
MMU
LANGUAGES C CXX ASM
VERSION ${PROJECT_VERSION}
)
#
# Command Line Options
#
# You should specify those options when invoking CMake. Example:
# ~~~
# cmake .. <other options> -DCUSTOM_COMPILE_OPTIONS=-DENABLE_FEATURE_X
# ~~~
set(PROJECT_VERSION_SUFFIX
"<auto>"
CACHE
STRING
"Full version suffix to be shown on the info screen in settings (e.g. full_version=4.0.3-BETA+1035.PR111.B4, suffix=-BETA+1035.PR111.B4). Defaults to '+<commit sha>.<dirty?>.<debug?>' if set to '<auto>'."
)
set(PROJECT_VERSION_SUFFIX_SHORT
"<auto>"
CACHE
STRING
"Short version suffix to be shown on splash screen. Defaults to '+<BUILD_NUMBER>' if set to '<auto>'."
)
set(BUILD_NUMBER
""
CACHE STRING "Build number of the firmware. Resolved automatically if not specified."
)
set(PROJECT_VERSION_TIMESTAMP
""
CACHE STRING "Timestamp for the build. Resolved automatically if not specified."
)
set(CUSTOM_COMPILE_OPTIONS
""
CACHE STRING "Allows adding custom C/C++ flags"
)
# Resolve BUILD_NUMBER and PROJECT_VERSION_* variables
resolve_version_variables()
add_compile_definitions(
PROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
PROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}
PROJECT_VERSION_REV=${PROJECT_VERSION_REV}
PROJECT_BUILD_NUMBER=${BUILD_NUMBER}
FW_VERSION_FULL_STR="${PROJECT_VERSION_FULL}"
FW_VERSION_STR="${PROJECT_VERSION}"
FW_VERSION_SUFFIX_STR="${PROJECT_VERSION_SUFFIX}"
FW_VERSION_SUFFIX_SHORT_STR="${PROJECT_VERSION_SUFFIX_SHORT}"
)
# Check GCC Version
get_recommended_gcc_version(RECOMMENDED_TOOLCHAIN_VERSION)
if(CMAKE_CROSSCOMPILING AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL
${RECOMMENDED_TOOLCHAIN_VERSION}
)
message(WARNING "Recommended AVR toolchain is ${RECOMMENDED_TOOLCHAIN_VERSION}"
", but you have ${CMAKE_CXX_COMPILER_VERSION}"
)
elseif(NOT CMAKE_CROSSCOMPILING AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(
WARNING
"Recommended compiler for host tools and unittests is GCC, you have ${CMAKE_CXX_COMPILER_ID}."
)
endif()
# Inform user about the resolved settings
message(STATUS "Project version: ${PROJECT_VERSION}")
message(STATUS "Project version with full suffix: ${PROJECT_VERSION_FULL}")
message(
STATUS "Project version with short suffix: ${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}"
)
message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}.")
# eclipse sets those variables, so lets just use them so we don't get a warning about unused
# variables
set(unused "${CMAKE_VERBOSE_MAKEFILE} ${CMAKE_RULE_MESSAGES}")
# append custom C/C++ flags
if(CUSTOM_COMPILE_OPTIONS)
string(REPLACE " " ";" CUSTOM_COMPILE_OPTIONS "${CUSTOM_COMPILE_OPTIONS}")
add_compile_options(${CUSTOM_COMPILE_OPTIONS})
endif()
#
# Global Compiler & Linker Configuration
#
# include symbols
add_compile_options(-g)
# optimizations
if(CMAKE_CROSSCOMPILING)
# set source epoch
set_source_epoch(${PROJECT_VERSION_TIMESTAMP})
# default optimization flags
set(CMAKE_CXX_FLAGS_DEBUG "-Og -g")
set(CMAKE_CXX_FLAGS_RELEASE "-Os -g -DNDEBUG")
set(CMAKE_C_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
set(CMAKE_C_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
# mcu related settings: It would be nice to add "-mrelax" but due to some avr-gcc/linker bug it
# breaks functionality of the FW (LoadFilament stopped working and only return Finished) The bad
# news is that -mrelax was saving ~450B of CPU FLASH
set(MCU_FLAGS -mmcu=atmega32u4 -DF_CPU=16000000L)
add_compile_options(${MCU_FLAGS})
add_link_options(${MCU_FLAGS})
# disable some C++ language features
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-threadsafe-statics>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
# disable exceptions
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables>)
# split and gc sections
add_compile_options(-ffunction-sections -fdata-sections)
add_link_options(-ffunction-sections -fdata-sections -Wl,--gc-sections)
else()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-O0)
else()
add_compile_options(-O2)
endif()
endif()
# enable all warnings (well, not all, but some)
add_compile_options(-Wall -Wsign-compare)
# support _DEBUG macro (some code uses to recognize debug builds)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(_DEBUG)
endif()
#
# Import definitions of all libraries
#
add_subdirectory(lib)
#
# MMU firmware
#
add_executable(firmware)
set_target_properties(firmware PROPERTIES CXX_STANDARD 17)
set_target_properties(firmware PROPERTIES INTERPROCEDURAL_OPTIMIZATION True)
if(CMAKE_CROSSCOMPILING)
# configure linker script
set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/src/avr5.xn)
target_link_options(firmware PUBLIC -Wl,-T,${LINKER_SCRIPT})
add_link_dependency(firmware ${LINKER_SCRIPT})
# limit the text section to 28K (32K - 4k reserved for the bootloader)
target_link_options(firmware PUBLIC -Wl,--defsym=__TEXT_REGION_LENGTH__=28K)
# generate firmware .hex file
objcopy(firmware "ihex" ".hex")
get_dependency_directory(prusa3dboards PRUSA_BOARDS_DIR)
add_custom_command(
TARGET firmware
POST_BUILD
COMMAND
${CMAKE_OBJCOPY} -I ihex -O binary
${PRUSA_BOARDS_DIR}/bootloaders/prusa_mm_control/Caterina-prusa_mm_control.hex bootloader.bin
COMMAND ${CMAKE_OBJCOPY} firmware -O binary --gap-fill 0xFF --pad-to 0x00007000 firmware.bin
COMMAND ${CMAKE_COMMAND} -E cat firmware.bin bootloader.bin > fw_bootloader.bin
COMMAND ${CMAKE_OBJCOPY} -I binary -O ihex fw_bootloader.bin
"MMU2S_MMU3_BOOTLOADER_${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}.hex"
COMMAND
${CMAKE_COMMAND} -E copy
"MMU2S_MMU3_BOOTLOADER_${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}.hex"
"MMU2S_MMU3_BOOTLOADER_${PROJECT_VERSION}.hex"
BYPRODUCTS bootloader.bin firmware.bin fw_bootloader.bin
)
# produce ASM listing
add_custom_command(
TARGET firmware
POST_BUILD
COMMAND ${CMAKE_OBJDUMP} --prefix ${CMAKE_SOURCE_DIR} -CSd firmware > firmware.asm
BYPRODUCTS firmware.asm
)
# inform about the firmware's size in terminal
add_custom_command(
TARGET firmware
POST_BUILD
COMMAND ${CMAKE_SIZE_UTIL} -C --mcu=atmega32u4 firmware
)
report_size(firmware)
# generate linker map file
target_link_options(firmware PUBLIC -Wl,-Map=firmware.map)
# Put Prusa Magic™ at the beginning of the hex
add_custom_command(
TARGET firmware
POST_BUILD
COMMAND
${CMAKE_COMMAND} -D WORK_DIR=${CMAKE_BINARY_DIR} -D
HEX_NAME="MMU2S_MMU3_FW${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}.hex" -P
${CMAKE_SOURCE_DIR}/cmake/HexConcat.cmake DEPENDS firmware.hex
COMMAND
${CMAKE_COMMAND} -E copy "MMU2S_MMU3_FW${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}.hex"
"MMU2S_MMU3_FW${PROJECT_VERSION}.hex"
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/autopublish
COMMAND ${CMAKE_COMMAND} -E copy
"MMU2S_MMU3_FW${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}.hex" "autopublish/"
)
endif()
target_include_directories(firmware PRIVATE src lib)
target_compile_options(firmware PRIVATE -Wdouble-promotion)
add_subdirectory(src)
if(CMAKE_CROSSCOMPILING)
set_all_targets_reproducible()
else()
# do not build the firmware by default (tests are the focus if not crosscompiling)
set_target_properties(firmware PROPERTIES EXCLUDE_FROM_ALL YES)
enable_testing()
add_subdirectory(tests)
endif()