1 # This file is part of OpenTTD.
2 # OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
3 # OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4 # See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
7 CXX_HOST = !!CXX_HOST!!
8 CC_BUILD = !!CC_BUILD!!
9 CXX_BUILD = !!CXX_BUILD!!
13 CFLAGS_BUILD = !!CFLAGS_BUILD!!
14 CXXFLAGS = !!CXXFLAGS!!
15 CXXFLAGS_BUILD = !!CXXFLAGS_BUILD!!
18 LDFLAGS_BUILD = !!LDFLAGS_BUILD!!
19 ROOT_DIR = !!ROOT_DIR!!
21 LANG_DIR = !!LANG_DIR!!
22 SRC_OBJS_DIR = !!SRC_OBJS_DIR!!
23 LANG_OBJS_DIR = !!LANG_OBJS_DIR!!
24 SETTING_OBJS_DIR= !!SETTING_OBJS_DIR!!
26 SCRIPT_SRC_DIR = !!SCRIPT_SRC_DIR!!
27 MEDIA_DIR = !!MEDIA_DIR!!
33 MAKEDEPEND = !!MAKEDEPEND!!
34 CFLAGS_MAKEDEP = !!CFLAGS_MAKEDEP!!
37 CONFIG_CACHE_COMPILER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_COMPILER!!
38 CONFIG_CACHE_LINKER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_LINKER!!
39 CONFIG_CACHE_SOURCE = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_SOURCE!!
40 CONFIG_CACHE_VERSION = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_VERSION!!
43 OBJS_CPP := !!OBJS_CPP!!
44 OBJS_MM := !!OBJS_MM!!
45 OBJS_RC := !!OBJS_RC!!
46 OBJS := $(OBJS_C) $(OBJS_CPP) $(OBJS_MM) $(OBJS_RC)
49 # All C-files depend on those 3 files
50 FILE_DEP := $(CONFIG_CACHE_COMPILER)
51 # Create all dirs and subdirs
52 RES := $(shell mkdir -p $(BIN_DIR) $(sort $(dir $(OBJS))))
54 CFLAGS += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
55 CFLAGS_MAKEDEP += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
57 CFLAGS_MAKEDEP += -I $(SCRIPT_SRC_DIR)
60 # Check if we want to show what we are doing
70 all: $(BIN_DIR)/$(TTD)
72 # This are 2 rules that are pointing back to STRGEN stuff.
73 # There is not really a need to have them here, but in case
74 # some weirdo wants to run 'make' in the 'src' dir and expects
75 # the languages to be recompiled, this catches that case and
76 # takes care of it nicely.
77 $(LANG_OBJS_DIR)/$(STRGEN):
78 $(MAKE) -C $(LANG_OBJS_DIR) $(STRGEN)
80 $(LANG_OBJS_DIR)/table/strings.h: $(LANG_DIR)/english.txt $(LANG_OBJS_DIR)/$(STRGEN)
81 $(MAKE) -C $(LANG_OBJS_DIR) table/strings.h
83 # Always run version detection, so we always have an accurate modified
85 VERSIONS := $(shell AWK="$(AWK)" "$(ROOT_DIR)/findversion.sh")
86 MODIFIED := $(shell echo "$(VERSIONS)" | cut -f 3 -d' ')
88 # Use autodetected revisions
89 VERSION := $(shell echo "$(VERSIONS)" | cut -f 1 -d' ')
90 ISODATE := $(shell echo "$(VERSIONS)" | cut -f 2 -d' ')
91 GITHASH := $(shell echo "$(VERSIONS)" | cut -f 4 -d' ')
92 ISTAG := $(shell echo "$(VERSIONS)" | cut -f 5 -d' ')
93 ISSTABLETAG := $(shell echo "$(VERSIONS)" | cut -f 6 -d' ')
95 # Make sure we have something in VERSION and ISODATE
103 # This helps to recompile if flags change
104 RES := $(shell if [ "`cat $(CONFIG_CACHE_COMPILER) 2>/dev/null`" != "$(CFLAGS) $(CXXFLAGS)" ]; then echo "$(CFLAGS) $(CXXFLAGS)" > $(CONFIG_CACHE_COMPILER); fi )
105 RES := $(shell if [ "`cat $(CONFIG_CACHE_LINKER) 2>/dev/null`" != "$(LDFLAGS) $(LIBS)" ]; then echo "$(LDFLAGS) $(LIBS)" > $(CONFIG_CACHE_LINKER); fi )
107 # If there is a change in the source-file-list, make sure we recheck the deps
108 RES := $(shell if [ "`cat $(CONFIG_CACHE_SOURCE) 2>/dev/null`" != "$(SRCS)" ]; then echo "$(SRCS)" > $(CONFIG_CACHE_SOURCE); fi )
109 # If there is a change in the revision, make sure we recompile rev.cpp
110 RES := $(shell if [ "`cat $(CONFIG_CACHE_VERSION) 2>/dev/null`" != "$(VERSION) $(MODIFIED)" ]; then echo "$(VERSION) $(MODIFIED)" > $(CONFIG_CACHE_VERSION); fi )
113 # The slow, but always correct, dep-check
115 DEPS := $(OBJS:%.o=%.d)
117 # Only include the deps if we are compiling everything
118 ifeq ($(filter %.o clean mrproper, $(MAKECMDGOALS)),)
121 # In case we want to compile a single target, include the .d file for it
122 ifneq ($(filter %.o, $(MAKECMDGOALS)),)
123 SINGLE_DEP := $(filter %.o, $(MAKECMDGOALS))
124 -include $(SINGLE_DEP:%.o=%.d)
128 # Find the deps via GCC. Rarely wrong, but a bit slow
130 $(OBJS_C:%.o=%.d): %.d: $(SRC_DIR)/%.c $(FILE_DEP)
131 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.c=%.c)'
132 $(Q)$(CC_HOST) $(CFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
134 $(OBJS_CPP:%.o=%.d): %.d: $(SRC_DIR)/%.cpp $(FILE_DEP)
135 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.cpp=%.cpp)'
136 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
138 $(OBJS_MM:%.o=%.d): %.d: $(SRC_DIR)/%.mm $(FILE_DEP)
139 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.mm=%.mm)'
140 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
142 $(OBJS_RC:%.o=%.d): %.d: $(SRC_DIR)/%.rc $(FILE_DEP)
143 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.rc=%.rc)'
147 # The much faster, but can be wrong, dep-check
151 # Only include the deps if we are not cleaning
152 ifeq ($(filter depend clean mrproper, $(MAKECMDGOALS)),)
153 -include Makefile.dep
156 ifeq ("$(SRC_OBJS_DIR)/$(DEPEND)","$(MAKEDEPEND)")
158 $(SRC_OBJS_DIR)/$(DEPEND): $(SRC_DIR)/depend/depend.cpp
159 $(E) '$(STAGE) Compiling and linking $(DEPEND)'
160 $(Q)$(CXX_BUILD) $(CFLAGS_BUILD) $(CXXFLAGS_BUILD) $(LDFLAGS_BUILD) -o $@ $<
163 # Macro for invoking a command on groups of 100 words at a time
164 # (analogous to xargs(1)). The macro invokes itself recursively
165 # until the list of words is depleted.
167 # Usage: $(call xargs,COMMAND,LIST)
169 # COMMAND should be a shell command to which the words will be
170 # appended as arguments in groups of 100.
172 $(1) $(wordlist 1,100,$(2))
173 $(if $(word 101,$(2)),$(call xargs,$(1),$(wordlist 101,$(words $(2)),$(2))))
176 # Make sure that only 'make depend' ALWAYS triggers a recheck
177 ifeq ($(filter depend, $(MAKECMDGOALS)),)
178 Makefile.dep: $(FILE_DEP) $(SRCS:%=$(SRC_DIR)/%) $(CONFIG_CACHE_SOURCE) $(DEP)
180 Makefile.dep: $(FILE_DEP) $(SRCS:%=$(SRC_DIR)/%) $(DEP) FORCE
182 $(E) '$(STAGE) DEP CHECK (all files)'
183 $(Q)rm -f Makefile.dep.tmp
184 $(Q)touch Makefile.dep.tmp
186 # Calculate the deps via makedepend
187 $(call xargs,$(Q)$(MAKEDEPEND) -f$(SRC_OBJS_DIR)/Makefile.dep.tmp -o.o -Y -v -a -- $(CFLAGS_MAKEDEP) -- 2>/dev/null,$(SRCS:%=$(SRC_DIR)/%))
189 # Remove all comments and includes that don't start with $(SRC_DIR)
190 # Remove $(SRC_DIR) from object-file-name
192 /^# DO NOT/ { print $$0 ; next} \
196 for (n = 2; n <= NF; n++) { \
197 if (match($$n, "^$(ROOT_DIR)") == 0) { \
202 gsub("$(SRC_DIR)/", "", $$1); \
205 $$1 = "Makefile.dep:"; \
213 ' < Makefile.dep.tmp | sed 's@ *@ @g;s@ $$@@' | LC_ALL=C $(SORT) > Makefile.dep
215 $(Q)rm -f Makefile.dep.tmp Makefile.dep.tmp.bak
219 # Avoid problems with deps if a .h/.hpp/.hpp.sq file is deleted without the deps
220 # being updated. Now the Makefile continues, the deps are recreated
221 # and all will be fine.
226 # Compile all the files according to the targets
228 $(OBJS_C): %.o: $(SRC_DIR)/%.c $(DEP_MASK) $(FILE_DEP)
229 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.c=%.c)'
230 $(Q)$(CC_HOST) $(CFLAGS) -c -o $@ $<
232 $(filter-out %sse2.o, $(filter-out %ssse3.o, $(filter-out %sse4.o, $(OBJS_CPP)))): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
233 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
234 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -o $@ $<
236 $(filter %sse2.o, $(OBJS_CPP)): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
237 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
238 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -msse2 -o $@ $<
240 $(filter %ssse3.o, $(OBJS_CPP)): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
241 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
242 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -mssse3 -o $@ $<
244 $(filter %sse4.o, $(OBJS_CPP)): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
245 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
246 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -msse4.1 -o $@ $<
248 $(OBJS_MM): %.o: $(SRC_DIR)/%.mm $(DEP_MASK) $(FILE_DEP)
249 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.mm=%.mm)'
250 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -o $@ $<
252 $(OBJS_RC): %.o: $(SRC_DIR)/%.rc $(FILE_DEP)
253 $(E) '$(STAGE) Compiling resource $(<:$(SRC_DIR)/%.rc=%.rc)'
254 $(Q)$(WINDRES) -o $@ $<
256 $(BIN_DIR)/$(TTD): $(TTD)
257 $(Q)cp $(TTD) $(BIN_DIR)/$(TTD)
259 $(Q)cp $(MEDIA_DIR)/openttd.32.bmp $(BIN_DIR)/baseset/
262 $(Q)cp $(ROOT_DIR)/os/macosx/splash.png $(BIN_DIR)/baseset/
265 $(TTD): $(OBJS) $(CONFIG_CACHE_LINKER)
266 $(E) '$(STAGE) Linking $@'
267 $(Q)+$(CXX_HOST) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
274 $(SRC_DIR)/rev.cpp: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/rev.cpp.in
275 $(Q)cat $(SRC_DIR)/rev.cpp.in | sed "s@\!\!ISODATE\!\!@$(ISODATE)@g;s@!!VERSION!!@$(VERSION)@g;s@!!MODIFIED!!@$(MODIFIED)@g;s@!!DATE!!@`date +%d.%m.%y`@g;s@!!GITHASH!!@$(GITHASH)@g;s@!!ISTAG!!@$(ISTAG)@g;s@!!ISSTABLETAG!!@$(ISSTABLETAG)@g" > $(SRC_DIR)/rev.cpp
277 $(SRC_DIR)/os/windows/ottdres.rc: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/os/windows/ottdres.rc.in
278 $(Q)cat $(SRC_DIR)/os/windows/ottdres.rc.in | sed "s@\!\!ISODATE\!\!@$(ISODATE)@g;s@!!VERSION!!@$(VERSION)@g;s@!!DATE!!@`date +%d.%m.%y`@g;s@!!GITHASH!!@$(GITHASH)@g;s@!!ISTAG!!@$(ISTAG)@g;s@!!ISSTABLETAG!!@$(ISSTABLETAG)@g" > $(SRC_DIR)/os/windows/ottdres.rc
285 $(E) '$(STAGE) Cleaning up object files'
286 $(Q)rm -f $(DEPS) $(OBJS) $(TTD) $(DEPEND) $(TTD:%=$(BIN_DIR)/%) $(BIN_DIR)/baseset/openttd.32.bmp $(CONFIG_CACHE_COMPILER) $(CONFIG_CACHE_LINKER) $(CONFIG_CACHE_SOURCE)
289 $(Q)rm -f $(SRC_DIR)/rev.cpp $(SRC_DIR)/os/windows/ottdres.rc
292 @echo '$(STAGE) No such source-file: $(@:%.o=%).[c|cpp|mm|rc]'
294 .PHONY: all mrproper depend clean FORCE