
cmake_minimum_required(VERSION 3.14)

include(${CMAKE_SOURCE_DIR}/cmake/VersionAutoIncrement.cmake)
auto_version("${CMAKE_SOURCE_DIR}/version/sdk_cpp.txt" "CPPSDK_VERSION")

project(darra-ethercat-cpp
    VERSION "${CPPSDK_VERSION_STRING}"
    DESCRIPTION "DarraEtherCAT C++ SDK - 面向对象封装"
    LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
    pugixml
    GIT_REPOSITORY https://github.com/zeux/pugixml.git
    GIT_TAG v1.14
)
FetchContent_MakeAvailable(pugixml)

if(MSVC)
    add_compile_options(/utf-8)
endif()

set(C_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../C/include"
    CACHE PATH "C SDK 头文件目录")

add_library(ethercat_cpp INTERFACE)
target_include_directories(ethercat_cpp INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<BUILD_INTERFACE:${C_INCLUDE_DIR}>
    $<INSTALL_INTERFACE:include/darra-ethercat-cpp>
)
target_compile_features(ethercat_cpp INTERFACE cxx_std_17)
target_link_libraries(ethercat_cpp INTERFACE pugixml::pugixml)

target_compile_definitions(ethercat_cpp INTERFACE ENABLE_ESI_XML)

add_library(darra::ethercat_cpp ALIAS ethercat_cpp)

option(CPP_BUILD_EXAMPLES "构建 C++ 示例与 ad-hoc 测试" OFF)
if(CPP_BUILD_EXAMPLES)
    enable_testing()
    add_subdirectory(examples)
endif()

include(GNUInstallDirs)
install(FILES
    include/ethercat.hpp
    include/ethercat_advanced.hpp
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/darra-ethercat-cpp)
install(DIRECTORY include/utils
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/darra-ethercat-cpp
    FILES_MATCHING PATTERN "*.hpp")
install(TARGETS ethercat_cpp EXPORT DarraEtherCATCppTargets)
install(EXPORT DarraEtherCATCppTargets
    NAMESPACE darra::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/DarraEtherCATCpp)
