cmake_minimum_required(VERSION 3.16)

set(PROJECT_NAME compLib_server)
project(${PROJECT_NAME})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})

if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif ()

find_package(Protobuf REQUIRED)
find_package(spdlog REQUIRED)

if (UNIX AND ${CMAKE_SYSTEM_PROCESSOR} STREQUAL armv7l)
    message("Running on Raspberry Pi")
    add_definitions(-DIS_RASPI)
    set(CMAKE_CXX_FLAGS_RELEASE "-O3")

    find_package(pigpio REQUIRED)
    set(LIBRARIES "pigpio" "spdlog::spdlog")
    set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-psabi")

    set(IS_RASPI true)
endif ()

if (NOT IS_RASPI)
    message("NOT Running on RaspberryPi")
    set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -gdwarf-3")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wextra -gdwarf-3")

    set(LIBRARIES "spdlog::spdlog")
endif ()

message("CMAKE_SYSTEM_PROCESSOR = ${CMAKE_SYSTEM_PROCESSOR}")

set(PROTO_FILES
        protos/CompLib.proto
        )

set(SRC_FILES
        src/main.cpp
        src/spi.cpp
        src/IRSensors.cpp
        src/Cache.cpp
        src/Motors.cpp
        src/Encoders.cpp
        src/ClosedLoopMotorController.cpp
        src/PID.cpp
        src/OdometryController.cpp
        src/Odometry.cpp
        src/communication/MessageProcessor.cpp
        src/communication/MessageBuilder.cpp
        src/communication/UnixSocketServer.cpp
        src/communication/TCPSocketServer.cpp
        src/GoToController.cpp)

set(HDR_FILES
        include/spi.hpp
        include/reset.hpp
        include/mathUtils.hpp
        include/IRSensors.hpp
        include/Robot.hpp
        include/Cache.hpp
        include/Motors.hpp
        include/Encoders.hpp
        include/ClosedLoopMotorController.hpp
        include/PID.hpp
        include/OdometryController.hpp
        include/Odometry.hpp
        include/communication/MessageProcessor.hpp
        include/communication/MessageBuilder.hpp
        include/communication/UnixSocketServer.hpp
        include/communication/TCPSocketServer.hpp
        include/GoToController.hpp)

include_directories(third_party/asio)

PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HDR ${PROTO_FILES})
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HDR_FILES} ${PROTO_SRC} ${PROTO_HDR} ${PROTO_FILES})
target_include_directories(${PROJECT_NAME}
        PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
        PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
        PUBLIC ${PROTOBUF_INCLUDE_DIRS}
        )
target_link_libraries(${PROJECT_NAME} ${PROTOBUF_LIBRARIES} ${LIBRARIES})
