cmake_minimum_required(VERSION 3.20)
project(owlanzi_tc002 VERSION 0.2.4 LANGUAGES CXX)
set(OWLANZI_APP_VERSION "${PROJECT_VERSION}" CACHE STRING "Owlanzi application version")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
option(OWLANZI_BUILD_TESTS "Build offline host tests" ON)
option(OWLANZI_BUILD_TC002 "Build FlyThings TC002 adapter" OFF)
if(OWLANZI_BUILD_TC002)
 include(cmake/FlyThingsSdk.cmake)
endif()
find_package(Threads REQUIRED)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
add_library(owlanzi_core src/core.cpp src/render.cpp src/time.cpp)
target_include_directories(owlanzi_core PUBLIC include)
target_compile_definitions(owlanzi_core PUBLIC OWLANZI_APP_VERSION="${OWLANZI_APP_VERSION}")
add_library(owlanzi_services src/config.cpp src/owlet.cpp src/http.cpp src/wifi.cpp src/update.cpp)
target_include_directories(owlanzi_services PUBLIC include vendor)
target_link_libraries(owlanzi_services PUBLIC owlanzi_core Threads::Threads)
if(WIN32)
 target_link_libraries(owlanzi_services PUBLIC winhttp bcrypt)
else()
 if(NOT TARGET CURL::libcurl)
  find_package(CURL REQUIRED)
 endif()
 target_link_libraries(owlanzi_services PUBLIC CURL::libcurl)
 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
  target_link_libraries(owlanzi_services PUBLIC stdc++fs)
 endif()
endif()
set(WEB_HEADER "${CMAKE_CURRENT_BINARY_DIR}/generated/web_assets.hpp")
file(GLOB LICENSE_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/vendor/licenses/*.txt" "${CMAKE_CURRENT_SOURCE_DIR}/vendor/LICENSE-*")
add_custom_command(OUTPUT "${WEB_HEADER}"
 COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/scripts/embed-web.py" "${CMAKE_CURRENT_SOURCE_DIR}/web" "${WEB_HEADER}"
 DEPENDS scripts/embed-web.py web/index.html web/app.js web/style.css LICENSE THIRD_PARTY_NOTICES.md ${LICENSE_FILES} VERBATIM)
add_library(owlanzi_runtime src/runtime.cpp "${WEB_HEADER}")
target_include_directories(owlanzi_runtime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_link_libraries(owlanzi_runtime PUBLIC owlanzi_services)
if(WIN32)
 target_link_libraries(owlanzi_runtime PUBLIC ws2_32 crypt32)
endif()
add_executable(owlanzi-tc002 src/main.cpp)
target_link_libraries(owlanzi-tc002 PRIVATE owlanzi_runtime)
if(MSVC)
 foreach(t owlanzi_core owlanzi_services owlanzi_runtime owlanzi-tc002)
  target_compile_options(${t} PRIVATE /W4 /utf-8 /EHsc)
  target_compile_definitions(${t} PRIVATE _CRT_SECURE_NO_WARNINGS NOMINMAX)
 endforeach()
else()
 foreach(t owlanzi_core owlanzi_services owlanzi_runtime owlanzi-tc002)
  target_compile_options(${t} PRIVATE -Wall -Wextra -Wpedantic)
 endforeach()
endif()
if(OWLANZI_BUILD_TC002)
 add_subdirectory(platform/tc002)
endif()
if(OWLANZI_BUILD_TESTS)
 enable_testing()
 foreach(test core render services wifi time update)
  add_executable(${test}_tests tests/${test}_tests.cpp)
  target_link_libraries(${test}_tests PRIVATE owlanzi_services)
  if(MSVC)
   target_compile_options(${test}_tests PRIVATE /utf-8 /UNDEBUG)
  else()
   target_compile_options(${test}_tests PRIVATE -UNDEBUG)
  endif()
  add_test(NAME ${test} COMMAND ${test}_tests)
 endforeach()
 add_test(NAME api COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/api_tests.py" "$<TARGET_FILE:owlanzi-tc002>")
 add_test(NAME local_device_helper COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/local_device_tests.py")
endif()
