-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile2.mk
More file actions
525 lines (369 loc) · 13.5 KB
/
Makefile2.mk
File metadata and controls
525 lines (369 loc) · 13.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# Setup from main Makefile
# ------------------------
# AARCH = 32 or 64 .... use 32-bit or 64-bit architecture if available: RASPPI=3 or 4 (64-bit preferred)
# RASPPI = 1..5 ... Raspberry model class
# DEVCLASS=base .... Setup device class (base = default board)
# PI_ROOT_PATH=../.. ... Path to root directory from the project directory (without trailing '/' delimiter)
# TARGET = target filename (without extension)
##############################################################################
# Makefile 2nd stage - Build
##############################################################################
# Path to root directory from the project directory (without trailing '/' delimiter)
ifeq ($(PI_ROOT_PATH),)
PI_ROOT_PATH = ../../..
endif
# Path to _font directory (without trailing '/' delimiter)
ifeq ($(PI_FONT_DIR),)
PI_FONT_DIR = ${PI_ROOT_PATH}/_font
endif
# Path to _lib directory (without trailing '/' delimiter)
ifeq ($(PI_LIB_DIR),)
PI_LIB_DIR = ${PI_ROOT_PATH}/_lib
endif
# Path to _loader directory (without trailing '/' delimiter)
ifeq ($(PI_LOADER_DIR),)
PI_LOADER_DIR = ${PI_ROOT_PATH}/_loader
endif
# Path to _sdk directory (without trailing '/' delimiter)
ifeq ($(PI_SDK_DIR),)
PI_SDK_DIR = ${PI_ROOT_PATH}/_sdk
endif
# Path to _tools directory (without trailing '/' delimiter)
ifeq ($(PI_TOOLS_DIR),)
PI_TOOLS_DIR = ${PI_ROOT_PATH}/_tools
endif
##############################################################################
# Input files
# Do not include stdio.h file
DEFINE += -D _STDIO_H_
# SDK
include ${PI_SDK_DIR}/_makefile.inc
# Libraries
include ${PI_LIB_DIR}/_makefile.inc
# fonts
include ${PI_FONT_DIR}/_makefile.inc
# C code define include base "includes.h" file
DEFINE += -D INCLUDES_H=\"${PI_ROOT_PATH}/includes.h\"
# C code define include base "config_def.h" file
DEFINE += -D CONFIG_DEF_H=\"${PI_ROOT_PATH}/config_def.h\"
# Includes
IPATH += -I.
##############################################################################
# Configuration
# temporary build directory
TEMP = ./build
# Base makefile (to ensure recompilation after change)
MAKEFILE = ./Makefile
# Base include file (to ensure recompilation after change)
INCLUDEFILE = ./include.h
# Linker script
LDSCRIPT = ${PI_ROOT_PATH}/linker.ld
##############################################################################
# Passing configuration parameters to the source code
# SDK subdir
DEFINE += -D SDK_SUBDIR=${PI_SDK_DIR}
# Run mode
DEFINE += -D RUNMODE=${RUNMODE}
# Architecture 32/64 bits
DEFINE += -D AARCH=${AARCH}
# RASPPI
DEFINE += -D RASPPI=${RASPPI}
# Number of cores
DEFINE += -D CORES=${CORES}
# Target filename
DEFINE += -D TARGET=\"${TARGET}\"
# System stack size
DEFINE += -D SYS_STACK_SIZE=${SYS_STACK_SIZE}
# Interrupt stack size (used for ABORT, IRQ and FIQ)
DEFINE += -D INT_STACK_SIZE=${INT_STACK_SIZE}
# Core stack size
DEFINE += -D CORE_STACK_SIZE=${CORE_STACK_SIZE}
# Load address
DEFINE += -D LOAD_ADDR=${LOAD_ADDR}
# Page table size
DEFINE += -D PAGE_TABLE_SIZE=${PAGE_TABLE_SIZE}
##############################################################################
# Linker setup
# verbose - use this to see linker errors
#LDFLAGS += -Wl,-verbose
# Architecture
LDFLAGS += $(ARCHCFG)
# Entry point
LDFLAGS += --entry _start
# Section start
LDFLAGS += -Wl,--section-start=.init=$(LOAD_ADDR)
# No built-in functions (This is case when compiller changes printf("x") to putchar('x'))
LDFLAGS += -Wl,--build-id=none
# Link with libnosys.a (libc without system functions)
# LDFLAGS += -Wl,--specs=nano.specs
# Generate map file
LDFLAGS += -Wl,-Map=$(TARGET).map
# Linker script
LDFLAGS += -T $(LDSCRIPT)
# Eliminate unused sections
LDFLAGS += -Wl,--gc-sections
# No warnings "LOAD segment with RWX permissions"
LDFLAGS += -Wl,--no-warn-rwx-segment
# No start files
LDFLAGS += -nostartfiles
# wrap library functions
ifeq ($(USE_MALLOC),1)
LDFLAGS += -Wl,--wrap=malloc
LDFLAGS += -Wl,--wrap=free
LDFLAGS += -Wl,--wrap=realloc
endif
LDFLAGS += -Wl,--wrap=memset
LDFLAGS += -Wl,--wrap=memcpy
LDFLAGS += -Wl,--wrap=memcmp
LDFLAGS += -Wl,--wrap=memmove
ifeq ($(USE_PRINT),1)
LDFLAGS += -Wl,--wrap=vsprintf
LDFLAGS += -Wl,--wrap=vsnprintf
LDFLAGS += -Wl,--wrap=sprintf
LDFLAGS += -Wl,--wrap=snprintf
LDFLAGS += -Wl,--wrap=putchar
LDFLAGS += -Wl,--wrap=puts
LDFLAGS += -Wl,--wrap=vprintf
LDFLAGS += -Wl,--wrap=printf
LDFLAGS += -Wl,--wrap=getchar
endif
# Linker script symbols
LDFLAGS += -Wl,--defsym=CORES=${CORES}
LDFLAGS += -Wl,--defsym=SYS_STACK_SIZE=${SYS_STACK_SIZE}
LDFLAGS += -Wl,--defsym=INT_STACK_SIZE=${INT_STACK_SIZE}
LDFLAGS += -Wl,--defsym=CORE_STACK_SIZE=${CORE_STACK_SIZE}
LDFLAGS += -Wl,--defsym=LOAD_ADDR=${LOAD_ADDR}
##############################################################################
# Common compiler setup
# Output dependency rule
ARCHCFG += -MMD
# Add phony target for each dependency
ARCHCFG += -MP
# Dependency file
ARCHCFG += -MF"$(@:%.o=%.d)"
# Target dependency generation
ARCHCFG += -MT"$(@)"
##############################################################################
# ASM setup
# Common compiler setup
AFLAGS += $(ARCHCFG)
# Default language is assembler
AFLAGS += -x assembler-with-cpp
##############################################################################
# C setup
# Common compiler setup
CFLAGS += $(ARCHCFG)
# Optimization level
#CFLAGS += -O0 # do no optimization
#CFLAGS += -O1 # optimize minimally
#CFLAGS += -O2 # optimize more
#CFLAGS += -O3 # optimize even more
CFLAGS += -Ofast # optimize for speed
#CFLAGS += -Og -g3 # optimize for debugger (use together with -g0..-g3, level of debugging information)
#CFLAGS += -Os # optimize for size
# Generate uninitialized global variable (to check multiple declarations)
CFLAGS += -fno-common
# Warn if variable is not initialized
CFLAGS += -Wuninitialized
# Create separate sections (to enable eliminate unused sections)
CFLAGS += -ffunction-sections -fdata-sections
# Use unsigned char (this is default for ARM cpu)
CFLAGS += -funsigned-char
# Report if function does not return value
CFLAGS += -Wreturn-type
# Warning as errors
CFLAGS += -Werror
# do not assume usual definitions
CFLAGS += -ffreestanding
# do not use builtin memset (it crashes on unaligned uncached memory)
CFLAGS += -fno-builtin-memset
# do not use builtin memcpy (it crashes on unaligned uncached memory)
CFLAGS += -fno-builtin-memcpy
# using coherent memory region
# Prohibiting non-aligned access to peripherals and shared memory with the GPU, as they do not allow it.
ifeq ($(strip $(AARCH)),32)
CFLAGS += -mno-unaligned-access
else
CFLAGS += -mstrict-align
endif
# ===== Disable optimizations. If some operations cause an error, try disabling some optimizations.
# Optimizations -fno-tree-vrp and -fno-tree-dominator-opts must be disabled in GCC v15.2
# - they caused an error in the calculation of the DateTime64UnpackAbsTime() function.
# --- Promotion of constants and rearrangement of expressions
# These optimizations may change the order of calculations, merge expressions, or remove
# intermediate results that are diagnostically visible but "unnecessary" to GCC.
# Value Range Propagation
CFLAGS += -fno-tree-vrp
CFLAGS += -fno-tree-forwprop
# Full Redundancy Elimination
CFLAGS += -fno-tree-fre
# Scalar Replacement of Aggregates
#CFLAGS += -fno-tree-sra
# Dead Code Elimination
CFLAGS += -fno-tree-dce
CFLAGS += -fno-tree-dominator-opts
# --- Optimizations that can break integer calculations
# These optimizations manipulate expressions of the type x / C and can
# generate erroneous instruction sequences if a specific pattern occurs.
# Partial Redundancy Elimination
CFLAGS += -fno-tree-pre
CFLAGS += -fno-split-wide-types
CFLAGS += -fno-tree-scev-cprop
CFLAGS += -fno-tree-dse
# --- Optimizations that can break instruction order and registry operations
# If GCC misestimates the lifetime of registers or dependencies between instructions,
# it may happen that the operands are correct but the result is wrong.
CFLAGS += -fno-schedule-insns
CFLAGS += -fno-schedule-insns2
CFLAGS += -fno-reorder-blocks
#CFLAGS += -fno-reorder-functions
#CFLAGS += -fno-cse-follow-jumps
#CFLAGS += -fno-cse-skip-blocks
# --- Optimizations that can break arithmetic due to aliasing or reordering
#CFLAGS += -fno-strict-aliasing
# Temporary Expression Replacement
CFLAGS += -fno-tree-ter
#CFLAGS += -fno-tree-coalesce-vars
# --- Optimizations that can break code in ARM bare-metal environments
# Interprocedural optimizations are a frequent source of errors
# if GCC misjudges the purity of a function or its side effects.
# Interprocedural Constant Propagation
CFLAGS += -fno-ipa-cp
CFLAGS += -fno-ipa-sra
#CFLAGS += -fno-ipa-pure-const
#CFLAGS += -fno-ipa-reference
# Register Allocation across functions
CFLAGS += -fno-ipa-ra
##############################################################################
# C++ setup
# Common compiler setup
CPPFLAGS += $(CFLAGS)
# Do not use exceptions and unwind frames
CPPFLAGS += -fno-exceptions -fno-unwind-tables
# No run-time, no typeinfo, no dynamic_cast
CPPFLAGS += -std=c++14 -fno-rtti
# No warning on "new alignas(xx)"
CPPFLAGS += -Wno-aligned-new
# No default C++ headers
CPPFLAGS += -nostdinc++
# No destructors
CPPFLAGS += -fno-use-cxa-atexit
# Do not lock local static variable
CPPFLAGS += -fno-threadsafe-statics
CPPFLAGS += -fno-asynchronous-unwind-tables
CPPFLAGS += -fno-non-call-exceptions
# Switch off schrink-wrapping (not recommended to use)
#CPPFLAGS += -fno-shrink-wrap -fno-shrink-wrap-separate
##############################################################################
# Compiler tools
# C compiler name
CC = ${PREFIX}gcc
# C++ compiler name
CPP = ${PREFIX}g++
# ASM compiler name
AS = ${PREFIX}gcc
# Linker
LD = ${PREFIX}g++
# Object dump
OD = ${PREFIX}objdump
# Object copy
OC = ${PREFIX}objcopy
# Names
NM = ${PREFIX}nm
# Size
SZ = ${PREFIX}size
##############################################################################
# File list
# List of all source files
ALLSRC = $(ASRC) $(CSRC) $(SRC)
# List of all files without directory and without extension
ALLBASE = $(notdir $(basename $(ALLSRC)))
# List of all object files *.o
ALLOBJ = $(addprefix $(TEMP)/, $(addsuffix .o, $(ALLBASE)))
ifeq ($(strip $(AARCH)),64)
CRTBEGIN = "$(shell $(CPP) -print-file-name=crtbegin.o)"
CRTEND = "$(shell $(CPP) -print-file-name=crtend.o)"
endif
##############################################################################
# Clear temporary directory (use "make clean")
clean:
@${PI_TOOLS_DIR}/rm -rf $(TEMP)
@${PI_TOOLS_DIR}/rm -f ./$(TARGET).lst ./$(TARGET).map ./$(TARGET).sym ./$(TARGET).siz ./$(TARGET).elf
##############################################################################
# Compile all (use "make all")
all: createdirs elf IMG lst sym siz
##############################################################################
# Create temporary directory
createdirs:
ifeq (${OS},Windows_NT)
@${PI_TOOLS_DIR}/mkdir.exe -p $(TEMP)
else
@mkdir -p $(TEMP)
endif
@echo Target: ${TARGET}, Device: ${DEVCLASS}, RunMode: ${RUNMODE}
##############################################################################
# Dependencies of output files
elf: ./$(TARGET).elf
IMG: ./$(TARGET).IMG
lst: ./$(TARGET).lst
sym: ./$(TARGET).sym
siz: ./$(TARGET).siz
##############################################################################
# Create .IMG file from .elf file
./$(TARGET).IMG: ./$(TARGET).elf
@echo IMG $@
@$(OC) -O binary $< $@
##############################################################################
# Create .lst file from .elf file
./$(TARGET).lst: ./$(TARGET).elf
@echo lst $@
@$(OD) --all-headers -d -S $< > $@
##############################################################################
# Create .sym file from .elf file
./$(TARGET).sym: ./$(TARGET).elf
@echo sym $@
@$(NM) -n $< > $@
##############################################################################
# Create .siz file from .elf file
./$(TARGET).siz: ./$(TARGET).elf
@echo siz $@
@$(SZ) $< > $@
@echo.
@type $@
##############################################################################
# Create .o files from .c files
define CC_TEMP
$(TEMP)/$(notdir $(basename $(1))).o : $(1) $(MAKEFILE) $(INCLUDEFILE)
@echo CC $$<
@$(CC) $$(DEFINE) $$(IPATH) $$(CFLAGS) -std=gnu11 -c $$< -o $$@
endef
$(foreach src, $(CSRC), $(eval $(call CC_TEMP, $(src))))
##############################################################################
# Create .o files from .cpp files
define CPP_TEMP
$(TEMP)/$(notdir $(basename $(1))).o : $(1) $(MAKEFILE) $(INCLUDEFILE)
@echo C++ $$<
@$(CPP) $$(DEFINE) $$(IPATH) $$(CPPFLAGS) -c $$< -o $$@
endef
$(foreach src, $(SRC), $(eval $(call CPP_TEMP, $(src))))
##############################################################################
# Create .o files from .S files
define AS_TEMP
$(TEMP)/$(notdir $(basename $(1))).o : $(1) $(MAKEFILE) $(INCLUDEFILE)
@echo ASM $$<
@$(CC) $$(DEFINE) $$(IPATH) $$(AFLAGS) -c $$< -o $$@
endef
$(foreach src, $(ASRC), $(eval $(call AS_TEMP, $(src))))
##############################################################################
# Create .elf from .o files
./$(TARGET).elf: $(ALLOBJ) $(MAKEFILE) $(LDSCRIPT)
@echo ld $@
@$(LD) $(LDFLAGS) $(CRTBEGIN) $(ALLOBJ) $(CRTEND) -o $@
##############################################################################
# Head dependencies, to recompile C after changing H
ifneq (${MAKECMDGOALS},clean)
-include $(wildcard $(TEMP)/*.d)
endif
##############################################################################
# List of dependencies not creating files
.PHONY: clean all createdirs elf IMG lst sym siz uf2