-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmakefile_global
200 lines (160 loc) · 6.69 KB
/
makefile_global
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
#
# [ M E G A D E V ] a Sega Mega CD devkit
#
# Global Makefile & Build Settings
# Toolchain Setup
# we assume all commands appear somewhere in $PATH
# if this is not the case, you will need to specify the full path in the
# prefix
M68K_PREFIX?=m68k-elf-
CC:=$(M68K_PREFIX)gcc
OBJCPY:=$(M68K_PREFIX)objcopy
NM:=$(M68K_PREFIX)nm
LD:=$(M68K_PREFIX)ld
AS:=$(M68K_PREFIX)as
# (Z80 building not yet supported)
Z80_AS:=sjasmplus
# Hardware Defaults
# in case they are not set in the project makefile
HW_CFG?=REGION=US VIDEO=NTSC VRAM_SIZE=VRAM_64K
################################################################################
# STOP!
# You should not need to change anything below this line unless you really,
# really know what you're doing.
################################################################################
# TODO test that setting this here won't overwrite an existing value
# maybe need to use ?= like below?
# MEGADEV_PATH:=/opt/megadev
# fancy colors cause we're fancy
CLEAR=\033[0m
BOLD=\033[1m
CYAN=\033[1;36m
RED=\033[1;31m
YELLOW=\033[1;33m
GREEN=\033[1;32m
## PATHS
# Megadev library code (ASM and C)
LIB_PATH:=$(MEGADEV_PATH)/lib
# build time tools
TOOLS_PATH:=$(MEGADEV_PATH)/tools
# linker scripts
CFG_PATH:=$(MEGADEV_PATH)/cfg
## build artifacts path
BUILD_PATH:=$(OUT_PATH)/$(SRC_PATH)
# Begin collecting project module configurations
MMD_DEFS:=$(wildcard $(SRC_PATH)/*.mmd.def)
SMD_DEFS:=$(wildcard $(SRC_PATH)/*.smd.def)
BIN_DEFS:=$(wildcard $(SRC_PATH)/*.bin.def)
MODULE_DEFS:=$(strip $(MMD_DEFS) $(SMD_DEFS) $(BIN_DEFS))
MODULES:=$(basename $(notdir $(MODULE_DEFS)))
#$(info MODULES: $(MODULES))
MODULES_OUT:=$(addprefix $(DISC_PATH)/,$(MODULES))
#$(info MODULES_OUT: $(MODULES_OUT))
# setup includes
INC:=-I$(SRC_PATH) -I$(LIB_PATH) -I$(RES_PATH) -I$(BUILD_PATH)
# We have to manually pass the includes to the assembler
# Each entry here should match each entry in INC, with a prefix of "-Wa,"
AS_INC:=-Wa,-I$(SRC_PATH) -Wa,-I$(LIB_PATH) -Wa,-I$(RES_PATH) -Wa,-I$(BUILD_PATH)
# build flags
# Note: we include "-Wa,--register-prefix-optional" as a default C option
# because it is useful for inline asm. However, "-Wa,--bitwise-or" will
# cause issues with the GCC created asm, so we split that off and only use it
# with asm source files
CC_FLAGS:=-m68000 -imacros build_def.h $(OPT_FLAGS) $(addprefix -D, $(HW_CFG)) $(if $(DEBUG), -DDEBUG) -fno-builtin -fomit-frame-pointer -fno-gcse -Wall -Wextra -Wno-main -Wa,--register-prefix-optional
AS_FLAGS:=-Wa,--bitwise-or
LD_FLAGS:=-nostdlib
define msg_info
@echo -e "${BOLD}${CYAN}$(1)${CLEAR}"
endef
define msg_warning
@echo -e "${BOLD}${RED}$(1)${CLEAR}"
endef
vpath %.mmd.def $(SRC_PATH)
vpath %.smd.def $(SRC_PATH)
vpath %.bin.def $(SRC_PATH)
vpath %.c $(SRC_PATH):$(LIB_PATH):$(LIB_PATH)/sub:$(LIB_PATH)/main
vpath %.h $(SRC_PATH):$(LIB_PATH):$(LIB_PATH)/sub:$(LIB_PATH)/main
vpath %.s $(SRC_PATH):$(LIB_PATH):$(LIB_PATH)/sub:$(LIB_PATH)/main
vpath %.mmd $(DISC_PATH)
vpath %.smd $(DISC_PATH)
vpath %.bin $(DISC_PATH)
vpath %.elf $(BUILD_PATH)
vpath %.c.o $(BUILD_PATH)
vpath %.s.o $(BUILD_PATH)
# need to specify paths here as they're called through a secondary make
$(BUILD_PATH)/%.c.o: %.c
$(call msg_info,Compiling source $(notdir $<))
@$(CC) $(CC_FLAGS) $(INC) -c $^ -o $@
$(BUILD_PATH)/%.s.o: %.s
$(call msg_info,Compiling source $(notdir $<))
@$(CC) $(CC_FLAGS) $(AS_FLAGS) $(INC) $(AS_INC) -x assembler-with-cpp -c $^ -o $@
# TODO merge the module exports since they're 99% identical
$(DISC_PATH)/%.mmd: $(BUILD_PATH)/%.mmd.elf
$(call msg_info,Exporting module $(notdir $@))
@$(OBJCPY) -O binary $< $@
$(DISC_PATH)/%.smd: $(BUILD_PATH)/%.smd.elf
$(call msg_info,Exporting module $(notdir $@))
@$(OBJCPY) -O binary $< $@
$(DISC_PATH)/%.bin: $(BUILD_PATH)/%.bin.elf
$(call msg_info,Exporting module $(notdir $@))
@$(OBJCPY) -O binary $< $@
# TODO merge the module linking rules since they're 99% identical
# (move it into a function that takes the linker script, maybe)
$(BUILD_PATH)/%.mmd.elf: %.mmd.def
$(eval LIST:=$(addprefix $(BUILD_PATH)/,$(file <$<)))
$(eval REQ_LIST:=$(addsuffix .elf, $(filter-out %.c %.s, $(LIST))))
$(eval BUILD_LIST:=$(addsuffix .o, $(filter %.c %.s, $(LIST))))
@$(MAKE) -s $(REQ_LIST)
@$(MAKE) -s $(BUILD_LIST)
$(call msg_info,Linking module $(notdir $@))
@$(LD) $(LD_FLAGS) -z muldefs -T $(CFG_PATH)/module_mmd.ld $(BUILD_LIST) $(foreach symref,$(REQ_LIST),-R $(symref)) -o $@
@$(NM) -n $@ > $(basename $@).sym
$(BUILD_PATH)/%.smd.elf: %.smd.def
$(eval LIST:=$(addprefix $(BUILD_PATH)/,$(file <$<)))
$(eval REQ_LIST:=$(addsuffix .elf, $(filter-out %.c %.s, $(LIST))))
$(eval BUILD_LIST:=$(addsuffix .o, $(filter %.c %.s, $(LIST))))
@$(MAKE) -s $(REQ_LIST)
@$(MAKE) -s $(BUILD_LIST)
$(call msg_info,Linking module $(notdir $@))
@$(LD) $(LD_FLAGS) -z muldefs -T $(CFG_PATH)/module_smd.ld $(BUILD_LIST) $(foreach symref,$(REQ_LIST),-R $(symref)) -o $@
@$(NM) -n $@ > $(basename $@).sym
$(BUILD_PATH)/%.bin.elf: %.bin.def
$(eval LIST:=$(addprefix $(BUILD_PATH)/,$(file <$<)))
$(eval REQ_LIST:=$(addsuffix .elf, $(filter-out %.c %.s, $(LIST))))
$(eval BUILD_LIST:=$(addsuffix .o, $(filter %.c %.s, $(LIST))))
@$(MAKE) -s $(REQ_LIST)
@$(MAKE) -s $(BUILD_LIST)
$(call msg_info,Linking module $(notdir $@))
@$(LD) $(LD_FLAGS) -z muldefs -T $(CFG_PATH)/module_bin.ld $(BUILD_LIST) $(foreach symref,$(REQ_LIST),-R $(symref)) -o $@
@$(NM) -n $@ > $(basename $@).sym
default: init boot_sector modules iso
.PHONY: init boot_sector modules iso clean
.SECONDARY:
clean:
$(call msg_warning,Removing build artifacts & disc image)
@rm -rf $(BUILD_PATH)/* $(MODULES_OUT)
@rm -rf $(OUT_PATH)/$(PROJECT_NAME).iso
boot_sector: $(BUILD_PATH)/boot.bin
modules: $(MODULES_OUT)
# TODO make the ISO settings user configurable
iso: $(BUILD_PATH)/boot.bin
$(call msg_info,Generating ISO image $(PROJECT_NAME).iso)
@mkisofs -quiet -iso-level 1 -G $< -pad -V "$(PROJECT_NAME)" \
-sysid "MEGA_CD" -appid "" -publisher "" -preparer "" \
-o $(OUT_PATH)/$(PROJECT_NAME).iso $(DISC_PATH)
init:
@mkdir -p $(BUILD_PATH) $(DISC_PATH) $(SRC_PATH)
# special rules for boot sector binaries
$(BUILD_PATH)/ip.bin.elf: $(BUILD_PATH)/ip.s.o
@$(LD) $(LD_FLAGS) -T$(CFG_PATH)/ip.ld -o$@ $<
@$(NM) -n $@ > $(basename $@).sym
$(BUILD_PATH)/sp.bin.elf: $(BUILD_PATH)/sp_header.s.o $(BUILD_PATH)/sp.s.o
@$(LD) $(LD_FLAGS) -T$(CFG_PATH)/sp.ld -o$@ $^
@$(NM) -n $@ > $(basename $@).sym
$(BUILD_PATH)/boot.s.o: ip.bin.elf sp.bin.elf
@$(OBJCPY) -O binary $(BUILD_PATH)/ip.bin.elf $(BUILD_PATH)/ip.bin
@$(OBJCPY) -O binary $(BUILD_PATH)/sp.bin.elf $(BUILD_PATH)/sp.bin
@$(CC) $(CC_FLAGS) $(AS_FLAGS) $(INC) $(AS_INC) -x assembler-with-cpp -c $(LIB_PATH)/boot.s -o$@
$(BUILD_PATH)/boot.bin: $(BUILD_PATH)/boot.s.o
$(call msg_info,Generating boot sector...)
@$(OBJCPY) -O binary $< $@