# Copyright (c) 1999-2025 Paul Mattes.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the names of Paul Mattes nor the names of his contributors
#       may be used to endorse or promote products derived from this software
#       without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Makefile for 3270 emulation library testing

RM = rm -f
CC = @CC@

all: test

JSON_OBJS = json_test.o json.o utf8.o varbuf.o sa_malloc.o
BIND_OPTS_OBJS = bind_opts_test.o bind-opt.o sa_malloc.o
UTF8_OBJS = utf8_test.o utf8.o sa_malloc.o
URI_OBJS = uri_test.o uri.o percent_decode.o varbuf.o sa_malloc.o
DEVNAME_OBJS = devname_test.o devname.o varbuf.o sa_malloc.o

CCOPTIONS = @CCOPTIONS@
XCPPFLAGS = -I$(THIS) -I$(THIS)/../include/unix -I$(THIS)/../include -I$(TOP)/include @CPPFLAGS@
override CFLAGS += $(CCOPTIONS) $(CDEBUGFLAGS) $(XCPPFLAGS) -fprofile-arcs -ftest-coverage @CFLAGS@

test: json_test bind_opts_test utf8_test uri_test devname_test
	$(RM) json_test.gcda bind_opts_test.gcda utf8_test.gcda devname_test.gcda
	./json_test $(TESTOPTIONS)
	./bind_opts_test $(TESTOPTIONS)
	./utf8_test $(TESTOPTIONS)
	./uri_test $(TESTOPTIONS)
	./devname_test $(TESTOPTIONS)

json_test: $(JSON_OBJS)
	$(CC) $(CFLAGS) -o $@ $(JSON_OBJS)

bind_opts_test: $(BIND_OPTS_OBJS)
	$(CC) $(CFLAGS) -o $@ $(BIND_OPTS_OBJS)

utf8_test: $(UTF8_OBJS)
	$(CC) $(CFLAGS) -o $@ $(UTF8_OBJS)

uri_test: $(URI_OBJS)
	$(CC) $(CFLAGS) -o $@ $(URI_OBJS)

devname_test: $(DEVNAME_OBJS)
	$(CC) $(CFLAGS) -o $@ $(DEVNAME_OBJS)

coverage: json_coverage bind_opts_coverage utf8_coverage uri_coverage devname_coverage

json_coverage: json_test
	./json_test
	gcov -k json.c

bind_opts_coverage: bind_opts_test
	./bind_opts_test
	gcov -k bind-opts.c

utf8_coverage: utf8_test
	./utf8_test
	gcov -k utf8.c

uri_coverage: uri_test
	./utf8_test
	gcov -k uri.c

devname_coverage: uri_test
	./devname_test
	gcov -k devname.c

clean:
	$(RM) *.o *.d *.gcda *.gcno *.gcov

clobber: clean
	$(RM) json_test bind_opts_test utf8_test uri_test devname_test

-include $(JSON_OBJS:.o=.d)
-include $(BIND_OPTS_OBJS:.o=.d)
-include $(UTF8_OBJS:.o=.d)
-include $(URI_OBJS:.o=.d)
