#
# CMakeLists.txt
# Top level CMake makefile.
#

#
# This file is part of awisetoolbox.
# Copyright (C) 2008-2017 by Alan Wise (awisesoftware@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


#
# Configuration
#

# CMake minimum version.
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

# Load user preferences.
INCLUDE("${CMAKE_SOURCE_DIR}/preferences.cmake" OPTIONAL)

# Default to Release build type. This needs to be before PROJECT().
IF(NOT DEFINED CMAKE_BUILD_TYPE)
  MESSAGE(WARNING "\nBuild type not set. Defaulting to \"Release\".")
  SET(CMAKE_BUILD_TYPE Release CACHE STRING
      "Build type unset. Forced to \"Release\"." FORCE)
ENDIF()

# Options
INCLUDE(CMakeDependentOption)
OPTION(OPTION_AWISETOOLBOX_USEDEBUGLOG "Enable debug logging." OFF)
OPTION(OPTION_AWISETOOLBOX_USEMESSAGELOG "Enable message logging." OFF)
OPTION(OPTION_AWISETOOLBOX_USEDEBUGTOOLS "Enable debugging tools." OFF)
CMAKE_DEPENDENT_OPTION(OPTION_AWISETOOLBOX_USECCACHE "Enable ccache." OFF UNIX OFF)

# Project name.
PROJECT("A.Wise Toolbox")

# Project information.
SET(AWISETOOLBOX_DISPLAYNAME "${PROJECT_NAME}")
STRING(TOLOWER "${PROJECT_NAME}" PROJECT_NAMELC)
STRING(REPLACE " " "" PROJECT_NAMELCNS "${PROJECT_NAMELC}")
STRING(REPLACE "." "" PROJECT_NAMELCNSNP "${PROJECT_NAMELCNS}")
SET(AWISETOOLBOX_BASENAME ${PROJECT_NAMELCNSNP})
SET(AWISETOOLBOX_MAJORVERSION "0")        # Overridable.
SET(AWISETOOLBOX_MINORVERSION "3")        # Overridable.
SET(AWISETOOLBOX_PATCHVERSION "0")        # Overridable.
SET(AWISETOOLBOX_DESCRIPTION              # Overridable.
    "A collection of code and data which can be used on multiple projects.")
STRING(REPLACE ";" "" AWISETOOLBOX_DESCRIPTION "${AWISETOOLBOX_DESCRIPTION}")
SET(AWISETOOLBOX_OWNERNAME "Alan Wise")
SET(AWISETOOLBOX_COPYRIGHTNOTICE "Copyright (C) 2008-2017 ${AWISETOOLBOX_OWNERNAME}.")
SET(AWISETOOLBOX_WEBSITE "https://gitlab.com/alanwise/awisetoolbox/")
SET(AWISETOOLBOX_LICENSE
    "License GPLv3: GNU GPL version 3 or later <http://www.gnu.org/licenses/>.")

# Additional compiler options and settings.
IF(${CMAKE_C_COMPILER_ID} STREQUAL GNU)
  SET(G_DEFINEPREFIX -D)
  ADD_DEFINITIONS(${G_DEFINEPREFIX}_GNU_SOURCE)
      # Need this define for certain prototypes.
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \
      -ansi -Wall -Wextra -Wcast-qual -Wconversion -Wformat=2 -Wshadow \
      -Wstrict-overflow=5 -Wunused -std=c99")
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
      -ansi -Wall -Wextra -Wcast-qual -Wconversion -Wformat=2 -Wshadow \
      -Wstrict-overflow=5 -Wunused")
  IF(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  ENDIF()
ELSEIF(${CMAKE_C_COMPILER_ID} STREQUAL MSVC)
  SET(G_DEFINEPREFIX /D)
  ADD_DEFINITIONS(
      ${G_DEFINEPREFIX}_CRT_SECURE_NO_WARNINGS
      ${G_DEFINEPREFIX}_CRT_NONSTDC_NO_DEPRECATE)
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Wall /wd4710 /wd4711 /wd4820")
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall /wd4710 /wd4711 /wd4820")
      # 4710/4711 - Don't care that a function isn't/is being inlined.
      # 4820 - Don't care about padding.
ELSE()
  MESSAGE(FATAL_ERROR
      "Unknown compiler configuration. CMakeLists.txt needs to be updated!")
ENDIF()

# Build environment.
IF(NOT UNIX AND NOT WIN32)
  MESSAGE(FATAL_ERROR
      "Unknown build configuration. CMakeLists.txt needs to be updated!")
ENDIF()

# If debug mode, set debug definition.
STRING(TOLOWER "${CMAKE_BUILD_TYPE}" BUILDTYPELC)
IF("${BUILDTYPELC}" STREQUAL "debug")
  ADD_DEFINITIONS(${G_DEFINEPREFIX}DEBUG)
ENDIF()

# Enable debug log, message log, and debug tools.
REMOVE_DEFINITIONS(
    ${G_DEFINEPREFIX}USE_DEBUGLOG
    ${G_DEFINEPREFIX}USE_MESSAGELOG
    ${G_DEFINEPREFIX}USE_DEBUGTOOLS)  # Remove the options set in other projects.
IF(OPTION_AWISETOOLBOX_USEDEBUGLOG)
  ADD_DEFINITIONS(${G_DEFINEPREFIX}USE_DEBUGLOG)
  SET(DOXYGEN_PREDEFINED "${DOXYGEN_PREDEFINED} USE_DEBUGLOG")
ENDIF()
IF(OPTION_AWISETOOLBOX_USEMESSAGELOG)
  ADD_DEFINITIONS(${G_DEFINEPREFIX}USE_MESSAGELOG)
  SET(DOXYGEN_PREDEFINED "${DOXYGEN_PREDEFINED} USE_MESSAGELOG")
ENDIF()
IF(OPTION_AWISETOOLBOX_USEDEBUGTOOLS)
  ADD_DEFINITIONS(${G_DEFINEPREFIX}USE_DEBUGTOOLS)
  SET(DOXYGEN_PREDEFINED "${DOXYGEN_PREDEFINED} USE_DEBUGTOOLS")
ENDIF()

# ccache.
IF(OPTION_AWISETOOLBOX_USECCACHE)
  # Find ccache if not previously found.
  IF(NOT CCACHE_EXECUTABLE)
    FIND_PROGRAM(CCACHE_EXECUTABLE ccache)
    MARK_AS_ADVANCED(CCACHE_EXECUTABLE)
  ENDIF()
  # Use ccache on awisetoolbox directories only.
  IF(CCACHE_EXECUTABLE)
    SET_PROPERTY(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_EXECUTABLE}")
    SET_PROPERTY(DIRECTORY PROPERTY RULE_LAUNCH_LINK "${CCACHE_EXECUTABLE}")
  ENDIF()
ENDIF()

# If release mode, set build release flag.
IF("${BUILDTYPELC}" STREQUAL "release")
  SET(G_RELEASEFLAG ON)
ELSE()
  SET(G_RELEASEFLAG OFF)
ENDIF()


#
# Include paths
#


#
# Sources
#


#
# Binaries
#


#
# Subdirectories
#

ADD_SUBDIRECTORY("generic/")
ADD_SUBDIRECTORY("qt/")


#
# Installation
#


#
# CMakeLists.txt
#
