-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
288 lines (248 loc) · 9.95 KB
/
Copy pathCMakeLists.txt
File metadata and controls
288 lines (248 loc) · 9.95 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
################################################################################
# CMakeLists.txt — ARM Cortex-M7 dynamically loaded ELF module
#
# Requirements:
# - arm-none-eabi-gcc in PATH (Linux/macOS)
# or TOOLCHAIN_PATH defined in cmake/arm-none-eabi-toolchain.cmake (Windows)
# - CMake >= 3.25
# - Ninja (recommended for cross-compilation with Visual Studio)
#
# Usage from command line:
#
# cmake --preset ${{ matrix.preset }} -DINCLUDES_API_PATH="../Mk/Mk/Includes"
# cmake --build --preset ${{ matrix.preset }}
#
################################################################################
cmake_minimum_required(VERSION 3.25)
# The toolchain must be defined BEFORE project()
# It is passed as an argument (-DCMAKE_TOOLCHAIN_FILE=...) or via a preset.
project(shell
LANGUAGES C ASM
VERSION 1.0.0
DESCRIPTION "ARM Cortex-M7 dynamically loaded ELF module"
)
################################################################################
# Paths
################################################################################
set(SOURCES_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Shell/Sources")
set(INCLUDES_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Shell/Includes")
set(LD_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/Shell/Make/linker.ld")
# Sanity checks
if(NOT EXISTS "${SOURCES_PATH}")
message(FATAL_ERROR "SOURCES_PATH '${SOURCES_PATH}' not found.")
endif()
if(NOT EXISTS "${INCLUDES_PATH}")
message(FATAL_ERROR "INCLUDES_PATH '${INCLUDES_PATH}' not found.")
endif()
if(NOT EXISTS "${INCLUDES_API_PATH}")
message(FATAL_ERROR "INCLUDES_API_PATH '${INCLUDES_API_PATH}' not found.")
endif()
if(NOT EXISTS "${LD_SCRIPT}")
message(FATAL_ERROR "Linker script '${LD_SCRIPT}' not found.")
endif()
################################################################################
# Sources
################################################################################
# Recursively collect .c and .asm files
file(GLOB_RECURSE C_SOURCES CONFIGURE_DEPENDS "${SOURCES_PATH}/*.c")
file(GLOB_RECURSE ASM_SOURCES CONFIGURE_DEPENDS "${SOURCES_PATH}/*.asm")
# Recursively collect include directories from both include trees
file(GLOB_RECURSE ALL_HEADERS CONFIGURE_DEPENDS
"${INCLUDES_PATH}/*.h"
"${INCLUDES_PATH}/*.asm"
"${INCLUDES_API_PATH}/*.h"
"${INCLUDES_API_PATH}/*.asm"
)
# Extract unique directories from header list
set(INCLUDE_DIRS "${INCLUDES_PATH}" "${INCLUDES_API_PATH}")
foreach(_hdr IN LISTS ALL_HEADERS)
get_filename_component(_dir "${_hdr}" DIRECTORY)
list(APPEND INCLUDE_DIRS "${_dir}")
endforeach()
list(REMOVE_DUPLICATES INCLUDE_DIRS)
################################################################################
# Assembler file properties
#
# .asm files require -x assembler-with-cpp to be treated
# as assembly with C preprocessor.
################################################################################
set_source_files_properties(${ASM_SOURCES} PROPERTIES
LANGUAGE ASM
COMPILE_OPTIONS "-x;assembler-with-cpp"
)
################################################################################
# Executable
################################################################################
add_executable(${PROJECT_NAME}
${C_SOURCES}
${ASM_SOURCES}
)
set_target_properties(${PROJECT_NAME} PROPERTIES
OUTPUT_NAME "${PROJECT_NAME}"
SUFFIX ".elf"
)
target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIRS})
target_compile_definitions(${PROJECT_NAME} PRIVATE MK_EXTERNAL_APPS)
################################################################################
# C dialect compilation options
################################################################################
set(CDIAL_OPTIONS
-std=c18
-fno-asm
-fno-builtin
-fno-cond-mismatch
-ffreestanding
-fsigned-bitfields
-fsigned-char
-fsso-struct=little-endian
)
################################################################################
# ARM options
################################################################################
set(ARM_OPTIONS
-mcpu=cortex-m7
-mthumb
-mfloat-abi=hard
-mfpu=fpv5-sp-d16
-mno-unaligned-access
-mlittle-endian
)
################################################################################
# PIC options
#
# Required for dynamically loaded modules:
# -fPIC : position-independent code
# -mlong-calls: all calls use long (32-bit) branches, needed when the module
# is relocated into SDRAM far from the kernel text section
################################################################################
set(PIC_OPTIONS
-fPIC
-mlong-calls
)
################################################################################
# Warnings
################################################################################
set(WARN_OPTIONS
-Wpedantic -pedantic-errors -Werror -Wall -Wextra
-Wdouble-promotion -Wnull-dereference -Wshift-negative-value
-Wswitch-default -Wswitch-enum -Wswitch-bool -Wswitch-unreachable
-Wstrict-overflow=5 -Wduplicated-branches -Wduplicated-cond
-Wdiscarded-qualifiers -Wdiscarded-array-qualifiers
-Wincompatible-pointer-types -Wint-conversion -Wint-to-pointer-cast
-Wpointer-to-int-cast -Wdiv-by-zero -Wsystem-headers -Wtrampolines
-Wfloat-equal -Wno-traditional -Wdeclaration-after-statement
-Wshadow -Wshadow=local -Wunsafe-loop-optimizations -Wpointer-compare
-Wunused-macros -Wbad-function-cast
-Wcast-align -Wcast-align=strict -Wwrite-strings -Wconversion
-Wjump-misses-init -Waddress-of-packed-member -Wlogical-op
-Waggregate-return -Waggressive-loop-optimizations -Wpointer-arith
-Wattributes -Wstrict-prototypes -Wold-style-definition
--warn-missing-parameter-type -Wmissing-prototypes -Wmissing-declarations
-Wmultichar -Wnormalized=none -Wattribute-warning
-Wdeprecated -Wdeprecated-declarations -Woverflow
-Woverride-init-side-effects -Wpacked -Wno-packed-bitfield-compat
-Wpadded -Wredundant-decls -Wnested-externs -Winline
-Wno-long-long -Wvla -Woverlength-strings
)
################################################################################
# Common options (C + ASM)
################################################################################
set(COMMON_OPTIONS
${ARM_OPTIONS}
${PIC_OPTIONS}
-fanalyzer # static analyzer
-fmessage-length=0
-nostdinc
-fstack-usage
)
################################################################################
# Debug / Release options
#
# CMake automatically injects CMAKE_BUILD_TYPE into flags;
# we disable its default flags (CACHE "") and manage our own.
################################################################################
set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELWITHDEBINFO "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_MINSIZEREL "" CACHE STRING "" FORCE)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(BUILD_OPT_OPTIONS
-g3 -ggdb -fno-eliminate-unused-debug-symbols
-O0 -fno-inline-functions -fno-short-enums -fno-move-loop-invariants
)
else() # Release (default)
set(BUILD_OPT_OPTIONS
-Ofast -fno-inline-functions -fno-short-enums -fno-move-loop-invariants
)
endif()
################################################################################
# Apply flags to target
################################################################################
target_compile_options(${PROJECT_NAME} PRIVATE
# C-only options
$<$<COMPILE_LANGUAGE:C>:
${CDIAL_OPTIONS}
${COMMON_OPTIONS}
${BUILD_OPT_OPTIONS}
${WARN_OPTIONS}
-x c
>
# ASM options (-x assembler-with-cpp is in set_source_files_properties)
$<$<COMPILE_LANGUAGE:ASM>:
${COMMON_OPTIONS}
${BUILD_OPT_OPTIONS}
${WARN_OPTIONS}
>
)
################################################################################
# Linker
#
# -shared : produce a shared object (dynamically loadable ELF)
# -z max-page-size=... : set the maximum page size to 64 KB to match the
# Mk dynamic loader page granularity
################################################################################
target_link_options(${PROJECT_NAME} PRIVATE
-nostartfiles
-nodefaultlibs
-nolibc
-nostdlib
-shared
-z max-page-size=65536
# Linker script
-T "${LD_SCRIPT}"
# Map file
-Wl,-Map=${CMAKE_BINARY_DIR}/${PROJECT_NAME}.map
)
################################################################################
# Post-build: strip + size
################################################################################
set(ELF_FILE "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.elf")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND "${CMAKE_STRIP_TOOL}" -s "${ELF_FILE}"
COMMAND "${CMAKE_SIZE_TOOL}" --format=berkeley --totals "${ELF_FILE}"
COMMENT "Post-build: strip -> size"
VERBATIM
)
else()
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND "${CMAKE_SIZE_TOOL}" --format=berkeley --totals "${ELF_FILE}"
COMMENT "Post-build: size (no strip in Debug)"
VERBATIM
)
endif()
################################################################################
# Utility target 'print' (size only, no rebuild)
################################################################################
add_custom_target(print
COMMAND ${CMAKE_SIZE_TOOL} --format=berkeley --totals "${ELF_FILE}"
COMMENT " SIZE ${PROJECT_NAME}.elf"
)
################################################################################
# Configuration summary
################################################################################
message(STATUS "Build type : ${CMAKE_BUILD_TYPE}")
message(STATUS "Sources : ${SOURCES_PATH}")
message(STATUS "Includes : ${INCLUDES_PATH}")
message(STATUS "Includes API : ${INCLUDES_API_PATH}")
message(STATUS "Linker script: ${LD_SCRIPT}")