cmake_minimum_required(VERSION 3.13)

project(KekEngine VERSION 1.0)

option(KEKENGINE_DEBUG "Build with debug information" OFF)
option(KEKENGINE_VR "Include VR support" OFF)
option(KEKENGINE_BUILD_KEKGAME "Build kekgame" OFF)

if(UNIX)
	set(KEKENGINE_TARGET_PLATFORM Linux)
endif()
if(WIN32)
	set(KEKENGINE_TARGET_PLATFORM Windows)
	#set(BUILD_SHARED_LIBS OFF)
endif()

message("Build shared libs: ${BUILD_SHARED_LIBS}")
message("Debug: ${KEKENGINE_DEBUG}")
message("Build kekgame: ${KEKENGINE_BUILD_KEKGAME}")
message("Target platform: ${KEKENGINE_TARGET_PLATFORM}")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(KEKENGINE_RESOURCE_DIR "${CMAKE_SOURCE_DIR}/src/kekengine/res")
set(KEKGAME_RESOURCE_DIR "${CMAKE_SOURCE_DIR}/src/kekgame/res")

file(GLOB KEKENGINE_SOURCE_FILES "src/kekengine/cpp/*.cpp")
file(GLOB_RECURSE KEKENGINE_RESOURCE_FILES "${KEKENGINE_RESOURCE_DIR}/*")
file(GLOB_RECURSE KEKENGINE_RELATIVE_RESOURCE_FILES RELATIVE ${KEKENGINE_RESOURCE_DIR} "${KEKENGINE_RESOURCE_DIR}/*")

file(GLOB KEKGAME_SOURCE_FILES "src/kekgame/cpp/*.cpp")
file(GLOB_RECURSE KEKGAME_RESOURCE_FILES "${KEKGAME_RESOURCE_DIR}/*")
file(GLOB_RECURSE KEKGAME_RELATIVE_RESOURCE_FILES RELATIVE ${KEKGAME_RESOURCE_DIR} "${KEKGAME_RESOURCE_DIR}/*")

add_compile_definitions(FT_CONFIG_OPTION_ERROR_STRINGS) # Freetype error strings

if(${KEKENGINE_DEBUG})
	add_compile_options(-Wall -g)
else()
	add_compile_options(-Wall -O3)
endif()

add_subdirectory(dependencies/microtar)

# kekengine resources
add_custom_target(kekengine_res ALL DEPENDS ${KEKENGINE_RESOURCE_FILES})

add_custom_command(TARGET kekengine_res PRE_BUILD
	BYPRODUCTS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar
	COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
	COMMAND ${CMAKE_COMMAND} -E
		chdir ${KEKENGINE_RESOURCE_DIR}
		tar cf ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar ${KEKENGINE_RELATIVE_RESOURCE_FILES})

# libkekengine
add_library(kekengine OBJECT ${KEKENGINE_SOURCE_FILES})
set_property(TARGET kekengine PROPERTY POSITION_INDEPENDENT_CODE 1)
add_dependencies(kekengine kekengine_res)

set_property(TARGET microtar PROPERTY POSITION_INDEPENDENT_CODE 1)
target_link_libraries(kekengine PUBLIC microtar)

if(UNIX)
	target_link_libraries(kekengine PUBLIC glfw GLEW GL)

	# GTK
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
	target_link_libraries(kekengine PUBLIC ${GTK3_LIBRARIES})
	target_include_directories(kekengine PRIVATE ${GTK3_INCLUDE_DIRS})
endif()
if(WIN32)
	target_link_libraries(kekengine PUBLIC glfw3 glew32s opengl32)
	target_compile_definitions(kekengine PUBLIC GLEW_STATIC)
endif()

if(${KEKENGINE_VR})
	target_link_directories(kekengine PUBLIC "dependencies/openvr/bin/linux64")
	target_link_libraries(kekengine PUBLIC openvr_api)
endif()

target_include_directories(kekengine PRIVATE "src/kekengine/include")

if(UNIX)
	target_compile_definitions(kekengine PUBLIC KEKENGINE_TARGET_LINUX)
endif()
if(WIN32)
	target_compile_definitions(kekengine PUBLIC KEKENGINE_TARGET_WINDOWS)
endif()

if(${KEKENGINE_VR})
	target_compile_definitions(kekengine PUBLIC KEKENGINE_VR)
	target_include_directories(kekengine PRIVATE "dependencies/openvr/headers")
endif()

add_library(kekengine_shared SHARED $<TARGET_OBJECTS:kekengine>)
target_link_libraries(kekengine_shared PUBLIC kekengine)
set_target_properties(kekengine_shared PROPERTIES OUTPUT_NAME kekengine)
add_library(kekengine_static STATIC $<TARGET_OBJECTS:kekengine>)
target_link_libraries(kekengine_static PUBLIC kekengine)
set_target_properties(kekengine_static PROPERTIES OUTPUT_NAME kekengine)

if(${KEKENGINE_BUILD_KEKGAME})
	# kekgame resources
	add_custom_target(kekgame_res ALL
		DEPENDS ${KEKGAME_RESOURCE_FILES} kekengine_res)

	add_custom_command(TARGET kekgame_res PRE_BUILD
		BYPRODUCTS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar
		COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
		COMMAND ${CMAKE_COMMAND} -E
			chdir ${KEKGAME_RESOURCE_DIR}
			tar rf ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar ${KEKGAME_RELATIVE_RESOURCE_FILES})

	# kekgame
	add_executable(kekgame ${KEKGAME_SOURCE_FILES})
	add_dependencies(kekgame kekgame_res)

	target_link_libraries(kekgame PUBLIC kekengine_static)
	target_include_directories(kekgame PRIVATE "src/kekengine/include")
endif()
