build: Support $(DESTDIR).
[paraslash.git] / Makefile.real
index ee77cafc4e0b4eeb671c5ea7ec8b7281efe2ac1e..4970696b67ec9b38a365aabb992e240b6ecce853 100644 (file)
@@ -1,7 +1,15 @@
-VARDIR := /var/paraslash
-MANDIR := $(datarootdir)/man/man1
+# Implicit rules are implemented in make as suffix rules. The following rule
+# empties the suffix list to disable the predefined implicit rules. This
+# increases performance and avoids hard-to-debug behaviour.
+.SUFFIXES:
+MAKEFLAGS += -Rr
+ifeq ("$(origin CC)", "default")
+        CC := cc
+endif
+
+vardir := /var/paraslash
+mandir := $(datarootdir)/man/man1
 STRIP := $(CROSS_COMPILE)strip
-HOSTCC ?= cc
 MKDIR_P := mkdir -p
 prefixed_executables := $(addprefix para_, $(executables))
 
@@ -10,6 +18,7 @@ uname_s := $(shell uname -s 2>/dev/null || echo "UNKNOWN_OS")
 uname_rs := $(shell uname -rs)
 cc_version := $(shell $(CC) --version | head -n 1)
 GIT_VERSION := $(shell ./GIT-VERSION-GEN git-version.h)
+COPYRIGHT_YEAR := 2017
 
 ifeq ("$(origin O)", "command line")
        build_dir := $(O)
@@ -24,7 +33,6 @@ cmdline_dir := $(build_dir)/cmdline
 cmdlist_dir := $(build_dir)/cmdlist
 m4depdir := $(build_dir)/m4deps
 help2man_dir := $(build_dir)/help2man
-hostbin_dir := $(build_dir)/host/bin
 m4_ggo_dir := m4/gengetopt
 test_dir := t
 
@@ -51,13 +59,12 @@ play_objs := $(addprefix $(object_dir)/, $(play_objs))
 man_pages := $(patsubst %, $(man_dir)/%.1, $(prefixed_executables))
 
 autocrap := config.h.in configure
-tarball_pfx := $(PACKAGE_TARNAME)-$(shell git describe --dirty)
+tarball_pfx := $(PACKAGE_TARNAME)-$(GIT_VERSION)
 tarball_delete := $(addprefix $(tarball_pfx)/, web .gitignore)
 tarball := $(tarball_pfx).tar.bz2
 
-.PHONY: dep all clean clean2 distclean maintainer-clean install man tarball
-all: dep $(prefixed_executables) $(man_pages)
-dep: $(deps)
+.PHONY: all mostlyclean clean distclean maintainer-clean install man tarball
+all: $(prefixed_executables) $(man_pages)
 man: $(man_pages)
 tarball: $(tarball)
 
@@ -69,7 +76,7 @@ ifeq ($(findstring clean, $(MAKECMDGOALS)),)
 endif
 
 $(object_dir) $(man_dir) $(ggo_dir) $(cmdline_dir) $(dep_dir) $(m4depdir) \
-               $(help2man_dir) $(hostbin_dir) $(cmdlist_dir):
+               $(help2man_dir) $(cmdlist_dir):
        $(Q) $(MKDIR_P) $@
 
 # When in doubt, use brute force (Ken Thompson)
@@ -81,12 +88,11 @@ $(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,\
 $(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,\
 $(subst z,Z,$1))))))))))))))))))))))))))
 
-CPPFLAGS += -DBINDIR='"$(BINDIR)"'
+CPPFLAGS += -DBINDIR='"$(bindir)"'
+CPPFLAGS += -DCOPYRIGHT_YEAR='"$(COPYRIGHT_YEAR)"'
 CPPFLAGS += -DBUILD_DATE='"$(build_date)"'
 CPPFLAGS += -DUNAME_RS='"$(uname_rs)"'
 CPPFLAGS += -DCC_VERSION='"$(cc_version)"'
-CPPFLAGS += -DMAIN_INPUT_FILE_IS_$(*F)
-CPPFLAGS += $(arch_cppflags)
 CPPFLAGS += -I/usr/local/include
 CPPFLAGS += -I$(cmdline_dir)
 CPPFLAGS += -I$(cmdlist_dir)
@@ -105,11 +111,13 @@ STRICT_CFLAGS += -Wredundant-decls
 STRICT_CFLAGS += -Wno-sign-compare -Wno-unknown-pragmas
 STRICT_CFLAGS += -Wformat -Wformat-security
 STRICT_CFLAGS += -Wmissing-format-attribute
+STRICT_CFLAGS += -Wdeclaration-after-statement
 
 LDFLAGS += $(clock_gettime_ldflags)
 
 ifeq ($(uname_s),Linux)
        # these cause warnings on *BSD
+       CPPFLAGS += -Wunused-macros
        STRICT_CFLAGS += -fdata-sections -ffunction-sections
        STRICT_CFLAGS += -Wstrict-prototypes
        STRICT_CFLAGS += -Wshadow
@@ -117,6 +125,13 @@ ifeq ($(uname_s),Linux)
        LDFLAGS += -Wl,--gc-sections
 endif
 
+cc-option = $(shell \
+       $(CC) $(1) -Werror -c -x c /dev/null -o /dev/null > /dev/null 2>&1 \
+       && echo "$(1)" \
+)
+
+STRICT_CFLAGS += $(call cc-option, -Wformat-signedness)
+
 # To put more focus on warnings, be less verbose as default
 # Use 'make V=1' to see the full commands
 ifeq ("$(origin V)", "command line")
@@ -131,7 +146,7 @@ $(cmdlist_dir)/%.command_list.h: %.cmd %.c | $(cmdlist_dir)
 $(cmdlist_dir)/%.command_list.man: %.cmd %.c | $(cmdlist_dir)
        @[ -z "$(Q)" ] || echo 'GEN $@'
        $(Q) ./command_util.bash man < $< > $@
-$(cmdlist_dir)/%.completion.h: %.cmd %.c | $(cmdlist_dir)
+$(cmdlist_dir)/%.completion.h: %.cmd | $(cmdlist_dir)
        @[ -z "$(Q)" ] || echo 'GEN $@'
        $(Q) ./command_util.bash compl $(strip $(call TOUPPER,$(*F)))_COMPLETERS \
                $(strip $(call TOUPPER,$(*F)))_COMMANDS < $< > $@
@@ -146,9 +161,9 @@ $(cmdlist_dir)/afs.command_list.man \
 $(cmdlist_dir)/afs.completion.h \
 : afs.c aft.c attribute.c
 
-$(cmdlist_dir)/audiod,command_list.h \
-$(cmdlist_dir)/audiod,command_list.man \
-$(cmdlist_dir)/audiod,completion.h \
+$(cmdlist_dir)/audiod.command_list.h \
+$(cmdlist_dir)/audiod.command_list.man \
+$(cmdlist_dir)/audiod.completion.h \
 : audiod_command.c
 
 server_command_lists := $(cmdlist_dir)/server.command_list.man \
@@ -164,7 +179,8 @@ $(man_dir)/para_server.1: man_util_command_lists := $(server_command_lists)
 $(man_dir)/para_audiod.1: man_util_command_lists := $(audiod_command_lists)
 $(man_dir)/para_play.1: man_util_command_lists := $(play_command_lists)
 
-$(man_dir)/para_%.1: $(ggo_dir)/%.ggo man_util.bash | $(man_dir) $(help2man_dir)
+$(man_dir)/para_%.1: $(ggo_dir)/%.ggo man_util.bash \
+               git-version.h | $(man_dir) $(help2man_dir)
        @[ -z "$(Q)" ] || echo 'MAN $<'
        $(Q) \
                COMMAND_LISTS="$(man_util_command_lists)" \
@@ -178,22 +194,32 @@ $(man_dir)/para_%.1: $(ggo_dir)/%.ggo man_util.bash | $(man_dir) $(help2man_dir)
                WRITERS="$(writers)" \
                ./man_util.bash $@
 
-$(hostbin_dir)/error2: error2.c | $(hostbin_dir)
-       @[ -z "$(Q)" ] || echo 'HCC $<'
-       $(Q) $(HOSTCC) -o $@ $<
-error2.h: $(hostbin_dir)/error2 config.h
-       @[ -z "$(Q)" ] || echo 'ER2 $<'
-       @echo "$(object_executable_matrix)" | $< > $@
-
 $(object_dir)/%.o: %.c | $(object_dir)
 
 $(object_dir)/opus%.o $(dep_dir)/opus%.d: CPPFLAGS += $(opus_cppflags)
-$(object_dir)/gui%.o $(dep_dir)/gui%.d: CPPFLAGS += $(curses_cppflags)
+$(object_dir)/gui.o $(object_dir)/gui%.o $(dep_dir)/gui%.d \
+: CPPFLAGS += $(curses_cppflags)
 $(object_dir)/spx%.o $(dep_dir)/spx%.d: CPPFLAGS += $(speex_cppflags)
+$(object_dir)/flac%.o $(dep_dir)/flac%.d: CPPFLAGS += $(flac_cppflags)
+
 $(object_dir)/mp3_afh.o $(dep_dir)/mp3_afh.d: CPPFLAGS += $(id3tag_cppflags)
 $(object_dir)/crypt.o $(dep_dir)/crypt.d: CPPFLAGS += $(openssl_cppflags)
 $(object_dir)/gcrypt.o $(dep_dir)/gcrypt.d: CPPFLAGS += $(gcrypt_cppflags)
 $(object_dir)/ao_write.o $(dep_dir)/ao_write.d: CPPFLAGS += $(ao_cppflags)
+$(object_dir)/aac_afh.o $(dep_dir)/aac_afh.d: CPPFLAGS += $(mp4v2_cppflags)
+$(object_dir)/alsa%.o $(dep_dir)/alsa%.d: CPPFLAGS += $(alsa_cppflags)
+
+$(object_dir)/interactive.o $(dep_dir)/interactive.d \
+: CPPFLAGS += $(readline_cppflags)
+
+$(object_dir)/resample_filter.o $(dep_dir)/resample_filter.d \
+: CPPFLAGS += $(samplerate_cppflags)
+
+$(object_dir)/oss_write.o $(dep_dir)/oss_write.d \
+: CPPFLAGS += $(oss_cppflags)
+
+$(object_dir)/ao_write.o $(dep_dir)/ao_write.d \
+: CPPFLAGS += $(ao_cppflags) $(pthread_cppflags)
 
 $(object_dir)/mp3dec_filter.o $(dep_dir)/mp3dec_filter.d \
 : CPPFLAGS += $(mad_cppflags)
@@ -247,7 +273,7 @@ $(object_dir)/%.cmdline.o: $(cmdline_dir)/%.cmdline.c $(cmdline_dir)/%.cmdline.h
 # The compiler outputs dependencies either as foo.h or as some_directory/foo.h,
 # depending on whether the latter file exists. Since make needs the directory
 # part we prefix the dependency as appropriate.
-$(dep_dir)/%.d: %.c error2.h | $(dep_dir)
+$(dep_dir)/%.d: %.c | $(dep_dir)
        @[ -z "$(Q)" ] || echo 'DEP $<'
        $(Q) $(CC) $(CPPFLAGS) -MM -MG -MP -MT $@ -MT $(object_dir)/$(*F).o $< \
                | sed -e "s@ \([a-zA-Z0-9_]\{1,\}\.cmdline.h\)@ $(cmdline_dir)/\1@g" \
@@ -255,7 +281,8 @@ $(dep_dir)/%.d: %.c error2.h | $(dep_dir)
                -e "s@ \([a-zA-Z0-9_]\{1,\}\.completion.h\)@ $(cmdlist_dir)/\1@g" > $@
 
 para_recv para_afh para_play para_server: LDFLAGS += $(id3tag_ldflags)
-para_write para_play para_audiod: LDFLAGS += $(ao_ldflags) $(core_audio_ldflags)
+para_write para_play para_audiod \
+: LDFLAGS += $(ao_ldflags) $(pthread_ldflags) $(core_audio_ldflags)
 para_client para_audioc para_play : LDFLAGS += $(readline_ldflags)
 para_server: LDFLAGS += $(osl_ldflags)
 para_gui: LDFLAGS += $(curses_ldflags)
@@ -263,14 +290,14 @@ para_server \
 para_client \
 para_audiod \
 :LDFLAGS += \
-       $(openssl_ldflags) \
-       $(gcrypt_ldflags)
+       $(crypto_ldflags)
 
 para_audiod \
 para_filter \
 para_play \
 : LDFLAGS += \
        $(mad_ldflags) \
+       $(faad_ldflags) \
        $(samplerate_ldflags) \
        -lm
 
@@ -297,42 +324,41 @@ para_recv \
        $(flac_ldflags)
 
 para_server \
-para_client \
-para_audioc \
-para_audiod \
+para_play \
+para_afh \
 para_recv \
 : LDFLAGS += \
-       $(socket_ldflags) $(nsl_ldflags)
+       $(mp4v2_ldflags)
+
+para_afh para_recv para_server para_play: LDFLAGS += $(iconv_ldflags)
 
 $(foreach exe,$(executables),$(eval para_$(exe): $$($(exe)_objs)))
 $(prefixed_executables):
        @[ -z "$(Q)" ] || echo 'LD $@'
        $(Q) $(CC) $^ -o $@ $(LDFLAGS)
 
-clean:
-       @[ -z "$(Q)" ] || echo 'CLEAN'
+mostlyclean:
+       @[ -z "$(Q)" ] || echo 'MOSTLYCLEAN'
        $(Q) rm -f para_*
        $(Q) rm -rf $(object_dir)
-
-clean2: clean
-       @[ -z "$(Q)" ] || echo 'CLEAN2'
+clean: mostlyclean
+       @[ -z "$(Q)" ] || echo 'CLEAN'
        $(Q) rm -rf $(build_dir)
-distclean: clean2 test-clean
+distclean: clean
        @[ -z "$(Q)" ] || echo 'DISTCLEAN'
-       $(Q) rm -f Makefile autoscan.log config.status config.log error2.h
-       $(Q) rm -f GPATH GRTAGS GSYMS GTAGS
-
+       $(Q) rm -f Makefile autoscan.log config.status config.log
+       $(Q) rm -f config.h configure config.h.in
 maintainer-clean: distclean
        @[ -z "$(Q)" ] || echo 'MAINTAINER-CLEAN'
-       $(Q) rm -f *.tar.bz2 config.h configure config.h.in
-       $(Q) rm -rf web_sync
+       $(Q) rm -f *.tar.bz2
+       $(Q) rm -f GPATH GRTAGS GSYMS GTAGS
 
 install: all man
-       $(MKDIR_P) $(BINDIR) $(MANDIR)
+       $(MKDIR_P) $(DESTDIR)$(bindir) $(DESTDIR)$(mandir)
        $(INSTALL) -s --strip-program $(STRIP) -m 755 \
-               $(prefixed_executables) $(BINDIR)
-       $(INSTALL) -m 644 $(man_pages) $(MANDIR)
-       $(MKDIR_P) $(VARDIR) >/dev/null 2>&1 || true # not fatal, so don't complain
+               $(prefixed_executables) $(DESTDIR)$(bindir)
+       $(INSTALL) -m 644 $(man_pages) $(DESTDIR)$(mandir)
+       $(MKDIR_P) $(DESTDIR)$(vardir) >/dev/null 2>&1 || true # not fatal, so don't complain
 
 $(tarball):
        $(Q) rm -rf $(tarball) $(tarball_pfx)