# Automake makefile for pam-krb5.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Copyright 2005-2007, 2014, 2017, 2020-2021 Russ Allbery <eagle@eyrie.org>
# Copyright 2009, 2011-2012
#     The Board of Trustees of the Leland Stanford Junior University
# Copyright 2005 Andres Salomon <dilinger@debian.org>
# Copyright 1999-2000 Frank Cusack <fcusack@fcusack.com>
#
# SPDX-License-Identifier: BSD-3-clause or GPL-1+

ACLOCAL_AMFLAGS = -I m4
EXTRA_DIST = .clang-format .gitignore .github LICENSE README.md bootstrap   \
	ci/README.md ci/files/heimdal/heimdal-kdc			    \
	ci/files/heimdal/kadmind.acl ci/files/heimdal/kdc.conf		    \
	ci/files/heimdal/krb5.conf ci/files/heimdal/pki-mapping		    \
	ci/files/mit/extensions.client ci/files/mit/extensions.kdc	    \
	ci/files/mit/kadm5.acl ci/files/mit/kdc.conf ci/files/mit/krb5.conf \
	ci/kdc-setup-heimdal ci/kdc-setup-mit ci/install ci/test	    \
	docs/docknot.yaml docs/pam_krb5.pod module/pam_krb5.map		    \
	module/pam_krb5.sym tests/README tests/TESTS tests/config/README    \
	tests/data/cppcheck.supp tests/data/generate-krb5-conf		    \
	tests/data/krb5-pam.conf tests/data/krb5.conf tests/data/perl.conf  \
	tests/data/scripts tests/data/valgrind.supp			    \
	tests/docs/pod-spelling-t tests/docs/pod-t			    \
	tests/docs/spdx-license-t tests/fakepam/README tests/tap/libtap.sh  \
	tests/tap/perl/Test/RRA.pm tests/tap/perl/Test/RRA/Automake.pm	    \
	tests/tap/perl/Test/RRA/Config.pm tests/style/obsolete-strings-t    \
	tests/valgrind/logs-t

# Everything we build needs the Kerbeors headers and library flags.
AM_CPPFLAGS = $(KRB5_CPPFLAGS)
AM_LDFLAGS  = $(KRB5_LDFLAGS)

noinst_LTLIBRARIES = pam-util/libpamutil.la portable/libportable.la
portable_libportable_la_SOURCES = portable/dummy.c portable/kadmin.h	    \
        portable/krb5.h portable/macros.h portable/pam.h portable/stdbool.h \
        portable/system.h
portable_libportable_la_LIBADD = $(LTLIBOBJS)
pam_util_libpamutil_la_SOURCES = pam-util/args.c pam-util/args.h	\
	pam-util/logging.c pam-util/logging.h pam-util/options.c	\
	pam-util/options.h pam-util/vector.c pam-util/vector.h

if HAVE_LD_VERSION_SCRIPT
    VERSION_LDFLAGS = -Wl,--version-script=${srcdir}/module/pam_krb5.map
else
    VERSION_LDFLAGS = -export-symbols ${srcdir}/module/pam_krb5.sym
endif

pamdir = $(libdir)/security
pam_LTLIBRARIES = module/pam_krb5.la
module_pam_krb5_la_SOURCES = module/account.c module/alt-auth.c		\
	module/auth.c module/cache.c module/context.c module/fast.c	\
	module/internal.h module/options.c module/password.c		\
	module/prompting.c module/public.c module/setcred.c		\
	module/support.c
module_pam_krb5_la_LDFLAGS = -module -shared \
	-avoid-version $(VERSION_LDFLAGS) $(AM_LDFLAGS)
module_pam_krb5_la_LIBADD = pam-util/libpamutil.la portable/libportable.la \
	$(KRB5_LIBS)
dist_man_MANS = docs/pam_krb5.5

# The manual page is normally generated by the bootstrap script, but add a
# Makefile rule to regenerate it if it is modified.
docs/pam_krb5.5: $(srcdir)/docs/pam_krb5.pod
	pod2man --release="$(VERSION)" --center=pam-krb5 -s 5 \
	    $(srcdir)/docs/pam_krb5.pod > $@

# Work around the GNU Coding Standards, which leave all the Autoconf and
# Automake stuff around after make maintainer-clean, thus making that command
# mostly worthless.
DISTCLEANFILES = config.h.in~ configure~
MAINTAINERCLEANFILES = Makefile.in aclocal.m4 build-aux/compile		    \
	build-aux/config.guess build-aux/config.sub build-aux/depcomp	    \
	build-aux/install-sh build-aux/ltmain.sh build-aux/missing	    \
	config.h.in configure docs/pam_krb5.5 m4/libtool.m4 m4/ltoptions.m4 \
	m4/ltsugar.m4 m4/ltversion.m4 m4/lt~obsolete.m4

# Separate target for a human to request building everything with as many
# compiler warnings enabled as possible.
warnings:
	$(MAKE) V=0 AM_CFLAGS='$(WARNINGS_CFLAGS) $(AM_CFLAGS)' \
	    KRB5_CPPFLAGS='$(KRB5_CPPFLAGS_WARNINGS)'
	$(MAKE) V=0 AM_CFLAGS='$(WARNINGS_CFLAGS) $(AM_CFLAGS)' \
	    KRB5_CPPFLAGS='$(KRB5_CPPFLAGS_WARNINGS)' $(check_PROGRAMS)

# The bits below are for the test suite, not for the main package.
check_PROGRAMS = tests/runtests tests/module/alt-auth-t			    \
	tests/module/bad-authtok-t tests/module/basic-t			    \
	tests/module/cache-cleanup-t tests/module/cache-t		    \
	tests/module/expired-t tests/module/fast-anon-t tests/module/fast-t \
	tests/module/long-t tests/module/no-cache-t tests/module/pam-user-t \
	tests/module/password-t tests/module/pkinit-t tests/module/realm-t  \
	tests/module/stacked-t tests/module/trace-t tests/pam-util/args-t   \
	tests/pam-util/fakepam-t tests/pam-util/logging-t		    \
	tests/pam-util/options-t tests/pam-util/vector-t		    \
	tests/portable/asprintf-t tests/portable/mkstemp-t		    \
	tests/portable/strndup-t
tests_runtests_CPPFLAGS = -DC_TAP_SOURCE='"$(abs_top_srcdir)/tests"' \
	-DC_TAP_BUILD='"$(abs_top_builddir)/tests"'
check_LIBRARIES = tests/fakepam/libfakepam.a tests/tap/libtap.a
tests_fakepam_libfakepam_a_SOURCES = tests/fakepam/config.c		   \
	tests/fakepam/data.c tests/fakepam/general.c			   \
	tests/fakepam/internal.h tests/fakepam/kuserok.c		   \
	tests/fakepam/logging.c tests/fakepam/pam.h tests/fakepam/script.c \
	tests/fakepam/script.h
tests_tap_libtap_a_CPPFLAGS = $(KADM5CLNT_CPPFLAGS) $(AM_CPPFLAGS)
tests_tap_libtap_a_SOURCES = tests/tap/basic.c tests/tap/basic.h	\
	tests/tap/kadmin.c tests/tap/kadmin.h tests/tap/kerberos.c	\
	tests/tap/kerberos.h tests/tap/macros.h tests/tap/process.c	\
	tests/tap/process.h tests/tap/string.c tests/tap/string.h

# The list of objects and libraries used for module testing by programs that
# link with the fake PAM library or with both it and the module.
MODULE_OBJECTS = module/account.lo module/alt-auth.lo module/auth.lo	   \
	module/cache.lo module/context.lo module/fast.lo module/options.lo \
	module/password.lo module/prompting.lo module/public.lo		   \
	module/setcred.lo module/support.lo pam-util/libpamutil.la	   \
	tests/fakepam/libfakepam.a

# The test programs themselves.
tests_module_alt_auth_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_bad_authtok_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_basic_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_cache_cleanup_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_cache_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_expired_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a	\
	portable/libportable.la $(KADM5CLNT_LDFLAGS) $(KADM5CLNT_LIBS)	\
	$(KRB5_LIBS)
tests_module_fast_anon_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_fast_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_long_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_no_cache_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_pam_user_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_password_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_pkinit_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_realm_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_stacked_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_module_trace_t_LDADD = $(MODULE_OBJECTS) tests/tap/libtap.a \
	portable/libportable.la $(KRB5_LIBS)
tests_pam_util_args_t_LDADD = pam-util/libpamutil.la	\
	tests/fakepam/libfakepam.a tests/tap/libtap.a	\
	portable/libportable.la $(KRB5_LIBS)
tests_pam_util_fakepam_t_LDADD = tests/fakepam/libfakepam.a	\
	tests/tap/libtap.a portable/libportable.la
tests_pam_util_logging_t_LDADD = pam-util/libpamutil.la	\
	tests/fakepam/libfakepam.a tests/tap/libtap.a	\
	portable/libportable.la $(KRB5_LIBS)
tests_pam_util_options_t_LDADD = pam-util/libpamutil.la	\
	tests/fakepam/libfakepam.a tests/tap/libtap.a	\
	portable/libportable.la $(KRB5_LIBS)
tests_pam_util_vector_t_LDADD = pam-util/libpamutil.la	\
	tests/fakepam/libfakepam.a tests/tap/libtap.a	\
	portable/libportable.la
tests_portable_asprintf_t_SOURCES = tests/portable/asprintf-t.c \
	tests/portable/asprintf.c
tests_portable_asprintf_t_LDADD = tests/tap/libtap.a portable/libportable.la
tests_portable_mkstemp_t_SOURCES = tests/portable/mkstemp-t.c \
	tests/portable/mkstemp.c
tests_portable_mkstemp_t_LDADD = tests/tap/libtap.a portable/libportable.la
tests_portable_strndup_t_SOURCES = tests/portable/strndup-t.c \
	tests/portable/strndup.c
tests_portable_strndup_t_LDADD = tests/tap/libtap.a portable/libportable.la

check-local: $(check_PROGRAMS)
	cd tests && ./runtests -l '$(abs_top_srcdir)/tests/TESTS'

# Used by maintainers to check the source code with cppcheck.
check-cppcheck:
	cd $(abs_top_srcdir) &&						\
	    find . -name .git -prune -o -name '*.[ch]' -print		\
	    | cppcheck -q --force --error-exitcode=2 --file-list=-	\
		--suppressions-list=tests/data/cppcheck.supp		\
		--enable=warning,performance,portability,style

# The full path to valgrind and its options, used when doing valgrind
# testing.
VALGRIND_COMMAND = $(PATH_VALGRIND) --leak-check=full			\
        --trace-children=yes						\
	--trace-children-skip=/bin/sh,*/generate-krb5-conf		\
	--suppressions=$(abs_top_srcdir)/tests/data/valgrind.supp	\
        --log-file=$(abs_top_builddir)/tests/tmp/valgrind/log.%p

# Used by maintainers to run the main test suite under valgrind.
check-valgrind: $(check_PROGRAMS)
	rm -rf $(abs_top_builddir)/tests/tmp
	mkdir $(abs_top_builddir)/tests/tmp
	mkdir $(abs_top_builddir)/tests/tmp/valgrind
	C_TAP_VALGRIND="$(VALGRIND_COMMAND)" tests/runtests \
	    -l '$(abs_top_srcdir)/tests/TESTS'

# Used by maintainers to reformat all source code using clang-format and
# excluding some files.
reformat:
	find . -name '*.[ch]' \! -name krb5-profile.c -print \
	    | xargs clang-format -style=file -i
