-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
147 lines (111 loc) · 4.2 KB
/
Makefile
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
# ================================ project options
PROJECT := snake
GAME := snake
INFO := gameinfo.properties
BUILD_DIR := build
DATA_DIR := data
GEN_DIR := gen
BIN_DIR := ../../bin
UZEM_RUN_OPTS := -f
AUX_TARGETS := $(GAME).hex # $(GAME).uze $(GAME).lss $(GAME).eep
AUX_DEPS := Makefile $(wildcard $(DATA_DIR)/*)
KERNEL_OPTS := -DVIDEO_MODE=1 -DINTRO_LOGO=0 -DSOUND_MIXER=0 -DMODE1_FAST_VSYNC=0
KERNEL_OPTS += -DTILE_WIDTH=8 -DTILE_HEIGHT=8 -DTRUE_RANDOM_GEN=1
AUX_LD_FLAGS := # -Wl,--section-start,.noinit=0x800100 -Wl,--section-start,.data=0x800500
FILES_C := $(shell find . -type f -iname '*.c')
# --------------------------------
FILES_C := $(patsubst ./%,%,$(FILES_C))
AUX_TARGETS := $(patsubst %,$(BUILD_DIR)/%,$(AUX_TARGETS))
DEP_DIR := $(BUILD_DIR)/dep
# ================================ other globals
MCU := atmega644
CC := avr-gcc
UZEBOX := ../../uzebox
KERNEL := $(UZEBOX)/kernel
# https://stackoverflow.com/a/8942216/
export PATH := $(PATH):$(UZEBOX)/bin:../bin
TARGET := $(BUILD_DIR)/$(GAME).elf
HEX_FLASH_FLAGS := -R .eeprom
HEX_EEPROM_FLAGS := -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings
# ================================ build flags
# options common to compile, link, and assembly rules
COMMON := -mmcu=$(MCU)
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2 -std=gnu99 -DF_CPU=28636360UL -Os
CFLAGS += -fsigned-char -ffunction-sections -fno-toplevel-reorder
CFLAGS += -MD -MP -MT $(*F).o -MF $(DEP_DIR)/$(@F).d
CFLAGS += $(KERNEL_OPTS)
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2
LDFLAGS := $(COMMON)
LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(GAME).map
LDFLAGS += -Wl,-gc-sections
LDFLAGS += $(AUX_LD_FLAGS)
INCLUDES := -I"$(KERNEL)"
# ================================ required objects
FILES_KERNEL := uzeboxVideoEngineCore.s uzeboxSoundEngineCore.s
FILES_KERNEL += uzeboxCore.c uzeboxSoundEngine.c uzeboxVideoEngine.c
FILES_KERNEL := $(patsubst %,$(KERNEL)/%,$(FILES_KERNEL))
# objects that must be built in order to link
OBJECTS := $(notdir $(FILES_KERNEL)) $(FILES_C)
OBJECTS := $(patsubst %.c,%.o,$(OBJECTS))
OBJECTS := $(patsubst %.s,%.o,$(OBJECTS))
OBJECTS := $(patsubst %,$(BUILD_DIR)/%,$(OBJECTS))
# ================================ build
.PHONY: all clean size run uze
all: $(TARGET) $(AUX_TARGETS)
# -------------------------------- ensure build dir gets created first
$(BUILD_DIR):
mkdir $(BUILD_DIR)
mkdir $(DEP_DIR)
mkdir $(GEN_DIR)
# order-only prerequisite; OBJECTS don't depend on modification date anymore
$(OBJECTS): $(AUX_DEPS) | $(BUILD_DIR)
# -------------------------------- compile kernel files
$(BUILD_DIR)/%.o: $(KERNEL)/%.c
@echo "CC $<"
@$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/%.o: $(KERNEL)/%.s
@echo "CC $<"
@$(CC) $(INCLUDES) $(ASMFLAGS) -c $< -o $@
# -------------------------------- compile or generate game files
$(BUILD_DIR)/%.o: %.c | $(GEN_DIR)
@echo "CC $<"
@$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@
# TODO: figure out how to get just the C files that actually depend on the inc/h files to have rules
# depending on them
$(GEN_DIR): $(patsubst $(DATA_DIR)/%.png,$(GEN_DIR)/%.inc, \
$(shell find $(DATA_DIR) -type f -iname '*.png'))
$(DATA_DIR)/%.png: $(DATA_DIR)/%.map.json
$(GEN_DIR)/%.inc $(GEN_DIR)/%.h: $(DATA_DIR)/%.png # $(DATA_DIR)/%.map.json
$(BIN_DIR)/tile_converter.py $< $(GEN_DIR)
# -------------------------------- final targets
$(TARGET): $(OBJECTS)
@$(CC) $(LDFLAGS) $(OBJECTS) -o $@
%.hex: $(TARGET)
avr-objcopy -O ihex $(HEX_FLASH_FLAGS) $< $@
%.eep: $(TARGET)
-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0
%.lss: $(TARGET)
avr-objdump -h -S $< > $@
uze: $(TARGET:.elf=.uze)
%.uze: $(TARGET:.elf=.hex)
-packrom $< $@ $(INFO)
# ================================ utility rules
clean:
-$(RM) -rf $(BUILD_DIR)
-$(RM) -rf $(GEN_DIR)
UNAME := $(shell sh -c 'uname -s 2>/dev/null || echo not')
AVRSIZEFLAGS := -A $(TARGET)
ifneq (,$(findstring MINGW,$(UNAME)))
AVRSIZEFLAGS := -C --mcu=$(MCU) $(TARGET)
endif
size: $(TARGET)
@avr-size $(AVRSIZEFLAGS)
run: $(BUILD_DIR)/$(GAME).hex
uzem $(UZEM_RUN_OPTS) $<
# ================================ add any other dependencies
-include $(wildcard $(DEP_DIR)/*)