cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(IotManagedIntegrationsEndDeviceSDK)

# Set the C standard
set(CMAKE_C_STANDARD 99)

# Options
option(IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG "Use managed integrations device log module" ON)
option(IOTMI_USE_WSS_PROVISIONEE "Enable WiFi Simple Setup provisionee support" OFF)
option(IOTMI_EMBEDDED_SDK_TESTS "Build tests" OFF)
option(IOTMI_EMBEDDED_SDK_EXAMPLES "Build examples" ON)

# For clang-format
include(cmake/clang-cxx-dev-tools.cmake)

# For submodule utility. By default, the submodule is not checkout until added by module.
include(cmake/submodule_utils.cmake)

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message("Setting build type to 'RelWithDebInfo' as none was specified.")
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
    "MinSizeRel" "RelWithDebInfo")
endif()

# List of allowed platforms
set(ALLOWED_PLATFORMS "posix" "freertos")

# Set default platform folder to build
set(IOTMI_EMBEDDED_SDK_PLATFORM "posix" CACHE STRING "PAL platform to build.")
set_property(CACHE IOTMI_EMBEDDED_SDK_PLATFORM PROPERTY STRINGS ${ALLOWED_PLATFORMS})

# Check if the specified value is in the allowed list
list(FIND ALLOWED_PLATFORMS ${IOTMI_EMBEDDED_SDK_PLATFORM} PLATFORM_INDEX)
if(PLATFORM_INDEX EQUAL -1)
  message(FATAL_ERROR "Invalid value for IOTMI_EMBEDDED_SDK_PLATFORM. Allowed values are ${ALLOWED_PLATFORMS}.")
endif()

# Set the default options for the freertos platform
if(IOTMI_EMBEDDED_SDK_PLATFORM STREQUAL "freertos")
  set(IOTMI_EMBEDDED_SDK_EXAMPLES OFF CACHE BOOL "Build examples" FORCE)
  set(IOTMI_EMBEDDED_SDK_TESTS OFF CACHE BOOL "Build tests" FORCE)
  set(IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG OFF CACHE BOOL "Use managed integrations device log module" FORCE)
endif()

# Print the options
message(STATUS "Options:")
message(STATUS "  IOTMI_EMBEDDED_SDK_PLATFORM: ${IOTMI_EMBEDDED_SDK_PLATFORM}")
message(STATUS "  IOTMI_EMBEDDED_SDK_EXAMPLES: ${IOTMI_EMBEDDED_SDK_EXAMPLES}")
message(STATUS "  IOTMI_EMBEDDED_SDK_TESTS: ${IOTMI_EMBEDDED_SDK_TESTS}")
message(STATUS "  IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG: ${IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG}")
message(STATUS "  IOTMI_USE_WSS_PROVISIONEE: ${IOTMI_USE_WSS_PROVISIONEE}")

# Override TOP_DIR for backward compatibility in iotmi::log_c
set(TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(IOTMI_EMBEDDED_C_TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR})

# Set the default static library output directory
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

option(GIT_VERSION "Updates the version number using the Git commit history" ON)

if(NOT IOTMI_EMBEDDED_SDK_BUILD_WITH_MQTT_PROXY)
  # Explicitly disbale pie for the compiler and the linker
  set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
  include(CheckCXXCompilerFlag)
  check_cxx_compiler_flag("-no-pie" HAS_NO_PIE_FLAG)

  if(HAS_NO_PIE_FLAG)
      message(STATUS "The compiler supports -no-pie.")
  else()
      message(STATUS "The compiler does not support -no-pie.")
  endif()

  if(HAS_NO_PIE_FLAG)
      set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie")
  endif()
endif ()



############################################################
# Add subdirectory
############################################################

# For commonDependencies
include(cmake/libcoreJSON.cmake)
include(cmake/libcJSON.cmake)
include(cmake/libIoTJobs.cmake)

add_subdirectory( docs )
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/CMakeLists.txt")
  add_subdirectory( src )
else()
  # Add source files from binaries and headers
  include(cmake/libsrc.cmake)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/components/CMakeLists.txt")
  add_subdirectory(components)
else()
  # Add components from binaries and headers
  include(cmake/libcomponents.cmake)
endif()
add_subdirectory( commonDependencies )
add_subdirectory( platform )
add_subdirectory( platform/${IOTMI_EMBEDDED_SDK_PLATFORM} )

# Examples
if(IOTMI_EMBEDDED_SDK_EXAMPLES)
  add_subdirectory( examples )
endif()

# Unit test
if(IOTMI_EMBEDDED_SDK_TESTS)
  include(cmake/create_weak_library.cmake)

  # Create weak versions of libraries to be used in unit tests:
  #   - The weak version of a library has all public functions defined
  #     as weak symbols, so we could freely override it in the unit
  #     tests.
  set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  find_package(Threads REQUIRED)
  create_weak_library(iotmi::pal iotmi_pal_weak)
  #add this weak library for mqtt proxy tests
  if(IOTMI_EMBEDDED_SDK_BUILD_WITH_MQTT_PROXY)
    create_weak_library(iotmi::MqttProxyCFactory iotmi_mqtt_proxy_c_factory_weak)
  endif ()


  set(BUILD_CLONE_SUBMODULES ON)
  add_subdirectory(test)
endif()

# Add dependencies to the root project targets so that all subdirectory clang-format
# targets are updated when ${CMAKE_PROJECT_NAME}"-clang-format is built.
# This cmake must be included after all the targets are created.
include(cmake/find_clang_format_target_utilities.cmake)
