#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

SUBDIRS =

AM_CPPFLAGS = -g -Wall -I../src $(GLIB_CFLAGS)
AM_LDFLAGS = $(GLIB_LIBS) $(GOBJECT_LIBS) @GCOV_LDFLAGS@

CFLAGS = @GCOV_CFLAGS@
CXXFLAGS = -g

check_PROGRAMS = \
  testtransportsocket \
  testbinaryprotocol \
  testbufferedtransport \
  testframedtransport \
  testmemorybuffer \
  teststruct \
  testsimpleserver \
  testdebugproto \
  testoptionalrequired \
  testthrifttest

if WITH_CPP
  check_PROGRAMS += testthrifttestclient
endif

testtransportsocket_SOURCES = testtransportsocket.c
testtransportsocket_LDADD = \
    ../libthrift_c_glib_la-thrift_transport.o \
    ../libthrift_c_glib_la-thrift_server_transport.o \
    ../libthrift_c_glib_la-thrift_server_socket.o

testbinaryprotocol_SOURCES = testbinaryprotocol.c
testbinaryprotocol_LDADD = \
    ../libthrift_c_glib_la-thrift_protocol.o \
    ../libthrift_c_glib_la-thrift_transport.o \
    ../libthrift_c_glib_la-thrift_socket.o \
    ../libthrift_c_glib_la-thrift_server_transport.o \
    ../libthrift_c_glib_la-thrift_server_socket.o

testbufferedtransport_SOURCES = testbufferedtransport.c
testbufferedtransport_LDADD = \
    ../libthrift_c_glib_la-thrift_transport.o \
    ../libthrift_c_glib_la-thrift_socket.o \
    ../libthrift_c_glib_la-thrift_server_transport.o \
    ../libthrift_c_glib_la-thrift_server_socket.o

testframedtransport_SOURCES = testframedtransport.c
testframedtransport_LDADD = \
    ../libthrift_c_glib_la-thrift_transport.o \
    ../libthrift_c_glib_la-thrift_socket.o \
    ../libthrift_c_glib_la-thrift_server_transport.o \
    ../libthrift_c_glib_la-thrift_server_socket.o

testmemorybuffer_SOURCES = testmemorybuffer.c
testmemorybuffer_LDADD = \
    ../libthrift_c_glib_la-thrift_transport.o

teststruct_SOURCES = teststruct.c
teststruct_LDADD = \
    ../libthrift_c_glib_la-thrift_protocol.o \
    ../libthrift_c_glib_la-thrift_transport.o

testsimpleserver_SOURCES = testsimpleserver.c
testsimpleserver_LDADD = \
    ../libthrift_c_glib_la-thrift_protocol.o \
    ../libthrift_c_glib_la-thrift_transport.o \
    ../libthrift_c_glib_la-thrift_transport_factory.o \
    ../libthrift_c_glib_la-thrift_processor.o \
    ../libthrift_c_glib_la-thrift_protocol_factory.o \
    ../libthrift_c_glib_la-thrift_binary_protocol.o \
    ../libthrift_c_glib_la-thrift_binary_protocol_factory.o \
    ../libthrift_c_glib_la-thrift_socket.o \
    ../libthrift_c_glib_la-thrift_server_transport.o \
    ../libthrift_c_glib_la-thrift_server_socket.o \
    ../libthrift_c_glib_la-thrift_server.o

testdebugproto_SOURCES = testdebugproto.c
testdebugproto_LDADD = libtestgenc.la

testoptionalrequired_SOURCES = testoptionalrequired.c
testoptionalrequired_LDADD = \
    ../libthrift_c_glib_la-thrift_protocol.o \
    ../libthrift_c_glib_la-thrift_transport.o \
    libtestgenc.la

testthrifttest_SOURCES = testthrifttest.c
testthrifttest_LDADD = libtestgenc.la

testthrifttestclient_SOURCES = testthrifttestclient.cpp
testthrifttestclient_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp -I../src -I./gen-c_glib $(GLIB_CFLAGS)
testthrifttestclient_LDADD = ../../cpp/.libs/libthrift.la ../libthrift_c_glib.la libtestgenc.la libtestgencpp.la
testthrifttestclient_LDFLAGS = -L../.libs -L../../cpp/.libs $(GLIB_LIBS) $(GOBJECT_LIBS)

check_LTLIBRARIES = libtestgenc.la

if WITH_CPP
  check_LTLIBRARIES += libtestgencpp.la
endif

nodist_libtestgenc_la_SOURCES = \
        gen-c_glib/t_test_debug_proto_test_types.c \
        gen-c_glib/t_test_empty_service.c \
        gen-c_glib/t_test_inherited.c \
        gen-c_glib/t_test_optional_required_test_types.c \
        gen-c_glib/t_test_reverse_order_service.c \
        gen-c_glib/t_test_second_service.c \
        gen-c_glib/t_test_service_for_exception_with_a_map.c \
        gen-c_glib/t_test_srv.c \
        gen-c_glib/t_test_thrift_test.c \
        gen-c_glib/t_test_thrift_test_types.c \
        gen-c_glib/t_test_debug_proto_test_types.h \
        gen-c_glib/t_test_empty_service.h \
        gen-c_glib/t_test_inherited.h \
        gen-c_glib/t_test_optional_required_test_types.h \
        gen-c_glib/t_test_reverse_order_service.h \
        gen-c_glib/t_test_second_service.h \
        gen-c_glib/t_test_service_for_exception_with_a_map.h \
        gen-c_glib/t_test_srv.h \
        gen-c_glib/t_test_thrift_test.h \
        gen-c_glib/t_test_thrift_test_types.h
libtestgenc_la_LIBADD = $(top_builddir)/lib/c_glib/libthrift_c_glib.la

nodist_libtestgencpp_la_SOURCES = \
        gen-cpp/ThriftTest.cpp \
        gen-cpp/ThriftTest_constants.cpp \
        gen-cpp/ThriftTest_types.cpp \
        gen-cpp/ThriftTest.h \
        gen-cpp/ThriftTest_constants.h \
        gen-cpp/ThriftTest_types.h
libtestgencpp_la_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp

THRIFT = $(top_builddir)/compiler/cpp/thrift

gen-c_glib/t_test_debug_proto_test_types.c gen-c_glib/t_test_debug_proto_test_types.h gen-c_glib/t_test_empty_service.c gen-c_glib/t_test_empty_service.h gen-c_glib/t_test_inherited.c gen-c_glib/t_test_inherited.h gen-c_glib/t_test_reverse_order_service.c gen-c_glib/t_test_reverse_order_service.h gen-c_glib/t_test_service_for_exception_with_a_map.c gen-c_glib/t_test_service_for_exception_with_a_map.h gen-c_glib/t_test_srv.c gen-c_glib/t_test_srv.h: ../../../test/DebugProtoTest.thrift
	$(THRIFT) --gen c_glib $<

gen-c_glib/t_test_optional_required_test_types.c gen-c_glib/t_test_optional_required_test_types.h: ../../../test/OptionalRequiredTest.thrift
	$(THRIFT) --gen c_glib $<

gen-c_glib/t_test_second_service.c gen-c_glib/t_test_thrift_test.c gen-c_glib/t_test_thrift_test_types.c gen-c_glib/t_test_second_service.h gen-c_glib/t_test_thrift_test-.h gen-c_glib/t_test_thrift_test_types.h: ../../../test/ThriftTest.thrift
	$(THRIFT) --gen c_glib $<

gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest.h: ../../../test/ThriftTest.thrift
	$(THRIFT) --gen cpp $<

TESTS = \
  $(check_PROGRAMS) \
  $(check_SCRIPTS)

# globally added to all instances of valgrind calls
# VALGRIND_OPTS = --suppressions=glib.suppress
VALGRIND_OPTS =

# globally added to all memcheck calls
VALGRIND_MEM_OPTS = --tool=memcheck \
                    --num-callers=10 \
                    ${myextravalgrindmemopts}

# globally added to all leakcheck calls
VALGRIND_LEAK_OPTS = --tool=memcheck \
                     --num-callers=10 \
                     --leak-check=full \
                     --leak-resolution=high \
                     ${myextravalgrindleakopts}

memcheck: $(check_PROGRAMS)
	@for x in $(check_PROGRAMS);                                     \
	  do                                                             \
	    $(MAKE) memcheck-$$x;                                        \
	  done

leakcheck: $(check_PROGRAMS)
	@for x in $(check_PROGRAMS);                                     \
	  do                                                             \
	    $(MAKE) leakcheck-$$x;                                       \
	done

memcheck-%: %
	@echo "*****************************************";               \
	echo "MEMCHECK: $<";                                             \
	echo "ARGS: ${VALGRIND_OPTS} ${VALGRIND_MEM_OPTS} ${$<_VALGRIND_MEM_OPTS}";                                                                      \
	$(LIBTOOL) --mode=execute                                        \
	  valgrind                                                       \
	    ${VALGRIND_OPTS}                                             \
	    ${VALGRIND_MEM_OPTS}                                         \
	    ${$<_VALGRIND_MEM_OPTS} ./$<

leakcheck-%: %
	@echo "*****************************************";              \
	echo "LEAKCHECK: $<";                                           \
	echo "ARGS: ${VALGRIND_OPTS} ${VALGRIND_LEAK_OPTS} ${$<_VALGRIND_LEAK_OPTS}";                                                                   \
	G_SLICE=always-malloc $(LIBTOOL) --mode=execute                 \
	  valgrind                                                      \
	    ${VALGRIND_OPTS}                                            \
	    ${VALGRIND_LEAK_OPTS}                                       \
	    ${$<_VALGRIND_LEAK_OPTS}  ./$<

clean-local:
	$(RM) -r gen-c_glib gen-cpp

CLEANFILES =                            \
    *.bb                                \
    *.bbg                               \
    *.da                                \
    *.gcno                              \
    *.gcda                              \
    *.gcov
