-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMakefile
More file actions
191 lines (164 loc) · 6.07 KB
/
Makefile
File metadata and controls
191 lines (164 loc) · 6.07 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
# Get the directory of this Makefile
export MKFILE_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
export TMPDIR=/tmp
# -------- Directory layout --------
BUILDDIR := $(MKFILE_DIR)/build
OBJDIR := $(BUILDDIR)
LIBDIR := $(MKFILE_DIR)/lib
DATA := $(MKFILE_DIR)/data
ifneq ($(CXX),arm-none-eabi-g++)
# Not on GBA. set bin2s dir to tools dir,
# because we need to use our locally built tool. (see bin2s_tool target below)
export BIN2S_DIR := $(MKFILE_DIR)/tools/bin2s/
else
# On GBA, bin2s comes with devkitPro.
export BIN2S_DIR :=
endif
#---------------------------------------------------------------------------------
# canned command sequence for converting binary data
# Taken from https://github.com/devkitPro/devkitarm-rules/blob/master/base_tools
#---------------------------------------------------------------------------------
define bin2o
tmpasm="$(OBJDIR)/$(@F).tmp.s"; \
$(BIN2S_DIR)bin2s -a 4 -H $$(echo $(<F) | tr . _)".h" $< > $$tmpasm && \
$(CC) -x assembler-with-cpp $(CPPFLAGS) $(ASFLAGS) -c $$tmpasm -o $@ && \
rm -f $$tmpasm
endef
# Like Poke_Transporter_GB, this is a multi-stage Makefile.
# If we are not in the build/ subdirectory, we are in the top-level dir.
# We need the multiple stages because we need to generate the tables before we can compile
# the rest of our library. (because we need them to get picked up in BINFILES)
ifneq (build,$(notdir $(CURDIR)))
# -------- Cleanup --------
# -------- Top-level targets --------
.PHONY: all lib clean dirs symlinks
all: dirs generate_tables bin2s_tool lib
lib: generate_tables bin2s_tool
@$(MAKE) -C build -f $(MKFILE_DIR)/Makefile
cp build/*.h lib/include/
cp -r include lib/
# Ensure the build directories exist
dirs:
@mkdir -p $(OBJDIR) $(LIBDIR) $(LIBDIR)/include
# This tool is normally bundled with devkitPro, but it's not available on Linux or Windows.
# Since we want to allow people to build PCCS independently of Poke_Transporter_GB,
# we have bundled a copy of this tool in the tools/ directory.
# But that means we need to build it ourselves here when not building for GBA.
bin2s_tool:
ifneq ($(CXX),arm-none-eabi-g++)
$(MAKE) -C $(MKFILE_DIR)/tools/bin2s
else
@echo "Skip building bin2s, it comes with DevkitPro!"
endif
# generate_tables will generate .bin files for various datatables relevant for the pokemon games.
# This is done this way because we want to be able to compress them for Poke_Transporter_GB on gba.
# regardless, these bin files - compressed or not- will get injected into the executable with "bin2o" later.
#
# NOTE on the use of env below:
# We need to nuke our environment in order to default to the system compiler for table-generator.
# We don't want to compile table-generator for the target host. Only for the build host.
# But for compatibility with MSYS2 MinGW64 in Windows, we can't use env -i
# Instead we need to use env like below and make sure we pass things like the temp dirs
# otherwise it won't compile with MinGW64
generate_tables:
mkdir -p data
mkdir -p to_compress
@env - \
PATH="$(PATH)" \
TMPDIR=/tmp TMP=/tmp TEMP=/tmp \
SYSTEMROOT="$(SYSTEMROOT)" \
CC=cc \
CXX=c++ \
CFLAGS= \
CXXFLAGS= \
LDFLAGS= \
AR=ar \
$(MAKE) -C tools/table-generator
@echo
@echo "----------------------------------------------------------------"
@echo
@tools/table-generator/table-generator to_compress
ifeq ($(CXX),arm-none-eabi-g++)
@echo "Compressing bin files for GBA!"
@echo -n "["
@find to_compress -name "*.bin" -print0 | xargs -0 -n1 ./compress_lz10.sh
@echo "]"
@echo "Compressing finished!"
else
rm -rf data
mv to_compress data
endif
@echo
@echo "----------------------------------------------------------------"
@echo
clean:
$(MAKE) -C tools/table-generator clean
$(MAKE) -C tools/bin2s clean
rm -rf $(BUILDDIR) $(MKFILE_DIR)/data $(MKFILE_DIR)/to_compress $(LIBDIR)
else
# -------- Toolchain defaults (can be overridden by parent) --------
CC ?= gcc
CXX ?= g++
AR ?= ar
CFLAGS ?= -O2
CXXFLAGS ?= $(CFLAGS)
LDFLAGS ?=
SOFLAGS ?= -shared
# -------- Library name + versioning --------
TARGET := pccs
MAJOR_VERSION := 1
MINOR_VERSION := 0
FULL_VERSION := $(MAJOR_VERSION).$(MINOR_VERSION)
STATIC_LIB := lib$(TARGET).a
SHARED_REAL := lib$(TARGET).so.$(FULL_VERSION)
SHARED_MAJOR := lib$(TARGET).so.$(MAJOR_VERSION)
SHARED_UNVER := lib$(TARGET).so
# -------- Automatic source scanning --------
C_SOURCES := $(wildcard $(MKFILE_DIR)/source/*.c)
CPP_SOURCES := $(wildcard $(MKFILE_DIR)/source/*.cpp)
SOURCES := $(C_SOURCES) $(CPP_SOURCES)
INCLUDES := $(MKFILE_DIR)/include $(INCLUDE) -I$(OBJDIR)
BINFILES := $(wildcard $(DATA)/*.bin)
BINOFILES := $(addprefix $(OBJDIR)/,$(notdir $(BINFILES:.bin=.bin.o)))
# create object paths
OBJS := $(addprefix $(OBJDIR)/,$(notdir $(C_SOURCES:.c=.o)))
OBJS += $(addprefix $(OBJDIR)/,$(notdir $(CPP_SOURCES:.cpp=.o)))
OBJS += $(BINOFILES)
#OBJS += $(patsubst $(DATA)/%.bin,$(OBJDIR)/%.bin.o,$(BINFILES))
VPATH += $(MKFILE_DIR)/source $(DATA)
# -------- Top-level targets --------
.PHONY: all symlinks
ifneq ($(CXX),arm-none-eabi-g++)
# Not on GBA. Compile the shared lib as well.
# We need -fPIC for shared libs, but it's not supported on GBA.
CFLAGS += -fPIC
all: $(LIBDIR)/$(STATIC_LIB) $(LIBDIR)/$(SHARED_REAL) symlinks
else
# On GBA. Only compile the static lib.
all: $(LIBDIR)/$(STATIC_LIB)
endif
# -------- Static library --------
$(LIBDIR)/$(STATIC_LIB): $(BINOFILES) $(OBJS)
$(AR) rcs $@ $(OBJS)
# -------- Shared library (real file with full version) --------
$(LIBDIR)/$(SHARED_REAL): $(BINOFILES) $(OBJS)
$(CXX) $(SOFLAGS) $(LDFLAGS) \
-Wl,-soname,$(SHARED_MAJOR) \
-o $@ $(OBJS)
# -------- Soname symlinks --------
symlinks: $(LIBDIR)/$(SHARED_REAL)
ln -sf $(SHARED_REAL) $(LIBDIR)/$(SHARED_MAJOR)
ln -sf $(SHARED_MAJOR) $(LIBDIR)/$(SHARED_UNVER)
# -------- Object file rules --------
# Generate object file from binary using bin2o
$(OBJDIR)/%.bin.o: $(DATA)/%.bin
@mkdir -p $(dir $@)
@echo "Converting $< → $@"
$(bin2o)
$(OBJDIR)/%.o: %.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -I$(INCLUDES) -c -o $@ $<
$(OBJDIR)/%.o: %.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -I$(INCLUDES) -c -o $@ $<
endif