#

# Use only LGPL or Public domain (preferred) code in libcommon, otherwise add
# your lib/file.c directly to the _SOURCES= of the target binary.
#
# THIS LIBRARY IS NOT DISTRIBUTED!
#
# It's just ar(1) archive used by build-system to keep things simple.
#
# Note that you need "make install-strip" (or proper rpm / Debian build)
# to generate binaries with only relevant stuff.
#
EXTRA_LTLIBRARIES += libcommon.la
libcommon_la_CFLAGS = $(AM_CFLAGS)
libcommon_la_SOURCES = \
	lib/blkdev.c \
	lib/buffer.c \
	lib/canonicalize.c \
	lib/color-names.c \
	lib/crc32.c \
	lib/crc32c.c \
	lib/crc64.c \
	lib/c_strtod.c \
	lib/debug.c \
	lib/default_shell.c \
	lib/encode.c \
	lib/env.c \
	lib/fileutils.c \
	lib/idcache.c \
	lib/jsonwrt.c \
	lib/mangle.c \
	lib/match.c \
	lib/mbsalign.c \
	lib/mbsedit.c\
	lib/md5.c \
	lib/pidutils.c \
	lib/pwdutils.c \
	lib/randutils.c \
	lib/sha1.c \
	lib/sha256.c \
	lib/signames.c \
	lib/strutils.c \
	lib/strv.c \
	lib/timeutils.c \
	lib/ttyutils.c \
	lib/xxhash.c \
	lib/configs.c

if LINUX
libcommon_la_SOURCES += \
	lib/linux_version.c \
	lib/loopdev.c \
	lib/pidfd-utils.c
endif

if !HAVE_LANGINFO_H
libcommon_la_SOURCES += lib/langinfo.c
endif

if HAVE_CPU_SET_T
libcommon_la_SOURCES += lib/cpuset.c
endif

if HAVE_OPENAT
if HAVE_DIRFD
libcommon_la_SOURCES += lib/path.c
libcommon_la_SOURCES += lib/sysfs.c
libcommon_la_SOURCES += lib/procfs.c
endif
endif

EXTRA_LTLIBRARIES += libcommon_logindefs.la
libcommon_logindefs_la_CFLAGS = $(AM_CFLAGS)
libcommon_logindefs_la_SOURCES = \
	lib/logindefs.c
if HAVE_ECONF
libcommon_logindefs_la_LIBADD = $(ECONF_LIBS)
endif

EXTRA_LTLIBRARIES += libcommon_shells.la
libcommon_shells_la_CFLAGS = $(AM_CFLAGS)
libcommon_shells_la_SOURCES = \
	lib/shells.c
if HAVE_ECONF
if USE_VENDORDIR
libcommon_shells_la_LIBADD = $(ECONF_LIBS)
endif
endif

EXTRA_LTLIBRARIES += libtcolors.la
libtcolors_la_CFLAGS = $(AM_CFLAGS)
libtcolors_la_SOURCES = lib/colors.c lib/color-names.c include/colors.h include/color-names.h
libtcolors_la_LIBADD = libcommon.la
# tinfo or ncurses are optional
if HAVE_TINFO
libtcolors_la_LIBADD += $(TINFO_LIBS)
libtcolors_la_CFLAGS += $(TINFO_CFLAGS)
else
if HAVE_NCURSES
libtcolors_la_LIBADD += $(NCURSES_LIBS)
libtcolors_la_CFLAGS += $(NCURSES_CFLAGS)
endif
endif # !HAVE_TINFO

MANPAGES += lib/terminal-colors.d.5
dist_noinst_DATA += lib/terminal-colors.d.5.adoc

check_PROGRAMS += \
	test_blkdev \
	test_buffer \
	test_canonicalize \
	test_colors \
	test_configs \
	test_fileeq \
	test_fileutils \
	test_ismounted \
	test_pwdutils \
	test_mangle \
	test_randutils \
	test_remove_env \
	test_strutils \
	test_ttyutils \
	test_timeutils \
	test_c_strtod \
	test_logindefs


if LINUX
if HAVE_CPU_SET_T
check_PROGRAMS += test_cpuset
endif
check_PROGRAMS += \
	test_sysfs \
	test_procfs \
	test_pager \
	test_caputils \
	test_loopdev \
	test_linux_version \
	test_netlink \
	test_netaddrq
endif

if HAVE_OPENAT
if HAVE_DIRFD
check_PROGRAMS += test_path
endif
endif

test_caputils_SOURCES = lib/caputils.c
test_caputils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_CAPUTILS
test_caputils_LDADD = $(LDADD) libcommon.la

test_ttyutils_SOURCES = lib/ttyutils.c
test_ttyutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_TTYUTILS
test_ttyutils_LDADD = $(LDADD) libcommon.la

test_blkdev_SOURCES = lib/blkdev.c
test_blkdev_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_BLKDEV
test_blkdev_LDADD = $(LDADD) libcommon.la

test_ismounted_SOURCES = lib/ismounted.c
test_ismounted_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_ISMOUNTED
test_ismounted_LDADD = libcommon.la $(LDADD)

test_mangle_SOURCES = lib/mangle.c
test_mangle_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_MANGLE

test_strutils_SOURCES = lib/strutils.c
test_strutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_STRUTILS
test_strutils_LDADD = $(LDADD) libcommon.la

test_c_strtod_SOURCES = lib/c_strtod.c
test_c_strtod_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM

test_colors_SOURCES = lib/colors.c lib/color-names.c
test_colors_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_COLORS
test_colors_LDADD = $(LDADD) libtcolors.la

test_randutils_SOURCES = lib/randutils.c
test_randutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_RANDUTILS

if HAVE_OPENAT
if HAVE_DIRFD
test_path_SOURCES = lib/path.c lib/fileutils.c
if HAVE_CPU_SET_T
test_path_SOURCES += lib/cpuset.c
endif
test_path_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_PATH
test_path_LDADD = $(LDADD) libcommon.la
endif
endif

if HAVE_PTY
check_PROGRAMS += test_pty
test_pty_SOURCES = lib/pty-session.c \
		   include/pty-session.h \
		   lib/monotonic.c
test_pty_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_PTY
test_pty_LDADD = $(LDADD) libcommon.la $(MATH_LIBS) $(REALTIME_LIBS) -lutil
endif

if LINUX
test_cpuset_SOURCES = lib/cpuset.c
test_cpuset_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_CPUSET
test_cpuset_LDADD = $(LDADD) libcommon.la

test_sysfs_SOURCES = lib/sysfs.c
test_sysfs_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_SYSFS
test_sysfs_LDADD = $(LDADD) libcommon.la

test_procfs_SOURCES = lib/procfs.c
test_procfs_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_PROCFS
test_procfs_LDADD = $(LDADD) libcommon.la

test_pager_SOURCES = lib/pager.c
test_pager_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_PAGER

test_linux_version_SOURCES = lib/linux_version.c
test_linux_version_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_LINUXVERSION

test_loopdev_SOURCES = lib/loopdev.c
test_loopdev_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_LOOPDEV
test_loopdev_LDADD = $(LDADD) libcommon.la

test_netlink_SOURCES = lib/debug.c lib/netlink.c
test_netlink_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_NETLINK

test_netaddrq_SOURCES = lib/debug.c lib/netaddrq.c lib/netlink.c
test_netaddrq_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_NETADDRQ
endif

test_configs_SOURCES = lib/configs.c
test_configs_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_CONFIGS
test_configs_LDADD = $(LDADD) libcommon.la

test_fileeq_SOURCES = lib/debug.c lib/fileeq.c
test_fileeq_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_FILEEQ

test_fileutils_SOURCES = lib/fileutils.c
test_fileutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_FILEUTILS
test_fileutils_LDADD = $(LDADD) libcommon.la

test_canonicalize_SOURCES = lib/canonicalize.c lib/fileutils.c
test_canonicalize_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_CANONICALIZE
test_canonicalize_LDADD = $(LDADD) libcommon.la

test_timeutils_SOURCES = lib/timeutils.c
test_timeutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_TIMEUTILS
test_timeutils_LDADD = $(LDADD) libcommon.la

test_pwdutils_SOURCES = lib/pwdutils.c
test_pwdutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM
test_pwdutils_LDADD = $(LDADD) libcommon.la

test_remove_env_SOURCES = lib/env.c
test_remove_env_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_ENV
test_remove_env_LDADD = $(LDADD) libcommon.la

test_buffer_SOURCES = lib/buffer.c
test_buffer_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_BUFFER
test_buffer_LDADD = $(LDADD) libcommon.la

test_logindefs_SOURCES = lib/logindefs.c
test_logindefs_CPPFLAGS = -DTEST_PROGRAM $(AM_CPPFLAGS)
test_logindefs_LDADD = $(LDADD) libcommon.la
if HAVE_ECONF
test_logindefs_LDADD += -leconf
endif
