#------------------------------------------------------------------------------
#
# \brief CMakeLists.txt file for "hello_cmake" with QT support
#
#------------------------------------------------------------------------------


project(pinmuxer)
cmake_minimum_required(VERSION 3.11)

option( BUILD_TESTING "Compile tests." False )

if( BUILD_TESTING )
   enable_testing()
endif( BUILD_TESTING )

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set( CMAKE_AUTOUIC_SEARCH_PATHS "ui" )

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

if( EXISTS /usr/include/catch2 )
        add_definitions( "-DCATCH2" )
        find_package(Catch2 REQUIRED)
        set( CATCH_LIBRARY "Catch2::Catch2WithMain" )
elseif( EXISTS /usr/include/catch )
        add_definitions( "-DCATCH" )
endif( EXISTS /usr/include/catch2 )

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set( CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake )

find_package( GitVersion REQUIRED )

aux_source_directory(src SRC_LIST)
aux_source_directory(include HEADER_LIST)
list(REMOVE_ITEM SRC_LIST "src/main.cpp")

aux_source_directory(tests TEST_LIST)
list(REMOVE_ITEM TEST_LIST "tests/main_tests.cpp")

set( CMAKE_CXX_FLAGS "-Wall -Wimplicit-fallthrough -fprofile-arcs -ftest-coverage")
add_definitions("-Wall -Wimplicit-fallthrough -Wunused -Wunreachable-code")

include_directories(
	src
)

if( BUILD_TESTING )

add_executable(
	pinmux_tests
	${TEST_LIST}
	${SRC_LIST}
	${HEADER_LIST}
)

TARGET_LINK_LIBRARIES(
	pinmux_tests
   Qt${QT_VERSION_MAJOR}::Core
   ${CATCH_LIBRARY}
) 

target_compile_definitions(
   pinmux_tests
   PRIVATE
      SOURCE_DIR="${CMAKE_SOURCE_DIR}"
      CURRENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
)

endif( BUILD_TESTING )

add_executable(
	${PROJECT_NAME}
	src/main.cpp
	${SRC_LIST}
	${HEADER_LIST}
   ${GIT_VERSION_HEADER}
)

target_compile_definitions(
   ${PROJECT_NAME}
   PRIVATE
      SOURCE_DIR="${CMAKE_SOURCE_DIR}"
      CURRENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
)

TARGET_LINK_LIBRARIES( ${PROJECT_NAME}
   Qt${QT_VERSION_MAJOR}::Core
)

install (
    TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)

add_test(NAME tests_pinmux
    COMMAND pinmux_tests
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    )


#---fin-----------------------------------------------------------------------
