#############################################################
#############################################################
# CMake configuration
cmake_minimum_required(VERSION 3.18.4)

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

project(libmassgui
  DESCRIPTION "A GUI library with useful objects."
  HOMEPAGE_URL "http://wwww.msxpertsuite.org/libmassgui")

set(LOWCASE_PROJECT_NAME ${CMAKE_PROJECT_NAME})

set(VERSION 10.0.0)

# Let the build system know that we are configuring a build of
# the library in standalone mode (in embedded mode this file is not used)

set(STANDALONE_BUILD_MODE 1)

# Set additional project information
set(COMPANY "msXpertSuite.org")
set(COPYRIGHT "Copyright (c) 2008-2024 Filippo Rusconi. Licensed under GPLv3+")
set(IDENTIFIER "org.msxpertsuite")


include(GNUInstallDirs)

set(HOME_DEVEL_DIR $ENV{HOME}/devel)
message("\n${BoldRed}The devel directory where all the development projects
should reside: ${HOME_DEVEL_DIR}.${ColourReset}\n")


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

# Include the system's uname that fills in SYSTEM_UNAME_S.
# Sets WIN32 if SYSTEM_UNAME_S is "^.*MING64.*"
# Note that WIN32 is set even on 64 bits systems.
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 ${CMAKE_PROJECT_NAME}${ColourReset}\n")

# This export will allow using the flags to be used by
# youcompleteme (vim plugin) and by the Clang-based code analyzer..
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 "${BoldGreen}CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}${ColourReset}")

#############################################################
# We do not want warnings for unknown pragmas:
message(STATUS "${BoldGreen}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 -pedantic.${ColourReset}")
add_definitions(-Wall -pedantic)
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("${BoldGreen}CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}${ColourReset}")


#############################################################
#############################################################
# Platform-specific CMake configuration

# This does not apply to this library, but we keep that
# configuration structure in case it will be necessary in the future.
if(WIN32 OR _WIN32)

  if(MXE)

    # Run the following cmake command line:
    # x86_64-w64-mingw32.shared-cmake -DCMAKE_BUILD_TYPE=Release -DMXE=1 ../../development
    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(WIN10MINGW64)
    include(${CMAKE_TOOLCHAINS_PATH}/win10-mingw64-toolchain.cmake)
  endif()

elseif(UNIX)

  #message("UNIX")

  # On UNIX, the libpappsomspp and libpappsomspp-widget libs
  # need to be installed and they ship the PappsoMSppConfig.cmake
  # config file.

  # However, when developing both libs and this program, we need to be able to
  # load the libs from their dev/build directory without relying on the system
  # libs (add -DLOCAL_DEV=1).
  if(LOCAL_DEV)
    # The development should use the locally built libs.
    message("Using the local libpappsomspp libraries.")
    include(${CMAKE_TOOLCHAINS_PATH}/unix-toolchain-local.cmake)
  else()
    # The development should use the system-wide libs.
    include(${CMAKE_TOOLCHAINS_PATH}/unix-toolchain.cmake)

  endif()

  message(STATUS "Include dir for PappsoMSpp is at: ${PappsoMSpp_INCLUDE_DIRS}")
  message(STATUS "PappsoMSpp and PappsoMSppWidget were found:
  \t${PappsoMSpp_LIBRARIES} and ${PappsoMSppWidget_LIBRARIES}")

endif()

message("")
message(STATUS "${BoldGreen}Starting configuration of ${CMAKE_PROJECT_NAME}${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.")
  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}")


if(PROFILE)
  message(STATUS "${BoldYellow}Profiling is requested, adding -pg flag.${ColourReset}")
  add_definitions(-pg)
endif()

message(STATUS "${BoldYellow}Main CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}${ColourReset}")
message(STATUS "${BoldYellow}CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}${ColourReset}")

#############################################################
##################
# BUILD OF LIBMASSGUI

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


#############################################################
####################
# BUILD OF THE TESTS

if(BUILD_TESTS)

message(STATUS "${BoldGreen} Build the tests ${ColourReset}")
message(STATUS "Adding subdirectory tests for PROJECT: ${CMAKE_PROJECT_NAME}")
add_subdirectory(tests)

endif()


#############################################################
# summary
message("")
message(STATUS "${BoldRed}~~~~~~~~~~~~~~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~~~~~~~~~~~~~~${ColourReset}")
message(STATUS "${BoldRed}   General configuration of the ${CMAKE_PROJECT_NAME} project ${ColourReset}")
message("")

message(STATUS "${BoldYellow}System is detected by CMake as ${SYSTEM_UNAME_S}${ColourReset}")
message(STATUS "${BoldYellow}CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}${ColourReset}")
message(STATUS "${BoldYellow}CMAKE_CXX_STANDARD: ${CMAKE_CXX_STANDARD}${ColourReset}")

message("")

if(PROFILE)
  message(STATUS "${BoldYellow}Profiling is requested (-DPROFILE=1)${ColourReset}")
else()
  message(STATUS "${BoldYellow}Profiling is NOT requested (-DPROFILE=0)${ColourReset}")
endif()

if(WARNASERR)
  message(STATUS "${BoldYellow}Warnings ARE treated as errors (-DWARNASERR=1)${ColourReset}")
else()
  message(STATUS "${BoldYellow}Warnings NOT treated as errors (-DWARNASERR=0)${ColourReset}")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  message(STATUS "${BoldYellow}Debug support IS requested.${ColourReset}")
else()
  message(STATUS "${BoldYellow}Debug support NOT requested.${ColourReset}")
endif()

message("")
message(STATUS "${BoldYellow}The typical cmake invocation on Debian GNU/Linux would be: mkdir build; cd build; cmake -DCMAKE_BUILD_TYPE=Debug ../${ColourReset}")
message(STATUS "${BoldYellow}The typical cmake invocation on win10-mingw64 would be: mkdir build; cd build; cmake -DCMAKE_BUILD_TYPE=Release  ../${ColourReset}")
message(STATUS "${BoldYellow}The typical cmake invocation on MXE would be: mkdir build; cd build; x86_64-w64-mingw32.shared-cmake -DCMAKE_BUILD_TYPE=Release -DMXE=1 ../../development${ColourReset}")
message("")

message("")
message(STATUS "${BoldRed}~~~~~~~~~~~~~~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~~~~~~~~~~~~~~${ColourReset}")
message("")
