# 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(-DPDV=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 PDV shared library")
find_library(LIBPDV_SHARED
  NAMES pdv
  PATHS
    ../
    ../bin
)
if (NOT LIBPDV_SHARED)
    message(FATAL_ERROR "  NOT FOUND")
else()
    message(STATUS      "  found: ${LIBPDV_SHARED}")
endif()

# Global includes
include_directories(../)

# Help CMake find libtiff included with this package, instead of looking in
# the system libraries.
if (NOT TIFF_FOUND)
    if (NOT TIFF_LIBRARY)
        find_library(TIFF_LIBRARY
            NAMES
                tiff libtiff tiff3 libtiff3
            PATHS
                ${CMAKE_CURRENT_SOURCE_DIR}/../bin # Windows DLL path
                ${CMAKE_CURRENT_SOURCE_DIR}/..     # Linux static lib path
            NO_DEFAULT_PATH
            REQUIRED
        )
    endif()

    if (NOT TIFF_INCLUDE_DIR)
        find_path(TIFF_INCLUDE_DIR tiff.h
            PATHS
                ${CMAKE_CURRENT_SOURCE_DIR}/../tiff/libtiff
                ${CMAKE_CURRENT_SOURCE_DIR}/../libtiff
            NO_DEFAULT_PATH
    )
    endif()
endif()

find_package(TIFF REQUIRED)

add_executable(send_tiffs send_tiffs.c)
target_link_libraries(send_tiffs PRIVATE
    ${LIBPDV_SHARED} TIFF::TIFF ${COMMON_LIBS}
)

add_executable(serial_cmd serial_cmd.c)
target_link_libraries(serial_cmd PRIVATE
    ${LIBPDV_SHARED} ${COMMON_LIBS}
)

add_executable(simple_clsend simple_clsend.c)
target_link_libraries(simple_clsend PRIVATE
    ${LIBPDV_SHARED} TIFF::TIFF ${COMMON_LIBS}
)

add_executable(simple_irig2 simple_irig2.c)
target_link_libraries(simple_irig2 PRIVATE
    ${LIBPDV_SHARED} ${COMMON_LIBS}
)

add_executable(simple_sequence simple_sequence.c)
target_link_libraries(simple_sequence PRIVATE
    ${LIBPDV_SHARED} ${COMMON_LIBS}
)

add_executable(simple_take simple_take.c)
target_link_libraries(simple_take PRIVATE
    ${LIBPDV_SHARED} ${COMMON_LIBS}
)
