cmake_minimum_required(VERSION 3.16.5)

project(FS)

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")
endif()

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

if(NOT TARGET EASTL)
  set(EASTL_DIR "${PROJECT_SOURCE_DIR}/../EASTL")
  set(EASTL_LIB_DIR "${EASTL_DIR}/release/lib/${LIB_FOLDER}")
  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_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 zlib)
  set(ZLIB_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/../zlib/release/include")
  set(ZLIB_LIB_DIR "${PROJECT_SOURCE_DIR}/../zlib/release/lib/${LIB_FOLDER}")
  add_library(zlib STATIC IMPORTED)
  if (MSVC)
    set_target_properties(zlib PROPERTIES 
                        IMPORTED_LOCATION "${ZLIB_LIB_DIR}/zlibstatic-ng.lib" 
                        INTERFACE_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIR})
  else()
    set_target_properties(zlib PROPERTIES 
                        IMPORTED_LOCATION "${ZLIB_LIB_DIR}/libz-ng.a" 
                        INTERFACE_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIR})
  endif()
endif()

file(GLOB_RECURSE SRC "src/*.cpp" "src/*.c" "src/*.h")
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.inl")

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(FS STATIC ${SRC} ${HEADERS})
target_include_directories(FS PUBLIC "include")
target_include_directories(FS PRIVATE "src")
target_include_directories(FS PRIVATE "src/libzip")
target_include_directories(FS PUBLIC "${PROJECT_SOURCE_DIR}/../Logger/include")
target_include_directories(FS PUBLIC "${PROJECT_SOURCE_DIR}/../cppcoro/include")
target_include_directories(FS PRIVATE "src/fast_io/include")

target_link_libraries(FS TL)
target_link_libraries(FS zlib)
target_link_libraries(FS EASTL)

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

#if(BUILD_SHARED_LIBS)
#  target_compile_definitions(FS PUBLIC FS_BUILD_SHARED_LIB)
#endif()