# The Makefile for rsync (configure creates it from Makefile.in).

prefix=@prefix@
datarootdir=@datarootdir@
exec_prefix=@exec_prefix@
bindir=@bindir@
libdir=@libdir@/rsync
mandir=@mandir@
with_rrsync=@with_rrsync@

LIBS=@LIBS@
CC=@CC@
AWK=@AWK@
CFLAGS=@CFLAGS@
CPPFLAGS=@CPPFLAGS@
CXX=@CXX@
CXXFLAGS=@CXXFLAGS@
EXEEXT=@EXEEXT@
LDFLAGS=@LDFLAGS@
LIBOBJDIR=lib/

INSTALLCMD=@INSTALL@
INSTALLMAN=@INSTALL@

srcdir=@srcdir@
MKDIR_P=@MKDIR_P@
VPATH=$(srcdir)
SHELL=/bin/sh

.SUFFIXES:
.SUFFIXES: .c .o

ROLL_SIMD_x86_64=simd-checksum-x86_64.o
ROLL_ASM_x86_64=simd-checksum-avx2.o
MD5_ASM_x86_64=lib/md5-asm-x86_64.o

GENFILES=configure.sh aclocal.m4 config.h.in rsync.1 rsync.1.html \
	 rsync-ssl.1 rsync-ssl.1.html rsyncd.conf.5 rsyncd.conf.5.html \
	 @GEN_RRSYNC@
HEADERS=byteorder.h config.h errcode.h proto.h rsync.h ifuncs.h itypes.h inums.h \
	lib/pool_alloc.h lib/mdigest.h lib/md-defines.h
LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o lib/md5.o \
	lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattrs.o lib/acl.o @LIBOBJS@
zlib_OBJS=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
	zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
OBJS1_NO_MAIN=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
	util1.o util2.o checksum.o match.o syscall.o log.o backup.o delete.o
OBJS1=$(OBJS1_NO_MAIN) main.o
OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
	usage.o fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
OBJS3=progress.o pipe.o @MD5_ASM@ @ROLL_SIMD@ @ROLL_ASM@
DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
popt_OBJS= popt/popt.o  popt/poptconfig.o \
	popt/popthelp.o popt/poptparse.o popt/poptint.o
OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) @BUILD_ZLIB@ @BUILD_POPT@

TLS_OBJ = tls.o syscall.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/permstring.o lib/sysxattrs.o @BUILD_POPT@

# Programs we must have to run the test cases
CHECK_PROGS = rsync$(EXEEXT) tls$(EXEEXT) getgroups$(EXEEXT) getfsdev$(EXEEXT) \
	testrun$(EXEEXT) trimslash$(EXEEXT) t_unsafe$(EXEEXT) t_chmod_secure$(EXEEXT) \
	t_rename_secure$(EXEEXT) t_symlink_secure$(EXEEXT) t_secure_relpath$(EXEEXT) t_acl$(EXEEXT) t_hashtable_overflow$(EXEEXT) t_iwildmatch$(EXEEXT) t_clean_fname$(EXEEXT) t_safe_arg$(EXEEXT) wildtest$(EXEEXT) simdtest$(EXEEXT)

CHECK_SYMLINKS = testsuite/chown-fake_test.py testsuite/devices-fake_test.py \
	testsuite/xattrs-hlink_test.py testsuite/exclude-lsh_test.py

# Objects for CHECK_PROGS to clean
CHECK_OBJS=tls.o testrun.o getgroups.o getfsdev.o t_stub.o t_unsafe.o t_chmod_secure.o t_rename_secure.o t_symlink_secure.o t_secure_relpath.o t_acl.o t_hashtable_overflow.o t_iwildmatch.o t_clean_fname.o t_safe_arg.o trimslash.o wildtest.o
# Compile-only feature-shape checks.
CHECK_COMPILE_OBJS=syscall-no-at-fdcwd.o

# note that the -I. is needed to handle config.h when using VPATH
.c.o:
@OBJ_SAVE@
	$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) -c $< @CC_SHOBJ_FLAG@
@OBJ_RESTORE@

# NOTE: consider running "packaging/smart-make" instead of "make" to auto-handle
# any changes to configure.sh and the main Makefile prior to a "make all".
all: Makefile rsync$(EXEEXT) stunnel-rsyncd.conf @MAKE_RRSYNC@ @MAKE_MAN@
.PHONY: all

syscall-no-at-fdcwd.o: syscall.c $(HEADERS)
	$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
	    -DRSYNC_TEST_NO_AT_FDCWD -c $(srcdir)/syscall.c -o $@

.PHONY: install
install: all
	-$(MKDIR_P) $(DESTDIR)$(bindir)
	$(INSTALLCMD) $(INSTALL_STRIP) -m 755 rsync$(EXEEXT) $(DESTDIR)$(bindir)
	$(INSTALLCMD) -m 755 $(srcdir)/rsync-ssl $(DESTDIR)$(bindir)
	-$(MKDIR_P) $(DESTDIR)$(mandir)/man1
	-$(MKDIR_P) $(DESTDIR)$(mandir)/man5
	for fn in rsync.1 rsync-ssl.1; do \
	    if test -f $$fn; then $(INSTALLMAN) -m 644 $$fn $(DESTDIR)$(mandir)/man1; \
	    elif test -f $(srcdir)/$$fn; then $(INSTALLMAN) -m 644 $(srcdir)/$$fn $(DESTDIR)$(mandir)/man1; fi; \
	done
	for fn in rsyncd.conf.5; do \
	    if test -f $$fn; then $(INSTALLMAN) -m 644 $$fn $(DESTDIR)$(mandir)/man5; \
	    elif test -f $(srcdir)/$$fn; then $(INSTALLMAN) -m 644 $(srcdir)/$$fn $(DESTDIR)$(mandir)/man5; fi; \
	done
	if test "$(with_rrsync)" = yes; then \
	    $(INSTALLCMD) -m 755 rrsync $(DESTDIR)$(bindir); \
	    fn=rrsync.1; \
	    if test -f $$fn; then $(INSTALLMAN) -m 644 $$fn $(DESTDIR)$(mandir)/man1; \
	    elif test -f $(srcdir)/$$fn; then $(INSTALLMAN) -m 644 $(srcdir)/$$fn $(DESTDIR)$(mandir)/man1; fi; \
	fi

install-ssl-daemon: stunnel-rsyncd.conf
	-$(MKDIR_P) $(DESTDIR)/etc/stunnel
	$(INSTALLCMD) -m 644 stunnel-rsyncd.conf $(DESTDIR)/etc/stunnel/rsyncd.conf
	@if ! ls /etc/rsync-ssl/certs/server.* >/dev/null 2>/dev/null; then \
	    echo "Note that you'll need to install the certificate used by /etc/stunnel/rsyncd.conf"; \
	fi

install-all: install install-ssl-daemon

install-strip:
	$(MAKE) INSTALL_STRIP='-s' install

.PHONY: uninstall
uninstall:
	rm -f $(DESTDIR)$(bindir)/rsync$(EXEEXT) $(DESTDIR)$(bindir)/rsync-ssl
	rm -f $(DESTDIR)$(bindir)/rrsync
	rm -f $(DESTDIR)$(mandir)/man1/rsync.1 $(DESTDIR)$(mandir)/man1/rsync-ssl.1
	rm -f $(DESTDIR)$(mandir)/man1/rrsync.1
	rm -f $(DESTDIR)$(mandir)/man5/rsyncd.conf.5

.PHONY: uninstall-ssl-daemon
uninstall-ssl-daemon:
	rm -f $(DESTDIR)/etc/stunnel/rsyncd.conf

.PHONY: uninstall-all
uninstall-all: uninstall uninstall-ssl-daemon

rsync$(EXEEXT): $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)

rrsync: support/rrsync
	cp -p $(srcdir)/support/rrsync rrsync

$(OBJS): $(HEADERS)
$(CHECK_OBJS): $(HEADERS)
tls.o xattrs.o: lib/sysxattrs.h
usage.o: version.h latest-year.h help-rsync.h help-rsyncd.h git-version.h default-cvsignore.h
loadparm.o: default-dont-compress.h daemon-parm.h

flist.o: rounding.h
log.o: rounding.h

default-cvsignore.h default-dont-compress.h: rsync.1.md define-from-md.awk
	$(AWK) -f $(srcdir)/define-from-md.awk -v hfile=$@ $(srcdir)/rsync.1.md

help-rsync.h help-rsyncd.h: rsync.1.md help-from-md.awk
	$(AWK) -f $(srcdir)/help-from-md.awk -v hfile=$@ $(srcdir)/rsync.1.md

daemon-parm.h: daemon-parm.txt daemon-parm.awk
	$(AWK) -f $(srcdir)/daemon-parm.awk $(srcdir)/daemon-parm.txt

rounding.h: rounding.c rsync.h proto.h
	@for r in 0 1 3; do \
	    if $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o rounding -DEXTRA_ROUNDING=$$r -I. $(srcdir)/rounding.c >rounding.out 2>&1; then \
		echo "#define EXTRA_ROUNDING $$r" >rounding.h; \
		if test -f "$$HOME/build_farm/build_test.fns"; then \
		    echo "EXTRA_ROUNDING is $$r" >&2; \
		fi; \
		break; \
	    fi; \
	done
	@rm -f rounding
	@if test -f rounding.h; then : ; else \
	    cat rounding.out 1>&2; \
	    echo "Failed to create rounding.h!" 1>&2; \
	    exit 1; \
	fi
	@rm -f rounding.out

git-version.h: ALWAYS_RUN
	$(srcdir)/mkgitver

.PHONY: ALWAYS_RUN
ALWAYS_RUN:

simd-checksum-x86_64.o: simd-checksum-x86_64.cpp
	@$(srcdir)/cmd-or-msg disable-roll-simd $(CXX) -I. $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $(srcdir)/simd-checksum-x86_64.cpp

simd-checksum-avx2.o: simd-checksum-avx2.S
	@$(srcdir)/cmd-or-msg disable-roll-asm $(CC) $(CFLAGS) -I. @NOEXECSTACK@ -c -o $@ $(srcdir)/simd-checksum-avx2.S

lib/md5-asm-x86_64.o: lib/md5-asm-x86_64.S lib/md-defines.h
	@$(srcdir)/cmd-or-msg disable-md5-asm $(CC) -I. @NOEXECSTACK@ -c -o $@ $(srcdir)/lib/md5-asm-x86_64.S

tls$(EXEEXT): $(TLS_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(TLS_OBJ) $(LIBS)

testrun$(EXEEXT): testrun.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ testrun.o

getgroups$(EXEEXT): getgroups.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ getgroups.o $(LIBS)

getfsdev$(EXEEXT): getfsdev.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ getfsdev.o $(LIBS)

TRIMSLASH_OBJ = trimslash.o syscall.o util2.o t_stub.o lib/compat.o lib/snprintf.o
trimslash$(EXEEXT): $(TRIMSLASH_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(TRIMSLASH_OBJ) $(LIBS)

T_UNSAFE_OBJ = t_unsafe.o syscall.o util1.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/wildmatch.o
t_unsafe$(EXEEXT): $(T_UNSAFE_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_UNSAFE_OBJ) $(LIBS)

T_HASHTABLE_OVERFLOW_OBJ = t_hashtable_overflow.o hashtable.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/wildmatch.o
t_hashtable_overflow$(EXEEXT): $(T_HASHTABLE_OVERFLOW_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_HASHTABLE_OVERFLOW_OBJ) $(LIBS)

T_IWILDMATCH_OBJ = t_iwildmatch.o lib/wildmatch.o
t_iwildmatch$(EXEEXT): $(T_IWILDMATCH_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_IWILDMATCH_OBJ) $(LIBS)

T_CLEAN_FNAME_OBJ = t_clean_fname.o syscall.o util1.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/wildmatch.o
t_clean_fname$(EXEEXT): $(T_CLEAN_FNAME_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_CLEAN_FNAME_OBJ) $(LIBS)

# safe_arg lives in options.c alongside the whole option parser.  Rather than
# rely on a non-portable linker --gc-sections to drop the parser (GNU ld only;
# macOS ld64 and the cygwin PE linker do not), link the real rsync objects so
# every dep resolves.  t_safe_arg_main.o is main.c with main() renamed out, to
# supply main.c's globals while letting t_safe_arg.o provide the test's main().
# OBJS minus main.o is spelled out via OBJS1_NO_MAIN because $(filter-out) is
# GNU-make-only; BSD and Solaris make expand it to nothing.
t_safe_arg_main.o: main.c $(HEADERS)
	$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) -Dmain=rsync_unused_main -c $(srcdir)/main.c -o t_safe_arg_main.o
T_SAFE_ARG_OBJ = t_safe_arg.o t_safe_arg_main.o $(OBJS1_NO_MAIN) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) @BUILD_ZLIB@ @BUILD_POPT@
t_safe_arg$(EXEEXT): $(T_SAFE_ARG_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_SAFE_ARG_OBJ) $(LIBS)

T_CHMOD_SECURE_OBJ = t_chmod_secure.o syscall.o util1.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/wildmatch.o lib/permstring.o
t_chmod_secure$(EXEEXT): $(T_CHMOD_SECURE_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_CHMOD_SECURE_OBJ) $(LIBS)

T_RENAME_SECURE_OBJ = t_rename_secure.o syscall.o util1.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/wildmatch.o lib/permstring.o
t_rename_secure$(EXEEXT): $(T_RENAME_SECURE_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_RENAME_SECURE_OBJ) $(LIBS)

T_SYMLINK_SECURE_OBJ = t_symlink_secure.o syscall.o util1.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/wildmatch.o lib/permstring.o
t_symlink_secure$(EXEEXT): $(T_SYMLINK_SECURE_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_SYMLINK_SECURE_OBJ) $(LIBS)

T_SECURE_RELPATH_OBJ = t_secure_relpath.o syscall.o util1.o util2.o t_stub.o lib/compat.o lib/snprintf.o lib/wildmatch.o lib/permstring.o
t_secure_relpath$(EXEEXT): $(T_SECURE_RELPATH_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_SECURE_RELPATH_OBJ) $(LIBS)

# Unit test for lib/acl.c: compares our fd/at ACL ops against the system libacl
# (linked via $(LIBS), which carries -lacl).  lib/acl.o references no rsync
# globals, so this links with no stubs.  Self-skips (exit 77) when built
# without SUPPORT_ACL_FD.
T_ACL_OBJ = t_acl.o lib/acl.o
t_acl$(EXEEXT): $(T_ACL_OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(T_ACL_OBJ) $(LIBS)

.PHONY: conf
conf: configure.sh config.h.in

.PHONY: gen
gen: conf proto.h man git-version.h

aclocal.m4: $(srcdir)/m4/*.m4
	aclocal -I $(srcdir)/m4

configure.sh config.h.in: configure.ac aclocal.m4
	@if test -f configure.sh; then cp -p configure.sh configure.sh.old; else touch configure.sh.old; fi
	@if test -f config.h.in; then cp -p config.h.in config.h.in.old; else touch config.h.in.old; fi
	autoconf -o configure.sh
	autoheader && touch config.h.in
	@if diff configure.sh configure.sh.old >/dev/null 2>&1; then \
	    echo "configure.sh is unchanged."; \
	    rm configure.sh.old; \
	else \
	    echo "configure.sh has CHANGED."; \
	fi
	@if diff config.h.in config.h.in.old >/dev/null 2>&1; then \
	    echo "config.h.in is unchanged."; \
	    rm config.h.in.old; \
	else \
	    echo "config.h.in has CHANGED."; \
	fi
	@if test -f configure.sh.old || test -f config.h.in.old; then \
	    if test "$(MAKECMDGOALS)" = reconfigure; then \
		echo 'Continuing with "make reconfigure".'; \
	    else \
		echo 'You may need to run:'; \
		echo '  make reconfigure'; \
		exit 1; \
	    fi \
	fi

.PHONY: reconfigure
reconfigure: configure.sh
	./config.status --recheck
	./config.status

.PHONY: restatus
restatus:
	./config.status

Makefile: Makefile.in config.status configure.sh config.h.in
	@if test -f Makefile; then cp -p Makefile Makefile.old; else touch Makefile.old; fi
	@./config.status
	@if diff Makefile Makefile.old >/dev/null 2>&1; then \
	    echo "Makefile is unchanged."; \
	    rm Makefile.old; \
	else \
	    if test "$(MAKECMDGOALS)" = reconfigure; then \
		echo 'Continuing with "make reconfigure".'; \
	    else \
		echo "Makefile updated -- rerun your make command."; \
		exit 1; \
	    fi \
	fi

stunnel-rsyncd.conf: $(srcdir)/stunnel-rsyncd.conf.in Makefile
	sed 's;\@bindir\@;$(bindir);g' <$(srcdir)/stunnel-rsyncd.conf.in >stunnel-rsyncd.conf

.PHONY: proto
proto: proto.h-tstamp

proto.h: proto.h-tstamp
	@if test -f proto.h; then :; else cp -p $(srcdir)/proto.h .; fi

proto.h-tstamp: $(srcdir)/*.c $(srcdir)/lib/compat.c daemon-parm.h
	$(AWK) -f $(srcdir)/mkproto.awk $(srcdir)/*.c $(srcdir)/lib/compat.c daemon-parm.h

.PHONY: man
man: rsync.1 rsync-ssl.1 rsyncd.conf.5 @MAKE_RRSYNC_1@

rsync.1: rsync.1.md md-convert version.h Makefile
	@$(srcdir)/maybe-make-man rsync.1.md

rsync-ssl.1: rsync-ssl.1.md md-convert version.h Makefile
	@$(srcdir)/maybe-make-man rsync-ssl.1.md

rsyncd.conf.5: rsyncd.conf.5.md md-convert version.h Makefile
	@$(srcdir)/maybe-make-man rsyncd.conf.5.md

rrsync.1: support/rrsync.1.md md-convert Makefile
	@$(srcdir)/maybe-make-man support/rrsync.1.md

.PHONY: clean
clean: cleantests
	rm -f *~ $(OBJS) $(CHECK_PROGS) $(CHECK_OBJS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS) @MAKE_RRSYNC@ \
		git-version.h rounding rounding.h *.old rsync*.1 rsync*.5 @MAKE_RRSYNC_1@ \
		*.html daemon-parm.h help-*.h default-*.h proto.h proto.h-tstamp
	rm -f *.gcno *.gcda lib/*.gcno lib/*.gcda zlib/*.gcno zlib/*.gcda popt/*.gcno popt/*.gcda
	rm -rf coverage coverage-tcp coverage-all coverage-fallback

.PHONY: cleantests
cleantests:
	rm -rf ./testtmp*

# We try to delete built files from both the source and build
# directories, just in case somebody previously configured things in
# the source directory.
.PHONY: distclean
distclean: clean
	for dir in $(srcdir) . ; do \
	    (cd "$$dir" && rm -rf Makefile config.h config.status stunnel-rsyncd.conf \
	     lib/dummy popt/dummy zlib/dummy config.cache config.log shconfig \
	     $(GENFILES) autom4te.cache) ; \
	done

# this target is really just for my use. It only works on a limited
# range of machines and is used to produce a list of potentially
# dead (ie. unused) functions in the code. (tridge)
.PHONY: finddead
finddead:
	nm *.o */*.o |grep 'U ' | awk '{print $$2}' | sort -u > nmused.txt
	nm *.o */*.o |grep 'T ' | awk '{print $$3}' | sort -u > nmfns.txt
	comm -13 nmused.txt nmfns.txt
	@rm nmused.txt nmfns.txt

# 'check' is the GNU name, 'test' is the name for everybody else :-)
.PHONY: test
test: check

# There seems to be no standard way to specify some variables as
# exported from a Makefile apart from listing them like this.

# This depends on building rsync; if we need any helper programs it
# should depend on them too.

# We try to run the scripts with POSIX mode on, in the hope that will
# catch Bash-isms earlier even if we're running on GNU.  Of course, we
# might lose in the future where POSIX diverges from old sh.

# `make check` runs tests in parallel by default. Override with
# `make check CHECK_J=1` (serial) or any other value.
CHECK_J = 8

# Parallelism for `make coverage`. Defaults to the same as CHECK_J: the
# coverage build sets -fprofile-update=atomic (atomic in-memory counters) and
# gcc's libgcov serializes the per-source .gcda read-modify-write merge with a
# file lock, so concurrent rsync processes (incl. the forked sender/generator/
# receiver) accumulate exactly -- verified by a count-linearity check (a hot
# line accumulates identically at -j1 and -P16). Override with
# `make coverage COVERAGE_J=1` if your libgcov does not lock .gcda merges.
COVERAGE_J = $(CHECK_J)

# Output directory and extra runtests.py flags for `make coverage`. The
# `coverage-tcp` target reuses the coverage recipe with --use-tcp (real
# loopback rsyncd, which exercises the TCP accept/auth path and the
# require_tcp-only tests) and a separate output directory.
COVERAGE_DIR = coverage
COVERAGE_RUNFLAGS =

# Excluded from the coverage report so the percentages reflect rsync's own
# runtime source.  Three buckets:
#  (1) Bundled third-party code rsync ships but does not own: zlib/, popt/, and
#      the named lib/ imports (PostgreSQL getaddrinfo, ISC inet_ntop/inet_pton,
#      standalone getpass).  The other lib/*.c are rsync's own and stay in.
#  (2) Test-helper / build-time programs that link against rsync objects but are
#      not the rsync runtime: t_*.c, tls.c, wildtest.c, testrun.c, getgroups.c,
#      getfsdev.c, trimslash.c, rounding.c.  These have their own main() and are
#      either driven directly by a test (counted there) or are configure-time
#      probes; counting them as "rsync uncovered" is noise.
#  (3) Compile-time-dead fallbacks under this build's config.h: lib/md5.c (the
#      reference md5 -- openssl's EVP path is used when HAVE_OPENSSL) and
#      lib/snprintf.c (only the #include line survives under
#      HAVE_C99_VSNPRINTF).  Covering these would mean a separate non-openssl /
#      non-C99 build, which is out of scope for this report.
COVERAGE_EXCLUDE = -e '(^|/)zlib/' -e '(^|/)popt/' \
	-e '(^|/)lib/(getaddrinfo|getpass|inet_ntop|inet_pton)\.' \
	-e '(^|/)(t_[a-z_]+|tls|wildtest|testrun|getgroups|getfsdev|trimslash|rounding)\.c$$' \
	-e '(^|/)lib/(md5|snprintf)\.c$$'

# Build everything the test suite needs (rsync + helper programs + symlinks)
# WITHOUT running it. Used by CI jobs that invoke runtests.py directly with
# custom options (e.g. the version-mix workflow's --rsync-bin2/--expect-result).
.PHONY: check-progs
check-progs: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)

.PHONY: check
check: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)
	"$(srcdir)/runtests.py" --rsync-bin="`pwd`/rsync$(EXEEXT)" -j $(CHECK_J)

.PHONY: check29
check29: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)
	"$(srcdir)/runtests.py" --rsync-bin="`pwd`/rsync$(EXEEXT)" -j $(CHECK_J) --protocol=29

.PHONY: check30
check30: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)
	"$(srcdir)/runtests.py" --rsync-bin="`pwd`/rsync$(EXEEXT)" -j $(CHECK_J) --protocol=30

# Whole-suite gcov coverage report (HTML, with branch + decision coverage).
# Requires a build configured with --enable-coverage and the `gcovr` tool
# (pip install gcovr). Runs the suite in parallel (COVERAGE_J, default CHECK_J):
# this is safe because the coverage build uses -fprofile-update=atomic and
# libgcov locks the per-source .gcda during its merge, so concurrent rsync
# processes accumulate exactly (see COVERAGE_J above). Use COVERAGE_J=1 if your
# toolchain's libgcov does not lock .gcda merges.
.PHONY: coverage
coverage: all $(CHECK_PROGS) $(CHECK_SYMLINKS)
	@case '$(CFLAGS)' in *--coverage*) ;; \
	  *) echo "*** not a coverage build; reconfigure with --enable-coverage"; exit 1 ;; esac
	@command -v gcovr >/dev/null 2>&1 || { echo "*** gcovr not found (pip install gcovr)"; exit 1; }
	find . -name '*.gcda' -delete
	@# Daemon modules with `uid = <non-root>` setuid the per-connection child
	@# (and so the forked generator/receiver), which then cannot create or
	@# merge .gcda files in a root-owned build dir -- silently dropping ALL
	@# coverage from those processes.  Make every .gcno's directory
	@# world-writable so any uid can create the sibling .gcda, and set a
	@# default ACL of o::rw so the .gcda are world-mergeable regardless of
	@# the creator's umask (every test process resets umask to 022 via
	@# rsyncfns.py, so a Makefile-level `umask 0` would not survive).
	@find . -name '*.gcno' -printf '%h\n' 2>/dev/null | sort -u | \
	  while read d; do \
	    chmod a+rwx "$$d"; \
	    setfacl -m 'd:u::rwx,d:g::rwx,d:o::rwx' "$$d" 2>/dev/null || true; \
	  done
	@rc=0; "$(srcdir)/runtests.py" --rsync-bin="`pwd`/rsync$(EXEEXT)" -j $(COVERAGE_J) $(COVERAGE_RUNFLAGS) || rc=$$?; \
	  rm -rf $(COVERAGE_DIR) && mkdir -p $(COVERAGE_DIR); \
	  gcovr --root $(srcdir) $(COVERAGE_EXCLUDE) --decisions --print-summary \
	      --gcov-ignore-parse-errors=negative_hits.warn_once_per_file \
	      --html-details -o $(COVERAGE_DIR)/index.html . || exit $$?; \
	  echo "Coverage report written to $(COVERAGE_DIR)/index.html"; \
	  if test $$rc != 0; then \
	    echo "*** test suite FAILED (status $$rc) -- coverage report still written above"; \
	  fi; \
	  exit $$rc

# Same as `make coverage` but with the daemon tests run over a real loopback
# rsyncd (--use-tcp), into a separate report directory.
.PHONY: coverage-tcp
coverage-tcp:
	$(MAKE) coverage COVERAGE_RUNFLAGS=--use-tcp COVERAGE_DIR=coverage-tcp

# Comprehensive single report: run the suite under several configurations,
# accumulating into the shared .gcda counters (NOT cleared between runs), then
# emit one merged, rsync-scoped report. Covers the default (pipe) transport, the
# protocol-29/30 compat branches, and the real-TCP daemon path (which also runs
# the require_tcp-only tests). Run under sudo to additionally cover root-only
# paths (devices, chown, use-chroot, protected-regular). Local target -- CI uses
# the plain `coverage`/`coverage-tcp` targets.
.PHONY: coverage-all
coverage-all: all $(CHECK_PROGS) $(CHECK_SYMLINKS)
	@case '$(CFLAGS)' in *--coverage*) ;; \
	  *) echo "*** not a coverage build; reconfigure with --enable-coverage"; exit 1 ;; esac
	@command -v gcovr >/dev/null 2>&1 || { echo "*** gcovr not found (pip install gcovr)"; exit 1; }
	find . -name '*.gcda' -delete
	@# See the `coverage` target above for why: setuid'd daemon children must
	@# be able to create/merge .gcda owned by a different uid.
	@find . -name '*.gcno' -printf '%h\n' 2>/dev/null | sort -u | \
	  while read d; do \
	    chmod a+rwx "$$d"; \
	    setfacl -m 'd:u::rwx,d:g::rwx,d:o::rwx' "$$d" 2>/dev/null || true; \
	  done
	@rc=0; \
	  for cfg in '' '--protocol=30' '--protocol=29' '--use-tcp'; do \
	    echo "===== coverage-all: runtests.py $$cfg ====="; \
	    "$(srcdir)/runtests.py" --rsync-bin="`pwd`/rsync$(EXEEXT)" -j $(COVERAGE_J) $$cfg || rc=$$?; \
	  done; \
	  rm -rf coverage-all && mkdir -p coverage-all; \
	  gcovr --root $(srcdir) $(COVERAGE_EXCLUDE) --decisions --print-summary \
	      --gcov-ignore-parse-errors=negative_hits.warn_once_per_file \
	      --html-details -o coverage-all/index.html . || exit $$?; \
	  echo "Merged coverage report written to coverage-all/index.html"; \
	  if test $$rc != 0; then \
	    echo "*** some suite runs FAILED (status $$rc) -- report still written above"; \
	  fi; \
	  exit $$rc

# Coverage for the portable (non-openat2) resolver tier. Requires a SEPARATE
# build configured with --enable-coverage --disable-openat2: its .gcno differ
# from the openat2 build, so this report cannot be merged with the others.
.PHONY: coverage-fallback
coverage-fallback:
	$(MAKE) coverage COVERAGE_DIR=coverage-fallback

wildtest.o: wildtest.c t_stub.o lib/wildmatch.c rsync.h config.h
wildtest$(EXEEXT): wildtest.o lib/compat.o lib/snprintf.o @BUILD_POPT@
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ wildtest.o lib/compat.o lib/snprintf.o @BUILD_POPT@ $(LIBS)

simdtest$(EXEEXT): simd-checksum-x86_64.cpp $(HEADERS)
	@if test x"@ROLL_SIMD@" != x; then \
	    $(CXX) -I. $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -DTEST_SIMD_CHECKSUM1 \
	        -o $@ $(srcdir)/simd-checksum-x86_64.cpp @ROLL_ASM@ $(LIBS); \
	else \
	    touch $@; \
	fi

testsuite/chown-fake_test.py:
	ln -s chown_test.py $(srcdir)/testsuite/chown-fake_test.py

testsuite/devices-fake_test.py:
	ln -s devices_test.py $(srcdir)/testsuite/devices-fake_test.py

testsuite/xattrs-hlink_test.py:
	ln -s xattrs_test.py $(srcdir)/testsuite/xattrs-hlink_test.py

testsuite/exclude-lsh_test.py:
	ln -s exclude_test.py $(srcdir)/testsuite/exclude-lsh_test.py

# This does *not* depend on building or installing: you can use it to
# check a version installed from a binary or some other source tree,
# if you want.

.PHONY: installcheck
installcheck: $(CHECK_PROGS) $(CHECK_SYMLINKS)
	"$(srcdir)/runtests.py" --rsync-bin="$(bindir)/rsync$(EXEEXT)" --srcdir="$(srcdir)" --tooldir="`pwd`" -j $(CHECK_J)

# TODO: Add 'dist' target; need to know which files will be included

# Run the SPLINT (Secure Programming Lint) tool.  <www.splint.org>
.PHONY: splint
splint:
	splint +unixlib +gnuextensions -weak rsync.c

.PHONY: doxygen
doxygen:
	cd $(srcdir) && rm dox/html/* && doxygen

# for maintainers only
.PHONY: doxygen-upload
doxygen-upload:
	rsync -avzv $(srcdir)/dox/html/ --delete \
	$${RSYNC_SAMBA_HOST-samba.org}:/home/httpd/html/rsync/doxygen/head/
