# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2021 Engineering Design Team, Inc.

# https://cmake.org/cmake/help/v3.10/index.html
cmake_minimum_required(VERSION 3.10)

if (WIN32 AND (${CMAKE_VERSION} VERSION_LESS "3.15.0"))
  # Properly sets the MSVC runtime library flags.
  # https://cmake.org/cmake/help/v3.15/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
  message(FATAL_ERROR "Need CMake 3.15.x or greater, for Windows builds")
endif()

cmake_policy(SET CMP0091 NEW)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

project(edt_examples
  LANGUAGES C
)

include(CheckCCompilerFlag)

add_definitions(-DPCD=1)

set(COMMON_LIBS "")

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)
set(CMAKE_C_EXTENSIONS False) # Don't allow gnu extensions (-std=c11)

if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
  add_compile_options(
    -Wall
    -Wextra
    -Wformat
    -Wswitch-default
    -Wimplicit-function-declaration
    -Wundef
    -ffunction-sections -fdata-sections -Wl,--gc-sections
    -fdiagnostics-color=always
  )

  check_c_compiler_flag("-Wnull-dereference" gcc_warn_null_dereference)
  if (gcc_warn_null_dereference)
    add_compile_options(-Wnull-dereference)
  endif()
  unset(gcc_warn_null_dereference)

  check_c_compiler_flag("-Wtautological-compare" gcc_warn_autological_compare)
  if (gcc_warn_autological_compare)
    add_compile_options(-Wtautological-compare)
  endif()
  unset(gcc_warn_autological_compare)

  list(APPEND COMMON_LIBS ${CMAKE_DL_LIBS} m) # dynamic and math libraries
endif()


message(STATUS "Finding EDT shared library")
find_library(LIBPCD_SHARED
  NAMES pcd
  PATHS
    ../
    ../bin
)
if (NOT LIBPCD_SHARED)
  message(FATAL_ERROR "  NOT FOUND")
else()
  message(STATUS      "  found: ${LIBPCD_SHARED}")
endif()

# Global includes
include_directories(
  ../
)

add_executable(simple_getdata simple_getdata.c)
target_link_libraries(simple_getdata PRIVATE
  ${LIBPCD_SHARED} ${COMMON_LIBS}
)

add_executable(simple_putdata simple_putdata.c)
target_link_libraries(simple_putdata PRIVATE
  ${LIBPCD_SHARED} ${COMMON_LIBS}
)
