PROJECT(httraqt)

# Configure CMake ...
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
#CMAKE_POLICY(SET CMP0003 OLD)
#CMAKE_POLICY(SET CMP0015 OLD)

# Qt versions to set: 4, 5, 6
SET(USE_QT_VERSION 6)

MESSAGE(STATUS "Using of Qt version for compiling: ${USE_QT_VERSION}")


OPTION (USE_DEBUGGER "Include in binary file debug information" OFF)
# SET(USE_DEBUGGER false)

#enable or disable profiling info
# SET(USE_PROFILER false)

OPTION (USE_PROFILER "Include in binary file profiling information" OFF)



IF(USE_DEBUGGER)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} -Wall")
ELSE()
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} -Wall")
ENDIF()

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DQT_NO_DEPRECATED_WARNINGS")

# INCLUDE(CPack)
INCLUDE(CheckIncludeFile)
INCLUDE(CheckIncludeFileCXX)
INCLUDE(TestCXXAcceptsFlag)
INCLUDE(CheckCXXCompilerFlag)
INCLUDE(FindPkgConfig)

# SET(TARGET_NAME httraqt )

SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

SET(CURRENT_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})

INCLUDE(OutOfSourceBuild) # Disallow in-source builds
INCLUDE(GenerateDEF) # Convenience macro for linking Win32 DLLs using MSVC
INCLUDE(Dependencies)
# INCLUDE(HTTRAQTWordSize) # Detect 32/64 bit platform
# INCLUDE(HTTRAQTCompiler) # Detect problematic compilers

# Set the HTTraQt version
#cat README | grep -E  '^[0-9]+\.[0-9]+\.[0-9]+' | head -n 1
EXECUTE_PROCESS (
   COMMAND cat README
   COMMAND grep -E  "^[0-9]+\\.[0-9]+\\.[0-9]+"
   COMMAND head -n 1
   WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
   RESULT_VARIABLE HTTQTVERSION
   OUTPUT_VARIABLE DESCRIBE_STRING
   OUTPUT_STRIP_TRAILING_WHITESPACE)



IF(NOT DESCRIBE_STRING)
        SET(DESCRIBE_STRING "no valid git tag for ${PROJECT_NAME} found")
ELSE()
        STRING(REGEX MATCH "([0-9]+\\.[0-9]+\\.[0-9]+)" HTTRAQT_VERSION "${DESCRIBE_STRING}" )
        IF (NOT HTTRAQT_VERSION)
                SET(DESCRIBE_STRING "no valid git tag ${PROJECT_NAME}-x.x.x found")
        ENDIF()
ENDIF()


SET(APP_RESOURCES  "${PROJECT_SOURCE_DIR}/sources/httraqt.qrc")


MESSAGE("-- Version info: ${HTTRAQT_VERSION}") 
SET(VERSION ${HTTRAQT_VERSION})

EXECUTE_PROCESS (
      COMMAND date +"%d %b %Y"
      COMMAND sed -e "s/\"//g"
      OUTPUT_VARIABLE BUILD_DATE
      OUTPUT_STRIP_TRAILING_WHITESPACE)

MESSAGE("-- Version build date: ${BUILD_DATE}")

configure_file (
  "${PROJECT_SOURCE_DIR}/sources/version.h.in"
  "${PROJECT_SOURCE_DIR}/sources/version.h" )

# 
SET(HTTRAQT_PACKAGE httraqt)
# SET(HTTRAQT_VERSION ${HTTRAQT_MAJOR_VERSION}.${HTTRAQT_MINOR_VERSION}.${HTTRAQT_RELEASE_VERSION}.${HTTRAQT_BUILD_VERSION})
SET(HTTRAQT_HOST ${CMAKE_SYSTEM} ${CMAKE_SYSTEM_PROCESSOR})
SET(HTTRAQT_COPYRIGHT "Copyright (c) 2012-2023, Eduard Kalinowski. All Rights Reserved.")
SET(HTTRAQT_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})

IF(${USE_QT_VERSION} MATCHES "4" OR ${USE_QT_VERSION} MATCHES "5")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-exceptions -fno-rtti")
ENDIF()

IF(${USE_QT_VERSION} MATCHES "6")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fno-exceptions -fno-rtti")
ENDIF()

# options for gprof 
IF(USE_PROFILER)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
  SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
  SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
  # after execution of program: gprof ./build/ponyprog
  # and get the information from gprof.txt file
  MESSAGE(STATUS "Compile with profiling information")
ELSE()
  MESSAGE(STATUS "Compile without profiling information")
ENDIF()

MESSAGE(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")

# Setup high-level build options
IF(WIN32 AND MSVC)
    SET(HTTRAQT_ENABLE_SYMBOL_VISIBILITY_DEFAULT ON)
ELSE()
    SET(HTTRAQT_ENABLE_SYMBOL_VISIBILITY_DEFAULT OFF)
ENDIF()


IF(${USE_QT_VERSION} MATCHES "4")
    FIND_PACKAGE(Qt4 REQUIRED)

    INCLUDE(FindQt4)
    IF(NOT QT4_FOUND)
        MESSAGE(FATAL_ERROR "Qt4 could not be found. "
        "If it's INSTALLed in a non-standard location, specify the path to qmake in QT_QMAKE_EXECUTABLE. "
        "You can do it in interactive mode (ccmake instead of cmake) or using -DVAR=VAL syntax.")
    ENDIF()
ENDIF()

IF(${USE_QT_VERSION} MATCHES "5")
    FIND_PACKAGE (Qt5 REQUIRED COMPONENTS Widgets)
    FIND_PACKAGE (Qt5 REQUIRED COMPONENTS Multimedia)
    FIND_PACKAGE (Qt5 REQUIRED COMPONENTS DBus)

    IF(NOT Qt5Widgets_FOUND)
        MESSAGE(FATAL_ERROR "Qt5 could not be found. "
        "If it's INSTALLed in a non-standard location, specify the path to qmake in QT_QMAKE_EXECUTABLE. "
        "You can do it in interactive mode (ccmake instead of cmake) or using -DVAR=VAL syntax.")
    ENDIF()
ENDIF()

IF(${USE_QT_VERSION} MATCHES "6")
    FIND_PACKAGE (Qt6 REQUIRED COMPONENTS Widgets)
    FIND_PACKAGE (Qt6 REQUIRED COMPONENTS Multimedia)
    FIND_PACKAGE (Qt6 REQUIRED COMPONENTS DBus)

    IF(NOT Qt6Widgets_FOUND)
        MESSAGE(FATAL_ERROR "Qt6 could not be found. "
        "If it's INSTALLed in a non-standard location, specify the path to qmake in QT_QMAKE_EXECUTABLE. "
        "You can do it in interactive mode (ccmake instead of cmake) or using -DVAR=VAL syntax.")
    ENDIF()
ENDIF()

INCLUDE(FindHttrack)

SET( HTTRACK_INCLUDES_DIR "" )

SET(HTTRAQT_CPACK_PACKAGE_SHLIBDEPS ON)

IF(${USE_QT_VERSION} MATCHES "4")
    INCLUDE_DIRECTORIES(${QT_INCLUDES})
    MESSAGE(STATUS "QT_INCLUDES ${QT_INCLUDES}")
#     LINK_DIRECTORIES(${QT_LIBRARY_DIR})
    MESSAGE(STATUS "QT_LIBRARY_DIR ${QT_LIBRARY_DIR}")
ENDIF()

IF(${USE_QT_VERSION} MATCHES "5")
    INCLUDE_DIRECTORIES(${Qt5Widgets_INCLUDE_DIRS}, ${Qt5Multimedia_INCLUDE_DIRS}, ${Qt5DBus_INCLUDE_DIRS})
    MESSAGE(STATUS "QT_INCLUDES  ${Qt5Widgets_INCLUDE_DIRS}, ${Qt5Multimedia_INCLUDE_DIRS}, ${Qt5DBus_INCLUDE_DIRS}")
ENDIF()

IF(${USE_QT_VERSION} MATCHES "6")
    INCLUDE_DIRECTORIES(${Qt6Widgets_INCLUDE_DIRS}, ${Qt6Multimedia_INCLUDE_DIRS}, ${Qt6DBus_INCLUDE_DIRS})
    MESSAGE(STATUS "QT_INCLUDES  ${Qt6Widgets_INCLUDE_DIRS}, ${Qt6Multimedia_INCLUDE_DIRS}, ${Qt6DBus_INCLUDE_DIRS}")
ENDIF()

SET ( OBJECTS_DIR temp )
SET ( MOC_DIR temp )
SET ( UI_HEADERS_DIR temp )
SET ( UI_SOURCES_DIR temp )

SET ( QT_USE_QTDBUS 1 )
  
ADD_SUBDIRECTORY(sources/main)
ADD_SUBDIRECTORY(sources/options)
#ADD_SUBDIRECTORY(sources/icons)


INCLUDE_DIRECTORIES( ${HTTRACK_INCLUDES_DIR}  ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/sources/options/includes ${CMAKE_CURRENT_SOURCE_DIR}/sources/main/includes )

IF(${USE_QT_VERSION} MATCHES "4")
    #QT4_WRAP_CPP(APP_HEADERS_MOC ${HTTRACK_HEADERS} ${MAIN_HEADERS} ${OPT_HEADERS} )
    QT4_WRAP_CPP(APP_HEADERS_MOC ${MAIN_HEADERS} ${OPT_HEADERS} )
    QT4_WRAP_UI(APP_FORMS_HEADERS ${MAIN_FORMS} ${OPT_FORMS})
    QT4_ADD_RESOURCES(APP_RESOURCES_RCC ${APP_RESOURCES})
ENDIF()

IF(${USE_QT_VERSION} MATCHES "5")
    #QT5_WRAP_CPP(APP_HEADERS_MOC ${HTTRACK_HEADERS} ${MAIN_HEADERS} ${OPT_HEADERS} )
    QT5_WRAP_CPP(APP_HEADERS_MOC ${MAIN_HEADERS} ${OPT_HEADERS} )
    QT5_WRAP_UI(APP_FORMS_HEADERS ${MAIN_FORMS} ${OPT_FORMS})
    QT5_ADD_RESOURCES(APP_RESOURCES_RCC ${APP_RESOURCES})
ENDIF()

IF(${USE_QT_VERSION} MATCHES "6")
    #QT5_WRAP_CPP(APP_HEADERS_MOC ${HTTRACK_HEADERS} ${MAIN_HEADERS} ${OPT_HEADERS} )
    QT6_WRAP_CPP(APP_HEADERS_MOC ${MAIN_HEADERS} ${OPT_HEADERS} )
    QT6_WRAP_UI(APP_FORMS_HEADERS ${MAIN_FORMS} ${OPT_FORMS})
    QT6_ADD_RESOURCES(APP_RESOURCES_RCC ${APP_RESOURCES})
ENDIF()

INCLUDE_DIRECTORIES( ${HTTRACK_INCLUDES_DIR} ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

SOURCE_GROUP( "Generated Files" FILES
    ${MOC_FILES_CPP}
    ${APP_FORMS_HEADERS}
    ${APP_RESOURCES_RCC}
)

IF(NOT ${USE_DEBUGGER})
    ADD_DEFINITIONS(${QT_DEFINITIONS} -DQT_NO_DEBUG_OUTPUT -DQT_USE_FAST_CONCATENATION -DQT_USE_FAST_OPERATOR_PLUS)
    #ADD_DEFINITIONS(${QT_DEFINITIONS} -DQT_NO_DEBUG_OUTPUT)
ENDIF()

IF(${USE_QT_VERSION} MATCHES "4")
    INCLUDE(${QT_USE_FILE})
    ADD_DEFINITIONS(${QT_DEFINITIONS} -DQT_PROJECT -DNO_QT3SUPPORT -DDISABLE_QT3SUPPORT)
ENDIF()

IF(${USE_QT_VERSION} MATCHES "5")
    ADD_DEFINITIONS( ${Qt5Widgets_DEFINITIONS})
ENDIF()

IF(${USE_QT_VERSION} MATCHES "6")
    ADD_DEFINITIONS( ${Qt6Widgets_DEFINITIONS})
ENDIF()

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}
    ${APP_SOURCES} 
    ${MAIN_SOURCES}
    ${OPT_SOURCES}
    ${APP_HEADERS_MOC} 
    ${APP_FORMS_HEADERS} 
    ${APP_RESOURCES_RCC}
)


IF(${USE_QT_VERSION} MATCHES "4")
    TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${QT_LIBRARIES} httrack)
ENDIF()

IF(${USE_QT_VERSION} MATCHES "5")
    TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${Qt5Widgets_LIBRARIES} ${Qt5DBus_LIBRARIES} httrack)
ENDIF()

IF(${USE_QT_VERSION} MATCHES "6")
    TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${Qt6Widgets_LIBRARIES} ${Qt6DBus_LIBRARIES} httrack)
ENDIF()

ADD_CUSTOM_TARGET (tags
    COMMAND  ctags -R -f tags ${CMAKE_SOURCE_DIR}/sources/main ${CMAKE_SOURCE_DIR}/sources/options
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
# MESSAGE("CMAKE_SOURCE_DIR main ${CMAKE_SOURCE_DIR}")
# MESSAGE("CMAKE_CURRENT_BINARY_DIR main ${CMAKE_CURRENT_BINARY_DIR}")
# INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})


# Setup module enabled/disabled defaults ...
IF(WIN32)
    SET(HTTRAQT_BUILD_GLX_MODULE_DEFAULT OFF)
    SET(HTTRAQT_BUILD_OSX_MODULE_DEFAULT OFF)
    SET(HTTRAQT_BUILD_VIRTUAL_OFFSCREEN_MODULE_DEFAULT OFF)
ENDIF()

IF(UNIX AND NOT APPLE)
    SET(HTTRAQT_BUILD_GLX_MODULE_DEFAULT OFF)
    SET(HTTRAQT_BUILD_OSX_MODULE_DEFAULT OFF)
    SET(HTTRAQT_BUILD_VIRTUAL_OFFSCREEN_MODULE_DEFAULT OFF)
ENDIF()

IF(APPLE)
    SET(HTTRAQT_BUILD_GLX_MODULE_DEFAULT OFF)
    SET(HTTRAQT_BUILD_OSX_MODULE_DEFAULT ON)
    SET(HTTRAQT_BUILD_VIRTUAL_OFFSCREEN_MODULE_DEFAULT OFF)
ENDIF()


OPTION(HTTRAQT_ENABLE_SYMBOL_VISIBILITY "Minimize the number of symbols exported from shared libraries." ${HTTRAQT_ENABLE_SYMBOL_VISIBILITY_DEFAULT})
MARK_AS_ADVANCED(HTTRAQT_ENABLE_SYMBOL_VISIBILITY)

# OPTION(HTTRAQT_ENABLE_TESTING "Build the HTTraQt regression test suite." OFF)
# SET(BUILD_TESTING ${HTTRAQT_ENABLE_TESTING} CACHE INTERNAL "" FORCE)
# INCLUDE(CTest)
# MARK_AS_ADVANCED(DART_TESTING_TIMEOUT)
# IF(HTTRAQT_ENABLE_TESTING)
#     CMAKE_MINIMUM_REQUIRED(VERSION 2.6.1 FATAL_ERROR)
#     CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake @ONLY)
# ENDIF(HTTRAQT_ENABLE_TESTING)

# Find required external packages
HTTRAQT_CHECK_REQUIRED_DEPENDENCY(HTTRAQT_HTTRACK_FOUND "libhttrack2" "http://www.httrack.com" "")
# HTTRAQT_CHECK_OPTIONAL_DEPENDENCY(HTTRAQT_BUILD_3DS_IO_MODULE HTTRAQT_3DS_FOUND "lib3ds")

# Capture system configuration
INCLUDE(SystemConfiguration)

# Win32 configuration
IF(WIN32)
    IF(NOT MSVC)
        SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-runtime-pseudo-reloc" CACHE STRING "" FORCE)
        SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--enable-runtime-pseudo-reloc -Wl,--export-all-symbols" CACHE STRING "" FORCE)
    ENDIF()
ENDIF()

# Setup output directories ...
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${HTTRAQT_BINARY_DIR}/bin)

SET(HTTRAQT_LIBDIR lib) # Allows us to handle 64-bit libs if/when it becomes necessary.
IF(WIN32)
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${HTTRAQT_BINARY_DIR}/bin)
ELSE()
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${HTTRAQT_BINARY_DIR}/${HTTRAQT_LIBDIR})
ENDIF()

# Setup a macro for compiling resources ...
MACRO(HTTRAQT_COMPILE_RESOURCE OUTPUT INPUT RESOURCE_PATH)

    SET(INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${INPUT}")
    SET(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${INPUT}.cpp")
    GET_FILENAME_COMPONENT(OUTPUT_PATH ${OUTPUT_FILE} PATH)

    GET_TARGET_PROPERTY(HTTRAQT_RESOURCE_COMPILER httraqt-resource-compiler LOCATION)

    ADD_CUSTOM_COMMAND(
        DEPENDS httraqt-resource-compiler
        DEPENDS ${INPUT_FILE}
        COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTPUT_PATH}
        COMMAND ${HTTRAQT_RESOURCE_COMPILER} --input ${INPUT_FILE} --path \"<path>${RESOURCE_PATH}</path>\" --output ${OUTPUT_FILE}
        OUTPUT ${OUTPUT_FILE}
        COMMENT "Compiling resource ${INPUT_FILE}"
        )

    LIST(APPEND ${OUTPUT} ${OUTPUT_FILE})

ENDMACRO(HTTRAQT_COMPILE_RESOURCE)

# Setup subdirectories ...
HTTRAQT_CONDITIONAL_BUILD(MSVC gendef)

OPTION(HTTRAQT_ENABLE_DISTRIBUTION "Enable distribution targets." ON)
HTTRAQT_CONDITIONAL_BUILD(HTTRAQT_ENABLE_DISTRIBUTION distribution)

# Hide some cruft ...
MARK_AS_ADVANCED(CMAKE_BACKWARDS_COMPATIBILITY)
MARK_AS_ADVANCED(CMAKE_EXECUTABLE_FORMAT)
MARK_AS_ADVANCED(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
MARK_AS_ADVANCED(CMAKE_OSX_ARCHITECTURES)
MARK_AS_ADVANCED(CMAKE_OSX_DEPLOYMENT_TARGET)
MARK_AS_ADVANCED(CMAKE_OSX_SYSROOT)
MARK_AS_ADVANCED(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
MARK_AS_ADVANCED(CMAKE_USE_CHRPATH)

