project(ovw)
cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0020 NEW)

# turn on show compile cmd line
SET( CMAKE_VERBOSE_MAKEFILE on )


if(WIN32)
    SET(MyQT_DIR "F:/Qt5.3.1/win32_static_2013")
else()
    SET(MyQT_DIR "/home/lxb/qt531_static")
endif()
SET(CMAKE_PREFIX_PATH ${MyQT_DIR})
# Set source dir of cpp
SET(SRC_DIR ${PROJECT_SOURCE_DIR}/..)

#Set Output dir
SET(EXECUTABLE_OUTPUT_PATH ${SRC_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${SRC_DIR}/bin)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)



#to set compiler flag

if(WIN32)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")
    #       set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
else()
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -W -D_REENTRANT -fPIE  -g -Wall -D_OS_LINUX_")
    #to link static
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}  -pthread")
    #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall")
endif()

if(WIN32)

    #create qtplugin file
    SET(plugin_cpp "${CMAKE_BINARY_DIR}/qt_plugin_moc.cpp")

    FILE(WRITE ${plugin_cpp} " #include <QtPlugin>\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)\n")

    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(AccessibleFactory)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QDDSPlugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QICNSPlugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QICOPlugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QJp2Plugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QMngPlugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QTgaPlugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QTiffPlugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QWbmpPlugin)\n")
    #FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QWebpPlugin)\n")
else()
    SET(plugin_cpp "${CMAKE_BINARY_DIR}/qt_plugin_moc.cpp")

    FILE(WRITE ${plugin_cpp} " #include <QtPlugin>\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(AccessibleFactory)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QDDSPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QICNSPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QICOPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QJp2Plugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QMngPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QTgaPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QTiffPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QWbmpPlugin)\n")
    FILE(APPEND ${plugin_cpp} "Q_IMPORT_PLUGIN(QWebpPlugin)\n")


endif()

find_package(Qt5Widgets REQUIRED)

# The Qt5Widgets_INCLUDES also includes the include directories for
# dependencies QtCore and QtGui
include_directories(${Qt5Widgets_INCLUDES})

# We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5.
add_definitions(${Qt5Widgets_DEFINITIONS})

# Executables fail to build with Qt 5 in the default configuration
# without -fPIE. We add that here.

#Set Include dir
INCLUDE_DIRECTORIES(
    ${SRC_DIR}
    ${SRC_DIR}/Controls
    ${SRC_DIR}/rapidxml
    )

#Set lib dir
LINK_DIRECTORIES(${MyQT_DIR}/plugins/platforms)
LINK_DIRECTORIES(${MyQT_DIR}/lib)
LINK_DIRECTORIES(${MyQT_DIR}/plugins/p_w_picpathformats)
LINK_DIRECTORIES(${MyQT_DIR}/plugins/accessible)



aux_source_directory(${SRC_DIR} SRC_LIST)
aux_source_directory(${SRC_DIR}/Controls Control_LIST)

#ui files
#qt5_wrap_ui(ui_FILES ${SRC_DIR}/MainWindow.ui)

#resource files
QT5_ADD_RESOURCES(RESOURCES ${SRC_DIR}/pic.qrc)
if(WIN32)
    add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST} ${Control_LIST} ${RESOURCES} ${plugin_cpp})
else()
    add_executable(${PROJECT_NAME} ${SRC_LIST} ${Control_LIST} ${RESOURCES} ${plugin_cpp})
endif()

# Use the Widgets module from Qt 5
qt5_use_modules(${PROJECT_NAME} Widgets)

if(WIN32)
    set(QT5_LIBS Ws2_32 opengl32 imm32 winmm Qt5PlatformSupportd qwindowsd qtaccessiblewidgetsd)
else()
    set(QT5_LIBS qtaccessiblewidgets Qt5Widgets qxcb X11-xcb dbus-1 xcb xcb-static Qt5PlatformSupport Xrender Xext X11 Qt5DBus
        qdds qicns qico qjp2 qmng qtga qtiff qwbmp qwebp Qt5Gui EGL Qt5Core m dl gthread-2.0  glib-2.0 rt  GL pthread
         )

     #Qt5OpenGL
endif()
target_link_libraries(${PROJECT_NAME} ${QT5_LIBS})