cmake_minimum_required(VERSION 3.12)

# CMake script for pwizlite library
# Author: Filippo Rusconi
# Created: 29/05/2020

############################################################
############################################################
# Basic information about project

set(PROJECT libpwizlite)
project(${PROJECT} CXX C)

set(PWIZLITE_VERSION_MAJOR "3")
set(PWIZLITE_VERSION_MINOR "0")
set(PWIZLITE_VERSION_PATCH "10")
set(PWIZLITE_VERSION "${PWIZLITE_VERSION_MAJOR}.${PWIZLITE_VERSION_MINOR}.${PWIZLITE_VERSION_PATCH}")

message(STATUS "${BoldYellow}CMAKE_LIBRARY_ARCHITECTURE: ${CMAKE_LIBRARY_ARCHITECTURE}${ColourReset}")

# This line MUST be located prior to include'ing GNUInstallDirs !!!
# if CMAKE_INSTALL_PREFIX is /usr/local (default), then
# the CMAKE_LIBRARY_ARCHITECTURE above is not honoured when
# computing the CMAKE_INSTALL_FULL_LIBDIR.

set(CMAKE_INSTALL_PREFIX /usr)
message(STATUS "Set CMAKE_INSTALL_PREFIX to ${CMAKE_INSTALL_PREFIX}")

include(GNUInstallDirs)

message(STATUS "Install directory prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "CMAKE_INSTALL_FULL_LIBDIR: ${CMAKE_INSTALL_FULL_LIBDIR}")
message(STATUS "CMAKE_INSTALL_FULL_INCLUDEDIR: ${CMAKE_INSTALL_FULL_INCLUDEDIR}")

# Add folder where are supportive functions
set(CMAKE_UTILS_PATH ${CMAKE_SOURCE_DIR}/CMakeStuff)
set(CMAKE_TOOLCHAINS_PATH ${CMAKE_UTILS_PATH}/toolchains)
set(CMAKE_MODULE_PATH ${CMAKE_UTILS_PATH}/modules)
#message("CMAKE_MODULE_PATH:" ${CMAKE_MODULE_PATH})

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Include the system's uname that fills in SYSTEM_UNAME_S.
# Sets WIN64 if SYSTEM_UNAME_S is "^.*MING64.*"
include(${CMAKE_UTILS_PATH}/systemUname.cmake)

# Include the various colors we want to use in the output
include(${CMAKE_UTILS_PATH}/outputColors.cmake)

set(CMAKE_COLOR_MAKEFILE ON)
set(CMAKE_VERBOSE_MAKEFILE ON)

message("\n${BoldRed}Configuring build for project ${PROJECT}${ColourReset}\n")

# This export will allow using the flags to be used by
# youcompleteme (vim plugin).
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json")
    execute_process(
        COMMAND cmake -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
                ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json
    )
endif()

# We want C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "Set CMAKE_CXX_STANDARD to 17 (required)")
message(STATUS "${BoldGreen}CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}${ColourReset}")

#############################################################
# We do not want warnings for unknown pragmas:
message(STATUS "Setting definition -Wno-unknown-pragmas.${ColourReset}")
add_definitions(-Wno-unknown-pragmas)
# Enable warnings and possibly treat them as errors
message(STATUS "${BoldGreen}Setting definition -Wall.${ColourReset}")
add_definitions(-Wall)
message(STATUS "${BoldGreen}Setting definition -Wextra.${ColourReset}")
add_definitions(-Wextra)

if(WARN_AS_ERROR)
    message(STATUS "${BoldYellow}Setting definition -Werror.${ColourReset}")
    add_definitions(-Werror)
endif()

message(STATUS "${BoldRed}CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}${ColourReset}")

# Initialize a list variable that will be updated depending on the specific
# platforms below.
list(APPEND PLATFORM_SPECIFIC_LINK_LIBRARIES "")
#############################################################
#############################################################
# Platform-specific CMake configuration
if(MXE)
    include(${CMAKE_TOOLCHAINS_PATH}/mxe-toolchain.cmake)

    # Set the name to the systemUname variable because in this situation that name
    # is not found, it it passed as a flag in the command line.
    set(SYSTEM_UNAME_S "mxe")

elseif(UNIX AND NOT APPLE)

    include(${CMAKE_TOOLCHAINS_PATH}/unix-toolchain.cmake)

elseif(WIN32 OR _WIN32)

    if(WIN10MINGW64)

        # The file below will actually #define WIN10MINGW64 for
        # consumption by the src/pwiz/utility/misc/Filesystem.cpp file !
        include(${CMAKE_TOOLCHAINS_PATH}/win10-mingw64-toolchain.cmake)

    endif()

elseif(APPLE)

    include(${CMAKE_TOOLCHAINS_PATH}/apple-macport-toolchain.cmake)

endif()

message("")
message(STATUS "${BoldGreen}Starting configuration of ${PROJECT}${ColourReset}")
message("")
message(STATUS "${BoldYellow}The build toolchain is: ${SYSTEM_UNAME_S}${ColourReset}")
message("")

#############################################################
#############################################################
# Essential software configuration
message(STATUS "CMAKE_CURRENT_BINARY_DIR: " ${CMAKE_CURRENT_BINARY_DIR})

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE
        Release
        CACHE STRING "Type of build, options are: None, Debug, Release, RelWithDebInfo, MinSizeRel." FORCE
    )
endif(NOT CMAKE_BUILD_TYPE)

if(CMAKE_BUILD_TYPE MATCHES "Release")
    message(STATUS "Compiling in release mode.")
    add_definitions("-DQT_NO_DEBUG_OUTPUT")
endif()

if(CMAKE_BUILD_TYPE MATCHES "Debug")
    message(STATUS "Compiling in debug mode: ${MAKE_TEST}.")
    message(STATUS "Add definition -ggdb3 to format debug output for GDB.")
    add_definitions(-ggdb3)
endif()

if(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
    message(STATUS "Compiling in release with debug info mode.")
endif(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")

message(STATUS "${BoldYellow}CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}.${ColourReset}")

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Set the CMAKE_PREFIX_PATH for the find_library fonction when using non
# standard install location
if(CMAKE_INSTALL_PREFIX)
    set(CMAKE_PREFIX_PATH "${CMAKE_INSTALL_PREFIX}" ${CMAKE_PREFIX_PATH})
endif()

# Subdirectories
message(STATUS "Adding subdirectory src for PROJECT: ${PROJECT}")
add_subdirectory(src)

# There are situations where we do not want CPack processing.
# For that, configure the build using -DNO_CPACK=1.

if(NOT NO_CPACK)

    message("STATUS: Now managing the CPack process")

    set(CPACK_CMAKE_GENERATOR "Unix Makefiles")
    set(CPACK_GENERATOR "STGZ;TGZ;TZ")
    set(CPACK_OUTPUT_CONFIG_FILE "./CPackConfig.cmake")
    #set(CPACK_PACKAGE_DESCRIPTION_FILE ${LIBODSSTREAM_SOURCE_DIR}/COPYING)
    set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
        "A library that contains a subset of the ProteoWizard libpwiz library with code mainly aimed at loading MS files (XML-based and MGF)."
    )
    set(CPACK_PACKAGE_EXECUTABLES "libpwizlite")
    set(CPACK_SOURCE_PACKAGE_FILE_NAME "libpwizlite-${PWIZLITE_VERSION}")
    set(CPACK_SYSTEM_NAME "Linux-i686")
    set(CPACK_PACKAGE_FILE_NAME "libpwizlite-${PWIZLITE_VERSION}-${CPACK_SYSTEM_NAME}")
    set(CPACK_PACKAGE_INSTALL_DIRECTORY "libpwizlite ${PWIZLITE_VERSION}")
    set(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "libpwizlite ${PWIZLITE_VERSION}")
    set(CPACK_PACKAGE_NAME "libpwizlite")
    set(CPACK_PACKAGE_VENDOR "PAPPSO")
    set(CPACK_PACKAGE_VERSION ${PWIZLITE_VERSION})
    set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/debian/copyright)
    set(CPACK_SOURCE_GENERATOR "TGZ;TZ")
    set(CPACK_SOURCE_OUTPUT_CONFIG_FILE "./CPackSourceConfig.cmake")
    set(CPACK_SOURCE_STRIP_FILES "")
    set(CPACK_SYSTEM_NAME "Linux-i686")
    set(CPACK_TOPLEVEL_TAG "Linux-i686")
    set(CPACK_SOURCE_PACKAGE_FILE_NAME "libpwizlite-${PWIZLITE_VERSION}")

    set(CPACK_SOURCE_IGNORE_FILES
        "\\\\.cache/"
        ".*\\\\.tar\\\\.gz"
        "moc_.*cxx"
        "\\\\.#.*"
        ".*\\\\.aux"
        ".*\\\\.bbl"
        ".*\\\\.blg"
        ".*\\\\.log"
        ".*\\\\.out"
        ".*\\\\.toc"
        "/devel_archives/"
        "/doc\\\\/html/"
        "/doc\\\\/latex/"
        "Makefile"
        "install_manifest.txt"
        "CMakeCache.txt"
        "CPackConfig.cmake"
        "CPackSourceConfig.cmake"
        "/CMakeFiles/"
        "/_CPack_Packages/"
        "/Debug/"
        "/Release/"
        "/tests/"
        "/\\\\.externalToolBuilders/"
        "/\\\\.git/"
        "/\\\\.kdev4/"
        "development.kdev4"
        "/\\\\.settings/"
        "Makefile"
        "\\\\.cdtbuild"
        "\\\\.cdtproject"
        "\\\\.project"
        "\\\\.cproject"
        "/win32/"
        "/win64/"
        "/build/"
        "/buildwin64/"
        "/cbuild/"
        "/wbuild/"
        "/bin/"
        "/buildmingw/"
        "test\\\\.ods"
        "buildwin32.cmd"
        "libpwizlite.so"
        "OpenDocument-v1.0-os.pdf"
        "debian/"
        "compile_commands.json"
    )

    set(CPACK_PACKAGE_EXECUTABLES "libpwizlite" "libpwizlite")

    # for debian package :
    set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Olivier Langella <olivier.langella@u-psud.fr>")
    set(DEBIAN_PACKAGE_BUILDS_DEPENDS "Olivier Langella <olivier.langella@u-psud.fr>")

    set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
    set(CPACK_DEBSOURCE_PACKAGE_FILE_NAME "lib${CMAKE_PROJECT_NAME}_${CPACK_PACKAGE_VERSION}.orig")
    include(CPack)

    # to create a TGZ archive of the  source code type shell command
    # cpack -G TGZ --config CPackSourceConfig.cmake

    add_custom_target(
        targz
        cpack
        -G
        TGZ
        --config
        CPackSourceConfig.cmake
        &&
        tar
        xvfz
        libpwizlite-${PWIZLITE_VERSION}.tar.gz
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
        COMMENT "Creating .tar.gz"
        VERBATIM
    )

    add_custom_target(
        deb
        ln
        -s
        libpwizlite-${PWIZLITE_VERSION}.tar.gz
        libpwizlite_${PWIZLITE_VERSION}.orig.tar.gz
        &&
        cd
        libpwizlite-${PWIZLITE_VERSION}
        &&
        dpkg-buildpackage
        -B
        DEPENDS targz
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
        COMMENT "Creating Debian package"
        VERBATIM
    )

    add_custom_target(
        debsrc
        ln
        -s
        libpwizlite-${PWIZLITE_VERSION}.tar.gz
        libpwizlite_${PWIZLITE_VERSION}.orig.tar.gz
        &&
        cd
        libpwizlite-${PWIZLITE_VERSION}
        &&
        debuild
        -S
        -sa
        DEPENDS targz
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
        COMMENT "Creating Debian package"
        VERBATIM
    )

else()

    message("STATUS: Not including CPack")

endif(NOT NO_CPACK)

#############################################################
###########################
# CREATE THE SOURCE PACKAGE
include(${CMAKE_UTILS_PATH}/targz-source-package-creation.cmake)
message("Including file : ${CMAKE_UTILS_PATH}/targz-source-package-creation.cmake")
message("After file inclusion CPACK_SOURCE_IGNORE_FILES: ${CPACK_SOURCE_IGNORE_FILES}")

add_custom_target(
    archive
    cpack
    -G
    TGZ
    --config
    CPackSourceConfig.cmake
    &&
    mv
    ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}-${PWIZLITE_VERSION}.tar.gz
    ${CMAKE_SOURCE_DIR}/../tarballs
    &&
    ln
    -sf
    ${CMAKE_SOURCE_DIR}/../tarballs/${CMAKE_PROJECT_NAME}-${PWIZLITE_VERSION}.tar.gz
    ${CMAKE_SOURCE_DIR}/../${CMAKE_PROJECT_NAME}_${PWIZLITE_VERSION}.orig.tar.gz
    &&
    ln
    -sf
    ${CMAKE_SOURCE_DIR}/../tarballs/${CMAKE_PROJECT_NAME}-${PWIZLITE_VERSION}.tar.gz
    ${CMAKE_SOURCE_DIR}/../tarballs/${CMAKE_PROJECT_NAME}_${PWIZLITE_VERSION}.orig.tar.gz
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMENT "Creating .tar.gz"
    VERBATIM
)
