# SPDX-License-Identifier: Apache-2.0

# 3.9 for LTO
cmake_minimum_required(VERSION 3.9)
# note: cxx-17 requires cmake 3.8, cxx-20 requires cmake 3.12
project(armadillo-twin-agent CXX)

include(CheckIPOSupported)
check_ipo_supported(RESULT supported OUTPUT error)

if( supported )
    message(STATUS "IPO / LTO enabled")
    # must be before defining targets
    set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
    message(STATUS "IPO / LTO not supported: <${error}>")
endif()

# AWS IoT source tree
set(AWS_IOT_DEVICE_SDK_CPP "../aws-iot-device-sdk-cpp-v2" CACHE STRING "Path to the aws IoT Device SDK cpp directory")
option(USE_OPENSSL "Use openssl for aws-iot (override aws-iot default)" ON)
add_subdirectory(${AWS_IOT_DEVICE_SDK_CPP} aws-iot-device-sdk-cpp)
set(IN_SOURCE_BUILD ON)

# this adds these libraries to ${DEP_AWS_LIBS}
aws_use_package(aws-crt-cpp)
aws_use_package(IotShadow-cpp)
aws_use_package(IotJobs-cpp)


# main armadillo-twin-agent target
file(GLOB SRC_FILES
       "*.cpp"
       "*.h"
)

add_executable(${PROJECT_NAME} ${SRC_FILES})

set_target_properties(${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 17)

#set warnings
if (MSVC)
    target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX)
else ()
    target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wno-unused-parameter -pedantic -Werror)
endif ()


find_package(PkgConfig REQUIRED)

pkg_check_modules(DBUS REQUIRED dbus-1)
include_directories(${DBUS_INCLUDE_DIRS})
link_directories(${DBUS_LIBRARY_DIRS})

install(TARGETS ${PROJECT_NAME} DESTINATION bin)

target_link_libraries(${PROJECT_NAME} PRIVATE
    ${DEP_AWS_LIBS} ${DBUS_LIBRARIES})

set(BUILD_TESTS OFF CACHE BOOL "Build tests")
if (BUILD_TESTS)
    include(CTest)
    add_subdirectory(tests)
endif()
