SET(CMAKE_CXX_FLAGS "-Wall -std=c++11")
SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
SET(CMAKE_CXX_FLAGS_MINSIZEREL "-O3 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")

# Some release optimization flags for GCC/Clang.
IF (NOT WIN32)
  # Clang/GCC
  SET(REL_OPTS "-pipe -fvisibility=hidden -fvisibility-inlines-hidden -ffast-math -funroll-loops")

  # GCC only
  IF ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
    SET(REL_OPTS "${REL_OPTS} -fno-implement-inlines")
  ENDIF()

  SET(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${REL_OPTS}")
  SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${REL_OPTS}")
  SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${REL_OPTS}")
ENDIF()

IF ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
  EXECUTE_PROCESS(
    COMMAND
    ${CMAKE_CXX_COMPILER} -dumpversion
    OUTPUT_VARIABLE GCC_VERSION
    )
  IF (NOT (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8))
    MESSAGE(FATAL_ERROR "Requires GCC >= 4.8.")
  ENDIF()
ELSEIF ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  EXECUTE_PROCESS(
    COMMAND
    ${CMAKE_CXX_COMPILER} -dumpversion
    OUTPUT_VARIABLE CLANG_VERSION
    )
  IF (NOT (CLANG_VERSION VERSION_GREATER 3.3 OR GCC_VERSION VERSION_EQUAL 3.3))
    MESSAGE(FATAL_ERROR "Requires Clang >= 3.3.")
  ELSE()
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  ENDIF()
ELSEIF (MSVC AND (MSVC10 OR MSVC11 OR MSVC12))
  # C++11 support is implicitly enabled.
ELSE()
  MESSAGE(FATAL_ERROR "Your compiler does not support C++11 - aborting!")
ENDIF()