# ---------------------------------------------------------------------------

# Preamble
cmake_minimum_required (VERSION 3.10)

# Updating instructions
#
# - Update version history in README.md
# - Adjust version below
# - Commit and push to gitlab
# - Add tag with version exactly like below
#
project(ecapp
        VERSION 0.3.2
        DESCRIPTION "EtherCAT Application Library"
        LANGUAGES CXX
    )

if (NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE "Release")
endif ()

# ---------------------------------------------------------------------------

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

include(GNUInstallDirs)
include(GenerateExportHeader)
include(CMakePackageConfigHelpers)
include(CTest)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

find_package(pdserv REQUIRED)
find_package(EtherCAT REQUIRED)

# ---------------------------------------------------------------------------

# Enable explicit symbol export
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

# ---------------------------------------------------------------------------

include(CMakeDependentOption)

option(USE_SYMBOL_VERSIONING "Use ELF symbol versioning" OFF)
cmake_dependent_option(
    CHECK_SYMBOL_VERSIONING
    "Verify symbol versioning after build"
    ON
    "USE_SYMBOL_VERSIONING"
    OFF
)

if (USE_SYMBOL_VERSIONING)
    find_package(PythonInterp 3 REQUIRED)
endif ()

# ---------------------------------------------------------------------------

add_subdirectory(src)

# ---------------------------------------------------------------------------

# Auto-generate files:
# Header file for version information
configure_file(
        src/config.h.in
        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/config.h @ONLY)

# Symbol visiblility control
generate_export_header(${PROJECT_NAME}
    NO_EXPORT_MACRO_NAME ECAPP_LOCAL
    EXPORT_FILE_NAME
    ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}_export.h)

# Cmake target export stuff
configure_package_config_file(Config.cmake.in
        "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

write_basic_package_version_file(
        "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
    COMPATIBILITY SameMajorVersion
)

# Pkg-config file
configure_file(
        ${PROJECT_NAME}.pc.in
        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc @ONLY)

# ---------------------------------------------------------------------------

# Doxygen documentation
option(BUILD_ECAPP_DOCS "Generate documentation" OFF)
if (BUILD_ECAPP_DOCS)
    add_subdirectory(doc)
endif ()

# Include tests (if needed)
if (BUILD_ECAPP_TESTING)
    add_subdirectory(unittest)
endif ()

# Example applications
option(BUILD_ECAPP_EXAMPLES "Build example applications" OFF)
if (BUILD_ECAPP_EXAMPLES)
    add_subdirectory(examples)
endif ()

# ecapp-list-devices -- lists every EtherCAT sub-device/IO-Link device
# this build of ecapp supports (--text, the default, for a terminal;
# --html to publish the project's device docs; --json for
# ecapp-generate, to tell which slaves in an "ethercat slaves --json"
# snapshot it can actually instantiate). Ships with the library, so
# unlike the optional targets above it is always built and installed.
add_executable(ecapp-list-devices tools/list_devices.cpp)
target_link_libraries(ecapp-list-devices PRIVATE ecapp)
set_target_properties(ecapp-list-devices PROPERTIES
    CXX_STANDARD 17
    # Without this, the installed binary can only find libecapp.so.0 by
    # accident (a system-wide prefix on the ld.so search path, or
    # LD_LIBRARY_PATH set by hand) -- silently failing to run otherwise,
    # which ecapp-generate then mistakes for "can't tell, assume every
    # device is supported" (see ecapp-generate.py's load_known_devices()).
    # $ORIGIN makes it find its sibling lib/ dir under any prefix.
    INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"
    BUILD_WITH_INSTALL_RPATH FALSE
)
install(TARGETS ecapp-list-devices RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

# ecapp-generate -- generates a buildable realtime application skeleton
# from "ethercat slaves --json" output. Pure Python3, needs no build
# step, so (like ecapp-list-devices above) it ships with the library
# unconditionally.
install(PROGRAMS tools/ecapp-generate.py
        DESTINATION ${CMAKE_INSTALL_BINDIR}
        RENAME ecapp-generate
)
install(DIRECTORY tools/rtapp_template/
        DESTINATION ${CMAKE_INSTALL_DATADIR}/ecapp/rtapp_template
)

# ---------------------------------------------------------------------------
