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!!
32 ENDIAN_CHECK = !!ENDIAN_CHECK!!
34 ENDIAN_FORCE = !!ENDIAN_FORCE!!
37 MAKEDEPEND = !!MAKEDEPEND!!
38 CFLAGS_MAKEDEP = !!CFLAGS_MAKEDEP!!
41 CONFIG_CACHE_COMPILER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_COMPILER!!
42 CONFIG_CACHE_LINKER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_LINKER!!
43 CONFIG_CACHE_ENDIAN = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_ENDIAN!!
44 CONFIG_CACHE_SOURCE = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_SOURCE!!
45 CONFIG_CACHE_VERSION = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_VERSION!!
48 OBJS_CPP := !!OBJS_CPP!!
49 OBJS_MM := !!OBJS_MM!!
50 OBJS_RC := !!OBJS_RC!!
51 OBJS := $(OBJS_C) $(OBJS_CPP) $(OBJS_MM) $(OBJS_RC)
54 # All C-files depend on those 3 files
55 FILE_DEP := $(CONFIG_CACHE_COMPILER) endian_target.h
56 # Create all dirs and subdirs
57 RES := $(shell mkdir -p $(BIN_DIR) $(sort $(dir $(OBJS))))
59 # Make sure endian_target.h is reasable as if it was in the src/ dir
60 CFLAGS += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
61 CFLAGS_MAKEDEP += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
63 CFLAGS_MAKEDEP += -I $(SCRIPT_SRC_DIR)
66 ENDIAN_TARGETS := endian_target.h $(ENDIAN_CHECK)
68 # Check if we want to show what we are doing
78 all: $(BIN_DIR)/$(TTD)
80 # This are 2 rules that are pointing back to STRGEN stuff.
81 # There is not really a need to have them here, but in case
82 # some weirdo wants to run 'make' in the 'src' dir and expects
83 # the languages to be recompiled, this catches that case and
84 # takes care of it nicely.
85 $(LANG_OBJS_DIR)/$(STRGEN):
86 $(MAKE) -C $(LANG_OBJS_DIR) $(STRGEN)
88 $(LANG_OBJS_DIR)/table/strings.h: $(LANG_DIR)/english.txt $(LANG_OBJS_DIR)/$(STRGEN)
89 $(MAKE) -C $(LANG_OBJS_DIR) table/strings.h
91 # Always run version detection, so we always have an accurate modified
93 VERSIONS := $(shell AWK="$(AWK)" "$(ROOT_DIR)/findversion.sh")
94 MODIFIED := $(shell echo "$(VERSIONS)" | cut -f 3 -d' ')
96 # Use autodetected revisions
97 REV := $(shell echo "$(VERSIONS)" | cut -f 1 -d' ')
98 REV_NR := $(shell echo "$(VERSIONS)" | cut -f 2 -d' ')
100 # Make sure we have something in REV and REV_NR
108 # This helps to recompile if flags change
109 RES := $(shell if [ "`cat $(CONFIG_CACHE_COMPILER) 2>/dev/null`" != "$(CFLAGS) $(CXXFLAGS)" ]; then echo "$(CFLAGS) $(CXXFLAGS)" > $(CONFIG_CACHE_COMPILER); fi )
110 RES := $(shell if [ "`cat $(CONFIG_CACHE_LINKER) 2>/dev/null`" != "$(LDFLAGS) $(LIBS)" ]; then echo "$(LDFLAGS) $(LIBS)" > $(CONFIG_CACHE_LINKER); fi )
111 RES := $(shell if [ "`cat $(CONFIG_CACHE_ENDIAN) 2>/dev/null`" != "$(ENDIAN_FORCE)" ]; then echo "$(ENDIAN_FORCE)" > $(CONFIG_CACHE_ENDIAN); fi )
113 # If there is a change in the source-file-list, make sure we recheck the deps
114 RES := $(shell if [ "`cat $(CONFIG_CACHE_SOURCE) 2>/dev/null`" != "$(SRCS)" ]; then echo "$(SRCS)" > $(CONFIG_CACHE_SOURCE); fi )
115 # If there is a change in the revision, make sure we recompile rev.cpp
116 RES := $(shell if [ "`cat $(CONFIG_CACHE_VERSION) 2>/dev/null`" != "$(REV) $(MODIFIED)" ]; then echo "$(REV) $(MODIFIED)" > $(CONFIG_CACHE_VERSION); fi )
119 # The slow, but always correct, dep-check
121 DEPS := $(OBJS:%.o=%.d)
123 # Only include the deps if we are compiling everything
124 ifeq ($(filter $(ENDIAN_TARGETS) %.o clean mrproper, $(MAKECMDGOALS)),)
127 # In case we want to compile a single target, include the .d file for it
128 ifneq ($(filter %.o, $(MAKECMDGOALS)),)
129 SINGLE_DEP := $(filter %.o, $(MAKECMDGOALS))
130 -include $(SINGLE_DEP:%.o=%.d)
134 # Find the deps via GCC. Rarely wrong, but a bit slow
136 $(OBJS_C:%.o=%.d): %.d: $(SRC_DIR)/%.c $(FILE_DEP)
137 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.c=%.c)'
138 $(Q)$(CC_HOST) $(CFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
140 $(OBJS_CPP:%.o=%.d): %.d: $(SRC_DIR)/%.cpp $(FILE_DEP)
141 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.cpp=%.cpp)'
142 $(Q)$(CXX_HOST) $(CFLAGS) $(CXXFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
144 $(OBJS_MM:%.o=%.d): %.d: $(SRC_DIR)/%.mm $(FILE_DEP)
145 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.mm=%.mm)'
146 $(Q)$(CC_HOST) $(CFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
148 $(OBJS_RC:%.o=%.d): %.d: $(SRC_DIR)/%.rc $(FILE_DEP)
149 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.rc=%.rc)'
153 # The much faster, but can be wrong, dep-check
157 # Only include the deps if we are not cleaning
158 ifeq ($(filter $(ENDIAN_TARGETS) depend clean mrproper, $(MAKECMDGOALS)),)
159 -include Makefile.dep
162 ifeq ("$(SRC_OBJS_DIR)/$(DEPEND)","$(MAKEDEPEND)")
164 $(SRC_OBJS_DIR)/$(DEPEND): $(SRC_DIR)/depend/depend.cpp
165 $(E) '$(STAGE) Compiling and linking $(DEPEND)'
166 $(Q)$(CXX_BUILD) $(CFLAGS_BUILD) $(CXXFLAGS_BUILD) $(LDFLAGS_BUILD) -o $@ $<
169 # Make sure that only 'make depend' ALWAYS triggers a recheck
170 ifeq ($(filter depend, $(MAKECMDGOALS)),)
171 Makefile.dep: $(FILE_DEP) $(SRCS:%=$(SRC_DIR)/%) $(CONFIG_CACHE_SOURCE) $(DEP)
173 Makefile.dep: $(FILE_DEP) $(SRCS:%=$(SRC_DIR)/%) $(DEP) FORCE
175 $(E) '$(STAGE) DEP CHECK (all files)'
176 $(Q)rm -f Makefile.dep.tmp
177 $(Q)touch Makefile.dep.tmp
179 # Calculate the deps via makedepend
180 $(Q)$(MAKEDEPEND) -f$(SRC_OBJS_DIR)/Makefile.dep.tmp -o.o -Y -v -- $(CFLAGS_MAKEDEP) -- $(SRCS:%=$(SRC_DIR)/%) 2>/dev/null
182 # Convert x:/... paths to /x/... for mingw
184 @cat Makefile.dep.tmp | sed 's@/\([a-zA-Z]\):\/@\/\1\/@g' > Makefile.dep.tmp.mingw
185 @cp Makefile.dep.tmp.mingw Makefile.dep.tmp
186 @rm -f Makefile.dep.tmp.mingw
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)$(CC_HOST) $(CFLAGS) -c -o $@ $<
252 $(OBJS_RC): %.o: $(SRC_DIR)/%.rc $(FILE_DEP)
253 $(E) '$(STAGE) Compiling resource $(<:$(SRC_DIR)/%.rc=%.rc)'
254 $(Q)$(WINDRES) -o $@ -I `basename $<` $<
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 $@'
268 # Because of a bug in the PSP GCC tools, linking via CXX results
269 # in total chaos and more problems then you can handle. So we need
270 # CC to link OpenTTD for PSP
271 $(Q)+$(CC_HOST) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
273 $(Q)+$(CXX_HOST) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
279 $(E) '$(STAGE) Adding CWSDPMI stub to $@'
280 $(Q)$(ROOT_DIR)/os/dos/make_dos_binary_selfcontained.sh $(SRC_OBJS_DIR)/$@
283 # The targets to compile the endian-code
285 endian_target.h: $(ENDIAN_CHECK) $(CONFIG_CACHE_ENDIAN)
286 $(E) '$(STAGE) Testing endianness for target'
287 $(Q)./$(ENDIAN_CHECK) $(ENDIAN_FORCE) > $@
289 $(ENDIAN_CHECK): $(SRC_DIR)/endian_check.cpp
290 $(E) '$(STAGE) Compiling and Linking $@'
291 $(Q)$(CXX_BUILD) $(CFLAGS_BUILD) $(CXXFLAGS_BUILD) $(LDFLAGS_BUILD) $< -o $@
295 $(SRC_DIR)/rev.cpp: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/rev.cpp.in
296 $(Q)cat $(SRC_DIR)/rev.cpp.in | sed "s@\!\!REVISION\!\!@$(REV_NR)@g;s@!!VERSION!!@$(REV)@g;s@!!MODIFIED!!@$(MODIFIED)@g;s@!!DATE!!@`date +%d.%m.%y`@g" > $(SRC_DIR)/rev.cpp
298 $(SRC_DIR)/os/windows/ottdres.rc: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/os/windows/ottdres.rc.in
299 $(Q)cat $(SRC_DIR)/os/windows/ottdres.rc.in | sed "s@\!\!REVISION\!\!@$(REV_NR)@g;s@!!VERSION!!@$(REV)@g;s@!!DATE!!@`date +%d.%m.%y`@g" > $(SRC_DIR)/os/windows/ottdres.rc
306 $(E) '$(STAGE) Cleaning up object files'
307 $(Q)rm -f $(DEPS) $(OBJS) $(TTD) $(DEPEND) $(TTD:%=$(BIN_DIR)/%) $(BIN_DIR)/baseset/openttd.32.bmp $(CONFIG_CACHE_COMPILER) $(CONFIG_CACHE_LINKER) $(CONFIG_CACHE_ENDIAN) $(CONFIG_CACHE_SOURCE) $(ENDIAN_TARGETS)
310 $(Q)rm -f $(SRC_DIR)/rev.cpp $(SRC_DIR)/os/windows/ottdres.rc
313 @echo '$(STAGE) No such source-file: $(@:%.o=%).[c|cpp|mm|rc]'
315 .PHONY: all mrproper depend clean FORCE