# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
# SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
# SPDX-License-Identifier: BSD-3-Clause


#########################################################################
# Generate compiler.h header

# In all the following files, change offset here in case the initial comment
# changes.
set(_offset_unix 221)
set(_offset_win 229)
# The first end of line is at the 3rd character of the file
set(_first_eol_offset 2)

# Test if the end of lines are windows or unix.
file(READ
    "${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf/compiler.dox.in"
    _test_eol
    OFFSET ${_first_eol_offset}
    LIMIT 2
    HEX)
if("${_test_eol}" STREQUAL "0d0a")
  set(_offset ${_offset_win})
else()
  set(_offset ${_offset_unix})
endif()

# Read the file containing all the documentation for compiler.h
file(READ
     "${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf/compiler.dox.in"
     _compiler_dox
     OFFSET ${_offset})

# Read the prolog file containing defines to skip parsing with custom
# pre-processors
file(READ
     "${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf/compiler.prolog.in"
     _compiler_prolog
     OFFSET ${_offset})

# Read the epilog file for swig
file(READ
     "${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf/compiler.epilog.swig.in"
     _compiler_epilog_swig
     OFFSET ${_offset})

# Read the epilog file for doxygen
file(READ
     "${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf/compiler.epilog.doxygen.in"
     _compiler_epilog_doxygen
     OFFSET ${_offset})

# Read the epilog file common for all custom pre-processors
file(READ
     "${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf/compiler.epilog.in"
     _compiler_epilog
     OFFSET ${_offset})


include(WriteCompilerDetectionHeader)

get_property(_cxx_known_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES)

write_compiler_detection_header(
  FILE "${CMAKE_CURRENT_BINARY_DIR}/yarp/conf/compiler.h"
  PREFIX YARP
  COMPILERS
    GNU
    Clang
    AppleClang
    MSVC
  FEATURES ${_cxx_known_features}
  VERSION 3.5.0
  PROLOG "${_compiler_dox}\n${_compiler_prolog}\n// BEGIN <content generated by write_compiler_detection_header>"
  EPILOG "// END <content generated by write_compiler_detection_header>\n\n${_compiler_epilog_swig}\n${_compiler_epilog_doxygen}\n${_compiler_epilog}")


#########################################################################
# Generate the other headers

set(_headers version
             system
             api
             numeric)
if(NOT YARP_NO_DEPRECATED) # Since YARP 3.2
  list(APPEND _headers options)
endif()
foreach(_header ${_headers})
  set(_out "${CMAKE_CURRENT_BINARY_DIR}/yarp/conf/${_header}.h")
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf/${_header}.h.in"
                 "${_out}")
endforeach()


#########################################################################
# Create the YARP_conf interface library

add_library(YARP_conf INTERFACE)
add_library(YARP::YARP_conf ALIAS YARP_conf)

target_include_directories(YARP_conf INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
                                               $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
                                               $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_compile_features(YARP_conf INTERFACE cxx_std_14)


#########################################################################
# Install all the headers

install(TARGETS YARP_conf
        EXPORT YARP_conf)

# INTERFACE libraries do not support the PUBLIC_HEADER property
# https://gitlab.kitware.com/cmake/cmake/issues/20056
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/yarp/conf"
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/yarp
        COMPONENT YARP_conf-dev
        FILES_MATCHING PATTERN "*.h")
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/yarp/conf"
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/yarp
        COMPONENT YARP_conf-dev
        FILES_MATCHING PATTERN "*.h")
