cmake_minimum_required(VERSION 3.16...3.31)

# This is the name of the project
project(anticipation LANGUAGES CXX)

# ---------------------------------------------------------------------------
# Language standard.
#
# NOTE: this is a fallback only. The Eclipse CDT4 indexer probe reads the
# standard out of CMAKE_CXX_FLAGS, and it runs BEFORE CMAKE_CXX_STANDARD is
# translated into a flag. To make Eclipse index at C++11, -std=c++11 must
# arrive in the cache via the command line (see createProject.sh).
# ---------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Emit compile_commands.json unconditionally so CDT's Compilation Database
# Parser (or clangd) can be pointed at it.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ---------------------------------------------------------------------------
# Sources and header directories
# ---------------------------------------------------------------------------
file(GLOB_RECURSE ANTICIPATION_SOURCES CONFIGURE_DEPENDS "*.cpp")
file(GLOB_RECURSE ANTICIPATION_HEADERS CONFIGURE_DEPENDS "*.h")

# Drop CMake-generated files from ANY build tree under the source tree
# (CMakeFiles/<ver>/CompilerIdCXX/CMakeCXXCompilerId.cpp contains a main()).
list(FILTER ANTICIPATION_SOURCES EXCLUDE REGEX "/CMakeFiles/")
list(FILTER ANTICIPATION_HEADERS EXCLUDE REGEX "/CMakeFiles/")

set(ANTICIPATION_INCLUDE_DIRS "")
foreach(_headerFile ${ANTICIPATION_HEADERS})
    get_filename_component(_dir ${_headerFile} PATH)
    list(APPEND ANTICIPATION_INCLUDE_DIRS ${_dir})
endforeach()
list(REMOVE_DUPLICATES ANTICIPATION_INCLUDE_DIRS)

# Listing the headers as target sources is what makes them appear in the
# generated Eclipse project's "Header Files" virtual folder. Without this the
# folder is created but left empty.
add_executable(anticipation ${ANTICIPATION_SOURCES} ${ANTICIPATION_HEADERS})

target_include_directories(anticipation PRIVATE ${ANTICIPATION_INCLUDE_DIRS})

# Sysroot locations that are not on the cross compiler's default search path.
# Derived from CMAKE_SYSROOT rather than hard-coded to /rpi_sysroot.
target_include_directories(anticipation SYSTEM PRIVATE
    ${CMAKE_SYSROOT}/usr/local/include)

# ---------------------------------------------------------------------------
# Compile and link options.
#
# These were previously all passed to add_definitions(), which routes them to
# COMPILE_OPTIONS only. The -L and -l entries therefore appeared on every
# compile line (where gcc ignores them) and on no link line at all.
# ---------------------------------------------------------------------------
target_compile_options(anticipation PRIVATE -Wall -Wextra -O0 -pg)

# -pg must be on the link line too, or gcrt1.o is never pulled in and no
# gmon.out is produced.
target_link_options(anticipation PRIVATE -pg)

target_link_directories(anticipation PRIVATE ${CMAKE_SYSROOT}/usr/local/lib)
target_link_libraries(anticipation PRIVATE wiringPi pthread rt)

# ---------------------------------------------------------------------------
# Documentation
# ---------------------------------------------------------------------------
find_package(Doxygen)

include(UseDoxygenDoc)

add_doxygen_doc(
  BUILD_DIR
    ${CMAKE_CURRENT_BINARY_DIR}/../docs/
  DOXY_FILE
    ${CMAKE_CURRENT_SOURCE_DIR}/../docs/Doxyfile.in
  TARGET_NAME
    docs
  COMMENT
    "HTML documentation"
  )
