set(CMAKE_CXX_STANDARD 11)

set(CMAKE_BUILD_TYPE "Release")

set(BOOST_COMPONENTS iostreams)

find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
find_package(Ceres REQUIRED)
find_package(Lua REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Protobuf 3.0.0 REQUIRED)

set(PROTO_OUT_DIR ${PROJECT_BINARY_DIR}/libs/)
set(PROTO_DIR ${PROJECT_SOURCE_DIR}/libs/)

set(ALL_PROTO_SRCS)
set(ALL_PROTO_HDRS)
file(GLOB_RECURSE ALL_PROTOS "*/*.proto")
foreach(ABS_FIL ${ALL_PROTOS})
  file(RELATIVE_PATH REL_FIL ${PROJECT_SOURCE_DIR} ${ABS_FIL})
  get_filename_component(DIR ${REL_FIL} DIRECTORY)
  get_filename_component(FIL_WE ${REL_FIL} NAME_WE)
  set(ABS_C_FIL "${PROJECT_BINARY_DIR}/${DIR}/${FIL_WE}.pb.cc")
  list(APPEND ALL_PROTO_SRCS ${ABS_C_FIL})
  list(APPEND ALL_PROTO_HDRS "${PROJECT_BINARY_DIR}/${DIR}/${FIL_WE}.pb.h")

  if((NOT(EXISTS ${ABS_C_FIL})) OR (${ABS_FIL} IS_NEWER_THAN ${ABS_C_FIL}))
    message(${PROTOBUF_PROTOC_EXECUTABLE} " " --cpp_out " " ${PROTO_OUT_DIR} " " -I " " ${PROTO_DIR} " " ${ABS_FIL})
    # 生成源码
    execute_process(
        COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out ${PROTO_OUT_DIR} -I ${PROTO_DIR} ${ABS_FIL}
    )
  endif()
endforeach()

set_source_files_properties(${ALL_PROTO_SRCS} ${ALL_PROTO_HDRS} PROPERTIES GENERATED TRUE)

file(GLOB_RECURSE ALL_CARTO_SRCS */*.cc)

message(${ALL_CARTO_SRCS})
message(${ALL_PROTO_SRCS})

add_library(slam STATIC 
    ${ALL_CARTO_SRCS}
    ${ALL_PROTO_SRCS}
    )

target_include_directories(slam PUBLIC
    ${BOOST_INCLUDE_DIRS}
    ${CERES_INCLUDE_DIRS}
    ${EIGEN3_INCLUDE_DIR}
    ${LUA_INCLUDE_DIR}
    ${PROTO_OUT_DIR}
    )

target_link_libraries(slam PUBLIC
    ${Boost_LIBRARIES}
    ${CERES_LIBRARIES}
    ${EIGEN3_LIBRARIES}
    ${LUA_LIBRARIES}
    glog
    gflags
    ${PROTOBUF_LIBRARY}
    )