#------------------------------------------------------------------------------
#
# \brief	CMakeLists.txt file for "liblepto"
#
#------------------------------------------------------------------------------


cmake_minimum_required(VERSION 3.11)
project(lepto LANGUAGES CXX)


set(CMAKE_INCLUDE_CURRENT_DIR ON)

# if ( NOT MCU_REV OR "${MCU_REV}" MATCHES "^x86" )
if( "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64" )
   set(CMAKE_AUTOUIC ON)
   set(CMAKE_AUTOMOC ON)
   set(CMAKE_AUTORCC ON)
   set( QT_COMPONENTS Core Widgets Network Xml )

   # Check either Qt6 or Qt5
   find_package( QT NAMES Qt5 Qt6 COMPONENTS ${QT_COMPONENTS} REQUIRED )
   find_package( Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS ${QT_COMPONENTS} )

   add_definitions( "-fPIC" )
   set( MCU_PLATFORM "linux" )
endif()

# set( USE_LEPTO 1 )
set( USE_LEPTO 1 PARENT_SCOPE )

set(headers_MOC
         #  include/lepto/serial2can.h
         include/lepto/ansiparser.h
)

set(headers
         include/lepto/ansi.h
         include/lepto/ansiparser.h
         include/lepto/lepto.h
         include/lepto/log.h
         include/lepto/logoutput.h
         include/lepto/logoutput_console.h
         include/lepto/tools.h
         include/lepto/time.h
         include/lepto/ring.h
         # include/lepto/serial2can.h
         include/lepto/string.h
         include/lepto/crc32.h
         include/lepto/crc8.h
			include/lepto/print.h
			include/lepto/bufferRing.hpp
         ${headers_MOC}
)

set(sources
         src/lepto.cpp
         src/tools.cpp
         src/debug.cpp
         src/log.cpp
         src/logoutput.cpp
         src/logoutput_console.cpp
         src/time.cpp
         src/ring.cpp
         # src/serial2can.cpp
         src/string.cpp
         src/signal.cpp

         src/crc32.cpp
         src/crc8.cpp
         src/base64.cpp
			src/print.cpp
)

if ( NOT MCU_REV OR "${MCU_REV}" MATCHES "x86" )
	set(sources
		${sources}
		src/ansiparser.cpp
	)
   set(headers
      ${headers}
   )
else()
   set(sources
		${sources}
   )
   set(headers
       ${headers}
   )
endif()

add_library(
   ${PROJECT_NAME}
   STATIC
      ${sources}
      ${headers}
      ${lepto_HEADERS_MOC}
)

target_compile_definitions( ${PROJECT_NAME} PUBLIC -DUSE_LEPTO )

if ( COMMON_CONFIG_HEADER )
   set ( LEPTO_CONFIG_HEADER "${COMMON_CONFIG_HEADER}" )
elseif( EXISTS "${CMAKE_SYSROOT}/usr/include/lepto/config.h" )
   set ( LEPTO_CONFIG_HEADER "${CMAKE_SYSROOT}/usr/include/lepto/config.h" )
endif()

# If you dont want a config header, stop lepto to complain about this.
if( LEPTO_CONFIGURED )
   target_compile_definitions(
      ${PROJECT_NAME}
      PUBLIC
         "-DLEPTO_CONFIGURED"
   )
endif()

if( LEPTO_CONFIG_HEADER )
   get_filename_component( LEPTO_CONFIG_PATH ${LEPTO_CONFIG_HEADER} DIRECTORY )
   get_filename_component( LEPTO_CONFIG_FILE ${LEPTO_CONFIG_HEADER} NAME )

   target_compile_options(
      ${PROJECT_NAME}
      PRIVATE
         "SHELL:-include ${LEPTO_CONFIG_HEADER}"
      PUBLIC
         "SHELL:-include ${LEPTO_CONFIG_HEADER}"
   )

   set_source_files_properties(
      ${sources}
      ${headers}
      PROPERTIES
      OBJECT_DEPENDS
      ${LEPTO_CONFIG_HEADER}
   )

   set( LEPTO_CONFIGURED Yes )
   # message( FATAL_ERROR "LEPTO_CONFIG_FILE: ${LEPTO_CONFIG_FILE}" )
endif()

if( NOT LEPTO_CONFIGURED )
   message( ERROR "LEPTO_CONFIGURED not set." )
   message( FATAL_ERROR "No config header for lepto defined. Either set "
            "'COMMON_CONFIG_HEADER' or 'LEPTO_CONFIG_HEADER' in your projects "
            "root cmake file. If you are sure that you don't need it, set "
            "'LEPTO_CONFIGURED' cmake variable")
endif()

if( "${MCU_PLATFORM}" MATCHES "linux" )
   target_link_libraries(
		${PROJECT_NAME}
      Qt${QT_VERSION_MAJOR}::Core
      Qt${QT_VERSION_MAJOR}::Xml
	)
endif()

target_include_directories(
   ${PROJECT_NAME}
   PRIVATE
      include/
      # ${LEPTO_CONFIG_PATH}
   INTERFACE
      include/
      # ${LEPTO_CONFIG_PATH}
)

if( "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64" )
    add_subdirectory(tests)
endif()


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