-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
216 lines (174 loc) · 6.54 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Usage:
# make [args]
# container-make.sh [args]
# args:
# DEBUG_INFO=1: print debug info
# EXTRA_CFLAGS=-fmax-errors=1: Add extra cflags (e.g. stop after 1 error)
# DEBUG=1: link with debug symbols and remove optimization
ifndef TARGET_DIR
TARGET_DIR=godot/addons/GodotNativeExec
endif
OPT=-O3
DEBUG_LDFLAGS=
DEBUG_CFLAGS=
CPPFLAGS=
LD_STRIP=-s
ifdef DEBUG
ifeq ($(DEBUG),1)
DEBUG_CFLAGS=-g
CPPFLAGS=-DDEBUG
DEBUG_LDFLAGS=-g
OPT=
LD_STRIP=
endif
endif
export PATH := $(PATH):$(CURDIR)/tools
CFLAGS=$(CPPFLAGS) $(DEBUG_CFLAGS) $(OPT) -std=c++17 -D_WIN32_WINNT=0x0600 $(WARNS) $(GODOT_INCLUDES) $(EXTRA_CFLAGS)
# TODO: separate ldflags per platform?
LDFLAGS=$(DEBUG_LDFLAGS) -shared -static $(LD_STRIP) -Wl,--subsystem,windows $(EXTRA_LDFLAGS)
OBJ_DIR=obj
SRC_DIR=src
TOOLS_DIR=tools
WARNS = -Wall -Wno-parentheses
LIBNAME=godot-native-exec
# windows (TODO: linux osx?)
# add platforms here
ifndef PLATFORM
PLATFORM=windows
endif
ifndef TOOL_SUFFIX
# could be '.exe' for msys2 toolchain on windows
TOOL_SUFFIX=
endif
ifndef TOOL_PREFIX
TOOL_PREFIX=
ifeq ($(PLATFORM),windows)
ifneq ($(OS),Windows_NT)
# cross compile from linux or docker?
TOOL_PREFIX=x86_64-w64-mingw32-
endif
endif
endif
RUN_DOCKERIZED=run-build-tool.sh
# build tools
CXX=$(TOOL_PREFIX)g++$(TOOL_SUFFIX)
AR=$(TOOL_PREFIX)ar$(TOOL_SUFFIX)
RANLIB=$(TOOL_PREFIX)ranlib$(TOOL_SUFFIX)
OBJDUMP=$(TOOL_PREFIX)objdump$(TOOL_SUFFIX)
CONTAINER_CXX=$(TOOLS_DIR)/$(CXX)
CONTAINER_AR=$(TOOLS_DIR)/$(AR)
CONTAINER_RANLIB=$(TOOLS_DIR)/$(RANLIB)
CONTAINER_OBJDUMP=$(TOOLS_DIR)/$(OBJDUMP)
CONTAINER_BASH=$(TOOLS_DIR)/bash$(TOOL_SUFFIX)
CONTAINER_SCONS=$(TOOLS_DIR)/scons$(TOOL_SUFFIX)
CONTAINER_MAKE=$(TOOLS_DIR)/make$(TOOL_SUFFIX)
BUILD_TOOLS_DOCKER=$(CONTAINER_MAKE)
EXTRA_TOOLS_DOCKER=$(CONTAINER_SCONS) $(CONTAINER_BASH) $(CONTAINER_OBJDUMP) $(CONTAINER_CXX) $(CONTAINER_AR) $(CONTAINER_RANLIB)
GODOT_INCLUDES=$(addprefix -Igodot-cpp/,include/ include/core/ include/gen/ godot-headers/)
GDNS=godot-native-exec.gdns
GDNLIB=godot-native-exec.gdnlib
EXEC_THREAD=ExecThread.gd
ADDON_NAME=GodotNativeExec
GDNLIB_TARGET=$(TARGET_DIR)/$(GDNLIB)
GDNS_TARGET=$(TARGET_DIR)/$(GDNS)
EXEC_THREAD_TARGET=$(TARGET_DIR)/$(EXEC_THREAD)
CP_TARGETS=$(GDNLIB_TARGET) $(GDNS_TARGET) $(EXEC_THREAD_TARGET)
LIBNAME_windows=$(TARGET_DIR)/$(LIBNAME).dll
# TODO?
LIBNAME_osx=$(TARGET_DIR)/$(LIBNAME).dylib
LIBNAME_linux=$(TARGET_DIR)/$(LIBNAME).so
# godot-cpp dependencies
GODOT_CPP_SUBMODULE=godot-cpp/.gitattributes
GODOT_CPP_GEN_DIR=godot-cpp/src/gen
__GODOT_CPP_GEN_CLASSES=__init_method_bindings __register_types
# Scrape generated class names from api.json and add them as dependencies
# TODO: This grep|sed combo is probably super fragile?
GODOT_CPP_API_JSON=godot-cpp/godot-headers/api.json
GODOT_CPP_GEN_CPP=$(addprefix godot-cpp/src/gen/,$(addsuffix .cpp,$(GODOT_CPP_GEN_CLASSES)))
GODOT_CPP_GEN_OBJS=$(addprefix godot-cpp/src/gen/,$(addsuffix .o,$(GODOT_CPP_GEN_CLASSES)))
GODOT_CPP_CORE_CPP=$(wildcard godot-cpp/src/core/*.cpp)
GODOT_CPP_CORE_OBJS=$(GODOT_CPP_CORE_CPP:%.cpp=%.o)
GODOT_CPP_OBJS=$(GODOT_CPP_GEN_OBJS) $(GODOT_CPP_CORE_OBJS)
# Project dependencies
SRCS=$(wildcard src/*.cpp)
OBJS=$(SRCS:src/%.cpp=$(OBJ_DIR)/$(PLATFORM)/%.o)
GODOT_CPP_API_MK=$(OBJ_DIR)/api_classes.mk
DEPS=$(SRCS:src/%.cpp=$(OBJ_DIR)/%.d) $(GODOT_CPP_API_MK)
ifneq ($(DEBUG_INFO),)
$(info GODOT_CPP_GEN_CPP=$(wordlist 1,5,$(GODOT_CPP_GEN_CPP)))
$(info GODOT_CPP_CORE_OBJS=$(GODOT_CPP_CORE_OBJS))
$(info GODOT_CPP_OBJS=$(GODOT_CPP_OBJS))
$(info PLATFORM=$(PLATFORM))
$(info TARGET_DIR=$(TARGET_DIR))
$(info GODOT_INCLUDES=$(GODOT_INCLUDES))
$(info OBJ_DIR/$(PLATFORM)=$(OBJ_DIR)/$(PLATFORM))
$(info LIB_NAME=$(LIB_NAME))
$(info OBJS=$(OBJS))
$(info CXX=$(CXX))
$(info LDFLAGS=$(LDFLAGS))
$(info CFLAGS=$(CFLAGS))
$(info DEPS=$(DEPS))
endif
### Phony RULES
.PHONY: $(PLATFORM) all clean distclean build_tools extra_tools
default: all
all: $(PLATFORM) $(CP_TARGETS)
clean:
rm -rf $(TARGET_DIR)/* $(OBJ_DIR) $(TOOLS_DIR)
distclean: clean
rm -rf godot-cpp
$(GDNLIB_TARGET): $(GDNLIB)
cp $^ $@
$(GDNS_TARGET): $(GDNS)
cp $^ $@
$(EXEC_THREAD_TARGET): $(EXEC_THREAD)
cp $^ $@
windows: $(LIBNAME_windows)
osx: $(LIBNAME_osx)
linux: $(LIBNAME_linux)
# containerized build tools
build_tools: $(BUILD_TOOLS_DOCKER)
# containerized toolchain tools and bash, etc
extra_tools: $(EXTRA_TOOLS_DOCKER)
### DIR rules
$(TARGET_DIR) $(OBJ_DIR) $(TOOLS_DIR) $(OBJ_DIR)/$(PLATFORM):
mkdir -p "$@"
### TOOL rules
$(BUILD_TOOLS_DOCKER) $(EXTRA_TOOLS_DOCKER): $(RUN_DOCKERIZED) | $(TOOLS_DIR)
ln -fs ../$(notdir $(RUN_DOCKERIZED)) $@
### godot-cpp rules
$(GODOT_CPP_API_JSON): $(GODOT_CPP_SUBMODULE)
$(GODOT_CPP_SUBMODULE):
git submodule update --init --recursive $(dir $@)
$(GODOT_CPP_API_MK): $(GODOT_CPP_API_JSON) Makefile $(OBJ_DIR)
echo API_CLASSES="$$(grep -P '^\t\t"name": "' "$<" | sed -E 's/^\t\t"name": "_?([^"]+)",?/\1/g')" | tr '\n' ' ' > $@
# TODO: can + make scons aware of the job server? using nproc for now.
# Without -j is very slow.
# TODO: multiplatform? mount godot-cpp/src/gen in a
# separate volume inside the docker container for each platform?
include $(GODOT_CPP_API_MK)
API_CLASSES+= $(__GODOT_CPP_GEN_CLASSES)
GODOT_CPP_GEN_CPP=$(API_CLASSES:%=godot-cpp/src/gen/%.cpp)
GODOT_CPP_GEN_OBJS=$(API_CLASSES:%=godot-cpp/src/gen/%.o)
# reset -I fixes the terminal which gets bunged up by this step...
# api.json is an order only prerequisite because the scons script doesn't take it's date
# into account when generating these artifacts which causes make to re-run it for each one when they
# don't get updated to a newer date than api.json
# You will have to clean the submodule to re-build these artifacts.
$(GODOT_CPP_GEN_CPP) $(GODOT_CPP_OBJS) $(GODOT_CPP_GEN_DIR)&: | $(GODOT_CPP_API_JSON)
cd godot-cpp; set -e; \
scons generate_bindings=yes platform=$(PLATFORM) use_mingw=yes -j$$(nproc) > /dev/null || (echo 'SCONS FAIL'; false)
### Compile Rules
$(OBJ_DIR)/%.d: src/%.cpp | $(OBJ_DIR) $(BUILD_TOOLS)
$(CXX) $(CPPFLAGS) -MM $< > $@ && $(CXX) $(CPPFLAGS) -MM $< -MT $*.d >> $@ || (rm $@; false)
$(OBJ_DIR)/$(PLATFORM)/%.o: src/%.cpp | $(DEPS) $(OBJ_DIR)/$(PLATFORM) $(BUILD_TOOLS) $(GODOT_CPP_GEN_CPP)
$(CXX) $(CFLAGS) -c "$<" -o "$@"
# save the list of .o files to a file because gnu make on windows may have a
# low limit on how many chars we are allowed. we need like 23888 chars.
# xargs --show-limits: Maximum length of command we could actually use: 19492
# @file is the answer! https://linux.die.net/man/1/ld
$(LIBNAME_$(PLATFORM)): $(OBJS) $(GODOT_CPP_OBJS) | $(DEPS) $(TARGET_DIR) $(BUILD_TOOLS)
$(file > obj/link_objs,$^)
$(CXX) @obj/link_objs -o "$@" $(LDFLAGS) && \
rm obj/link_objs
include $(DEPS)