# Author: Olivier Langella
# Created: 06/10/2018

# Global parameters
cmake_minimum_required(VERSION 3.0)
project(specoms CXX)


SET(SOFTWARE_NAME "SpecOMS")
SET(DEEPPROT_VERSION_MAJOR "0")
SET(DEEPPROT_VERSION_MINOR "0")
SET(DEEPPROT_VERSION_PATCH "25")
SET(VERSION "${DEEPPROT_VERSION_MAJOR}.${DEEPPROT_VERSION_MINOR}.${DEEPPROT_VERSION_PATCH}")


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

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)


# We want C++17
set(CMAKE_CXX_STANDARD 20)
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 "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}")


#############################################################
#############################################################
# Platform-specific CMake configuration
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)
	include(${CMAKE_TOOLCHAINS_PATH}/mxe-toolchain-olivier.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(systemUname "mxe")

elseif(UNIX AND NOT APPLE)
	# Run the following cmake command line:
	# cmake -DBUILD_USER_MANUAL=1 -DCMAKE_BUILD_TYPE=Debug

  if(LOCAL_DEV)

    # The development should use the locally built libs.
    #include(${CMAKE_TOOLCHAINS_PATH}/unix-toolchain-olivier.cmake)
    include(${CMAKE_TOOLCHAINS_PATH}/unix-toolchain-local.cmake)
  else()

    # Use the system-wide libraries.
    include(${CMAKE_TOOLCHAINS_PATH}/unix-toolchain.cmake)

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

elseif(WIN64)
	# Run the following cmake command line:
	# cmake -DBUILD_USER_MANUAL=0 -DCMAKE_BUILD_TYPE=Release ../development

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

elseif(APPLE)
	# Run the following cmake command line:
	# cmake -DBUILD_USER_MANUAL=0 -DCMAKE_BUILD_TYPE=Release ../development

	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: ${SystemUname}${ColourReset}")
message("")



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

# Gee, do not set that to default!
# set(MAKE_TEST 1)

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 with MAKE_TEST: ${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}")


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


# Subdirectories
#ADD_SUBDIRECTORY(doc)
ADD_SUBDIRECTORY(src)

if(JAVABUILD)
	ADD_SUBDIRECTORY(java)
endif(JAVABUILD)

# Packager
SET(CPACK_PACKAGE_NAME "specoms")
SET(CPACK_PACKAGE_VENDOR "SpecOMS Development Team")
SET(CPACK_PACKAGE_VERSION "${VERSION}")
SET(CPACK_PACKAGE_VERSION_MAJOR "${DEEPPROT_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR "${DEEPPROT_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${DEEPPROT_VERSION_PATCH}")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "SpecOMS")
#SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")
#SET(CPACK_RESOURCE_FILE_AUTHORS "${CMAKE_SOURCE_DIR}/AUTHORS")
#SET(CPACK_RESOURCE_FILE_INSTALL "${CMAKE_SOURCE_DIR}/INSTALL")
SET(CPACK_SOURCE_GENERATOR "TGZ")
SET(CPACK_SOURCE_IGNORE_FILES
 "CMakeFiles"
 "Makefile"
 "_CPack_Packages"
 "CMakeCache.txt"
 ".*\\\\.git"
 ".*\\\\.gz"
 ".*\\\\.deb"
 ".*\\\\.rpm"
 ".*\\\\.dmg"
 ".*\\\\..*\\\\.swp"
 "src/\\\\..*"
 "src/libbpp*"
 "debian/tmp"
 "debian/libbpp.*/"
 "debian/libbpp.*\\\\.so.*"
 "debian/libbpp.*\\\\.a"
 "debian/libbpp.*\\\\.substvars"
 "debian/libbpp.*\\\\.debhelper"
 "debian/debhelper\\\\.log"
 "html"
 "Core.tag"
 "Testing"
 "build/"
 "cbuild/"
 "wbuild/"
 "buildwin64/"
 "build-stamp"
 "install_manifest.txt"
 "DartConfiguration.tcl"
 ${CPACK_SOURCE_IGNORE_FILES}
)



#dch -Dstretch "message"
#cmake ..
#make deb
# scp specoms* proteus:/var/www/apt/incoming
# reprepro -Vb /var/www/apt processincoming default
# apt-get remove specoms*
#
#  debuild -S -sa
# dput -f olivier-langella *changes


#dpkg-deb -x libpappsomspp-qt4_0.0.4-1_amd64.deb libpappsomspp-qt4_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt4 -Plibpappsomspp-qt4_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt4.symbols


#dpkg-deb -x libpappsomspp-qt5_0.0.4-1_amd64.deb libpappsomspp-qt5_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt5 -Plibpappsomspp-qt5_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt5.symbols


SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
SET(CPACK_DEBSOURCE_PACKAGE_FILE_NAME "lib${CMAKE_PROJECT_NAME}_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}.orig")
INCLUDE(CPack)

IF (UNIX)
#This creates deb packages:



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


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


	add_custom_target(debsrc
		ln -s specoms-${VERSION}.tar.gz specoms-${VERSION}.orig.tar.gz && cd specoms-${VERSION} && debuild -S -sa
		DEPENDS targz
		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
		COMMENT "Creating Debian package" VERBATIM
	)
	
	file(GLOB changesdebs "${CMAKE_BINARY_DIR}/pappsomspp_*.dsc")

	foreach(libfile ${changesdebs})
	    SET(changesdeb "${libfile}")
	endforeach(libfile)

	message ("changes debian file : ${changesdeb}")
	#lintian -IEi --pedantic tandem-mass_2013.06.15-1_amd64.changes
	add_custom_target(lintian
		lintian -IEi --pedantic ${changesdeb}
		DEPENDS deb
		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
		COMMENT "lintian check" VERBATIM
	)
	
	
	
#dpkg-deb -x libpappsomspp-qt4_0.0.4-1_amd64.deb libpappsomspp-qt4_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt4 -Plibpappsomspp-qt4_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt4.symbols


#dpkg-deb -x libpappsomspp-qt5_0.0.4-1_amd64.deb libpappsomspp-qt5_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt5 -Plibpappsomspp-qt5_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt5.symbols

  # Conditional addition of the test directory.

ENDIF(UNIX)


if(MAKE_TESTS)

  #message("MAKE_TESTS was set to ${MAKE_TESTS}")

  enable_testing()

  include(CTest)

  if(BUILD_TESTING)

    add_subdirectory(tests)

  endif(BUILD_TESTING)

endif(MAKE_TESTS)

configure_file (${CMAKE_SOURCE_DIR}/win64/specoms.iss.cmake ${CMAKE_SOURCE_DIR}/win64/specoms.iss)


INSTALL(PROGRAMS ${CMAKE_BINARY_DIR}/src/specoms DESTINATION bin)
