#FILE(WRITE "${CMAKE_BINARY_DIR}/CMakeDefines.h" "#define CMAKE_BUILD_DIR \"${CMAKE_BINARY_DIR}\"\n")

message(STATUS "Compiler configuration")

INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})
SET(CONFIG_BINARY_DIR ${CMAKE_BINARY_DIR})
IF(NOT CONFIG_DIR)
  SET(CONFIG_DIR ${CMAKE_CURRENT_SOURCE_DIR})
ENDIF(NOT CONFIG_DIR)

# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# set(CMAKE_CONFIG_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/cmake)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_CONFIG_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/cmake)

LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH})
SET(CONFIG_VECTOR_TARGET "AVX2" CACHE STRING "Target Vector ISA on host (SSE,AVX,AVX2, AVX512KNL, AVX512SKX)")
SET_PROPERTY(CACHE CONFIG_VECTOR_TARGET PROPERTY STRINGS NONE SSE AVX AVX2 AVX512KNL AVX512SKX)


include(cmake/macros.cmake)

IF ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  SET(CONFIG_COMPILER ${CMAKE_CXX_COMPILER_ID} CACHE STRING "Clang Compiler?")
  INCLUDE(cmake/clang.cmake)
ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  SET(CONFIG_COMPILER ${CMAKE_CXX_COMPILER_ID} CACHE STRING "GCC Compiler?")
  INCLUDE(cmake/gcc.cmake)
ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
  SET(CONFIG_COMPILER ${CMAKE_CXX_COMPILER_ID} CACHE STRING "Intel Compiler?")
  INCLUDE(cmake/icc.cmake)
ELSE ()
  MESSAGE(FATAL_ERROR "Compiler not supported")
ENDIF()

IF (${CONFIG_VECTOR_TARGET} STREQUAL "AVX2")
  ADD_DEFINITIONS(-DCONFIG_SIMD_WIDTH=8)
  ADD_DEFINITIONS(-DCONFIG_TARGET_AVX2=1)
  SET(CONFIG_ISPC_TARGET "avx2")
ELSEIF (${CONFIG_VECTOR_TARGET} STREQUAL "AVX")
  ADD_DEFINITIONS(-DCONFIG_SIMD_WIDTH=8)
  ADD_DEFINITIONS(-DCONFIG_TARGET_AVX=1)
  SET(CONFIG_ISPC_TARGET "avx")
ELSEIF (${CONFIG_VECTOR_TARGET} STREQUAL "AVX512KNL")
 ADD_DEFINITIONS(-DCONFIG_SIMD_WIDTH=16)
 ADD_DEFINITIONS(-DCONFIG_TARGET_AVX512=1)
 SET(CONFIG_ISPC_TARGET "avx512knl-i32x16")
ELSEIF (${CONFIG_VECTOR_TARGET} STREQUAL "AVX512SKX")
 ADD_DEFINITIONS(-DCONFIG_SIMD_WIDTH=16)
 ADD_DEFINITIONS(-DCONFIG_TARGET_AVX512=1)
 SET(CONFIG_ISPC_TARGET "avx512skx-i32x16")
ELSEIF (${CONFIG_VECTOR_TARGET} STREQUAL "SSE")
  ADD_DEFINITIONS(-DCONFIG_SIMD_WIDTH=4)
  ADD_DEFINITIONS(-DCONFIG_TARGET_SSE=1)
  SET(CONFIG_ISPC_TARGET "sse4")
ELSE()
  ADD_DEFINITIONS(-DCONFIG_SIMD_WIDTH=1)
  ADD_DEFINITIONS(-DCONFIG_TARGET_SINGLE=1)
  SET(CONFIG_ISPC_TARGET "sse4")
  MESSAGE("unknown CONFIG_XEON_TARGET '${CONFIG_VECTOR_TARGET}'")
ENDIF()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CONFIG_COMPILER__FLAGS__${CONFIG_XEON_TARGET}}")

IF (CONFIG_MPI)
  ADD_DEFINITIONS(-DCONFIG_MPI=1)
ENDIF()
#ENDMACRO()
