# Set up OpenMP

# Summary of CMake flags and compile definitions:
#
# CMake flags:
#
# Compile definitions:
#   WITH_OPENMP: whether to enable OpenMP support

message(STATUS "")

# 'omp for simd' requires OpenMP >= 4.0, which is not supported by MSVC.
if(MSVC)
    message(FATAL_ERROR "  OpenMP 4.0 not supported by MSVC")
else()
    find_package(OpenMP 4.0 REQUIRED)
endif()

if(OPENMP_FOUND)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    # Alternatively, set both compile_options and link_options, otherwise executables
    # will fill to link.
    # add_compile_options(${OpenMP_C_FLAGS})
    # add_compile_options(${OpenMP_CXX_FLAGS})
    # add_link_options(${OpenMP_C_FLAGS})
    # add_link_options(${OpenMP_CXX_FLAGS})
endif()

message(STATUS "")
if(OPENMP_FOUND)
    message(STATUS "  OpenMP support enabled")
    # C
    message(STATUS "     OpenMP C version: " ${OpenMP_C_VERSION})
    message(STATUS "     OpenMP C library: ")
    foreach(__lib ${OpenMP_C_LIBRARIES})
        message(STATUS "       " ${__lib})
    endforeach()

    # CXX
    message(STATUS "     OpenMP CXX version: " ${OpenMP_CXX_VERSION})
    message(STATUS "     OpenMP CXX library: ")
    foreach(__lib ${OpenMP_CXX_LIBRARIES})
        message(STATUS "       " ${__lib})
    endforeach()
else()
    message(FATAL_ERROR "  OpenMP support not found")
endif()
message(STATUS "")

# OpenMP is used through the project, so we make it globally accessible.
add_compile_definitions(WITH_OPENMP)
