include(CheckCSourceCompiles)

macro(check_attribute_syntax)
    if (NOT ATTRIBUTE_STYLE)
        if (GCC)
            set(LOCAL_ATTRIBUTE_STYLE "GCC")
        elseif (MSVC)
            set(LOCAL_ATTRIBUTE_STYLE "MSVC")
        else ()
            # Default to GCC style
            set(ATTR "__attribute__((noreturn))")
            set(SOURCE_STRING "
#include <stdlib.h>
${ATTR} void func() { exit(1); }
int main() { return 0; }
")
            check_c_source_compiles("${SOURCE_STRING}" GCC_ATTRIBUTE_SYNTAX)
            if (GCC_ATTRIBUTE_SYNTAX)
                set(LOCAL_ATTRIBUTE_STYLE "GCC")
            else ()
                set(ATTR "__declspec(noreturn)")
                set(SOURCE_STRING "
#include <stdlib.h>
${ATTR} void func() { exit(1); }
int main() { return 0; }
")
                check_c_source_compiles("${SOURCE_STRING}" MSVC_ATTRIBUTE_SYNTAX)
                if (MSVC_ATTRIBUTE_SYNTAX)
                    set(LOCAL_ATTRIBUTE_STYLE "MSVC")
                endif ()
            endif ()
        endif ()
        if (NOT LOCAL_ATTRIBUTE_STYLE)
            message(WARNING "Couldn't figure out attribute syntax!")
        endif ()
        set(ATTRIBUTE_STYLE "${LOCAL_ATTRIBUTE_STYLE}" CACHE STRING
                "Attribute syntax to use")
    endif ()
    if ("${ATTRIBUTE_STYLE}" STREQUAL "GCC")
        add_definitions(-DGCC_ATTRIBUTE_SYNTAX)
    elseif (ATTRIBUTE_STYLE STREQUAL "MSVC")
        add_definitions(-DMSVC_ATTRIBUTE_SYNTAX)
    endif ()
endmacro()