PROJECT(ponyprog)

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

IF(WIN32)
    CMAKE_POLICY(SET CMP0020 NEW)
ENDIF()

# set the Qt version to 4 or 5
OPTION (USE_QT5 "Using of Qt5 version for compiling" ON)

IF(${USE_QT5})
    SET(USE_QT_VERSION 5)
ELSE(${USE_QT5})
    SET(USE_QT_VERSION 4)
ENDIF(${USE_QT5})

# # SET(FILES_TO_TRANSLATE)
IF(WIN32)
    IF(MSVC)
        SET (CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}:${QTDIR}\\msvc2015\\")
    ENDIF()

    IF(MinGW)
        SET (CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}:${QTDIR}\\mingw53_32\\")
    ENDIF()
ENDIF()

#disable -rdynamic
# SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")


# FILE (GLOB TRANSLATIONS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/translations/*.ts)

# OPTION (UPDATE_TRANSLATIONS "Update source translation ${CMAKE_CURRENT_SOURCE_DIR}/translations/*.ts" OFF)


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

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

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


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

MESSAGE(STATUS "CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}")

INCLUDE(CheckIncludeFile)
INCLUDE(CheckIncludeFileCXX)
INCLUDE(TestCXXAcceptsFlag)
INCLUDE(CheckCXXCompilerFlag)

SET(TARGET_NAME ponyprog )
SET(CMAKE_HELPERS_BINARY_DIR ${PROJECT_SOURCE_DIR}/cmake)
SET(CMAKE_HELPERS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/cmake)
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

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



INCLUDE(OutOfSourceBuild) # Disallow in-source builds

INCLUDE(GenerateDEF) # Convenience macro for linking Win32 DLLs using MSVC
INCLUDE(Dependencies)


SET(CURRENT_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})

INCLUDE(FindLibUSB-1.0)

IF(NOT ${LIBUSB_VERSION} LESS "1.0.16")
  MESSAGE(STATUS "LibUSB version ${LIBUSB_VERSION}")
ELSE()
  MESSAGE(FATAL_ERROR "-- LibUSB version ${LIBUSB_VERSION} TOO OLD! Please Install this library newer as 1.0.16")
ENDIF()


# SET(APP_VERSION "3.1.4")
#
# Set the PonyProg version
# this for generating of version file from HISTORY file
# the idea is to descript all changes in this file, last changes with version are in the top of file
FILE(READ "HISTORY" DESCRIBE_STRING)
STRING(REGEX MATCH "([0-9]+\\.[0-9]+\\.[0-9]+)" APP_VERSION "${DESCRIBE_STRING}" )

MESSAGE(STATUS "Version info: ${APP_VERSION}")
SET(VERSION ${APP_VERSION})

SET(APP_AUTHOR "Claudio Lanconelli")
SET(APP_EMAIL "PonyProg2000@gmail.com")
SET(APP_NAME "PonyProg")
SET(AUTHORWEB "http://www.LancOS.com")
SET(APP_YEAR "2025")

SET(PORTERGQT "Eduard Kalinowski")
SET(PORTERMAIL "eduard_kalinowski@yahoo.de")


IF(WIN32)
  EXECUTE_PROCESS (
    COMMAND "cmd" " /c date /t"
    OUTPUT_VARIABLE APP_BUILD_DATE
    OUTPUT_STRIP_TRAILING_WHITESPACE)
ELSE()
  EXECUTE_PROCESS (
    COMMAND date +"%d-%m-%Y"
    OUTPUT_VARIABLE APP_BUILD_DATE
    OUTPUT_STRIP_TRAILING_WHITESPACE)
    STRING(REGEX REPLACE "\"" "" APP_BUILD_DATE ${APP_BUILD_DATE})
ENDIF()

MESSAGE(STATUS "Version build date: ${APP_BUILD_DATE}")

#
SET(PONYPROG_PACKAGE ponyprog)
SET(PONYPROG_HOST ${CMAKE_SYSTEM} ${CMAKE_SYSTEM_PROCESSOR})
#SET(PONYPROG_COPYRIGHT "Copyright (c) 1997-${APP_YEAR} ${APP_AUTHOR}, All Rights Reserved.")
SET(PONYPROG_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})

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

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-exceptions")

# 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()



    FIND_PACKAGE(Qt5 COMPONENTS Core PrintSupport Gui Widgets Multimedia REQUIRED QUIET)

    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(NOT Qt5Widgets_FOUND)

    IF(NOT Qt5PrintSupport_FOUND)
        MESSAGE(FATAL_ERROR "Qt5 PrintSupport development files are not found. "
        "Qt5PrintSupport development package is to install.")
    ENDIF(NOT Qt5PrintSupport_FOUND)

    IF(NOT Qt5Multimedia_FOUND)
        MESSAGE(FATAL_ERROR "Qt5 Multimedia development files are not found. "
        "Qt5Multimedia development package is to install.")
    ENDIF(NOT Qt5Multimedia_FOUND)

IF (NOT WIN32)
    FIND_PACKAGE ( LibFTDI1 NO_MODULE REQUIRED )

    IF(NOT LibFTDI1_FOUND)
        MESSAGE(FATAL_ERROR "LibFTDI1 files are not found. "
        "need to install LibFTDI1 development packages.")
    ENDIF(NOT LibFTDI1_FOUND)

    INCLUDE ( ${LIBFTDI_USE_FILE} )
ELSE()
    SET ( LIBFTDI_ROOT_DIR     "C:/libftdi1-1.5_devkit_x86_x64_19July2020" )

	SET ( LIBFTDI_LIBRARY      "ftdi1" )
    SET ( LIBFTDIPP_LIBRARY    "ftdipp1" )

    SET ( LIBFTDI_VERSION_STRING "1.5" )
    SET ( LIBFTDI_VERSION_MAJOR  "1" )
    SET ( LIBFTDI_VERSION_MINOR  "5" )

	INCLUDE_DIRECTORIES("${LIBFTDI_ROOT_DIR}/include")
	INCLUDE_DIRECTORIES("${LIBFTDI_ROOT_DIR}/include/libftdi")
	LINK_DIRECTORIES("${LIBFTDI_ROOT_DIR}/lib32")
ENDIF()

INCLUDE_DIRECTORIES(${LIBUSB_INCLUDE_DIRS})

# for this feature install debhelper or
SET(PONYPROG_CPACK_PACKAGE_SHLIBDEPS ON)

    INCLUDE_DIRECTORIES(${QT_INCLUDES} ${Qt5Widgets_INCLUDE_DIRS}  ${Qt5PrintSupport_INCLUDE_DIRS} ${Qt5Multimedia_INCLUDE_DIRS})

    #MESSAGE(STATUS "QT_INCLUDES ${QT_INCLUDES} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Multimedia_INCLUDE_DIRS}")

    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS} ${Qt5Multimedia_EXECUTABLE_COMPILE_FLAGS} ${Qt5PrintSupport_EXECUTABLE_COMPILE_FLAGS}")

configure_file (
  "${PROJECT_SOURCE_DIR}/SrcPony/version-cmake.h.in"
  "${PROJECT_SOURCE_DIR}/SrcPony/version.h" )


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

ADD_SUBDIRECTORY(qhexedit2/src)
ADD_SUBDIRECTORY(SrcPony)

INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/qhexedit2/src/ ${CMAKE_CURRENT_SOURCE_DIR}/SrcPony/ )


#     QT5_ADD_TRANSLATION(APP_TRANSLATIONS ${TRANSLATION})
    QT5_WRAP_CPP(APP_HEADERS_MOC ${HEX_HEADERS} ${PONY_HEADERS} )
    QT5_WRAP_UI(APP_FORMS_HEADERS ${PONY_FORMS} )
    QT5_ADD_RESOURCES(APP_RESOURCES_RCC ${APP_RESOURCES})


MESSAGE(STATUS "PONY_RESOURCES ${APP_RESOURCES_RCC}")

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

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

message(STATUS "Include dirs ")
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
  message(STATUS "dir='${dir}'")
endforeach()


IF(NOT ${USE_DEBUGGER})
    ADD_DEFINITIONS(${QT_DEFINITIONS} -DQT_NO_DEBUG_OUTPUT -DQT_NO_DEBUG -DQT_USE_FAST_CONCATENATION -DQT_USE_FAST_OPERATOR_PLUS)
ELSE()
    ADD_DEFINITIONS(${QT_DEFINITIONS} -DQT_DEBUG)
ENDIF()

ADD_DEFINITIONS(${Qt5Widgets_DEFINITIONS} ${Qt5PrintSupport_DEFINITIONS} ${Qt5Multimedia_DEFINITIONS} -DQT_PROJECT)

IF(WIN32)
    ADD_DEFINITIONS(-DUNICODE -D_UNICODE)
ENDIF()

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}
    ${APP_SOURCES}
    ${HEX_SOURCES}
    ${PONY_SOURCES}
    ${APP_HEADERS_MOC}
    ${APP_FORMS_HEADERS}
#     ${APP_TRANSLATIONS}
    ${APP_RESOURCES_RCC}
)


TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${QT_LIBRARIES} ${Qt5Widgets_LIBRARIES} ${Qt5Multimedia_LIBRARIES} ${Qt5PrintSupport_LIBRARIES})
#MESSAGE(STATUS "QT LIBRARIES: ${QT_LIBRARIES} ${Qt5Widgets_LIBRARIES} ${Qt5Multimedia_LIBRARIES} ${Qt5PrintSupport_LIBRARIES} ${Qt5Core_LIBRARIES}")

#TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${LIBFTDI_LIBRARIES} )
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${LIBFTDI_LIBRARY} ${LIBUSB_LIBRARIES})

ADD_CUSTOM_TARGET (tags
    COMMAND  ctags -R -f tags ${CMAKE_SOURCE_DIR}/SrcPony
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

#message(STATUS "Link Libraries ")
#get_target_property(OUTP ${CMAKE_PROJECT_NAME} LINK_LIBRARIES)
#message(STATUS ${OUTP})


# Setup module enabled/disabled defaults ...
IF(MSVC)
    SET(PONYPROG_BUILD_GLX_MODULE_DEFAULT ON)
    SET(PONYPROG_BUILD_OSX_MODULE_DEFAULT OFF)
    SET(PONYPROG_BUILD_VIRTUAL_OFFSCREEN_MODULE_DEFAULT OFF)
ENDIF(MSVC)

IF(UNIX AND NOT APPLE)
    SET(PONYPROG_BUILD_GLX_MODULE_DEFAULT ON)
    SET(PONYPROG_BUILD_OSX_MODULE_DEFAULT OFF)
    SET(PONYPROG_BUILD_VIRTUAL_OFFSCREEN_MODULE_DEFAULT OFF)
ENDIF(UNIX AND NOT APPLE)

IF(APPLE)
    SET(PONYPROG_BUILD_GLX_MODULE_DEFAULT ON)
    SET(PONYPROG_BUILD_OSX_MODULE_DEFAULT ON)
    SET(PONYPROG_BUILD_VIRTUAL_OFFSCREEN_MODULE_DEFAULT OFF)
ENDIF(APPLE)


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

# OPTION(PONYPROG_ENABLE_TESTING "Build the Ponyprog regression test suite." OFF)
# SET(BUILD_TESTING ${PONYPROG_ENABLE_TESTING} CACHE INTERNAL "" FORCE)
# INCLUDE(CTest)
# MARK_AS_ADVANCED(DART_TESTING_TIMEOUT)
# IF(PONYPROG_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(PONYPROG_ENABLE_TESTING)

# Capture system configuration
INCLUDE(SystemConfiguration)

# MSVC configuration
IF(MSVC)
    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(NOT MSVC)
ENDIF(MSVC)

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


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

# Setup a macro for compiling resources ...
MACRO(PONYPROG_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(PONYPROG_RESOURCE_COMPILER ponyprog-resource-compiler LOCATION)

    ADD_CUSTOM_COMMAND(
        DEPENDS ponyprog-resource-compiler
        DEPENDS ${INPUT_FILE}
        COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTPUT_PATH}
        COMMAND ${PONYPROG_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(PONYPROG_COMPILE_RESOURCE)

# Setup subdirectories ...
PONYPROG_CONDITIONAL_BUILD(MSVC gendef)
#
OPTION(PONYPROG_ENABLE_DISTRIBUTION "Enable distribution targets." ON)
PONYPROG_CONDITIONAL_BUILD(PONYPROG_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)
