message(".==============================================================================.")
message("|           QUANTIS Random Number Generator software by id Quantique           |")
message("|                                                                              |")
message("|         Copyright (C) 2004-2010 id Quantique SA. All Rights Reserved.        |")
message("|                                                                              |")
message("|                                                                              |")
message("|  Redistribution and use in source and binary forms, with or without          |")
message("|  modification, are permitted provided that the following conditions          |")
message("|  are met:                                                                    |")
message("|  1. Redistributions of source code must retain the above copyright           |")
message("|     notice, this list of conditions, and the following disclaimer,           |")
message("|     without modification.                                                    |")
message("|  2. Redistributions in binary form must reproduce the above copyright        |")
message("|     notice, this list of conditions and the following disclaimer in the      |")
message("|     documentation and/or other materials provided with the distribution.     |")
message("|  3. The name of the author may not be used to endorse or promote products    |")
message("|     derived from this software without specific prior written permission.    |")
message("|                                                                              |")
message("|  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND      |")
message("|  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       |")
message("|  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  |")
message("|  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR  |")
message("|  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      |")
message("|  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS     |")
message("|  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)       |")
message("|  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  |")
message("|  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.  |")
message("|                                                                              |")
message("+  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   +")
message("|                                                                              |")
message("|  Alternatively, this software may be distributed under the terms of the      |")
message("|  GNU General Public License version 2 as published by the Free Software      |")
message("|  Foundation.                                                                 |") 
message("|                                                                              |")
message("|  This program is distributed in the hope that it will be useful, but         |")
message("|  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  |")
message("|  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License     |")
message("|  for more details.                                                           |")
message("|                                                                              |")
message("|  You should have received a copy of the GNU General Public License           |")
message("|  along with this program; if not, write to the Free Software                 |")
message("|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA    |")
message("|                                                                              |")
message("+------------------------------------------------------------------------------+")
message("|                                                                              |")
message("| Usage: cmake . [OPTIONS]                                                     |")
message("|                                                                              |")
message("|  OPTIONS:                                                                    |")
message("|                                                                              |")
message("|     -DDISABLE_QUANTIS_PCI=ON            Disable Quantis PCI support. Use     |")
message("|                                         this only if you know what you are   |")
message("|                                         doing!                               |")
message("|                                                                              |")
message("|     -DDISABLE_QUANTIS_USB=ON            Disable Quantis USB support. Use     |")
message("|                                         this only if you know what you are   |")
message("|                                         doing!                               |")
message("|                                                                              |")
message("|     -DDISABLE_QUANTIS_JAVA=ON           Disable Java support. Use this only  |")
message("|                                         if you know what you are doing!      |")
message("|                                                                              |")
message("|     -DCMAKE_INSTALL_PREFIX:PATH=/my_path  Set install directory prefix to    |")
message("|                                           /my_path (default is /usr).        |")
message("|                                                                              |")
message("|     -DCMAKE_BUILD_TYPE=None | Debug | Release | RelWithDebInfo | MinSizeRel  |")
message("|                                                                              |")
message("|     -DCMAKE_VERBOSE_MAKEFILE=ON | OFF   Enable or disable verbose mode.      |")
message("|                                         Verbose can be useful for debugging  |")
message("|                                         and is disabled by default.          |")
message("|                                                                              |")
message("+  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   +")
message("|                                                                              |")
message("| NOTE:                                                                        |")
message("|   Libraries and binaries are compiled using the GNU Compiler Collection.     |")
message("|   It is possible to use use an alternative compiler by overloading the       |")
message("|   CC and CXX variables. For instance you can use the Intel C++ Compiler      |")
message("|   with following command (without the \"\"): \"CC=icc CXX=icpc cmake .\"         |")
message("°==============================================================================°")
message("")


# ########## Project setup ##########
project(QuantisRNG)

cmake_minimum_required(VERSION 2.6.2)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
include(InstallDirs)

message("-------------> '${CMAKE_C_COMPILER}' '${CMAKE_CXX_COMPILER}'")
message("-------------> 'CMAKE_COMPILER_IS_GNUCC' '${CMAKE_COMPILER_IS_GNUCC}'")
message("-------------> 'CMAKE_COMPILER_IS_GNUCXX' '${CMAKE_COMPILER_IS_GNUCXX}'")


# ##### Set FLAGS for GCC/G++ #####
if(CMAKE_COMPILER_IS_GNUCC)
  # Enables warnings for many common errors
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  
  # Warns about a selection of common programming errors, such as functions
  # which can return without a value
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")

  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -pedantic")
endif()

if(CMAKE_COMPILER_IS_GNUCXX)
  # protects applications from stack-smashing attacks
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector -fstack-protector-all")
  
  # Enables warnings for many common errors
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") 

  # Warns about a selection of common programming errors, such as functions
  # which can return without a value
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra")

  # Warns about implicit type conversions that could cause unexpected results,
  # such as conversions between floating-point and integer types, between
  # signed and unsigned types and between types of different width (e.g. long
  # and short integers)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconversion") 

  # Warns about pointers that are cast to remove a type qualifier, such as const
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-qual")

  # Warns when a derived class declares a function that may be an erroneous
  # attempt to define a virtual function
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual")

  # Cause gcc to reject all GNU C extensions, not just those that are
  # incompatible with the ANSI/ISO standard.
  # (do not warn if long long type is used.)
  #
  # "-pedantic" is currently disabled since there are several errors of type
  # "ISO C++ forbids variable-size array name"
  # For help see #http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/compiling_warnings.html#variable%20len%20arrays
  #
  # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ansi -pedantic -Wno-long-long")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ansi")
endif()


# ##### Set FLAGS for INTEL Compiler #####
if("${CMAKE_C_COMPILER}" MATCHES "icc$")
  # Enables warnings for many common errors
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")

  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")

# disable compiler-specific warnings

# Intel compiler:
# 177  - handler parameter was declared but never referenced
# 279  - controlling expression is constant
# 383  - value copied to temporary, reference to temporary used
# 444  - no virtual destructor in base class (used when inheriting from vector)
# 810  - conversion from float to int possible loss of data
# 981  - operands are evaluated in unspecified order
# 1418 - external definition with no prior declaration
# 1469 - cc clobber ignored
# 1476 - field uses tail padding of a base class

#CXXFLAGS         += -wd177,279,383,444,810,869,981,1418,1469,1476

  # Disable compiler-specific warnings:
  #   * 1418 - external definition with no prior declaration
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -wd1418")
endif()


if("${CMAKE_CXX_COMPILER}" MATCHES "icpc$")
  # protects applications from stack-smashing attacks
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
  
  # Enables warnings for many common errors
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") 

  # Warns about implicit type conversions that could cause unexpected results,
  # such as conversions between floating-point and integer types, between
  # signed and unsigned types and between types of different width (e.g. long
  # and short integers)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconversion") 

  # Warns about pointers that are cast to remove a type qualifier, such as const
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-qual")

  # Warns when a derived class declares a function that may be an erroneous
  # attempt to define a virtual function
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual")

  # Cause gcc to reject all GNU C extensions, not just those that are
  # incompatible with the ANSI/ISO standard.
  # (do not warn if long long type is used.)
  #
  # "-pedantic" is currently disabled since there are several errors of type
  # "ISO C++ forbids variable-size array name"
  # For help see #http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/compiling_warnings.html#variable%20len%20arrays
  #
  # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ansi -pedantic -Wno-long-long")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ansi")


  # Disable compiler-specific warnings:
  #   * 177  - handler parameter was declared but never referenced
  #   * 383  - value copied to temporary, reference to temporary used
  #   * 981  - operands are evaluated in unspecified order  
  #   * 1418 - external definition with no prior declarationremark 
  #   * 1419 - external declaration in primary source file
  #   * 2259 - non-pointer conversion from "unsigned int" to "float" may lose
  #            significant bits
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd177,383,981,1418,1419,2259")

# Intel compiler:
# 279  - controlling expression is constant
# 444  - no virtual destructor in base class (used when inheriting from vector)
# 810  - conversion from float to int possible loss of data

# 1469 - cc clobber ignored
# 1476 - field uses tail padding of a base class

#CXXFLAGS         += -wd177,279,383,444,810,869,981,1418,1469,1476

endif()







if(NOT CMAKE_BUILD_TYPE)
  message("-- CMAKE_BUILD_TYPE not set")
  message("--   setting build type to 'Release'")
  set (CMAKE_BUILD_TYPE Release)
endif()

if(${CMAKE_BUILD_TYPE} MATCHES Debug)
  message("-- Debug enabled")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG -D_DEBUG")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -D_DEBUG")
endif()

# ########## Global external libraries ##########

# ---------- Boost libs ----------
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)

# ########## Generates installing packages ##########

# http://www.cmake.org/Wiki/CMake:Packaging_With_CPack
# http://www.cmake.org/Wiki/CMake:CPackConfiguration
# http://www.cmake.org/Wiki/CMake:CPackPackageGenerators

include(InstallRequiredSystemLibraries)


if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  set(PACKAGE_ARCHITECTURE "amd64")
else()
  set(PACKAGE_ARCHITECTURE "i386")
endif()

set(CPACK_PACKAGE_NAME "QuantisRNG")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Quantis Random Number Generator library with EasyQuantis application")
set(CPACK_PACKAGE_VENDOR "IDQ")
#set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/ReadMe.txt")
#set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/Copyright.txt")
set(API_VERSION "2") # API and ABI version
set(CPACK_PACKAGE_VERSION "2.2.0")
set(CPACK_PACKAGE_VERSION_MAJOR "2")
set(CPACK_PACKAGE_VERSION_MINOR "2")
set(CPACK_PACKAGE_VERSION_PATCH "0")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_SYSTEM_NAME}-${PACKAGE_ARCHITECTURE}")
set(CPACK_PACKAGE_CONTACT "support@idquantique.com")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "CMake ${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}")
#if(WIN32 AND NOT UNIX)
#  # There is a bug in NSI that does not handle full unix paths properly. Make
#  # sure there is at least one set of four (4) backlasshes.
#  SET(CPACK_PACKAGE_ICON "${CMake_SOURCE_DIR}/Utilities/Release\\\\InstallIcon.bmp")
#  SET(CPACK_NSIS_INSTALLED_ICON_NAME "bin\\\\MyExecutable.exe")
#  SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY} My Famous Project")
#  SET(CPACK_NSIS_HELP_LINK "http:\\\\\\\\www.my-project-home-page.org")
#  SET(CPACK_NSIS_URL_INFO_ABOUT "http:\\\\\\\\www.my-personal-home-page.com")
#  SET(CPACK_NSIS_CONTACT "me@my-personal-home-page.com")
#  SET(CPACK_NSIS_MODIFY_PATH ON)
#else(WIN32 AND NOT UNIX)
#  SET(CPACK_STRIP_FILES "bin/MyExecutable")
#  SET(CPACK_SOURCE_STRIP_FILES "")
#endif(WIN32 AND NOT UNIX)

# ---------- DEB-Specific ----------
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${PACKAGE_ARCHITECTURE})
#set(CPACK_DEBIAN_PACKAGE_MAINTAINER "") # CPACK_PACKAGE_CONTACT
#set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "") # use CPACK_PACKAGE_DESCRIPTION_SUMMARY
set(CPACK_DEBIAN_PACKAGE_SECTION "devel" )
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libqtcore4 (>= 4.1), libqtgui4 (>= 4.1), libusb-1.0-0 (>=  2:1.0)")


# ---------- RPM-Specific ----------
set(CPACK_RPM_PACKAGE_ARCHITECTURE ${PACKAGE_ARCHITECTURE})
set(CPACK_RPM_PACKAGE_DEPENDS "qt (>= 4.1), libusb1 (>= 1.0")
#set(CPACK_RPM_PACKAGE_DEPENDS "libqtcore4 (>= 4.1), libqtgui4 (>= 4.1), libusb1 (>= 1.0")

# ---------- Build packages ----------
#set(CPACK_GENERATOR "TBZ2;DEB;RPM")
set(CPACK_GENERATOR "TBZ2")

include(CPack)


# ########## Various ##########

# Adds current directory (base src directory) to the include path.
# This way includes like
#    #include "../../MyLibrary/MyHeader.h"
# can be simplified to
#    #include "MyLibrary/MyHeader.h"
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# ########## Includes sub-directories ##########

if(DISABLE_QUANTIS_PCI)
  message("-- Disabling PCI support")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_QUANTIS_PCI")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDISABLE_QUANTIS_PCI")
endif()

if(DISABLE_QUANTIS_USB)
  message("-- Disabling USB support")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_QUANTIS_USB")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDISABLE_QUANTIS_USB")
endif()

if(DISABLE_QUANTIS_JAVA)
  message("-- Disabling Java support")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_QUANTIS_JAVA")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDISABLE_QUANTIS_JAVA")
endif()

add_subdirectory (EasyQuantis)
add_subdirectory (Quantis)

