aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: 8da65eb91438f55c5c8decc76b8da4d8bb5ecc9c (plain)
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
.POSIX:
.SUFFIXES:

# Path to the development kit (devkitARM) and the GBA library.
DEVKITPRO   := /opt/devkitpro
DEVKITBIN   := $(DEVKITPRO)/devkitARM/bin
DEVKITTOOLS := $(DEVKITPRO)/tools/bin
LIBGBA_DIR  := $(DEVKITPRO)/libgba
LIBGBA_SRC  := $(DEVKITPRO)/libgba/include/
LIBGBA      := $(LIBGBA_DIR)/lib/libgba.a
LIBGBA      += $(LIBGBA_DIR)/lib/libfat.a
LIBGBA      += $(LIBGBA_DIR)/lib/libmm.a

# Source code location and files to watch for changes.
SRC_DIR     := src
BUILD_DIR   := build
SRC_MAIN    := $(SRC_DIR)/main.c
UXN_ROM     := roms/dvd.rom
ROM_FILE    := $(BUILD_DIR)/uxn.rom
ROM_OBJ     := $(BUILD_DIR)/rom.o
ASM_FILES   := $(wildcard $(SRC_DIR)/*.s)
WATCH_SRC   := $(shell find $(SRC_DIR) -name *.c -or -name *.s -or -name *.h)
INC_DIRS    := $(shell find $(SRC_DIR) -type d)
INC_DIRS    += $(BUILD_DIR)
INC_FLAGS   := $(addprefix -I,$(INC_DIRS))
INC_FLAGS   += -I$(LIBGBA_SRC)

# Output library names and executables.
TARGET         := uxngba
ELF            := $(BUILD_DIR)/$(TARGET).elf
BIN            := $(BUILD_DIR)/$(TARGET).gba

# Compiler and linker configuration.
CC             := $(DEVKITBIN)/arm-none-eabi-gcc
OBJCOPY        := $(DEVKITBIN)/arm-none-eabi-objcopy
ARCH           := -mthumb -mthumb-interwork
SPECS          := -specs=gba.specs
CONFIG         :=
CFLAGS         := -Wall -Wextra -pedantic -Wno-incompatible-pointer-types -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter
CFLAGS         += -fno-strict-aliasing
CFLAGS         += -mcpu=arm7tdmi -mtune=arm7tdmi $(ARCH)
CFLAGS         += $(INC_FLAGS)
CFLAGS         += $(CONFIG)
LDFLAGS        := $(ARCH) $(SPECS)
LDLIBS         := $(LIBGBA)
RELEASE_CFLAGS := -DNDEBUG -O2
DEBUG_CFLAGS   := -DDEBUG -O2 -g

.PHONY: clean run

# Setup debug/release builds.
#     make clean && make <target> DEBUG=0
#     make clean && make <target> DEBUG=1
DEBUG ?= 0
ifeq ($(DEBUG), 1)
    CFLAGS += $(DEBUG_CFLAGS)
else
    CFLAGS += $(RELEASE_CFLAGS)
endif

main: $(BUILD_DIR) $(ROM) $(BIN)

# Creates the rom object file from the given UXN_ROM.
$(ROM_FILE):
	cp $(UXN_ROM) $(ROM_FILE)

$(ROM_OBJ): $(ROM_FILE)
	$(OBJCOPY) --rename-section .data=.rodata \
		-I binary -O elf32-littlearm $(ROM_FILE) $(ROM_OBJ)

# Strip and fix header to create final .gba file.
$(BIN): $(ELF)
	$(OBJCOPY) -v -O binary $(ELF) $(BIN)
	$(DEVKITTOOLS)/gbafix $(BIN)
	rm $(ROM_FILE)

# Link files.
$(ELF): $(SRC_MAIN) $(WATCH_SRC) $(ROM_OBJ) | $(BUILD_DIR)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(ELF) $(SRC_MAIN) $(ASM_FILES) $(LDLIBS) $(ROM_OBJ)

# Create build directory if needed.
$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

# Test the output .gba in an emulator.
run: main
	mgba-qt $(BIN)

# Remove build directory.
clean:
	rm -rf $(BUILD_DIR)