########################################################
#  
#  This is a CMake configuration file.
#  To use it you need CMake which can be 
#  downloaded from here: 
#    http://www.cmake.org/cmake/resources/software.html
#
#########################################################

# Hard minimum version of cmake for all sub projects and Sigil
cmake_minimum_required( VERSION 3.18 ) 

project( Sigil )

# Change this URL if the location of the CI Windows Qt6 download ever changes
set (WINQTURL "https://github.com/dougmassay/win-qtwebkit-5.212/releases/download/v5.212-1")

# Used to keep downloaded Qt and PySide6 versions in sync.
# Check https://github.com/dougmassay/win-qtwebkit-5.212/releases/ to
# see what versions are available. Defaults to 6.7.3.
if(NOT DEFINED QTVER)
    set(QTVER 6.7.3)
endif()

# Default to using newe findPython3 on Windows and Linux
if ( NOT DEFINED TRY_NEWER_FINDPYTHON3 )
    if ( NOT APPLE )
        set ( TRY_NEWER_FINDPYTHON3 1 )
    else()
        set ( TRY_NEWER_FINDPYTHON3 0 )
    endif()
endif()

# Set to 1 to create and use a virtual Python venv on Windows and Linux.
# Use in conjunction with QTVER to download/install correct PySide6 module
if ( NOT DEFINED USE_VIRT_PY )
    set ( USE_VERT_PY 0 )
endif()

# Set to 1 to download and use a custom-built Qt from WINQTURL.
# Use in conjunction with QTVER to clarify which version of Qt to download.
if ( NOT DEFINED DOWNLOAD_QT )
    set ( DOWNLOAD_QT 0 )
endif()

# Set Inno minimum Windows version 
if ( NOT DEFINED BUILD_FOR_WIN7 )
    # Windows 10 (1809)
    set( BUILD_FOR_WIN7 0 )
    set ( WIN_MIN_VERSION 10.0.17763 )
else()
    # Windows 7sp1
    set ( WIN_MIN_VERSION 6.1.7601 )
endif()

if (TRY_NEWER_FINDPYTHON3)
    set ( USE_NEWER_FINDPYTHON3 1 )
else()
    set ( USE_NEWER_FINDPYTHON3 0 )
endif()

set( CMAKE_DEBUG_POSTFIX "d" )
# Avoid the whole Release/Debug folder creation with Visual Studio
if (MSVC_IDE)
   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/bin")
   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/bin")
   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${PROJECT_BINARY_DIR}/bin")
   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/lib")
   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/lib")
   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${PROJECT_BINARY_DIR}/lib")
   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/lib")
   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/lib")
   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${PROJECT_BINARY_DIR}/lib")
endif()
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib )
set( PACKAGE_DIRECTORY ${PROJECT_BINARY_DIR}/package )
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake_extras" )

set( SIGIL_MAJOR_VERSION 2 )
set( SIGIL_MINOR_VERSION 4 )
set( SIGIL_REVISION_VERSION 2 )
set( SIGIL_FULL_VERSION ${SIGIL_MAJOR_VERSION}.${SIGIL_MINOR_VERSION}.${SIGIL_REVISION_VERSION} )

if( UNIX AND NOT APPLE )
   include(GNUInstallDirs)

   # Make sure the correct rpath is set for the sigil executable
   # on Linux -- for both the build and the installed versions.
   # Needed to make sure the installed shared hunspell and gumbo
   # libraries are found/used at runtime.

   # use, i.e. don't skip the full RPATH for the build tree
   SET(CMAKE_SKIP_BUILD_RPATH  FALSE)

   # when building, don't use the install RPATH already
   # (but later on when installing)
   SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

   SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}/sigil")

   # add the automatically determined parts of the RPATH
   # which point to directories outside the build tree to the install RPATH.
   # This will ensure the Qt5 lib directory is always included after install.
   SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

   # Because GCC's link time optimization is willfully choosing to suck.
   add_compile_options(-fno-lto)
endif()

# Check if platform is 64 bit
if( NOT APPLE )
   if( CMAKE_SIZEOF_VOID_P EQUAL 4 )
     set( 64_BIT_PLATFORM 0 )
   else()
     set( 64_BIT_PLATFORM 1 )
   endif()
endif()

# Profiler configuration for GCC
if( CMAKE_COMPILER_IS_GNUCXX  )
    set( CMAKE_BUILD_TYPE profiling )
    set( CMAKE_CXX_FLAGS_PROFILING "-O2 -DNDEBUG -pg -g -fno-omit-frame-pointer -fno-inline-functions -fno-inline-functions-called-once -fno-optimize-sibling-calls" )

    # After creating the new build type,
    # we clear the default back to empty
    set( CMAKE_BUILD_TYPE  )
endif()

if (USE_SYSTEM_LIBS)
    MESSAGE(STATUS "Trying to use System Libraries...")
    find_package( ZLIB )
    find_package( PkgConfig )
    find_package( MiniZip )
    pkg_check_modules( HUNSPELL hunspell )
    if ( NOT APPLE )
        pkg_check_modules( PCRE2 libpcre2-16 )
    endif()
endif()

message(STATUS "CMake version in use: ${CMAKE_VERSION}")

if (${USE_NEWER_FINDPYTHON3})
    message(STATUS "Using newer findpython3 cmake module")
    if ( APPLE )
        find_package(Python3 3.9 COMPONENTS Interpreter Development)
    endif()
    if ( WIN32 )
        if ( DOWNLOAD_QT )
            include(windownloadqt.cmake)
            set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_BINARY_DIR}/Qt${QTVER}/lib/cmake")
            set(Qt6_Dir "${CMAKE_CURRENT_BINARY_DIR}/Qt${QTVER}/lib/cmake/Qt6")
            if(NOT EXISTS "${Qt6_Dir}")
                message(FATAL_ERROR "Qt6 download/configure unsuccessful!")
            endif()
        endif()
        if ( USE_VIRT_PY )
            include(winvirtpy.cmake)
            # Set hints so virtual Python environement is found first
            set(Python3_FIND_VIRTUAL ONLY)
            set(Python3_FIND_REGISTRY NEVER)
            set(Python3_FIND_STRATEGY LOCATION)
            # Trick cmake's findPython3 module into thinking the virtual python is activated
            set(VIRT_PATH "${CMAKE_CURRENT_BINARY_DIR}/sigilpy")
            set(ENV{VIRTUAL_ENV} "${VIRT_PATH}")
            if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/sigilpy/Scripts/python.exe")
                message(FATAL_ERROR "Virtual Python setup unsuccessful!")
            endif()
        endif()
        find_package(Python3 3.9 REQUIRED COMPONENTS Interpreter Development)
    endif()
    if( UNIX AND NOT APPLE )
        if ( USE_VIRT_PY )
            # Must be run in an environment with the venv activated
            set ( Python3_FIND_VIRTUAL ONLY )
            add_definitions( -DLINUX_VIRT_PY )
        endif()
        find_package(Python3 3.9 COMPONENTS Interpreter Development)
    endif()
else()
    message(STATUS "Using older findpython cmake module")
    if ( APPLE )
        find_package(PythonInterp 3.11)
        find_package (PythonLibs 3.11)
    endif()
    if ( WIN32 )
        find_package(PythonInterp 3.9)
        find_package (PythonLibs 3.9)
    endif()
    if( UNIX AND NOT APPLE )
        find_package(PythonInterp 3.9)
        find_package (PythonLibs 3.9)
    endif()
endif()

# gumbo-parser it is our main xhtml/html5 parser.
# We have an internal version because it diverges from Google's and GitHub's
# versions and neither want's our epub specific changes.
add_subdirectory( internal/gumbo )

add_subdirectory( 3rdparty/ )
add_subdirectory( src/ )
