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!!
40 REVISION = !!REVISION!!
42 CONFIG_CACHE_COMPILER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_COMPILER!!
43 CONFIG_CACHE_LINKER = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_LINKER!!
44 CONFIG_CACHE_ENDIAN = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_ENDIAN!!
45 CONFIG_CACHE_SOURCE = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_SOURCE!!
46 CONFIG_CACHE_VERSION = $(SRC_OBJS_DIR)/!!CONFIG_CACHE_VERSION!!
49 OBJS_CPP := !!OBJS_CPP!!
50 OBJS_MM := !!OBJS_MM!!
51 OBJS_RC := !!OBJS_RC!!
52 OBJS := $(OBJS_C) $(OBJS_CPP) $(OBJS_MM) $(OBJS_RC)
55 # All C-files depend on those 3 files
56 FILE_DEP := $(CONFIG_CACHE_COMPILER) endian_target.h
57 # Create all dirs and subdirs
58 RES := $(shell mkdir -p $(BIN_DIR) $(sort $(dir $(OBJS))))
60 # Make sure endian_target.h is reasable as if it was in the src/ dir
61 CFLAGS += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
62 CFLAGS_MAKEDEP += -I $(SRC_OBJS_DIR) -I $(LANG_OBJS_DIR) -I $(SETTING_OBJS_DIR)
64 CFLAGS_MAKEDEP += -I $(SCRIPT_SRC_DIR)
67 ENDIAN_TARGETS := endian_target.h $(ENDIAN_CHECK)
69 # Check if we want to show what we are doing
79 all: $(BIN_DIR)/$(TTD)
81 # This are 2 rules that are pointing back to STRGEN stuff.
82 # There is not really a need to have them here, but in case
83 # some weirdo wants to run 'make' in the 'src' dir and expects
84 # the languages to be recompiled, this catches that case and
85 # takes care of it nicely.
86 $(LANG_OBJS_DIR)/$(STRGEN):
87 $(MAKE) -C $(LANG_OBJS_DIR) $(STRGEN)
89 $(LANG_OBJS_DIR)/table/strings.h: $(LANG_DIR)/english.txt $(LANG_OBJS_DIR)/$(STRGEN)
90 $(MAKE) -C $(LANG_OBJS_DIR) table/strings.h
92 # Always run version detection, so we always have an accurate modified
94 VERSIONS := $(shell AWK="$(AWK)" "$(ROOT_DIR)/findversion.sh")
95 MODIFIED := $(shell echo "$(VERSIONS)" | cut -f 3 -d' ')
98 # Use specified revision (which should be of the form "r000").
100 REV_NR := $(shell echo $(REVISION) | sed "s@[^0-9]@@g")
102 # Use autodetected revisions
103 REV := $(shell echo "$(VERSIONS)" | cut -f 1 -d' ')
104 REV_NR := $(shell echo "$(VERSIONS)" | cut -f 2 -d' ')
107 # Make sure we have something in REV and REV_NR
115 # This helps to recompile if flags change
116 RES := $(shell if [ "`cat $(CONFIG_CACHE_COMPILER) 2>/dev/null`" != "$(CXXFLAGS) $(CFLAGS)" ]; then echo "$(CXXFLAGS) $(CFLAGS)" > $(CONFIG_CACHE_COMPILER); fi )
117 RES := $(shell if [ "`cat $(CONFIG_CACHE_LINKER) 2>/dev/null`" != "$(LDFLAGS) $(LIBS)" ]; then echo "$(LDFLAGS) $(LIBS)" > $(CONFIG_CACHE_LINKER); fi )
118 RES := $(shell if [ "`cat $(CONFIG_CACHE_ENDIAN) 2>/dev/null`" != "$(ENDIAN_FORCE)" ]; then echo "$(ENDIAN_FORCE)" > $(CONFIG_CACHE_ENDIAN); fi )
120 # If there is a change in the source-file-list, make sure we recheck the deps
121 RES := $(shell if [ "`cat $(CONFIG_CACHE_SOURCE) 2>/dev/null`" != "$(SRCS)" ]; then echo "$(SRCS)" > $(CONFIG_CACHE_SOURCE); fi )
122 # If there is a change in the revision, make sure we recompile rev.cpp
123 RES := $(shell if [ "`cat $(CONFIG_CACHE_VERSION) 2>/dev/null`" != "$(REV) $(MODIFIED)" ]; then echo "$(REV) $(MODIFIED)" > $(CONFIG_CACHE_VERSION); fi )
126 # The slow, but always correct, dep-check
128 DEPS := $(OBJS:%.o=%.d)
130 # Only include the deps if we are compiling everything
131 ifeq ($(filter $(ENDIAN_TARGETS) %.o clean mrproper, $(MAKECMDGOALS)),)
134 # In case we want to compile a single target, include the .d file for it
135 ifneq ($(filter %.o, $(MAKECMDGOALS)),)
136 SINGLE_DEP := $(filter %.o, $(MAKECMDGOALS))
137 -include $(SINGLE_DEP:%.o=%.d)
141 # Find the deps via GCC. Rarely wrong, but a bit slow
143 $(OBJS_C:%.o=%.d): %.d: $(SRC_DIR)/%.c $(FILE_DEP)
144 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.c=%.c)'
145 $(Q)$(CC_HOST) $(CFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
147 $(OBJS_CPP:%.o=%.d): %.d: $(SRC_DIR)/%.cpp $(FILE_DEP)
148 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.cpp=%.cpp)'
149 $(Q)$(CXX_HOST) $(CXXFLAGS) $(CFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
151 $(OBJS_MM:%.o=%.d): %.d: $(SRC_DIR)/%.mm $(FILE_DEP)
152 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.mm=%.mm)'
153 $(Q)$(CC_HOST) $(CFLAGS) -MM $< | sed 's@^$(@F:%.d=%.o):@$@ $(@:%.d=%.o):@' > $@
155 $(OBJS_RC:%.o=%.d): %.d: $(SRC_DIR)/%.rc $(FILE_DEP)
156 $(E) '$(STAGE) DEP $(<:$(SRC_DIR)/%.rc=%.rc)'
160 # The much faster, but can be wrong, dep-check
164 # Only include the deps if we are not cleaning
165 ifeq ($(filter $(ENDIAN_TARGETS) depend clean mrproper, $(MAKECMDGOALS)),)
166 -include Makefile.dep
169 ifeq ("$(SRC_OBJS_DIR)/$(DEPEND)","$(MAKEDEPEND)")
171 $(SRC_OBJS_DIR)/$(DEPEND): $(SRC_DIR)/depend/depend.cpp
172 $(E) '$(STAGE) Compiling and linking $(DEPEND)'
173 $(Q)$(CXX_BUILD) $(CXXFLAGS_BUILD) $(CFLAGS_BUILD) $(LDFLAGS_BUILD) -o $@ $<
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 $(Q)$(MAKEDEPEND) -f$(SRC_OBJS_DIR)/Makefile.dep.tmp -o.o -Y -v -- $(CFLAGS_MAKEDEP) -- $(SRCS:%=$(SRC_DIR)/%) 2>/dev/null
189 # Convert x:/... paths to /x/... for mingw
191 @cat Makefile.dep.tmp | sed 's@/\([a-zA-Z]\):\/@\/\1\/@g' > Makefile.dep.tmp.mingw
192 @cp Makefile.dep.tmp.mingw Makefile.dep.tmp
193 @rm -f Makefile.dep.tmp.mingw
196 # Remove all comments and includes that don't start with $(SRC_DIR)
197 # Remove $(SRC_DIR) from object-file-name
199 /^# DO NOT/ { print $$0 ; next} \
203 for (n = 2; n <= NF; n++) { \
204 if (match($$n, "^$(ROOT_DIR)") == 0) { \
209 gsub("$(SRC_DIR)/", "", $$1); \
212 $$1 = "Makefile.dep:"; \
220 ' < Makefile.dep.tmp | sed 's@ *@ @g;s@ $$@@' | $(SORT) > Makefile.dep
222 $(Q)rm -f Makefile.dep.tmp Makefile.dep.tmp.bak
226 # Avoid problems with deps if a .h/.hpp/.hpp.sq file is deleted without the deps
227 # being updated. Now the Makefile continues, the deps are recreated
228 # and all will be fine.
233 # Compile all the files according to the targets
235 $(OBJS_C): %.o: $(SRC_DIR)/%.c $(DEP_MASK) $(FILE_DEP)
236 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.c=%.c)'
237 $(Q)$(CC_HOST) $(CFLAGS) -c -o $@ $<
239 $(OBJS_CPP): %.o: $(SRC_DIR)/%.cpp $(DEP_MASK) $(FILE_DEP)
240 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
241 $(Q)$(CXX_HOST) $(CXXFLAGS) $(CFLAGS) -c -o $@ $<
243 $(OBJS_MM): %.o: $(SRC_DIR)/%.mm $(DEP_MASK) $(FILE_DEP)
244 $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.mm=%.mm)'
245 $(Q)$(CC_HOST) $(CFLAGS) -c -o $@ $<
247 $(OBJS_RC): %.o: $(SRC_DIR)/%.rc $(FILE_DEP)
248 $(E) '$(STAGE) Compiling resource $(<:$(SRC_DIR)/%.rc=%.rc)'
249 $(Q)$(WINDRES) -o $@ -I `basename $<` $<
251 $(BIN_DIR)/$(TTD): $(TTD)
252 $(Q)cp $(TTD) $(BIN_DIR)/$(TTD)
254 $(Q)cp $(MEDIA_DIR)/openttd.32.bmp $(BIN_DIR)/data/
257 $(Q)cp $(ROOT_DIR)/os/macosx/splash.png $(BIN_DIR)/data/
260 $(TTD): $(OBJS) $(CONFIG_CACHE_LINKER)
261 $(E) '$(STAGE) Linking $@'
263 # Because of a bug in the PSP GCC tools, linking via CXX results
264 # in total chaos and more problems then you can handle. So we need
265 # CC to link OpenTTD for PSP
266 $(Q)+$(CC_HOST) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
268 $(Q)+$(CXX_HOST) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
274 $(E) '$(STAGE) Adding CWSDPMI stub to $@'
275 $(Q)$(ROOT_DIR)/os/dos/make_dos_binary_selfcontained.sh $(SRC_OBJS_DIR)/$@
278 # The targets to compile the endian-code
280 endian_target.h: $(ENDIAN_CHECK) $(CONFIG_CACHE_ENDIAN)
281 $(E) '$(STAGE) Testing endianness for target'
282 $(Q)./$(ENDIAN_CHECK) $(ENDIAN_FORCE) > $@
284 $(ENDIAN_CHECK): $(SRC_DIR)/endian_check.cpp
285 $(E) '$(STAGE) Compiling and Linking $@'
286 $(Q)$(CXX_BUILD) $(CXXFLAGS_BUILD) $(CFLAGS_BUILD) $< -o $@
290 $(SRC_DIR)/rev.cpp: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/rev.cpp.in
291 $(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
293 $(SRC_DIR)/os/windows/ottdres.rc: $(CONFIG_CACHE_VERSION) $(SRC_DIR)/os/windows/ottdres.rc.in
294 $(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
301 $(E) '$(STAGE) Cleaning up object files'
302 $(Q)rm -f $(DEPS) $(OBJS) $(TTD) $(DEPEND) $(TTD:%=$(BIN_DIR)/%) $(CONFIG_CACHE_COMPILER) $(CONFIG_CACHE_LINKER) $(CONFIG_CACHE_ENDIAN) $(CONFIG_CACHE_SOURCE) $(ENDIAN_TARGETS)
305 $(Q)rm -f $(SRC_DIR)/rev.cpp $(SRC_DIR)/os/windows/ottdres.rc
308 @echo '$(STAGE) No such source-file: $(@:%.o=%).[c|cpp|mm|rc]'
310 .PHONY: all mrproper depend clean FORCE