cmake_minimum_required(VERSION 2.7.8)

project(dot)

if(CMAKE_VERSION VERSION_LESS "2.8.12")
    function(add_compile_options)
        add_definitions(${ARGN})
    endfunction(add_compile_options)
endif()

SET(DOT_USEFMA 0)
if (CMAKE_COMPILER_IS_GNUCC)
    execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
    if (GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7)
        message(STATUS "Version >= 4.7")
        SET(DOT_USEFMA 1)
    endif()
endif()

add_compile_options(-std=c99 -Wall -Wextra -Wno-unused-parameter -Wconversion -Wshadow -pedantic)

add_library(dot src/dot.c)
add_library(dot_naive src/dot_naive.c)
add_library(dot_sse2 src/dot_sse2.c)
add_library(dot_avx src/dot_avx.c)
if(DOT_USEFMA)
    add_library(dot_avxfma src/dot_avx.c)
    target_link_libraries(dot dot_naive dot_sse2 dot_avx dot_avxfma cpuinfo)
else()
    target_link_libraries(dot dot_naive dot_sse2 dot_avx cpuinfo)
endif()

if(CMAKE_VERSION VERSION_LESS "2.8.12")
    if(DOT_USEFMA)
        set_target_properties(dot_avxfma PROPERTIES COMPILE_FLAGS "-mfma -mavx -funroll-loops")
    else()
        set_target_properties(dot PROPERTIES COMPILE_FLAGS "-DDOT_NOFMA")
    endif()
    set_target_properties(dot_avx PROPERTIES COMPILE_FLAGS "-mavx -funroll-loops")
    set_target_properties(dot_sse2 PROPERTIES COMPILE_FLAGS "-msse2")
    include_directories(../cpuinfo/src)
else()
    if(DOT_USEFMA)
        target_compile_options(dot_avxfma PRIVATE -mfma -mavx -funroll-loops)
    else()
        target_compile_definitions(dot PRIVATE "DOT_NOFMA")
    endif()
    target_compile_options(dot_avx PRIVATE -mavx -funroll-loops)
    target_compile_options(dot_sse2 PRIVATE -msse2)
    target_include_directories(dot PRIVATE ../cpuinfo/src)
endif()
