include(CheckIPOSupported)

function(SetOptimisationSettings)
    if(CMAKE_BUILD_TYPE STREQUAL "Release")
        if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
            check_ipo_supported(RESULT result)
            if(result)
                set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE PARENT_SCOPE)
            endif()
        endif()
    endif()

    if (LINUX)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -mtune=native" PARENT_SCOPE)
    elseif (APPLE)
        add_compile_options("$<$<CONFIG:Release>:-O3>$<$<CONFIG:Debug>:-O0>$<$<CONFIG:MinSizeRel>:-Os>$<$<CONFIG:RelWithDebInfo>:-O2>")
        set(CMAKE_Swift_FLAGS_DEBUG "-Onone" PARENT_SCOPE)
        set(CMAKE_Swift_FLAGS_RELEASE "-O" PARENT_SCOPE)
        set(CMAKE_Swift_FLAGS_MINSIZEREL "-Osize" PARENT_SCOPE)
    endif ()
endfunction()

function(SetWarningSettings TARGET)
    if (LINUX)
        target_compile_options(${TARGET} PRIVATE -Wall -Wno-unknown-pragmas -Werror=return-type)
    elseif (WIN32)
        if (MSVC)
            add_definitions(-D_CRT_SECURE_NO_WARNINGS)
            target_compile_options(${TARGET} PRIVATE "/wd4068;")
            set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "/we4715")
            set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "/ignore:4099")
        endif()
    endif()
endfunction()