]> git.pond.sub.org Git - empserver/blob - Make.mk
w32: Modernize for MinGW 6.0.0, and clean up
[empserver] / Make.mk
1 #
2 #   Empire - A multi-player, client/server Internet based war game.
3 #   Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 #                 Ken Stevens, Steve McClure, Markus Armbruster
5 #
6 #   Empire is free software: you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation, either version 3 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #   ---
20 #
21 #   See files README, COPYING and CREDITS in the root of the source
22 #   tree for related information and legal notices.  It is expected
23 #   that future projects/authors will amend these files as needed.
24 #
25 #   ---
26 #
27 #   Make.mk: The real Makefile, included by GNUmakefile
28 #
29 #   Known contributors to this file:
30 #      Markus Armbruster, 2005-2020
31 #
32
33 # This makefile was inspired by `Recursive Make Considered Harmful',
34 # Peter Miller, 1997.
35 # http://miller.emu.id.au/pmiller/books/rmch/
36
37 # Recursively expanded variables are occasionally useful, but can be
38 # slow and tricky.  Do not use them gratuitously.  If you don't
39 # understand this, always use `:=' rather than `='.
40
41 # Default goal
42 all:
43
44 # Delete target on error.  Every Makefile should have this.
45 .DELETE_ON_ERROR:
46
47 # Source files
48 ifeq ($(revctrl),git)
49 src := $(shell cd $(srcdir) && git ls-files | uniq)
50 version := $(shell cd $(srcdir) && build-aux/git-version-gen /dev/null)
51 ifeq ($(version),UNKNOWN)
52 $(warning cannot figure out version number, falling back to git hash)
53 version := UNKNOWN-$(shell cd $(srcdir) && git-rev-parse --verify --short HEAD || echo "UNKNOWN")
54 endif
55 else
56 include $(srcdir)/sources.mk
57 version := $(shell cat $(srcdir)/.tarball-version || echo "UNKNOWN")
58 endif
59 ifeq ($(version),UNKNOWN)
60 $(error cannot figure out version number)
61 endif
62 dirs := $(sort $(dir $(src)))
63 csrc := $(filter %.c, $(src))
64 tsrc := $(filter %.t, $(src))
65 man6 := $(filter man/%.6, $(src))
66 builtins := $(filter src/lib/global/%.config, $(src))
67
68 # Info subjects
69 include $(srcdir)/info/subjects.mk
70
71 # Abbreviations
72 topics := $(patsubst %.t,%,$(notdir $(tsrc)))
73 info := $(topics) $(subjects) all TOP
74 scripts := $(srcdir)/src/scripts
75 depcomp := $(SHELL) $(srcdir)/build-aux/depcomp
76 tarball := $(SHELL) -e $(scripts)/tarball
77 econfig := $(sysconfdir)/empire/econfig
78 schedule := $(sysconfdir)/empire/schedule
79 gamedir := $(localstatedir)/empire
80 edatadir := $(datadir)/empire
81 builtindir := $(edatadir)/builtin
82 einfodir := $(edatadir)/info.nr
83 ehtmldir := $(edatadir)/info.html
84 client/w32 := arpa/inet.h netdb.h netinet/in.h sys/time.h sys/socket.h  \
85 sys/uio.h unistd.h w32io.c w32sockets.c w32types.h
86
87 # Abbreviate make output
88 # Run make with a V=1 parameter for full output.
89 ifneq ($(origin V),command line)
90 V:=
91 endif
92 # $(call quiet-command COMMAND,ABBREV) runs COMMAND, but prints only ABBREV.
93 # Recursively expanded so that variables in COMMAND and ABBREV work.
94 ifneq ($V$(findstring s,$(MAKEFLAGS)),)
95 quiet-command = $1
96 else
97 quiet-command = @echo $2 && $1
98 endif
99
100 # Helper for running tests
101 # Usage: $(call run-test, SHELL-SCRIPT)
102 # Recursively expanded, or else parameters don't work
103 run-test = $(call quiet-command, $(SHELL) -e $1 $(srcdir), TEST $1)
104
105 # How to substitute Autoconf output variables
106 # Recursively expanded so that $@ and $< work.
107 subst.in = sed \
108         -e 's?@configure_input\@?$(notdir $@): Generated from $(notdir $<) by make.?g' \
109         -e 's?@builtindir\@?$(builtindir)?g'    \
110         -e 's?@econfig\@?$(econfig)?g'          \
111         -e 's?@einfodir\@?$(einfodir)?g'        \
112         -e 's?@gamedir\@?$(gamedir)?g'          \
113         -e 's/@EMPIREHOST\@/$(EMPIREHOST)/g'    \
114         -e 's/@EMPIREPORT\@/$(EMPIREPORT)/g'
115
116 # Generated files
117 # See `Cleanliness' below
118 # Generated makefiles, distributed by dist-source from $(srcdir):
119 mk := sources.mk
120 # Generated by Autoconf, not distributed:
121 ac := config.h config.log config.status info.html info.nr lib stamp-h
122 ac += $(basename $(filter %.in, $(src)))
123 ac += $(srcdir)/autom4te.cache $(srcdir)/src/client/autom4te.cache
124 # distributed by dist-source from $(srcdir):
125 acdist := aclocal.m4 config.h.in configure stamp-h.in
126 # Object files:
127 obj := $(csrc:.c=.o) $(filter %.o, $(ac:.c=.o))
128 # Dependencies:
129 deps := $(obj:.o=.d)
130 # Library archives:
131 libs := $(addprefix lib/, libcommon.a libgen.a libglobal.a)
132 # Programs:
133 util := $(addprefix src/util/, $(addsuffix $(EXEEXT), empdump empsched fairland files pconfig))
134 client := src/client/empire$(EXEEXT)
135 server := src/server/emp_server$(EXEEXT)
136 # Info subjects:
137 tsubj := $(addprefix info/, $(addsuffix .t, $(subjects)))
138 # Formatted info:
139 info.nr := $(addprefix info.nr/, $(info))
140 info.html := $(addprefix info.html/, $(addsuffix .html, $(info)))
141 info.all := $(info.nr) $(info.html) info.ps info/stamp-subj
142 # Tests
143 # sandbox
144
145 # Conditionally generated files:
146 empth_obj := src/lib/empthread/io.o
147 empth_lib :=
148 ifeq ($(empthread),LWP)
149 empth_obj += src/lib/empthread/lwp.o src/lib/empthread/posix.o
150 empth_lib += lib/liblwp.a
151 endif
152 ifeq ($(empthread),POSIX)
153 empth_obj += src/lib/empthread/pthread.o src/lib/empthread/posix.o
154 endif
155 ifeq ($(empthread),Windows)
156 empth_obj += src/lib/empthread/ntthread.o
157 endif
158
159 ifeq ($(empthread),Windows)     # really: W32, regardless of thread package
160 libs += lib/libw32.a
161 $(client): lib/libw32.a
162 endif
163
164 # Cleanliness
165 # Each generated file should be in one of the following sets.
166 # Removed by clean:
167 clean := $(obj) $(deps) $(libs) $(util) $(client) $(server) $(tsubj)    \
168 info/toc info/TOP.t $(info.all) $(empth_obj) $(empth_lib) sandbox
169 # Removed by distclean:
170 distclean := $(ac)
171 ifeq ($(revctrl),git)
172 distclean += $(addprefix $(srcdir)/, $(mk))
173 endif
174 # Distributed by dist-source from $(srcdir):
175 src_distgen := $(acdist) $(mk)
176
177 # Compiler flags
178 CPPFLAGS += -I$(srcdir)/include -I.
179 ifeq ($(empthread),Windows)     # really: W32, regardless of thread package
180 CPPFLAGS += -I$(srcdir)/src/lib/w32
181 endif
182 $(client): LDLIBS := $(LIBS_client)
183 $(server): LDLIBS := $(LIBS_server)
184
185
186 ### Advertized goals
187
188 .PHONY: all
189 all: $(util) $(client) $(server) info
190
191 .PHONY: info html
192 info: $(info.nr)
193 html: $(info.html)
194
195 .PHONY: clean
196 clean:
197         $(call quiet-command,rm -rf $(clean),CLEAN)
198
199 .PHONY: distclean
200 distclean: clean
201         $(call quiet-command,rm -rf $(distclean),DISTCLEAN)
202
203 .PHONY: install
204 install: all installdirs
205         $(INSTALL_PROGRAM) $(util) $(server) $(sbindir)
206         $(INSTALL_PROGRAM) $(client) $(bindir)
207         $(INSTALL) -m 444 $(addprefix $(srcdir)/, $(builtins)) $(builtindir)
208         rm -f $(einfodir)/*
209         $(INSTALL_DATA) $(info.nr) $(einfodir)
210         $(INSTALL_DATA) $(addprefix $(srcdir)/, $(man6)) $(mandir)/man6
211         sed -e '1,/^$$/d' -e 's/^/# /g' <$(srcdir)/doc/schedule >$(schedule).dist
212         echo >>$(schedule).dist
213         echo 'every 10 minutes' >>$(schedule).dist
214         [ -e $(schedule) ] || mv $(schedule).dist $(schedule)
215         if [ -e $(econfig) ]; then                                      \
216             echo "Attempting to update your econfig";                   \
217             if src/util/pconfig $(econfig) >$(econfig).dist; then       \
218                 if cmp -s $(econfig) $(econfig).dist; then              \
219                     echo "$(econfig) unchanged";                        \
220                     rm $(econfig).dist;                                 \
221                 fi;                                                     \
222             else                                                        \
223                 echo "Your $(econfig) doesn't work";                    \
224                 src/util/pconfig >$(econfig).dist;                      \
225             fi;                                                         \
226             if [ -e $(econfig).dist ]; then                             \
227                 echo "Check out $(econfig).dist";                       \
228             fi;                                                         \
229         else                                                            \
230             src/util/pconfig >$(econfig);                               \
231         fi
232
233 .PHONY: installdirs
234 installdirs:
235         mkdir -p $(sbindir) $(bindir) $(builtindir) $(einfodir) $(mandir)/man6 $(dir $(econfig)) $(gamedir)
236
237 .PHONY: install-html
238 install-html: html
239         mkdir -p $(ehtmldir)
240         rm -f $(ehtmldir)/*
241         $(INSTALL_DATA) $(info.html) $(ehtmldir)
242
243 .PHONY: uninstall
244 uninstall:
245         rm -f $(addprefix $(sbindir)/, $(notdir $(util) $(server)))
246         rm -f $(addprefix $(bindir)/, $(notdir $(client)))
247         rm -rf $(builtindir) $(einfodir) $(ehtmldir)
248         rmdir $(edatadir)
249         rm -f $(addprefix $(mandir)/man6/, $(notdir $(man6)))
250         @echo "$(dir $(econfig)) and $(gamedir) not removed, you may wish to remove it manually."
251
252 .PHONY: dist
253 dist: dist-source dist-client dist-info
254
255 .PHONY: check check-accept _check
256 check check-accept: _check
257 check:        export EMPIRE_CHECK_ACCEPT :=
258 check-accept: export EMPIRE_CHECK_ACCEPT := y
259 _check: all
260         @echo "Warning: test suite is immature and needs work." >&2
261         $(call run-test, $(srcdir)/tests/files-test)
262         $(call run-test, $(srcdir)/tests/fairland-test)
263         $(call run-test, $(srcdir)/tests/info-test)
264 ifeq ($(empthread),LWP)
265         $(call run-test, $(srcdir)/tests/smoke-test)
266         $(call run-test, $(srcdir)/tests/actofgod-test)
267         $(call run-test, $(srcdir)/tests/build-test)
268         $(call run-test, $(srcdir)/tests/load-tend-test)
269         $(call run-test, $(srcdir)/tests/navi-march-test)
270         $(call run-test, $(srcdir)/tests/fire-test)
271         $(call run-test, $(srcdir)/tests/torpedo-test)
272         $(call run-test, $(srcdir)/tests/bridgefall-test)
273         $(call run-test, $(srcdir)/tests/retreat-test)
274         $(call run-test, $(srcdir)/tests/update-test)
275         $(call run-test, $(srcdir)/tests/version-test)
276 else
277         @echo "TEST $(srcdir)/tests/smoke-test SKIPPED"
278         @echo "TEST $(srcdir)/tests/actofgod-test SKIPPED"
279         @echo "TEST $(srcdir)/tests/build-test SKIPPED"
280         @echo "TEST $(srcdir)/tests/load-tend-test SKIPPED"
281         @echo "TEST $(srcdir)/tests/navi-march-test SKIPPED"
282         @echo "TEST $(srcdir)/tests/fire-test SKIPPED"
283         @echo "TEST $(srcdir)/tests/torpedo-test SKIPPED"
284         @echo "TEST $(srcdir)/tests/bridgefall-test SKIPPED"
285         @echo "TEST $(srcdir)/tests/retreat-test SKIPPED"
286         @echo "TEST $(srcdir)/tests/update-test SKIPPED"
287         @echo "TEST $(srcdir)/tests/version-test SKIPPED"
288 endif
289         $(call run-test, $(srcdir)/tests/empdump-test)
290
291
292 ### Implicit rules
293
294 # Compile with dependencies as side effect, i.e. create %.d in
295 # addition to %.o.
296 ifeq ($(how_to_dep),fast)
297 %.o: %.c
298         $(call quiet-command,$(COMPILE.c) -MT $@ -MMD -MP -MF $(@:.o=.d) \
299         $(OUTPUT_OPTION) $< || { rm -f $(@:.o=.d) $@; false; },CC $@)
300 # Why the rm?  If gcc's preprocessor chokes, it leaves an empty
301 # dependency file behind, and doesn't touch the object file.  If an
302 # old object file exists, and is newer than the .c file, make will
303 # then consider the object file up-to-date.
304 endif
305 ifeq ($(how_to_dep),depcomp)
306 %.o: %.c
307         $(call quiet-command,source='$<' object='$@' depfile='$(@:.o=.d)' \
308         $(CCDEPMODE) $(depcomp) $(COMPILE.c) $(OUTPUT_OPTION) $<,CC $@)
309 endif
310 # Cancel the rule to compile %.c straight to %, it interferes with
311 # automatic dependency generation
312 %: %.c
313
314 %$(EXEEXT): %.o
315         $(call quiet-command,$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@,LINK $@)
316
317
318 info.nr/%: info/%.t
319         $(call quiet-command,$(NROFF) $(filter %.MAC, $^) $< | $(AWK) -f $(filter %/Blank.awk, $^) >$@ && test -s $@,NROFF $@)
320 # Pipes in make are a pain.  The "test -s" catches obvious errors.
321
322 info.html/%.html: info/%.t
323         $(call quiet-command,perl $(srcdir)/info/emp2html.pl $(info) <$< >$@,GEN $@)
324
325
326 ### Explicit rules
327
328 # Compilation
329
330 $(server): $(filter src/server/% src/lib/commands/% src/lib/player/% src/lib/subs/% src/lib/update/%, $(obj)) $(empth_obj) $(empth_lib) $(libs)
331         $(call quiet-command,$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@,LINK $@)
332
333 $(client): $(filter src/client/%, $(obj)) src/lib/global/version.o src/lib/gen/fnameat.o
334         $(call quiet-command,$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@,LINK $@)
335
336 $(util): $(libs)
337
338 lib/libcommon.a: $(filter src/lib/common/%, $(obj))
339 lib/libgen.a: $(filter src/lib/gen/%, $(obj))
340 lib/libglobal.a: $(filter src/lib/global/%, $(obj))
341 lib/liblwp.a: $(filter src/lib/lwp/%, $(obj))
342 lib/libw32.a: $(filter src/lib/w32/%, $(obj))
343
344 $(libs) $(empth_lib):
345         $(call quiet-command,$(AR) rc $@ $?,AR $@)
346         $(RANLIB) $@
347
348 src/lib/global/version.o: CPPFLAGS += -DVERSION='"$(version)"'
349 src/lib/global/version.o: $(src)
350
351 ifneq ($(revctrl),git)
352 $(srcdir)/.tarball-version: $(src)
353         v=`sed -e 's/-dirty$$//' <$@`; echo "$$v-dirty" >$@
354 # Force Make to start over after updating .tarball-version, so that
355 # $(version) gets the new value
356 $(srcdir)/.dirty-stamp: $(srcdir)/.tarball-version
357         >$@
358 include $(srcdir)/.dirty-stamp
359 endif
360
361 # Info formatting
362
363 # mksubj.pl reads $(tsrc) and writes $(tsubj).  A naive rule
364 #     $(tsubj): $(tsrc)
365 #           COMMAND
366 # runs COMMAND once for each target.  That's because multiple targets
367 # in an explicit rule is just a shorthand for one rule per target,
368 # each with the same prerequisites and commands.  Use a stamp file.
369 $(tsubj) info/toc: info/stamp-subj ;
370 info/stamp-subj: info/mksubj.pl $(tsrc)
371         $(call quiet-command,perl $(srcdir)/info/mksubj.pl $(subjects) $(filter %.t, $^),GEN '$(tsubj) info/toc')
372         >$@
373
374 info/TOP.t: info/mktop.pl info/subjects.mk
375         $(call quiet-command,perl $(srcdir)/info/mktop.pl $@ $(subjects),GEN $@)
376
377 info.nr/all: $(filter-out info.nr/all, $(info.nr))
378         >$@
379         (cd info.nr && LC_ALL=C ls -C $(info)) >>$@
380
381 info.html/all.html: info.nr/all info/ls2html.pl
382         expand $< | perl $(srcdir)/info/ls2html.pl >$@
383
384 $(info.nr): info/CRT.MAC info/INFO.MAC info/Blank.awk
385
386 $(info.html): info/emp2html.pl
387
388 info.ps: info/TROFF.MAC info/INFO.MAC info/TOP.t $(tsubj) $(tsrc)
389         groff $^ >$@
390
391 # Distributing
392
393 .PHONY: dist-source
394 dist-source: $(addprefix $(srcdir)/, $(src_distgen))
395         $(tarball) -x $(srcdir)/src/scripts/gen-tarball-version $(TARNAME) $(version) -C $(srcdir) $(src_distgen) $(src)
396
397 ifeq ($(revctrl),git)
398 .PHONY: $(srcdir)/sources.mk
399 $(srcdir)/sources.mk:
400         $(call quiet-command,echo "src := $(src)" >$@,GEN $@)
401 endif
402
403 .PHONY: dist-client
404 dist-client:
405         $(tarball) -x $(srcdir)/src/scripts/gen-client-configure        \
406         $(TARNAME)-client $(version)                                    \
407         -C $(srcdir)/src/client                                         \
408                 $(notdir $(filter src/client/%, $(src)))                \
409         -C $(srcdir)/include fnameat.h proto.h version.h                \
410         -C $(srcdir)/src/lib/global version.c                           \
411         -C $(srcdir)/src/lib/gen fnameat.c                              \
412         -C $(srcdir)/src/lib $(addprefix w32/, $(client/w32))           \
413         -C $(srcdir)/man empire.6                                       \
414         -C $(srcdir)/build-aux install-sh                               \
415         -C $(srcdir) COPYING INSTALL                                    \
416                 m4/ax_lib_socket_nsl.m4 m4/my_lib_readline.m4           \
417                 m4/my_terminfo.m4 m4/my_windows_api.m4
418
419 .PHONY: dist-info
420 dist-info: info html
421         $(tarball) $(TARNAME)-info-text $(version) -C info.nr $(info)
422         $(tarball) $(TARNAME)-info-html $(version) -C info.html $(addsuffix .html, $(info))
423
424 # Dependencies
425
426 ifneq ($(deps),)
427 -include $(deps)
428 endif
429
430
431 # Automatic remake of configuration
432 # See (autoconf)Automatic Remaking.
433 # This requires sufficiently recent versions of autoconf and automake
434
435 $(srcdir)/configure: configure.ac aclocal.m4
436         cd $(srcdir) && autoconf
437
438 # autoheader might not change config.h.in, so touch a stamp file.
439 $(srcdir)/config.h.in: stamp-h.in ;
440 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
441         cd $(srcdir) && autoheader
442         touch $@
443
444 $(srcdir)/aclocal.m4: $(filter m4/%.m4, $(src))
445         cd $(srcdir) && aclocal -I m4
446
447 # config.status might not change config.h; use the stamp file.
448 config.h: stamp-h ;
449 stamp-h: config.h.in config.status
450         ./config.status config.h stamp-h
451
452 GNUmakefile: GNUmakefile.in config.status
453         ./config.status $@
454
455 config.status: configure
456         ./config.status --recheck
457
458 src/lib/global/path.c src/client/ipglob.c: %: %.in GNUmakefile Make.mk
459         $(call quiet-command,$(subst.in) <$< >$@,GEN $@)