get_filename_component(name ${CMAKE_CURRENT_LIST_DIR} NAME)

set(ecap_rely global::drogon global::libarchive global::SQLiteCpp global::mimalloc)

library_get_missing_with_rely(ecap_dependencies_miss ${ecap_rely})
if (ecap_dependencies_miss)
    rcl_log_append(${CMAKE_CURRENT_LIST_LINE} "[FATAL_ERROR] ecap dependencies are not installed. Build Aethera_Kernel_env first")
    return()
endif ()
rcl_load_dependency_environment(${ecap_rely})
find_package(Drogon)
find_package(SQLiteCpp CONFIG REQUIRED)
set(ecap_runtime_optimization_default OFF)
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(ecap_runtime_optimization_default ON)
endif ()
option(ECAP_SERVER_RUNTIME_OPTIMIZATION "Optimize the deployed server while retaining debug symbols" ${ecap_runtime_optimization_default})
set(Core Psc_Core_Static)
set(md "${CMAKE_CURRENT_LIST_DIR}/module")
set(data_dir ${CMAKE_CURRENT_LIST_DIR}/data)
append_glob_source(dll_source_srcs "${md}/dll_source")
add_library(dll_source SHARED ${dll_source_srcs})
add_executable(data_process "${dll_source_srcs}")
target_include_directories(dll_source PUBLIC "${md}")
target_link_libraries(dll_source PUBLIC ${Core})
target_link_libraries(data_process PUBLIC ${Core})
_attach_post_build_file(dll_source
        ${data_dir}/data_progress_config.json
        ${data_dir}/BIN_Blank_Text/ADS-B_195_0205.txt
        #            "${CMAKE_CURRENT_LIST_DIR}/data/BIN_Blank_Text/ADS-B_195_0205.txt"
)
append_glob_source(mlat_source_srcs "${md}/mlat_source")
add_library(mlat_source SHARED ${mlat_source_srcs})
target_include_directories(mlat_source PUBLIC "${md}")
target_link_libraries(mlat_source PUBLIC ${Core})
append_glob_source(ecap_server_srcs "${md}/Local_Server")
add_executable(ecap_server "${md}/Local_Server_main.cpp" ${ecap_server_srcs})
if (ECAP_SERVER_RUNTIME_OPTIMIZATION)
    foreach (ecap_optimized_target ecap_server SSR Psc_Core_Static)
        if (MSVC)
            target_compile_options(${ecap_optimized_target} PRIVATE /O2)
        else ()
            target_compile_options(${ecap_optimized_target} PRIVATE -O2 -fno-omit-frame-pointer)
        endif ()
    endforeach ()
endif ()
set(ecap_version "v1.0.5")
set(ecap_build_version_header "${CMAKE_CURRENT_BINARY_DIR}/generated/ecap_server/Build_Version.h")
set(ecap_build_version_source "${CMAKE_CURRENT_BINARY_DIR}/generated/ecap_server/Build_Version.cpp")
add_custom_target(ecap_server_build_version
        COMMAND ${CMAKE_COMMAND}
        -DOUTPUT_HEADER=${ecap_build_version_header}
        -DOUTPUT_SOURCE=${ecap_build_version_source}
        -DVERSION=${ecap_version}
        -P ${CMAKE_CURRENT_LIST_DIR}/toolchain/Generate_Build_Version.cmake
        BYPRODUCTS ${ecap_build_version_header} ${ecap_build_version_source}
        VERBATIM
)
add_dependencies(ecap_server ecap_server_build_version)
set_source_files_properties(${ecap_build_version_source} PROPERTIES GENERATED TRUE)
target_sources(ecap_server PRIVATE ${ecap_build_version_source})
if (WIN32)
    target_compile_definitions(ecap_server PRIVATE ECAP_TARGET_WINDOWS)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_compile_definitions(ecap_server PRIVATE ECAP_TARGET_LINUX)
else ()
    message(FATAL_ERROR "Unsupported ecap_server target platform: ${CMAKE_SYSTEM_NAME}")
endif ()
target_include_directories(ecap_server PUBLIC "${md}")
target_include_directories(ecap_server PRIVATE
        "${CMAKE_CURRENT_LIST_DIR}/third_party/Adminive/backend/library/include"
        "${CMAKE_CURRENT_LIST_DIR}/third_party/Adminive/backend/service/include"
        "${CMAKE_CURRENT_LIST_DIR}/third_party/Adminive/backend/service/third_party/include"
        "${CMAKE_CURRENT_LIST_DIR}/third_party/Structive/core/include"
)
target_include_directories(ecap_server PRIVATE ${ZLIB_INCLUDE_DIR})
target_link_libraries(ecap_server PUBLIC SSR)
target_link_libraries(ecap_server PUBLIC ${Core})
target_link_libraries(ecap_server PRIVATE Drogon::Drogon)
target_link_libraries(ecap_server PRIVATE SQLiteCpp)
target_link_libraries(ecap_server PRIVATE ${ZLIB_LIBRARY})
target_link_libraries(ecap_server PRIVATE mimalloc-static)
if (MSVC)
    target_compile_options(ecap_server PRIVATE /bigobj /utf-8)
endif ()
message("LibArchive_SUB_LIBRARIES  {${LibArchive_SUB_LIBRARIES}}")
target_link_libraries(ecap_server PRIVATE
        LibArchive::LibArchive
        ${LibArchive_SUB_LIBRARIES}
)
target_compile_definitions(ecap_server PRIVATE LIBARCHIVE_STATIC)
target_compile_definitions(ecap_server PRIVATE NOMINMAX)
target_compile_definitions(ecap_server PRIVATE INCL_EXTRA_HTON_FUNCTIONS) # drogon用
if (MSVC)
    target_compile_definitions(ecap_server PRIVATE
            _CRTDBG_MAP_ALLOC
    )
endif ()
add_dependencies(ecap_server dll_source)
set(ecap_package_includes
        "^.*$"
)
set(ecap_package_excludes
        #        "^[.]git/.*$"
        #        "^[.]idea/.*$"
        "^data/.*$"
        "^cmake-build-[^/]*/.*$"
        "^(.*/)?node_modules/.*$"
        "^(.*/)?.git/.*$"
        "^(.*/)?wwwroot/.*$"
        "^(.*/)?dist/.*$"
        "^(.*/)?htdocs/.*$"
        "^(.*/)?model/.*$"
        "^(.*/)?.idea/.*$"
        "^(.*/)?dll_source_lzy/.*$"
        "^.*[.]tsbuildinfo$"
        "^.*[.]zip$"
)
_add_project_zip_target(
        ecap_package_zip
        "${CMAKE_CURRENT_LIST_DIR}/ecap_package.zip"
        "${CMAKE_SOURCE_DIR}"
        ecap_package_includes
        ecap_package_excludes
)
#_add_directory_to_clion("C:/tiles")
rcl_unload_dependency_environment(${ecap_rely})

