cmake_minimum_required(VERSION 3.21)

execute_process(
	COMMAND git describe --tags --abbrev=0
	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
	RESULT_VARIABLE GIT_RESULT
	OUTPUT_VARIABLE BASE_VERSION
	OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)

if(NOT
   GIT_RESULT
   EQUAL
   0)
	message(WARNING "aeveon_decoder: could not read a version from git tags. A tagged "
					"git checkout is preferred (use FetchContent with GIT_TAG, not a shallow "
					"clone or a source archive without tags).")
	set(VERSION "2.3.0")
else()
	execute_process(
		COMMAND git status --porcelain
		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
		OUTPUT_VARIABLE GIT_STATUS
		OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)

	if(GIT_STATUS)
		set(VERSION "${BASE_VERSION}+dirty")
	else()
		set(VERSION "${BASE_VERSION}")
	endif()
endif()

# Strip the trimmings (leading `v`, the +dirty suffix) down to a plain x.y.z that project() and the package version file
# accept.
if(VERSION MATCHES "^v?([0-9]+\\.[0-9]+\\.[0-9]+)")
	set(NUMERIC_VERSION "${CMAKE_MATCH_1}")
else()
	message(FATAL_ERROR "aeveon_decoder: version tag '${VERSION}' is not of the form "
						"X.Y.Z; cannot derive a package version.")
endif()

project(
	aeveon_decoder
	VERSION ${NUMERIC_VERSION}
	DESCRIPTION "Header-only decoder library for Aeveon Eye Virtual Frames along with Python bindings"
	LANGUAGES CXX)

message(STATUS "Version: ${VERSION}")

option(AEVEON_DECODER_BUILD_TESTS "Build and register the tests (only those runnable with the current build setup)" ON)
option(AEVEON_DECODER_BUILD_TOOLS "Build the standalone decoder CLI tool (requires OpenCV)" OFF)
option(AEVEON_DECODER_BUILD_SINKS "Build the OpenCV-backed sinks library (aeveon_decoder::sinks)" OFF)
option(AEVEON_DECODER_BUILD_BINDINGS "Build the pybind11 Python bindings (requires Python, pybind11 and sinks)" OFF)
option(AEVEON_DECODER_INSTALL "Generate install and export rules for aeveon_decoder" ${PROJECT_IS_TOP_LEVEL})

foreach(_feature BINDINGS TOOLS)
	if(AEVEON_DECODER_BUILD_${_feature} AND NOT AEVEON_DECODER_BUILD_SINKS)
		message(FATAL_ERROR "AEVEON_DECODER_BUILD_${_feature} requires "
							"AEVEON_DECODER_BUILD_SINKS=ON (it builds on the OpenCV-backed sinks).")
	endif()
endforeach()

include(GNUInstallDirs)

add_library(aeveon_decoder_core INTERFACE)
add_library(aeveon_decoder::core ALIAS aeveon_decoder_core)
set_target_properties(aeveon_decoder_core PROPERTIES EXPORT_NAME core)

target_include_directories(
	aeveon_decoder_core INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/core>
								  $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/aeveon_decoder_core>)

target_compile_features(aeveon_decoder_core INTERFACE cxx_std_20)

if(AEVEON_DECODER_BUILD_SINKS)
	find_package(OpenCV REQUIRED COMPONENTS core imgproc)
	add_library(aeveon_decoder_sinks INTERFACE)
	add_library(aeveon_decoder::sinks ALIAS aeveon_decoder_sinks)
	set_target_properties(aeveon_decoder_sinks PROPERTIES EXPORT_NAME sinks)
	target_include_directories(
		aeveon_decoder_sinks INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/sinks>
									   $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/aeveon_decoder_sinks>)
	# Linking core brings the core include root, so a sinks consumer can include both namespaces; a core-only consumer
	# gets neither sinks libs nor headers.
	target_link_libraries(aeveon_decoder_sinks INTERFACE aeveon_decoder_core opencv_core opencv_imgproc)
endif()

if(AEVEON_DECODER_BUILD_BINDINGS)
	add_subdirectory(bindings)
endif()

if(AEVEON_DECODER_BUILD_TOOLS)
	add_subdirectory(tools)
endif()

if(AEVEON_DECODER_BUILD_TESTS)
	enable_testing()
	add_subdirectory(tests)
endif()

if(AEVEON_DECODER_INSTALL)
	include(CMakePackageConfigHelpers)

	set(AEVEON_DECODER_INSTALL_TARGETS aeveon_decoder_core)
	if(AEVEON_DECODER_BUILD_SINKS)
		list(APPEND AEVEON_DECODER_INSTALL_TARGETS aeveon_decoder_sinks)
	endif()

	install(TARGETS ${AEVEON_DECODER_INSTALL_TARGETS} EXPORT aeveon_decoderTargets)

	# Mirror the split include roots in the install tree so the same isolation holds for find_package() consumers.
	install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/core/aeveon_decoder
			DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/aeveon_decoder_core)
	if(AEVEON_DECODER_BUILD_SINKS)
		install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/sinks/aeveon_decoder
				DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/aeveon_decoder_sinks)
	endif()

	set(AEVEON_DECODER_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/aeveon_decoder)

	install(
		EXPORT aeveon_decoderTargets
		NAMESPACE aeveon_decoder::
		DESTINATION ${AEVEON_DECODER_INSTALL_CMAKEDIR})

	configure_package_config_file(
		${CMAKE_CURRENT_SOURCE_DIR}/cmake/aeveon_decoderConfig.cmake.in
		${CMAKE_CURRENT_BINARY_DIR}/aeveon_decoderConfig.cmake INSTALL_DESTINATION ${AEVEON_DECODER_INSTALL_CMAKEDIR})

	write_basic_package_version_file(
		${CMAKE_CURRENT_BINARY_DIR}/aeveon_decoderConfigVersion.cmake
		VERSION ${PROJECT_VERSION}
		COMPATIBILITY SameMajorVersion)

	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/aeveon_decoderConfig.cmake
				  ${CMAKE_CURRENT_BINARY_DIR}/aeveon_decoderConfigVersion.cmake
			DESTINATION ${AEVEON_DECODER_INSTALL_CMAKEDIR})
endif()
