# Set up compilers

# Summary of CMake flags and compile definitions:
#
# CMake flags for compiler vendors:
#   TBPLAS_USE_MSVC: MSVC compiler
#   TBPLAS_USE_INTEL: Intel compiler
#   TBPLAS_USE_CLANG: clang/llvm compiler
#   TBPLAS_USE_GCC: GNU compiler
#   TBPLAS_USE_OTHER_COMPILER: other compiler

# Duplicate with 
#   project("tbplas" LANGUAGES CXX C)
# in main CMake file, to make sure that this command and
#   include(cmake/TBPLASDetectCompiler.cmake)
# can appear in any order.
enable_language(CXX)
enable_language(C)

# Reset all flags
# Used through the build system. DO NOT delete them.
set(TBPLAS_USE_MSVC OFF)
set(TBPLAS_USE_INTEL OFF)
set(TBPLAS_USE_CLANG OFF)
set(TBPLAS_USE_GCC OFF)
set(TBPLAS_USE_OTHER_COMPILER OFF)

# Check for user-specified compiler.
#
# If this file is included after the project() command, then CMAKE_CXX_COMPILER
# will have the full path to the compiler, e.g., /usr/bin/g++. So we must match
# the tail of compiler name rather than the whole word. e.g., "g\\+\\+$" instead
# of "^g\\+\\+$".
#
# If you have optimization flags to add, DO NOT add them here.
if(MSVC)
    set(TBPLAS_USE_MSVC ON)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
    message(STATUS "Using MSVC runtime library: ${CMAKE_MSVC_RUNTIME_LIBRARY}")
    if(WITH_CUDA)
        message(STATUS "Using CUDA with MSVC runtime library: ${CMAKE_MSVC_RUNTIME_LIBRARY}")
        # Set the runtime library to /MD for CUDA compilation
        # This is required for compatibility with the CUDA compiler
        # and to avoid linker errors.
        set(CUDA_NVCC_FLAGS " ${CUDA_NVCC_FLAGS} -Xcompiler=/MD$<$<CONFIG:Debug>:d>")
    endif()
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "IntelLLVM")
    set(TBPLAS_USE_INTEL ON)
    find_program(_cxx_executable icpx)
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Intel")
    set(TBPLAS_USE_INTEL ON)
    find_program(_cxx_executable icpc)
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
    set(TBPLAS_USE_CLANG ON)
    find_program(_cxx_executable clang++)
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
    set(TBPLAS_USE_GCC ON)
    find_program(_cxx_executable g++)
else()
    set(TBPLAS_USE_OTHER_COMPILER ON)
    find_program(_cxx_executable ${CMAKE_CXX_COMPILER})
endif()

# Echo status
if((NOT MSVC) AND (NOT _cxx_executable))
    message(FATAL_ERROR "C++ compiler: ${CMAKE_CXX_COMPILER} found.")
endif()
unset(_cxx_executable)

# Set compiler options
set(CMAKE_CXX_STANDARD 17)  
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Python extensions require position-independent code
if(UNIX)
    add_compile_options(-fPIC)
endif()

# Turn on aggressive optimizations in release mode
# Add your optimization flags here.
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
   if(TBPLAS_USE_INTEL)
       add_compile_options(-xHost)
   else()
       add_compile_options(-mavx2)
   endif()
endif()

# Settings below are no-longer in use will be removed soon.

# # ----------------------------------------------------------------------------
# # Detect Intel ICXC compiler
# # ----------------------------------------------------------------------------
# if(UNIX)
#     if(__INTEL_COMPILER)
#         set(TB_ICX __INTEL_LLVM_COMPILER)
#     elseif(CMAKE_C_COMPILER MATCHES "icx")
#         set(TB_ICX icx_matches_c_compiler)
#     elseif(CMAKE_CXX_COMPILER MATCHES "icpx")
#         set(TB_ICX icpx_matches_cxx_compiler)
#     endif()
# endif()

# if(MSVC AND CMAKE_CXX_COMPILER MATCHES ".*(dpcpp-cl|dpcpp|icx-cl|icpx|icx)(.exe)?$")
#     set(TB_ICX __INTEL_LLVM_COMPILER_WINDOWS)
# endif()

# # Similar code exists in TBPLASConfig.cmake
# if(NOT DEFINED TBPLAS_STATIC)
#   # look for global setting
#   if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS)
#     set(TBPLAS_STATIC OFF)
#   else()
#     set(TBPLAS_STATIC ON)
#   endif()
# endif()
