cmake_minimum_required(VERSION 3.5)
project(libspark)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
include(${CMAKE_SOURCE_DIR}/tools/safe_dep_glob.cmake)
include(${CMAKE_SOURCE_DIR}/tools/Protobuf.cmake)

# DEPENDENCIES
file(GLOB_RECURSE Config_Protos RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} config/protos/*.proto)
file (GLOB_RECURSE Service_Protos RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} api/services/*.proto)
file(GLOB_RECURSE NonConfig_Protos RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.proto)
list(REMOVE_ITEM NonConfig_Protos ${Service_Protos} ${Config_Protos})
file(GLOB_RECURSE Conf_Defaults RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} config/defaults/*.conf)

if (WIN32)
  file(GLOB_RECURSE SparkLib_Sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    api/*.cpp
    config/*.cpp
    domain_model/*.cpp
    expression/*.cpp 
    logging/*.cpp 
    md/*.cpp 
    os/*.cpp
    quickbus/*.cpp 
    sparkproto/*.cpp 
    testing/*.cpp 
    utils/*.cpp)
else ()
  file(GLOB_RECURSE SparkLib_Sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
endif()

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

# generate static defaults
set(config_defaults_file ${CMAKE_CURRENT_BINARY_DIR}/config_defaults.h)
# Defaults cannot be a single string because the compiler complains about a string longer than 65k
set(config_defaults_str "const char * config_static_defaults[] = { ")
foreach (cfg_file_path ${Conf_Defaults})
  file(READ ${cfg_file_path} cfg_file_content)
  string(REPLACE "\"" "\\\"" cfg_file_content_escaped ${cfg_file_content})
  string(REPLACE "\n" ";" cfg_lines ${cfg_file_content_escaped})
  foreach(line ${cfg_lines})
    string(APPEND config_defaults_str "\n  \"${line}\",")
  endforeach(line)
endforeach(cfg_file_path)
string(APPEND config_defaults_str "};")
set(old_defaults_str "")
if(EXISTS ${config_defaults_file})
  file(READ ${config_defaults_file} old_defaults_str)
endif()
if(NOT config_defaults_str STREQUAL old_defaults_str)
  file(WRITE "${config_defaults_file}" "${config_defaults_str}")
endif()


# lib 
if (WIN32)
  add_library(libspark SHARED 
    ${SparkLib_Sources}
    ${PROTO_SRCS}
    ${SERVICE_COMPILER_SRCS}
    )
else ()
  add_library(libspark SHARED 
    ${SparkLib_Sources}
    ${PROTO_SRCS}
    ${SERVICE_COMPILER_SRCS}
    )
endif()

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

if (WIN32)
  target_link_libraries( libspark
    libcurl
    libfmt
    libtbb
    libboost_system
    libboost_thread
    libboost_filesystem
    libboost_regex
    libboost_chrono
    libboost_date_time
    libboost_atomic
    libprotobuf
    ) 
else ()
  target_link_libraries( libspark
    protobuf
    double-conversion
    boost_system
    boost_thread
    boost_filesystem
    boost_regex
    boost_chrono
    boost_date_time
    boost_atomic
    SQLiteCpp
    sqlite3
    curl
    uuid
    ssl
    crypto
    fmt
    tbb
    cryptopp
    pugixml
    lttng-ust
    ssh2
    -lrt
    -ldl
    -static-libgcc
    -static-libstdc++
    -pthread)

  install(TARGETS libspark DESTINATION bin)
endif()

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

 
