cmake_minimum_required(VERSION 3.16)
project(animone LANGUAGES CXX)
set(SRC_FILES
	# any non-platform-specific files go here
	src/animone.cc
	src/player.cc
	src/util.cc
	src/strategist.cc
	src/fd.cc
	src/a11y.cc
	src/win.cc
)

set(LIBRARIES)
set(INCLUDE_DIRS)
set(DEFINES)

list(APPEND DEFINES DLL_EXPORT)

if(APPLE)
	list(APPEND DEFINES USE_MACOSX)
	list(APPEND SRC_FILES
		# xnu stuff
		src/fd/xnu.cc
		src/win/quartz.cc
	)

	# ...
	find_library(OBJC_LIBRARY objc REQUIRED)
	find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
	find_library(COREGRAPHICS_LIBRARY CoreGraphics REQUIRED)
	find_library(APPLICATIONSERVICES_LIBRARY ApplicationServices REQUIRED)
	list(APPEND LIBRARIES ${FOUNDATION_LIBRARY} ${COREGRAPHICS_LIBRARY} ${APPLICATIONSERVICES_LIBRARY} ${OBJC_LIBRARY})
elseif(WIN32)
	list(APPEND DEFINES USE_WIN32)
	list(APPEND SRC_FILES
		# win32
		src/a11y/win32.cc
		src/fd/win32.cc
		src/win/win32.cc
		src/util/win32.cc
	)

	find_library(OLE32_LIB ole32 REQUIRED)
	find_library(OLEAUT32_LIB oleaut32 REQUIRED)
	list(APPEND LIBRARIES ${OLE32_LIB} ${OLEAUT32_LIB})
else() # NOT WIN32 AND NOT APPLE
	find_library(LIBUTIL_LIB util)
	find_library(LIBKVM_LIB kvm)

	if(LINUX)
		list(APPEND DEFINES USE_LINUX)
		list(APPEND SRC_FILES src/fd/proc.cc)
	elseif(LIBUTIL_LIBRARY) # FreeBSD
		# stupid hackarounds
		get_filename_component(LIBUTIL_DIR ${LIBUTIL_LIBRARY} DIRECTORY)

		include(CheckLibraryExists)
		check_library_exists(util kinfo_getfile ${LIBUTIL_DIR} LIBUTIL_GOOD)

		if(LIBUTIL_GOOD)
			list(APPEND DEFINES USE_FREEBSD)
			list(APPEND LIBRARIES ${LIBUTIL_LIBRARY})
			list(APPEND SRC_FILES src/fd/freebsd.cc)
		endif() # LIBUTIL_GOOD
	elseif(LIBKVM_LIBRARY) # OpenBSD
		get_filename_component(LIBKVM_DIR ${LIBKVM_LIBRARY} DIRECTORY)

		include(CheckLibraryExists)
		check_library_exists(kvm kvm_getfiles ${LIBKVM_DIR} LIBKVM_HAS_GETFILES)

		if(LIBKVM_HAS_GETFILES)
			list(APPEND DEFINES USE_OPENBSD)
			list(APPEND LIBRARIES ${LIBKVM_LIBRARY})
			list(APPEND SRC_FILES src/fd/openbsd.cc)
		endif() # LIBKVM_HAS_GETFILES
	endif() # LINUX
endif() # WIN32 AND APPLE

# X11
find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
	pkg_check_modules(XCB xcb xcb-res)
	if (XCB_FOUND)
		list(APPEND DEFINES USE_X11)
		list(APPEND LIBRARIES ${XCB_LINK_LIBRARIES})
		list(APPEND INCLUDE_DIRS ${XCB_INCLUDE_DIRS})
		list(APPEND SRC_FILES src/win/x11.cc)
	endif() # XCB_FOUND

	pkg_check_modules(ATSPI atspi-2)
	if (ATSPI_FOUND)
		list(APPEND DEFINES USE_ATSPI)
		list(APPEND LIBRARIES ${ATSPI_LINK_LIBRARIES})
		list(APPEND INCLUDE_DIRS ${ATSPI_INCLUDE_DIRS})
		list(APPEND SRC_FILES src/a11y/atspi.cc)
	endif() # ATSPI_FOUND
endif() # PKG_CONFIG_FOUND

add_library(animia SHARED ${SRC_FILES})
set_target_properties(animia PROPERTIES
	PUBLIC_HEADER include/animia.h
	CXX_STANDARD 17
)

target_compile_definitions(animia PRIVATE ${DEFINES})
target_include_directories(animia PUBLIC include PRIVATE ${INCLUDE_DIRS})
target_link_libraries(animia PUBLIC ${LIBRARIES})
