cmake_minimum_required(VERSION 3.5)
project(spark_lib)

include(${CMAKE_SOURCE_DIR}/tools/safe_dep_glob.cmake)
include(${CMAKE_SOURCE_DIR}/tools/Protobuf.cmake)

# DEPENDENCIES
# config protos
file(GLOB_RECURSE Config_Protos RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} config/protos/*.proto)

# service protos
file (GLOB_RECURSE Service_Protos RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} api/services/*.proto)

# non config/non service protos
file(GLOB_RECURSE NonConfig_Protos RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.proto)
list(REMOVE_ITEM NonConfig_Protos ${Service_Protos} ${Config_Protos})

# normal cpps
file(GLOB_RECURSE SparkLib_Sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)

# defaults
file(GLOB_RECURSE Conf_Defaults RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} config/defaults/*.conf)

# this is used to communicate to other repositories that the build
# needs to happen again (because a file was added)
update_deps_file("${SparkLib_Sources};${Config_Protos};${Service_Protos};${NonConfig_Protos};${Conf_Defaults}")
# END DEPENDENCIES

protobuf_generate_cpp( PROTO_SRCS PROTO_HDRS ${NonConfig_Protos} ${Config_Protos})
service_compiler_generate_cpp(SERVICE_COMPILER_SRCS SERVICE_COMPILER_HDRS ${Service_Protos})

# generate list of config protos
set(config_proto_file ${CMAKE_CURRENT_BINARY_DIR}/config_proto_files.h)

# generate a string with the current config files
set(config_proto_str "const char * config_proto_files[] = {  // generated file\n")
foreach(protopath ${Config_Protos})
  get_filename_component(protoname ${protopath} NAME)
  string(APPEND config_proto_str "\"${protoname}\",\n")
endforeach(protopath)
string(APPEND config_proto_str "} ;\n")

# compare with file and if there is any difference update the file
set(old_config_proto_str "")
if(EXISTS "${config_proto_file}")
  file(READ "${config_proto_file}" old_config_proto_str)
endif()
if(NOT old_config_proto_str STREQUAL config_proto_str)
  file(WRITE "${config_proto_file}" "${config_proto_str}")
endif()

# copy defaults to output directory
foreach (cfg_file_path ${Conf_Defaults})
  get_filename_component(cfg_file_name ${cfg_file_path} NAME)
  configure_file(${cfg_file_path} ${CMAKE_BINARY_DIR}/src/server/sparkd/defaults/${cfg_file_name})
endforeach(cfg_file_path)

# spark_lib 
if (WIN32)
  add_library(spark_lib SHARED 
    ${SparkLib_Sources}
    ${PROTO_SRCS}
    ${SERVICE_COMPILER_SRCS}
    )
else ()
  add_library(spark_lib STATIC
    ${SparkLib_Sources}
    ${PROTO_SRCS}
    ${SERVICE_COMPILER_SRCS}
    )
endif()

#include paths
target_include_directories( spark_lib PUBLIC ${CMAKE_SOURCE_DIR}/src ${CMAKE_BINARY_DIR}/src)

if (WIN32)
  target_link_libraries( spark_lib
    libcurl
    libfmt
    libtbb
    libboost_system
    libboost_thread
    libboost_filesystem
    libboost_regex
    libboost_chrono
    libboost_date_time
    libboost_atomic
    libprotobuf
    ) 
else ()
  target_link_libraries( spark_lib
    curl
    ssl
    crypto
    fmt
    tbb
    boost_system
    boost_thread
    boost_filesystem
    boost_regex
    boost_chrono
    boost_date_time
    boost_atomic
    protobuf
    uuid.a
    sqlite3
    cryptopp
    -lrt
    -ldl
    -static-libgcc
    -static-libstdc++
    -pthread) 
endif()

if (WIN32)
  include(${CMAKE_SOURCE_DIR}/tools/vs_filters.cmake)
endif()

 