include (CheckCXXSourceCompiles)

function (test_visibility_macros EXPORT_VAR_NAME IMPORT_VAR_NAME LOCAL_VAR_NAME)
    check_cxx_source_compiles ("__declspec(dllexport) void foo(void); int main(void) { return 0; }" DLLEXPORT_VISIBILITY)
    check_cxx_source_compiles ("__declspec(dllimport) void foo(void); int main(void) { return 0; }" DLLIMPORT_VISIBILITY)
    check_cxx_source_compiles ("__attribute__((visibility(\"default\"))) void foo(void); int main(void) { return 0; }" DEFAULT_VISIBILITY)
    check_cxx_source_compiles ("__attribute__((visibility(\"hidden\"))) void foo(void); int main(void) { return 0; }"  HIDDEN_VISIBILITY)
    if (DLLEXPORT_VISIBILITY AND DLLIMPORT_VISIBILITY)
        set (${EXPORT_VAR_NAME} "__declspec(dllexport)" PARENT_SCOPE)
        set (${IMPORT_VAR_NAME} "__declspec(dllimport)" PARENT_SCOPE)
        set (${LOCAL_VAR_NAME} "")
    elseif (DEFAULT_VISIBILITY AND HIDDEN_VISIBILITY)
        set (${EXPORT_VAR_NAME} "__attribute__((visibility(\"default\")))" PARENT_SCOPE)
        set (${IMPORT_VAR_NAME} "__attribute__((visibility(\"default\")))" PARENT_SCOPE)
        set (${LOCAL_VAR_NAME}  "__attribute__((visibility(\"hidden\")))" PARENT_SCOPE)
    else ()
        message (FATAL_ERROR "Don't know how to define visibility macros for this compiler")
    endif ()
endfunction ()
