# GNU make syntax reigns in this file.

all_cflags += -Werror
all_cppflags += -MD -MP -MF .deps/$(subst .._,,$(subst /,_,$<)).d

ASCIIDOC = asciidoc
CPPCHECK = cppcheck
CPPCHECK_SUPPRESSIONS = misc/cppcheck-suppressions.txt
SCAN_BUILD = scan-build
DOCKER = docker
GPERF = gperf
XSLTPROC = xsltproc
MANPAGE_XSL = $(shell if [ -e /usr/local/etc/asciidoc/docbook-xsl/manpage.xsl ]; \
                then echo /usr/local/etc/asciidoc/docbook-xsl/manpage.xsl; \
                else echo /etc/asciidoc/docbook-xsl/manpage.xsl; fi)


version := \
    $(shell (git --git-dir=$(srcdir)/.git describe --dirty || git --git-dir=$(srcdir)/.git describe || echo vunknown) \
            2>/dev/null | sed -e 's/v//' -e 's/-/+/' -e 's/-/_/g')

dist_dir = ccache-$(version)
dist_archives = \
    ccache-$(version).tar.bz2 \
    ccache-$(version).tar.gz
ifneq ($(shell uname), Darwin)
    dist_archives += ccache-$(version).tar.xz
endif

generated_docs = ccache.1 doc/AUTHORS.html LICENSE.html doc/MANUAL.html doc/NEWS.html
built_dist_files = $(generated_docs)

headers = \
    src/ccache.h \
    src/compopt.h \
    src/conf.h \
    src/counters.h \
    src/getopt_long.h \
    src/hashtable.h \
    src/hashtable_itr.h \
    src/hashtable_private.h \
    src/hashutil.h \
    src/language.h \
    src/macroskip.h \
    src/manifest.h \
    src/mdfour.h \
    src/murmurhashneutral2.h \
    src/system.h \
    unittest/framework.h \
    unittest/suites.h \
    unittest/util.h

files_to_clean += *.tar.bz2 *.tar.gz *.tar.xz *.xml .deps/* perfdir.*
files_to_distclean += $(built_dist_files) src/version.c unittest/suites.h
files_to_distclean += .deps dev.mk

source_dist_files = \
    $(base_sources) \
    $(headers) \
    $(test_sources) \
    CONTRIBUTING.md \
    GPL-3.0.txt \
    LICENSE.txt \
    Makefile.in \
    README.md \
    autogen.sh \
    config.guess \
    config.h.in \
    config.sub \
    configure \
    configure.ac \
    dev.mk.in \
    doc/AUTHORS.txt \
    doc/MANUAL.txt \
    doc/NEWS.txt \
    install-sh \
    m4 \
    src/confitems.gperf \
    src/confitems_lookup.c \
    src/envtoconfitems.gperf \
    src/envtoconfitems_lookup.c \
    src/main.c \
    src/zlib/*.c \
    src/zlib/*.h \
    test/run \
    test/suites/*

dist_files = \
    $(addprefix $(srcdir)/, $(source_dist_files)) \
    $(built_dist_files)

uncrustify_exclude_files = \
    src/getopt_long.c \
    src/hashtable.c \
    src/hashtable_itr.c \
    src/snprintf.c

ifneq ($(shell sed 's/.*"\(.*\)".*/\1/' src/version.c 2>/dev/null),$(version))
  $(shell echo 'const char CCACHE_VERSION[] = "$(version)";' >src/version.c)
endif
src/version.o: src/version.c

%_lookup.c: %.gperf
	$(GPERF) $< | awk '/#ifdef __GNUC__/ { ++i; if (i == 2) { print "static"; }} {print}' >$@
	echo "static const size_t $$(echo $(notdir $*) | tr a-z A-Z)_TOTAL_KEYWORDS = $$(sed -nr 's/.*TOTAL_KEYWORDS = ([0-9]+).*/\1/p' $@);" >>$@

.PHONY: dist
dist: $(dist_archives)

$(dist_archives): $(dist_files)
	tmpdir=$$(mktemp -d /tmp/tmp-ccache-dist.XXXXXX) && \
	dir=$$tmpdir/$(dist_dir) && \
	mkdir $$dir && \
	(cd $(srcdir) && \
	 rsync -r --relative $(source_dist_files) $$dir) && \
	cp $(srcdir)/doc/INSTALL-from-release-archive.md $$dir/INSTALL.md && \
	cp $(built_dist_files) $$dir && \
	echo "Remove this file to enable developer mode." >$$dir/dev_mode_disabled && \
	(cd $$tmpdir && \
	 tarcompression= && \
	 case $@ in \
	     *.bz2) tarcompression=-j ;; \
	     *.gz) tarcompression=-z ;; \
	     *.xz) tarcompression=-J ;; \
	 esac && \
	 tar -c $$tarcompression -f $(CURDIR)/$@ $(dist_dir)) && \
	rm -rf $$tmpdir

.PHONY: distcheck
distcheck: $(firstword $(dist_archives))
	tmpdir=$$(mktemp -d /tmp/tmp-ccache-distcheck.XXXXXX) && \
	(cd $$tmpdir && \
	 tar xjf $(CURDIR)/$< && \
	 mkdir -p $(dist_dir)/build && \
	 cd $(dist_dir)/build && \
	 ../configure --prefix=$$tmpdir/root && \
	 $(MAKE) install && \
	 $(MAKE) installcheck) && \
	rm -rf $$tmpdir

.PHONY: docs
docs: $(generated_docs)

%.html: %.txt
	$(ASCIIDOC) -a revnumber=$(version) -a toc -b xhtml11 $<

%.xml: %.txt
	$(ASCIIDOC) -a revnumber=$(version) -d manpage -b docbook $<

ccache.1: doc/MANUAL.xml
	$(XSLTPROC) --nonet $(MANPAGE_XSL) $<

.PHONY: update-authors
update-authors:
	git log --pretty=format:"* %aN <%aE>" \
	  | sort -u \
	  | perl -00 -p -i -e 's/^\*.*/<STDIN> . "\n"/es' doc/AUTHORS.txt

.PHONY: check-syntax
check-syntax:
	$(CC) $(all_cppflags) -I. $(all_cflags) -S -o /dev/null $(CHK_SOURCES)

.PHONY: cppcheck
cppcheck:
	$(CPPCHECK) --suppressions-list=$(CPPCHECK_SUPPRESSIONS) \
	  --inline-suppr -q --enable=all --force \
	  $(non_3pp_sources) src/main.c $(test_sources)

.PHONY: uncrustify
uncrustify:
	uncrustify -c misc/uncrustify.cfg --no-backup --replace $(filter-out $(uncrustify_exclude_files), $(base_sources)) $(test_sources)

.PHONY: analyze
analyze:
	$(SCAN_BUILD) --use-cc=$(CC) ./configure
	$(SCAN_BUILD) --use-cc=$(CC) --status-bugs $(MAKE) -B

.PHONY: docker
docker: misc/Dockerfile
	$(DOCKER) build $(srcdir)

-include .deps/*.d
