StrictCompile()

add_library(bongoconnio SHARED
	sockets.c
	connio.c
	trace.c
	addrpool.c
	unix-ip.c
	nmap.c
	proxy-protocol.c
)

target_link_libraries(bongoconnio
	GnuTLS::GnuTLS
)

install(TARGETS bongoconnio DESTINATION ${LIB_INSTALL_DIR})

if(BUILD_TESTING)
	# Several legacy Bongo DSOs form a runtime dependency cycle (connio,
	# util, json, cal, and msgapi).  A small unit test does not reference all
	# of them directly, so Gentoo's --as-needed can discard a provider before
	# a later DSO needs it.  Keep the explicitly listed test dependencies on
	# Linux; this is local to the test executables and does not affect Bongo.
	function(connio_keep_test_dependencies target)
		if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
			target_link_options(${target} PRIVATE "LINKER:--no-as-needed")
		endif()
	endfunction()

	add_executable(connio-data-test tests/data-test.c)
	connio_keep_test_dependencies(connio-data-test)
	target_link_libraries(connio-data-test PRIVATE
		bongoxpl
		bongoconnio
		bongoutil
		bongojson
		bongomsgapi
		GnuTLS::GnuTLS
		Threads::Threads)
	add_test(NAME connio-data COMMAND connio-data-test)

	add_executable(connio-proxy-protocol-test tests/proxy-protocol-test.c)
	connio_keep_test_dependencies(connio-proxy-protocol-test)
	target_link_libraries(connio-proxy-protocol-test PRIVATE
		bongoxpl
		bongoconnio
		bongoutil
		bongojson
		bongomsgapi
		GnuTLS::GnuTLS
		Threads::Threads)
	add_test(NAME connio-proxy-protocol COMMAND connio-proxy-protocol-test)

	add_executable(connio-interface-list-test tests/interface-list-test.c)
	connio_keep_test_dependencies(connio-interface-list-test)
	target_link_libraries(connio-interface-list-test PRIVATE
		bongoxpl
		bongoconnio
		bongoutil
		bongojson
		bongomsgapi
		GnuTLS::GnuTLS
		Threads::Threads)
	add_test(NAME connio-interface-list COMMAND connio-interface-list-test)
	set_tests_properties(connio-interface-list PROPERTIES TIMEOUT 10)

	add_executable(connio-queue-registration-cancel-test
		tests/queue-registration-cancel-test.c)
	connio_keep_test_dependencies(connio-queue-registration-cancel-test)
	target_link_libraries(connio-queue-registration-cancel-test PRIVATE
		bongoxpl
		bongoconnio
		bongoutil
		bongojson
		bongomsgapi
		GnuTLS::GnuTLS
		Threads::Threads)
	add_test(NAME connio-queue-registration-cancel
		COMMAND connio-queue-registration-cancel-test)
	set_tests_properties(connio-queue-registration-cancel PROPERTIES TIMEOUT 5)

	find_program(OPENSSL_EXECUTABLE openssl)
	if(OPENSSL_EXECUTABLE)
		set(TLS_TEST_KEY "${CMAKE_CURRENT_BINARY_DIR}/tls-peer-test.key")
		set(TLS_TEST_CERT "${CMAKE_CURRENT_BINARY_DIR}/tls-peer-test.crt")
		add_test(NAME connio-tls-test-certificate
			COMMAND ${OPENSSL_EXECUTABLE} req -x509 -newkey rsa:2048 -nodes
				-keyout ${TLS_TEST_KEY} -out ${TLS_TEST_CERT} -days 1
				-subj /CN=localhost -addext subjectAltName=DNS:localhost)
		set_tests_properties(connio-tls-test-certificate PROPERTIES
			FIXTURES_SETUP connio_tls_certificate)

		add_executable(connio-tls-peer-test tests/tls-peer-test.c)
		connio_keep_test_dependencies(connio-tls-peer-test)
		target_link_libraries(connio-tls-peer-test PRIVATE
			bongoxpl
			bongoconnio
			bongoutil
			bongojson
			bongomsgapi
			GnuTLS::GnuTLS
			Threads::Threads)
		add_test(NAME connio-tls-peer
			COMMAND connio-tls-peer-test ${TLS_TEST_CERT} ${TLS_TEST_KEY})
		set_tests_properties(connio-tls-peer PROPERTIES
			FIXTURES_REQUIRED connio_tls_certificate)
	endif()
endif()
