3 # This file is part of OpenTTD.
4 # 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.
5 # 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.
6 # 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/>.
9 CXX_HOST = !!CXX_HOST!!
10 CC_BUILD = !!CC_BUILD!!
11 CXX_BUILD = !!CXX_BUILD!!
15 CFLAGS_BUILD = !!CFLAGS_BUILD!!
16 CXXFLAGS = !!CXXFLAGS!!
17 CXXFLAGS_BUILD = !!CXXFLAGS_BUILD!!
20 LDFLAGS_BUILD = !!LDFLAGS_BUILD!!
21 ROOT_DIR = !!ROOT_DIR!!
23 LANG_DIR = !!LANG_DIR!!
24 SRC_OBJS_DIR = !!SRC_OBJS_DIR!!
25 LANG_OBJS_DIR = !!LANG_OBJS_DIR!!
26 SETTING_OBJS_DIR= !!SETTING_OBJS_DIR!!
28 SCRIPT_SRC_DIR = !!SCRIPT_SRC_DIR!!
29 MEDIA_DIR = !!MEDIA_DIR!!
35 MAKEDEPEND = !!MAKEDEPEND!!
36 CFLAGS_MAKEDEP = !!CFLAGS_MAKEDEP!!
39 CONFIG_CACHE_COMPILER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_COMPILER!!
40 CONFIG_CACHE_LINKER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_LINKER!!
41 CONFIG_CACHE_SOURCE = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_SOURCE!!
42 CONFIG_CACHE_VERSION = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_VERSION!!
45 OBJS_CPP := !!OBJS_CPP!!
46 OBJS_MM := !!OBJS_MM!!
47 OBJS_RC := !!OBJS_RC!!
48 OBJS := $(OBJS_C) $(OBJS_CPP) $(OBJS_MM) $(OBJS_RC)
51 # All C-files depend on those 3 files
52 FILE_DEP := $(CONFIG_CACHE_COMPILER)
53 # Create all dirs and subdirs
54 RES := $(shell mkdir -p $(BIN_DIR) $(sort $(dir $(OBJS))))
56 CFLAGS += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
57 CFLAGS_MAKEDEP += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
59 CFLAGS_MAKEDEP += -I $(SCRIPT_SRC_DIR)
62 # Check if we want to show what we are doing
72 all: $(BIN_DIR)/$(TTD)
74 # This are 2 rules that are pointing back to STRGEN stuff.
75 # There is not really a need to have them here, but in case
76 # some weirdo wants to run 'make' in the 'src' dir and expects
77 # the languages to be recompiled, this catches that case and
78 # takes care of it nicely.
79 $(LANG_OBJS_DIR)/$(STRGEN):
80 $(MAKE) -C $(LANG_OBJS_DIR) $(STRGEN)
82 $(LANG_OBJS_DIR)/table/strings.h: $(LANG_DIR)/english.txt $(LANG_OBJS_DIR)/$(STRGEN)
83 $(MAKE) -C $(LANG_OBJS_DIR) table/strings.h
85 # Always run version detection, so we always have an accurate modified
87 VERSIONS := $(shell AWK="$(AWK)" "$(ROOT_DIR)/findversion.sh")
88 MODIFIED := $(shell echo "$(VERSIONS)" | cut -f 3 -d' ')
90 # Use autodetected revisions
91 VERSION := $(shell echo "$(VERSIONS)" | cut -f 1 -d' ')
92 ISODATE := $(shell echo "$(VERSIONS)" | cut -f 2 -d' ')
93 GITHASH := $(shell echo "$(VERSIONS)" | cut -f 4 -d' ')
94 ISTAG := $(shell echo "$(VERSIONS)" | cut -f 5 -d' ')
95 ISSTABLETAG := $(shell echo "$(VERSIONS)" | cut -f 6 -d' ')
97 # Make sure we have something in VERSION and ISODATE
105 # This helps to recompile if flags change
106 RES := $(shell if [ "`cat $(CONFIG_CACHE_COMPILER) 2>/dev/null`" != "$(CFLAGS) $(CXXFLAGS)" ]; then echo "$(CFLAGS) $(CXXFLAGS)" > $(CONFIG_CACHE_COMPILER); fi )
107 RES := $(shell if [ "`cat $(CONFIG_CACHE_LINKER) 2>/dev/null`" != "$(LDFLAGS) $(LIBS)" ]; then echo "$(LDFLAGS) $(LIBS)" > $(CONFIG_CACHE_LINKER); fi )
109 # If there is a change in the source-file-list, make sure we recheck the deps
110 RES := $(shell if [ "`cat $(CONFIG_CACHE_SOURCE) 2>/dev/null`" != "$(SRCS)" ]; then echo "$(SRCS)" > $(CONFIG_CACHE_SOURCE); fi )
111 # If there is a change in the revision, make sure we recompile rev.cpp
112 RES := $(shell if [ "`cat $(CONFIG_CACHE_VERSION) 2>/dev/null`" != "$(VERSION) $(MODIFIED)" ]; then echo "$(VERSION) $(MODIFIED)" > $(CONFIG_CACHE_VERSION); fi )
115 # The slow, but always correct, dep-check
117 DEPS := $(OBJS:%.o=%.d)
119 # Only include the deps if we are compiling everything
120 ifeq ($(filter %.o clean mrproper, $(MAKECMDGOALS)),)
123 # In case we want to compile a single target, include the .d file for it
124 ifneq ($(filter %.o, $(MAKECMDGOALS)),)
125 SINGLE_DEP := $(filter %.o, $(MAKECMDGOALS))
126 -include $(SINGLE_DEP:%.o=%.d)
130 # Find the deps via GCC. Rarely wrong, but a bit slow
132 $(OBJS_C:%.o=%.d): %.d: $(SRC_DIR)/%.c $(FILE_DEP)
133 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.c=%.c)'
134 $(Q)$(CC_HOST) $(CFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
136 $(OBJS_CPP:%.o=%.d): %.d: $(SRC_DIR)/%.cpp $(FILE_DEP)
137 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.cpp=%.cpp)'
138 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
140 $(OBJS_MM:%.o=%.d): %.d: $(SRC_DIR)/%.mm $(FILE_DEP)
141 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.mm=%.mm)'
142 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
144 $(OBJS_RC:%.o=%.d): %.d: $(SRC_DIR)/%.rc $(FILE_DEP)
145 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.rc=%.rc)'
149 # The much faster, but can be wrong, dep-check
153 # Only include the deps if we are not cleaning
154 ifeq ($(filter depend clean mrproper, $(MAKECMDGOALS)),)
155 -include Makefile.dep
158 ifeq ("$(SRC_OBJS_DIR)/$(DEPEND)","$(MAKEDEPEND)")
160 $(SRC_OBJS_DIR)/$(DEPEND): $(SRC_DIR)/depend/depend.cpp
161 $(E) '$(STAGE) Compiling and linking $(DEPEND)'
162 $(Q)$(CXX_BUILD) $(CFLAGS_BUILD) $(CXXFLAGS_BUILD) $(LDFLAGS_BUILD) -o $@ $<
165 # Macro for invoking a command on groups of 100 words at a time
166 # (analogous to xargs(1)). The macro invokes itself recursively
167 # until the list of words is depleted.
169 # Usage: $(call xargs,COMMAND,LIST)
171 # COMMAND should be a shell command to which the words will be
172 # appended as arguments in groups of 100.
174 $(1) $(wordlist 1,100,$(2))
175 $(if $(word 101,$(2)),$(call xargs,$(1),$(wordlist 101,$(words $(2)),$(2))))
178 # Make sure that only 'make depend' ALWAYS triggers a recheck
179 ifeq ($(filter depend, $(MAKECMDGOALS)),)
180 Makefile.dep: $(FILE_DEP) $(SRCS:%=$(SRC_DIR)/%) $(CONFIG_CACHE_SOURCE) $(DEP)
182 Makefile.dep: $(FILE_DEP) $(SRCS:%=$(SRC_DIR)/%) $(DEP) FORCE
184 $(E) '$(STAGE) DEP CHECK (all files)'
185 $(Q)rm -f Makefile.dep.tmp
186 $(Q)touch Makefile.dep.tmp
188 # Calculate the deps via makedepend
189 $(call xargs,$(Q)$(MAKEDEPEND) -f$(SRC_OBJS_DIR)/Makefile.dep.tmp -o.o -Y -v -a -- $(CFLAGS_MAKEDEP) -- 2>/dev/null,$(SRCS:%=$(SRC_DIR)/%))
191 # Remove all comments and includes that don't start with $(SRC_DIR)
192 # Remove $(SRC_DIR) from object-file-name
194 /^# DO NOT/ { print $$0 ; next} \
198 for (n = 2; n <= NF; n++) { \
199 if (match($$n, "^$(ROOT_DIR)") == 0) { \
204 gsub("$(SRC_DIR)/", "", $$1); \
207 $$1 = "Makefile.dep:"; \
215 ' < Makefile.dep.tmp | sed 's@ *@ @g;s@ $$@@' | LC_ALL=C $(SORT) > Makefile.dep
217 $(Q)rm -f Makefile.dep.tmp Makefile.dep.tmp.bak
221 # Avoid problems with deps if a .h/.hpp/.hpp.sq file is deleted without the deps
222 # being updated. Now the Makefile continues, the deps are recreated
223 # and all will be fine.
228 # Compile all the files according to the targets
230 $(OBJS_C): %.o: $(SRC_DIR)/%.c $(DEP_MASK) $(FILE_DEP)
231 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.c=%.c)'
232 $(Q)$(CC_HOST) $(CFLAGS) -c -o $@ $<
234 $(filter-out %sse2.o, $(filter-out %ssse3.o, $(filter-out %sse4.o, $(OBJS_CPP)))): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
235 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
236 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -o $@ $<
238 $(filter %sse2.o, $(OBJS_CPP)): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
239 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
240 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -msse2 -o $@ $<
242 $(filter %ssse3.o, $(OBJS_CPP)): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
243 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
244 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -mssse3 -o $@ $<
246 $(filter %sse4.o, $(OBJS_CPP)): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
247 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
248 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -msse4.1 -o $@ $<
250 $(OBJS_MM): %.o: $(SRC_DIR)/%.mm $(DEP_MASK) $(FILE_DEP)
251 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.mm=%.mm)'
252 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -c -o $@ $<
254 $(OBJS_RC): %.o: $(SRC_DIR)/%.rc $(FILE_DEP)
255 $(E) '$(STAGE) Compiling resource $(<:$(SRC_DIR)/%.rc=%.rc)'
256 $(Q)$(WINDRES) -o $@ $<
258 $(BIN_DIR)/$(TTD): $(TTD)
259 $(Q)cp $(TTD) $(BIN_DIR)/$(TTD)
261 $(Q)cp $(MEDIA_DIR)/openttd.32.bmp $(BIN_DIR)/baseset/
264 $(Q)cp $(ROOT_DIR)/os/macosx/splash.png $(BIN_DIR)/baseset/
267 $(TTD): $(OBJS) $(CONFIG_CACHE_LINKER)
268 $(E) '$(STAGE) Linking $@'
269 $(Q)+$(CXX_HOST) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
276 $(SRC_DIR)/rev.cpp: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/rev.cpp.in
277 $(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
279 $(SRC_DIR)/os/windows/ottdres.rc: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/os/windows/ottdres.rc.in
280 $(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
287 $(E) '$(STAGE) Cleaning up object files'
288 $(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)
291 $(Q)rm -f $(SRC_DIR)/rev.cpp $(SRC_DIR)/os/windows/ottdres.rc
294 @echo '$(STAGE) No such source-file: $(@:%.o=%).[c|cpp|mm|rc]'
296 .PHONY: all mrproper depend clean FORCE