-
Notifications
You must be signed in to change notification settings - Fork 4
/
pjit.mk
94 lines (75 loc) · 2.8 KB
/
pjit.mk
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
# All critical output, source folders
OUTDIR := binaries
BINARY := pjit.elf
SRCDIRS := . clib/ hal/ pjit/ src/
CROSS := arm-none-eabi-
# CPU type and common options
CCFLAGS_CPU := \
-fno-exceptions -fno-unwind-tables -nostdlib -fno-common -ffreestanding \
-marm -mcpu=cortex-a8 -mfpu=vfpv3 -mfloat-abi=hard \
-ffunction-sections -fdata-sections -fomit-frame-pointer \
-std=gnu11 -MMD -fmax-errors=5
# Universally good compiler warnings
CCFLAGS_WARN := \
-Wall -Wshadow -Wdouble-promotion -Wformat-overflow -Wformat-truncation \
-Wundef -Wno-unused-parameter -Wno-discarded-qualifiers
# Compiler optimizations
ifeq ($(BUILD),release)
# Optimize for release and raise the bar for errors
CCFLAGS_OPT := -O3 -ffast-math -Werror -Wno-clobbered -s
else
# Optimize for debugging, but include basic optimizers for not-terrible-code
CCFLAGS_OPT := -Og -ftree-vectorize -D_DEBUG
endif
# Any build defintions
DEFINES := -DVERSION="${shell git describe --tags --no-dirty | sed 's/\(.*\)-\(.*\)-.*/\1-\2/'}"
# Combine all the flags so far
CFLAGS := $(CCFLAGS_WARN) $(CCFLAGS_CPU) $(CCFLAGS_OPT) $(DEFINES)
AS := $(QUIET)$(CROSS)gcc
AR := $(QUIET)$(CROSS)ar
SZ := $(QUIET)$(CROSS)size
CC := $(QUIET)$(CROSS)gcc
OC := $(QUIET)$(CROSS)objcopy
DI := $(QUIET)$(CROSS)objdump --disassemble
RM := $(QUIET)rm -rf
RD := $(QUIET)rmdir
MD := $(QUIET)mkdir -p
OUTPUT := $(OUTDIR)/$(BUILD)/$(BINARY)
OBJDIR := $(OUTDIR)/$(BUILD)/obj
BINARY := $(patsubst %.elf,%.bin,$(OUTPUT))
MAPFILE := $(patsubst %.elf,%.map,$(OUTPUT))
DISFILE := $(patsubst %.elf,%.asm,$(OUTPUT))
LDFLAGS := -static -ffreestanding -T linker.lds \
-Wl,--gc-sections \
-Wl,--Map=$(MAPFILE)
INCLUDES := -I./inc $(addprefix -I./,$(SRCDIRS))
SOURCES := $(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c) $(wildcard $(dir)/*.s))
OBJECTS := $(addprefix $(OBJDIR)/,$(addsuffix .o,$(basename $(foreach file,$(SOURCES),$(notdir $(file))))))
VPATH := $(SRCDIRS)
.PHONY: all premake clean debug release
all: premake $(OUTPUT)
clean:
$(RM) $(OBJDIR)/* 2>/dev/null || true
$(RD) $(OBJDIR) 2>/dev/null || true
$(RM) $(OUTPUT) 2>/dev/null || true
$(RM) $(BINARY) 2>/dev/null || true
$(RM) $(MAPFILE) 2>/dev/null || true
$(RM) $(MAPFILE).bak 2>/dev/null || true
$(RM) $(DISFILE) 2>/dev/null || true
$(RM) $(OUTDIR)/$(BUILD) 2>/dev/null || true
premake:
$(MD) $(OBJDIR)
$(OUTPUT): $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $@
$(DI) $@ > $(DISFILE) &
$(OC) -O binary $@ $(BINARY)
$(SZ) $@
$(QUIET)nm --print-size --size-sort --radix=x $@ | grep -v -E "\.debug|\.group" > $(@:%.elf=%.sz)
$(OBJDIR)/%.o: %.c
@echo Compiling $<...
$(CC) $(CFLAGS) $(INCLUDES) -S -c $< -o $(@:%.o=%.s) 2>/dev/null &
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
$(OBJDIR)/%.o: %.s
@echo Assembling $<...
$(AS) $(CFLAGS) -c $< -o $@
-include $(addprefix $(OBJDIR)/,$(notdir $(filter %.d,$(SOURCES:%.c=%.d))))