/
arm.cmake
287 lines (235 loc) · 8.53 KB
/
arm.cmake
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
###| CMAKE Kiibohd Controller |###
#
# Jacob Alexander 2011-2020
# Due to this file's usefulness:
#
# Released into the Public Domain
#
# ARM CMake Build Configuration
#
###
###
# Compiler Check
#
#|
#| In CMake 3.6 a new feature to configure try_compile to work with cross-compilers
#| https://cmake.org/cmake/help/v3.6/variable/CMAKE_TRY_COMPILE_TARGET_TYPE.html#variable:CMAKE_TRY_COMPILE_TARGET_TYPE
#| If we detect CMake 3.6 or higher, use the new method
#|
if ( NOT CMAKE_VERSION VERSION_LESS "3.6" )
# Prepare for cross-compilation
set( CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY" )
set( CMAKE_SYSTEM_NAME "Generic" )
message( STATUS "Compiler Selected:" )
if ( "${COMPILER}" MATCHES "gcc" )
set( CMAKE_C_COMPILER arm-none-eabi-gcc )
set( _CMAKE_TOOLCHAIN_PREFIX arm-none-eabi- )
message( "gcc" )
elseif ( "${COMPILER}" MATCHES "clang" )
set( CMAKE_C_COMPILER clang )
set( CMAKE_C_COMPILER_ID ARMCCompiler )
set( _CMAKE_TOOLCHAIN_PREFIX llvm- )
message( "clang" )
else ()
message( AUTHOR_WARNING "COMPILER: ${COMPILER} - Unknown compiler selection" )
endif ()
#|
#| Before CMake 3.6 the cmake_force_c_compiler command was necessary to select cross-compilers
#| and other problemmatic compilers.
#|
else ()
# Set the Compilers (must be set first)
include( CMakeForceCompiler )
message( STATUS "Compiler Selected:" )
if ( "${COMPILER}" MATCHES "gcc" )
cmake_force_c_compiler( arm-none-eabi-gcc ARMCCompiler )
set( _CMAKE_TOOLCHAIN_PREFIX arm-none-eabi- )
message( "gcc" )
elseif ( "${COMPILER}" MATCHES "clang" )
cmake_force_c_compiler( clang ARMCCompiler )
set( _CMAKE_TOOLCHAIN_PREFIX llvm- )
message( "clang" )
else ()
message( AUTHOR_WARNING "COMPILER: ${COMPILER} - Unknown compiler selection" )
endif ()
endif ()
###
# ARM Defines and Linker Options
#
#| Chip Name (Linker)
message( STATUS "Chip Selected:" )
message( "${CHIP}" )
set( MCU "${CHIP}" ) # For loading script compatibility
#| Kinetis
if ( "${CHIP}" MATCHES "^mk.*$" )
include( kinetis )
#| SAM
elseif ( "${CHIP}" MATCHES "^sam.*$" )
include( sam )
#| Unknown
else ()
message( FATAL_ERROR "CHIP: ${CHIP} - Unknown ARM microcontroller" )
endif ()
#| Chip Base Type
message( STATUS "Chip Family:" )
message( "${CHIP_FAMILY}" )
#| CPU Type
message( STATUS "CPU Selected:" )
message( "${CPU}" )
#| Extra Compiler Sources
#| Mostly for convenience functions like interrupt handlers
set( COMPILER_SRCS
Lib/${CHIP_SUPPORT}.c
Lib/delay.c
Lib/entropy.c
Lib/gpio.c
Lib/periodic.c
Lib/sleep.c
Lib/storage.c
Lib/time.c
Lib/utf8.c
)
if ( "${CPU}" MATCHES "cortex-m4" )
list( APPEND COMPILER_SRCS
Lib/arm_cortex.c
)
# KLL Options
set( Device_KLL "${CMAKE_CURRENT_SOURCE_DIR}/Lib/arm_cortex.kll" )
endif ()
set( Device_KLL ${Device_KLL} "${CMAKE_CURRENT_SOURCE_DIR}/Lib/sleep.kll" )
#| SAM Sources
if ( "${CHIP_SUPPORT}" MATCHES "sam" )
list( APPEND COMPILER_SRCS
Lib/ASF/common/services/clock/sam4s/sysclk.c
Lib/ASF/common/utils/interrupt/interrupt_sam_nvic.c
Lib/ASF/sam/drivers/efc/efc.c
Lib/ASF/sam/drivers/pio/pio.c
Lib/ASF/sam/drivers/pmc/pmc.c
Lib/ASF/sam/drivers/supc/supc.c
Lib/ASF/sam/drivers/wdt/wdt.c
Lib/ASF/sam/utils/cmsis/sam4s/source/templates/system_sam4s.c
Lib/ASF/sam/services/flash_efc/flash_efc.c
)
# KLL Options
set( Device_KLL ${Device_KLL} "${CMAKE_CURRENT_SOURCE_DIR}/Lib/sam.kll" )
endif ()
#| Kinetis Sources
if ( "${CHIP_SUPPORT}" MATCHES "kinetis" )
# KLL Options
set( Device_KLL ${Device_KLL} "${CMAKE_CURRENT_SOURCE_DIR}/Lib/kinetis.kll" )
endif ()
#| Clang needs a few more functions for linking
if ( "${COMPILER}" MATCHES "clang" )
set( COMPILER_SRCS ${COMPILER_SRCS}
Lib/clang.c
)
endif ()
message( STATUS "Compiler Source Files:" )
message( "${COMPILER_SRCS}" )
#| USB Defines, this is how the loader programs detect which type of chip base is used
message( STATUS "Bootloader Type:" )
if ( DFU )
# XXX (HaaTa) These VID's are deprecated, but are kept as the default for unspecified keyboards
set( VENDOR_ID "0x1C11" CACHE STRING "USB Firmware VID" )
set( PRODUCT_ID "0xB04D" CACHE STRING "USB Firmware PID" )
set( BOOT_VENDOR_ID "0x1C11" CACHE STRING "USB Bootloader VID" )
set( BOOT_PRODUCT_ID "0xB007" CACHE STRING "USB Bootloader PID" )
# The | symbol is replaced by a space if in secure mode, or a \0 if not (at runtime)
set( BOOT_DFU_ALTNAME "Kiibohd DFU|Secure" )
#set( BOOT_DFU_ALTNAME "@Kiibohd DFU /0x00404000/61*8Kg|Secure" )
set( BOOT_DFU_ALTNAME2 "Kiibohd DFU (BLE)|Secure" )
message( "dfu" )
elseif ( TEENSY )
set( VENDOR_ID "0x1C11" CACHE STRING "USB Firmware VID" )
set( PRODUCT_ID "0xB04D" CACHE STRING "USB Firmware PID" )
set( BOOT_VENDOR_ID "0x16c0" CACHE STRING "USB Bootloader VID" )# TODO Double check, this is likely incorrect
set( BOOT_PRODUCT_ID "0x0487" CACHE STRING "USB Bootloader PID" )
message( "Teensy" )
endif ()
#| Compiler flag to set the C Standard level.
#| c89 = "ANSI" C
#| gnu89 = c89 plus GCC extensions
#| c99 = ISO C99 standard
#| gnu99 = c99 plus GCC extensions
#| gnu11 = c11 plus GCC extensions
set( CSTANDARD "-std=gnu11" )
#| Warning Options
#| -Wall...: warning level
set( WARN "-Wall -ggdb3" )
#| Tuning Options
#| -f...: tuning, see GCC manual
#| NOTE: -fshort-wchar is specified to allow USB strings be passed conveniently
#| Bootloader Compiler Flags
if ( BOOTLOADER )
## Clang Compiler
if ( "${COMPILER}" MATCHES "clang" )
# TODO Not currently working, clang doesn't support all the neccessary extensions
message ( AUTHOR_WARNING "clang doesn't support all the needed extensions, code may need rework to use clang" )
set ( TUNING "-D_bootloader_ -Wno-main -msoft-float -target arm-none-eabi -mtbm -fdata-sections -ffunction-sections -fshort-wchar -fno-builtin -fplan9-extensions -fstrict-volatile-bitfields -flto -fno-use-linker-plugin -nostdlib" )
## GCC Compiler
else ()
set ( TUNING "-D_bootloader_ -Wno-main -msoft-float -mthumb -fplan9-extensions -ffunction-sections -fdata-sections -fno-builtin -fstrict-volatile-bitfields -fno-use-linker-plugin -nostdlib" )
#set( TUNING "-mthumb -fdata-sections -ffunction-sections -fno-builtin -msoft-float -fstrict-volatile-bitfields -flto -fno-use-linker-plugin -fwhole-program -Wno-main -nostartfiles -fplan9-extensions -D_bootloader_" )
endif ()
#| Firmware Compiler Flags
else ()
## Clang Compiler
if ( "${COMPILER}" MATCHES "clang" )
set ( TUNING "-target arm-none-eabi -nostdlib -fdata-sections -ffunction-sections -fshort-wchar -fno-builtin ${FLOAT}" )
## GCC Compiler
else ()
set( TUNING "-mthumb -nostdlib -fdata-sections -ffunction-sections -fshort-wchar -fno-builtin -nostartfiles -fstack-protector-all ${FLOAT}" )
endif ()
endif ()
#| Optimization level, can be [0, 1, 2, 3, s].
#| 0 = turn off optimization. s = optimize for size.
#| (Note: 3 is not always the best optimization level.)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set( OPT "0" )
add_definitions("-DDEBUG")
else ()
set( OPT "s" )
endif ()
#| JLink support
#| Used to drop to a breakpoint hardfault handler
#| This isn't something most people would want
if ( JLINK )
add_definitions("-DJLINK")
endif ()
#| Compiler Flags
add_definitions( "-mcpu=${CPU} -DF_CPU=${F_CPU} -D_${CHIP}_=1 -O${OPT} ${TUNING} ${WARN} ${CSTANDARD}" )
#| Linker Flags
if ( BOOTLOADER )
# Bootloader linker flags
set( LINKER_FLAGS "${TUNING} -Wl,--gc-sections -fwhole-program -L${CMAKE_CURRENT_SOURCE_DIR}/../Lib/ld -T${CHIP}.bootloader.ld -nostartfiles -Wl,-Map=link.map" )
else ()
## Clang Compiler
if ( "${COMPILER}" MATCHES "clang" )
set( LINKER_FLAGS "-mcpu=${CPU} ${TUNING} -Wl,-Map=link.map,--cref -Wl,--gc-sections -Wl,--no-wchar-size-warning -L${CMAKE_CURRENT_SOURCE_DIR}/Lib/ld -T${CHIP}.ld" )
else ()
# Normal linker flags
set( LINKER_FLAGS "-mcpu=${CPU} ${TUNING} -Wl,-Map=link.map,--cref -Wl,--gc-sections -Wl,--no-wchar-size-warning -L${CMAKE_CURRENT_SOURCE_DIR}/Lib/ld -T${CHIP}.ld" )
endif ()
endif ()
#| Enable color output with Ninja
if( CMAKE_GENERATOR STREQUAL "Ninja" )
## Clang Compiler
if ( "${COMPILER}" MATCHES "clang" )
add_definitions( "-fcolor-diagnostics" )
## GCC Compiler
else ()
add_definitions( "-fdiagnostics-color=always" )
endif ()
# We always use the gcc linker for arm-none-eabi
set( LINKER_FLAGS "${LINKER_FLAGS} -fdiagnostics-color=always" )
endif ()
#| Hex Flags (XXX, CMake seems to have issues if you quote the arguments for the custom commands...)
set( HEX_FLAGS -O ihex -R .eeprom )
#| Binary Flags
set( BIN_FLAGS -O binary )
#| Lss Flags
if ( "${COMPILER}" MATCHES "clang" )
set( LSS_FLAGS -section-headers -triple=arm-none-eabi )
else ()
set( LSS_FLAGS -h -S -z )
endif ()