# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2019 Intel Corporation
# All rights reserved.

TEST_EXTENSIONS += .fuzz
FUZZ_LOG_COMPILER = $(srcdir)/script/fuzz-log-compiler.sh
INCLUDE_DIRS += -I$(srcdir)/test/fuzz/tcti
TESTS_LIBADD = $(lib_LTLIBRARIES) $(libtss2_mu) $(libtss2_sys) $(libutil)

EXTRA_DIST += test/fuzz/main-sys.c
# tcti library used for fuzzing
if ENABLE_TCTI_FUZZING
libtss2_tcti_fuzzing = test/fuzz/tcti/libtss2-tcti-fuzzing.la
check_LTLIBRARIES += $(libtss2_tcti_fuzzing)

test_fuzz_tcti_libtss2_tcti_fuzzing_la_LIBADD   = $(TESTS_LIBADD)
test_fuzz_tcti_libtss2_tcti_fuzzing_la_SOURCES  = \
    src/tss2-tcti/tcti-common.c src/tss2-tcti/tcti-common.h \
    test/fuzz/tcti/tcti-fuzzing.c test/fuzz/tcti/tcti-fuzzing.h \
    test/fuzz/tcti/tss2_tcti_fuzzing.h
endif # ENABLE_TCTI_FUZZING

if ENABLE_FUZZING
FUZZ_CFLAGS = $(TESTS_CFLAGS) -I$(srcdir)/test/integration
FUZZ_CPPFLAGS = $(INCLUDE_DIRS) -I$(srcdir)/test/integration $(LIB_FUZZING_ENGINE)

libtss2_utils_fuzzing = test/fuzz/libfuzz_utils.la
check_LTLIBRARIES += $(libtss2_utils_fuzzing)
test_fuzz_libfuzz_utils_la_LDFLAGS = $(TESTS_LDFLAGS)
test_fuzz_libfuzz_utils_la_LIBADD = $(TESTS_LIBADD)
test_fuzz_libfuzz_utils_la_CFLAGS = $(AM_CFLAGS) $(FUZZ_CFLAGS)
test_fuzz_libfuzz_utils_la_SOURCES = \
    test/integration/test-common.c test/integration/test-common.h

TESTS_LDADD += $(libtss2_utils_fuzzing)
FUZZLDADD = $(TESTS_LDADD) $(TESTS_LDFLAGS) $(FUZZ_LDFLAGS)

fuzzdir = $(srcdir)
fuzz-targets: $(fuzz_PROGRAMS)

check_PROGRAMS += $(TESTS_FUZZ)
TESTS += $(TESTS_FUZZ)
fuzz_PROGRAMS = $(TESTS_FUZZ)
FUZZ = $(check_PROGRAMS)

DISTCLEANFILES += Makefile-fuzz-generated.am

# Non Autogenerated Fuzz Tests
noinst_PROGRAMS += test/fuzz/tcti-spi-helper-fuzz-test
test_fuzz_tcti_spi_helper_fuzz_test_CFLAGS         = $(FUZZ_CFLAGS) $(FUZZ_CFLAGS) $(AM_CFLAGS)
test_fuzz_tcti_spi_helper_fuzz_test_LDADD          = $(FUZZLDADD)
nodist_test_fuzz_tcti_spi_helper_fuzz_test_SOURCES = test/fuzz/tcti-spi-helper-fuzz-test.c

include Makefile-fuzz-generated.am
endif # ENABLE_FUZZING
