
cmake_minimum_required(VERSION 2.8)

project(uRPC_debugger)

# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)

# Tell what qt module win version should be used
if (WIN32)
    set(QT_QMAKE_EXECUTABLE "C:/Qt/msvc2013/4.8.6_x64/bin/qmake.exe")
    if(CMAKE_SIZEOF_VOID_P EQUAL 4)
        set (QT_QMAKE_EXECUTABLE "C:/Qt/msvc2013/4.8.6/bin/qmake.exe")
    endif()
endif()
message ("QMake found in ${QT_QMAKE_EXECUTABLE}.")

if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.10.0")
    cmake_policy(SET CMP0071 OLD)
endif()

# Include current directory
set (CMAKE_INCLUDE_CURRENT_DIR ON)

# Search libraries in current directory
link_directories(${CMAKE_SOURCE_DIR})

set (USE_QT5 FALSE)

if(${FORCE_QT4})
    set (USE_QT5 FALSE)
elseif(${FORCE_QT5})
    set (USE_QT5 TRUE)
else()
    # Detect QT version
    find_package(Qt4)
    if(${Qt4_FOUND})
        message("Qt4 will be used")
        set (USE_QT5 FALSE)
    else()
        find_package(Qt5Widgets)
        if(${Qt5Widgets_FOUND})
            message("Qt5 will be used")
            set (USE_QT5 TRUE)
        else()
            message(FATAL_ERROR "No Qt4/Qt5 found")
        endif()
    endif()
endif()

if(NOT ${USE_QT5})
    find_package(Qt4 REQUIRED QtCore QtGui QtMain)
    include(${QT_USE_FILE})
endif()

if(${USE_QT5})
    find_package(Qt5Widgets REQUIRED)
    include_directories(${Qt5Widgets_INCLUDES})
    add_definitions(${QtWidgets_DEFINITIONS})
    set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
endif()

set(SOURCES
    main.cpp
    floatinput.cpp
    mainwindow.cpp
    intinput.cpp
    iopanel.cpp
    floatarrinput.cpp
    intarrinput.cpp
    floatoutput.cpp
    intoutput.cpp
    floatarroutput.cpp
    intarroutput.cpp
    multilist.cpp
    validlineedit.cpp
    base_io_widget.cpp
    container.cpp)

set(HEADERS
    floatinput.h
    mainwindow.h
    intinput.h
    iopanel.h
    floatarrinput.h
    intarrinput.h
    floatoutput.h
    intoutput.h
    floatarroutput.h
    intarroutput.h
    multilist.h
    validlineedit.h
    base_io_widget.h
    container.h
    usbadc10.h)

set(UIS
    floatinput.ui
    mainwindow.ui
    intinput.ui
    iopanel.ui
    floatarrinput.ui
    intarrinput.ui
    floatoutput.ui
    intoutput.ui
    floatarroutput.ui
    intarroutput.ui
    container.ui)

if(${USE_QT5})
    qt5_wrap_ui(GENERATED_SOURCES ${UIS})
else()
    qt4_wrap_ui(GENERATED_SOURCES ${UIS})
endif()

source_group("Generated Sources - Do Not Edit" FILES ${GENERATED_SOURCES})

include_directories(${CMAKE_BINARY_DIR})

add_executable(uRPC_debugger
     # source files that are actually built directLy
     ${SOURCES}
     ${GENERATED_SOURCES}

     # items included so they show up in your IDE
     ${HEADERS}
     ${UIS}
     ${RESOURCES})
if(MSVC)
     target_compile_options(uRPC_debugger PRIVATE /W3 /WX)
else()
     target_compile_options(uRPC_debugger PRIVATE -Wall -Wextra -Werror)
endif()

if(${USE_QT5})
    set(QT_LIBS Qt5::Widgets)
else()
    set(QT_LIBS ${QT_LIBRARIES})
endif()

target_link_libraries(uRPC_debugger ${QT_LIBS} usbadc10 ${CMAKE_THREAD_LIBS_INIT})

if(WIN32)
    set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT uRPC_debugger)
    set_target_properties(uRPC_debugger PROPERTIES WIN32_EXECUTABLE YES)
endif()

if (WIN32)
add_custom_command(TARGET uRPC_debugger POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${PROJECT_SOURCE_DIR}/usbadc10.dll"
            $<TARGET_FILE_DIR:uRPC_debugger>)
endif()
