aboutsummaryrefslogtreecommitdiff
path: root/tests/ECMGenerateExportHeaderTest/consumer/testAPI_NO_DEPRECATED.cmake
blob: b01dfc3d607c9f68342094f9cecb4dae5e142687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
if(GROUP_MODE STREQUAL "GROUP_USE_GROUP")
    set(_deprecation_macros_base_name "LIBGROUP")
else()
    set(_deprecation_macros_base_name "LIBRARY")
endif()

function(testAPI code_var_name)
    set(options BUILD_TIME_ONLY_DISABLABLE NO_WARNING)
    set(oneValueArgs DEPRECATED_AT CXX_STANDARD)
    set(multiValueArgs)
    cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if((NOT ARGS_DEPRECATED_AT) OR
       (ARGS_BUILD_TIME_ONLY_DISABLABLE AND ARGS_DEPRECATED_AT VERSION_GREATER DEPRECATED_EXCLUDED_BEFORE_AND_AT))
       set(_build_result_expected TRUE)
    else()
       set(_build_result_expected FALSE)
    endif()

    if (ARGS_CXX_STANDARD)
        if(MSVC)
            set(std_flag  "/std:c++${ARGS_CXX_STANDARD}")
        else()
            set(std_flag "-std=c++${ARGS_CXX_STANDARD}")
        endif()
    else()
        set(std_flag)
    endif()

    set(CMAKE_REQUIRED_FLAGS "${std_flag}")
    set(CMAKE_REQUIRED_LIBRARIES library)
    set(CMAKE_REQUIRED_DEFINITIONS "-D${_deprecation_macros_base_name}_NO_DEPRECATED")

    set(_code "
#include <library.hpp>
int main(int, char**)
{
    ${${code_var_name}}
}
")

    unset(_result CACHE) # clear out as check_cxx_source_compiles caches the result
    check_cxx_source_compiles("${_code}" _result)

    assert_var_bool_value(_result ${_build_result_expected})

    # check warning
    if(_build_result_expected)
        if(ARGS_BUILD_TIME_ONLY_DISABLABLE AND NOT ARGS_NO_WARNING)
            set(_dep_warning_as_error_result_expected FALSE)
        else()
            set(_dep_warning_as_error_result_expected TRUE)
        endif()

        if(MSVC)
            # warning C4996 warns about deprecated declarations
            set(dep_warning_as_error_flag "-we4996")
        else()
            set(dep_warning_as_error_flag "-Werror=deprecated-declarations")
        endif()

        set(CMAKE_REQUIRED_FLAGS "${std_flag} ${dep_warning_as_error_flag}")
        set(CMAKE_REQUIRED_DEFINITIONS) # unset LIBRARY_DISABLE_DEPRECATED_BEFORE_AND_AT, as LIBRARY_DEPRECATED_WARNINGS_SINCE defaults to it
        unset(_dep_warning_result CACHE) # clear out as check_cxx_source_compiles caches the result
        check_cxx_source_compiles("${_code}" _dep_warning_result)
        assert_var_bool_value(_dep_warning_result ${_dep_warning_as_error_result_expected})
    endif()
endfunction()