# ============================================================================ #
#                                CMake options                                 #
# ============================================================================ #

# CMake version
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)

# Be nice to vim users
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

# Include cmake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

include(GenerateExportHeader)
include(ExternalProject)

include(WriteCompilerDetectionHeader)

# Git version
include(${CMAKE_CURRENT_LIST_DIR}/GetGitRevisionDescription.cmake)

# Custom cmake functions
include(${CMAKE_CURRENT_LIST_DIR}/Custom.cmake)

# Compiler settings and options
include(${CMAKE_CURRENT_LIST_DIR}/CompileOptions.cmake)

# Set policies
set_policy(CMP0028 NEW) # ENABLE CMP0028: Double colon in target name means ALIAS or IMPORTED target.
set_policy(CMP0054 NEW) # ENABLE CMP0054: Only interpret if() arguments as variables or keywords when unquoted.
set_policy(CMP0042 NEW) # ENABLE CMP0042: MACOSX_RPATH is enabled by default.
set_policy(CMP0063 NEW) # ENABLE CMP0063: Honor visibility properties for all target types.

# ============================================================================ #
#   Set output paths for binaries and libraries if not provided by the user    #
# ============================================================================ #

# Prohibit in-source builds
if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
    message(FATAL_ERROR "In-source builds are not allowed. Please use "
                        "./configure to choose a build directory and "
                        "initialize the build configuration.")
endif()

# Set archive output path if not defined by user
if("${ARCHIVE_OUTPUT_PATH}" STREQUAL "")
  set(ARCHIVE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/build/lib")
endif()

# Set binary output path if not defined by user
if("${EXECUTABLE_OUTPUT_PATH}" STREQUAL "")
  set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/build/bin")
endif()

# Set library output path if not defined by user
if("${LIBRARY_OUTPUT_PATH}" STREQUAL "")
  set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/build/lib")
endif()

# Set assets output path if not defined by user
if("${ASSETS_OUTPUT_PATH}" STREQUAL "")
  set(ASSETS_OUTPUT_PATH "${CMAKE_BINARY_DIR}/build/assets")
endif()

# Set archive output path if not defined by user
if("${INCLUDE_OUTPUT_PATH}" STREQUAL "")
  set(INCLUDE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/include")
endif()

# Set cmake output path if not defined by user
if("${CMAKE_OUTPUT_PATH}" STREQUAL "")
  set(CMAKE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/cmake")
endif()

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
  # First for the generic no-config case (e.g. with mingw)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/bin)
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
  # Second, for multi-config builds (e.g. msvc)
  foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
      string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
      set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/build/bin)
      set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/build/lib)
      set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/build/lib)
  endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
endif()

# Set runtime path
set(CMAKE_SKIP_BUILD_RPATH            FALSE) # Add absolute path to all dependencies for BUILD
set(CMAKE_BUILD_WITH_INSTALL_RPATH    FALSE) # Use CMAKE_INSTALL_RPATH for INSTALL
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) # Do NOT add path to dependencies for INSTALL
