cmake_minimum_required(VERSION 3.16.5)

project(IUI)

include(${PROJECT_SOURCE_DIR}/../build-utils/cmake/compiler_settings.cmake)

if (MSVC)
    add_definitions(/bigobj)
    set(LIB_FOLDER "vs2022")
elseif (ANDROID)
    set(LIB_FOLDER "Android")
elseif (APPLE)
    set(LIB_FOLDER "macos")
elseif (UNIX)
    set(LIB_FOLDER "linux")
endif()

if(NOT TARGET TL)
    set(WITH_OPENSSL OFF)
    set(WITH_ZLIB OFF)
    add_subdirectory("${PROJECT_SOURCE_DIR}/../TL" "./TL")
endif()

if(NOT TARGET EASTL)
    set(EASTL_DIR "${PROJECT_SOURCE_DIR}/../EASTL")
    add_library(EASTL STATIC IMPORTED)
    list(APPEND EASTL_INCLUDE_DIRS "${EASTL_DIR}/include"
    "${EASTL_DIR}/test/packages/EAAssert/include"
    "${EASTL_DIR}/test/packages/EABase/include/Common"
    "${EASTL_DIR}/test/packages/EAMain/include"
    "${EASTL_DIR}/test/packages/EAStdC/include"
    "${EASTL_DIR}/test/packages/EATest/include"
    "${EASTL_DIR}/test/packages/EAThread/include")
    list(APPEND LIB_INCLUDE_DIRS "dir2")
    set(EASTL_LIB_DIR "${EASTL_DIR}/release/lib/${LIB_FOLDER}")
    set_target_properties(EASTL PROPERTIES 
            IMPORTED_LOCATION "${EASTL_LIB_DIR}/Release/${LIB_PREFIX}EASTL${LIB_SUFFIX}" 
            IMPORTED_LOCATION_DEBUG "${EASTL_LIB_DIR}/Debug/${LIB_PREFIX}EASTL${LIB_SUFFIX}" 
            INTERFACE_INCLUDE_DIRECTORIES "${EASTL_INCLUDE_DIRS}")
endif()

if(NOT TARGET VMath)
    add_subdirectory("${PROJECT_SOURCE_DIR}/../VMath" "./VMath")
endif()
if(NOT TARGET FS)
    add_subdirectory("${PROJECT_SOURCE_DIR}/../FS" "./FS")
endif()

if(NOT TARGET cppcoro)
    add_subdirectory("${PROJECT_SOURCE_DIR}/../cppcoro" "./cppcoro")
endif()

if (NOT TARGET Kit)
    add_subdirectory("${PROJECT_SOURCE_DIR}/../../libs/Kit" "./Kit")
endif()

set(FREETYPE_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/../freetype/release/${LIB_FOLDER}/include/freetype2")
set(FREETYPE_LIB_DIR "${PROJECT_SOURCE_DIR}/../freetype/release/${LIB_FOLDER}/lib")
add_library(freetype STATIC IMPORTED)
set_target_properties(freetype PROPERTIES 
                        IMPORTED_LOCATION "${FREETYPE_LIB_DIR}/${LIB_PREFIX}freetype${LIB_SUFFIX}" 
                        INTERFACE_INCLUDE_DIRECTORIES ${FREETYPE_INCLUDE_DIR})

# FILAMENT
if (MSVC OR APPLE OR UNIX)
    set(USE_STATIC_CRT OFF) 
    set(FILAMENT_SKIP_SAMPLES ON)
    set(FILAMENT_SKIP_SDL2 ON)
    if (MSVC)
        set(FILAMENT_SUPPORTS_VULKAN ON)
    endif()
    if(NOT TARGET filament)
        add_subdirectory("${PROJECT_SOURCE_DIR}/../filament" "./filament")
    endif()
else()
    set(FILAMENT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/../filament/release/include")
    set(FILAMENT_LIB_DIR "${PROJECT_SOURCE_DIR}/../filament/release/lib/${LIB_FOLDER}/")

    add_library(filament_backend STATIC IMPORTED)
    set_target_properties(filament_backend PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libbackend.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libbackend.a" INTERFACE_INCLUDE_DIRECTORIES ${FILAMENT_INCLUDE_DIR})
    add_library(filament_bluevk STATIC IMPORTED)
    set_target_properties(filament_bluevk PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libbluevk.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libbluevk.a" INTERFACE_INCLUDE_DIRECTORIES ${FILAMENT_INCLUDE_DIR})
    add_library(filament_filabridge STATIC IMPORTED)
    set_target_properties(filament_filabridge PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libfilabridge.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libfilabridge.a")
    add_library(filament_filaflat STATIC IMPORTED)
    set_target_properties(filament_filaflat PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libfilaflat.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libfilaflat.a")
    add_library(filament_filamat STATIC IMPORTED)
    set_target_properties(filament_filamat PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libfilamat.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libfilamat.a")
    add_library(filament_filament STATIC IMPORTED)
    set_target_properties(filament_filament PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libfilament.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libfilament.a")
    add_library(filament_ibl STATIC IMPORTED)
    set_target_properties(filament_ibl PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libibl.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libibl.a")
    add_library(filament_image STATIC IMPORTED)
    set_target_properties(filament_image PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libimage.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libimage.a")
    add_library(filament_utils STATIC IMPORTED)
    set_target_properties(filament_utils PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libutils.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libutils.a")
    add_library(filament_geometry STATIC IMPORTED)
    set_target_properties(filament_geometry PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libgeometry.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libgeometry.a")
    add_library(filament_shaders STATIC IMPORTED)
    set_target_properties(filament_shaders PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libshaders.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libshaders.a")
    add_library(filament_smol-v STATIC IMPORTED)
    set_target_properties(filament_smol-v PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libsmol-v.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libsmol-v.a")
    add_library(filament_vkshaders STATIC IMPORTED)
    set_target_properties(filament_vkshaders PROPERTIES IMPORTED_LOCATION "${FILAMENT_LIB_DIR}/Release/libvkshaders.a" IMPORTED_LOCATION_DEBUG "${FILAMENT_LIB_DIR}/Debug/libvkshaders.a")
    add_library(filament INTERFACE IMPORTED)
    set_property(TARGET filament PROPERTY INTERFACE_LINK_LIBRARIES 
         filament_backend 
         filament_bluevk 
         filament_filabridge 
         filament_filaflat 
         filament_filamat 
         filament_filament 
         filament_ibl 
         filament_image 
         filament_utils 
         filament_geometry 
         filament_smol-v 
         filament_shaders
         filament_vkshaders)
endif()


if (APPLE)
    find_package(BZIP2 REQUIRED)
endif()

file(GLOB_RECURSE SRC "src/*.cpp" "src/*.h" "src/*.inl" "src/*.c")
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.inl" "include/*.hpp" "include/*.ipp")
file(GLOB_RECURSE NATVIS "natvis/*.natjmc" "natvis/*.natstepfilter" "natvis/*.natvis")

if (NOT UNIX)
    #remove libbacktrace stuff on non-unixes
    set (EXCLUDE_DIR "/libbacktrace/")
    foreach (TMP_PATH ${SRC})
        string (FIND ${TMP_PATH} ${EXCLUDE_DIR} EXCLUDE_DIR_FOUND)
        if (NOT ${EXCLUDE_DIR_FOUND} EQUAL -1)
            list (REMOVE_ITEM SRC ${TMP_PATH})
        endif ()
    endforeach(TMP_PATH)
endif()

foreach(_source IN ITEMS ${HEADERS})
get_filename_component(_source_path "${_source}" PATH)
file(RELATIVE_PATH _source_path_rel "${PROJECT_SOURCE_DIR}" "${_source_path}")
string(REPLACE "/" "\\" _group_path "${_source_path_rel}")
source_group("${_group_path}" FILES "${_source}")
endforeach()

foreach(_source IN ITEMS ${SRC})
get_filename_component(_source_path "${_source}" PATH)
file(RELATIVE_PATH _source_path_rel "${PROJECT_SOURCE_DIR}" "${_source_path}")
string(REPLACE "/" "\\" _group_path "${_source_path_rel}")
source_group("${_group_path}" FILES "${_source}")
endforeach()

add_library(IUI STATIC ${SRC} ${HEADERS} ${NATVIS})
target_include_directories(IUI PUBLIC "include")
target_include_directories(IUI PUBLIC "include/IUI/util/ImGui")
target_include_directories(IUI PRIVATE "src")
target_include_directories(IUI PUBLIC "${PROJECT_SOURCE_DIR}/../Logger/include")
target_include_directories(IUI PUBLIC "${PROJECT_SOURCE_DIR}/../xsimd/include")
target_include_directories(IUI PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(IUI EASTL)

target_link_libraries(IUI VMath)
target_link_libraries(IUI FS)
target_link_libraries(IUI Kit)
target_link_libraries(IUI freetype)
target_link_libraries(IUI filament)
if (MSVC OR APPLE OR UNIX)
    target_link_libraries(IUI filamat)
    target_link_libraries(IUI geometry)
    target_link_libraries(IUI filabridge)
    target_link_libraries(IUI image)
    target_link_libraries(IUI imageio)
    if (MSVC)
        target_link_libraries(IUI Ws2_32)
        target_link_libraries(IUI Mswsock)
        target_link_libraries(IUI Synchronization)
    endif()
else()
    target_link_libraries(IUI -lz)
    target_link_libraries(IUI -lGLESv3)
    target_link_libraries(IUI -lEGL)
    target_link_libraries(IUI -lvulkan)
endif()
if (APPLE)
    target_link_libraries(IUI ${BZIP2_LIBRARY})
endif()

target_precompile_headers(IUI PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/src/StdAfx.h>")

