cmake_minimum_required(VERSION 3.5)

project(cdt2d
	VERSION 0.0.0
	DESCRIPTION
	"Implementation of 2D operations to manipulate the delta-clipped \
	distance representation"
	LANGUAGES CXX
)

# export information about compilation
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")

# change module path
set(
	CMAKE_MODULE_PATH
	${CMAKE_MODULE_PATH}
	"${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake"
)

# project_warnings target
add_library(project_warnings INTERFACE)

if(MSVC)
	target_compile_options(project_warnings INTERFACE /W4)
else()
	target_compile_options(project_warnings
		INTERFACE
		-Wall
		-Wextra

		# warn the user if a variable declaration shadows one from a
		# parent context
		-Wshadow		
		-Wnon-virtual-dtor	# warn the user if a class with virtual functions has a
					# non-virtual destructor. This helps catch hard to
					# track down memory errors
		-Wold-style-cast	# warn for c-style casts
		-Wcast-align		# warn for potential performance problem casts
		-Wunused		# warn on anything being unused
		-Woverloaded-virtual	# warn if you overload (not override) a virtual
					# function

		-Wpedantic		# warn if non-standard C++ is used
		-Wconversion		# warn on type conversions that may lose data
		-Wsign-conversion	# warn on sign conversions
		-Wmisleading-indentation# warn if identation implies blocks where blocks
					# do not exist

		-Wduplicated-cond	# warn if if / else chain has duplicated conditions
		-Wduplicated-branches	# warn if if / else branches have duplicated code
		-Wlogical-op		# warn about logical operations being used where bitwise were
					# probably wanted

		-Wnull-dereference	# warn if a null dereference is detected
		-Wuseless-cast		# warn if you perform a cast to the same type
		-Wdouble-promotion	# warn if float is implicit promoted to double
		-Wformat=2		# warn on security issues around functions that format output
					# (ie printf)
	)
endif()

# find dependencies
find_package(OpenCV 4.0.1 REQUIRED)
find_package(Cairo REQUIRED)
find_package(CGAL REQUIRED COMPONENTS CORE)

# libcdt
add_library(
	cdt2d
	SHARED	./src/cdt2d/cdt2d.cpp
		./src/cdt2d/ruler.cpp
		./src/cdt2d/cairo-helpers.cpp
)

target_include_directories(
	cdt2d
	PUBLIC ./include
	PRIVATE	./src
		${OpenCV_INCLUDE_DIRS}
		${CGAL_INCLUDE_DIRS}
		${CAIRO_INCLUDE_DIRS}
)

target_link_libraries(
	cdt2d
	PUBLIC	project_warnings
		${OpenCV_LIBS}
		${CGAL_LIB}
		${CAIRO_LIBRARIES}
)
target_compile_features(cdt2d PUBLIC cxx_std_17)

enable_testing()

# tools
add_subdirectory(tools/shapetor)
add_subdirectory(tools/plygn)

# testing
function(test TEST_NAME)
	add_executable(${TEST_NAME} ./tests/${TEST_NAME}.cpp)
	target_link_libraries(${TEST_NAME} PRIVATE project_warnings cdt2d)
	add_test(NAME ${TEST_NAME} COMMAND ./${TEST_NAME})
endfunction(test)

test(00-ruler)
test(00-cairo-minimal)
test(00-pixel-point-adaptor)

# documentation
find_package(Doxygen)
if(DOXYGEN_FOUND)
	configure_file(
		${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in
		${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
		@ONLY
	)

	add_custom_target(docs
		${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
		COMMENT "Generating API documentation with Doxygen" VERBATIM
	)
endif(DOXYGEN_FOUND)
