CMakeLists.txt 5.9 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6

cmake_policy(SET CMP0057 NEW)

include(CTest)

find_package(Threads REQUIRED)
Paul's avatar
Paul committed
7
8
9
include(ProcessorCount)
ProcessorCount(N)
set(CTEST_PARALLEL_LEVEL ${N} CACHE STRING "CTest parallel level")
Paul's avatar
Paul committed
10
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -j ${CTEST_PARALLEL_LEVEL} -C ${CMAKE_CFG_INTDIR} --timeout 1000)
Paul's avatar
Paul committed
11
12
add_custom_target(tests)

Paul's avatar
Paul committed
13
find_program(MIGRAPH_GDB gdb)
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
16
if(MIGRAPH_GDB)
    set(MIGRAPH_TEST_GDB On CACHE BOOL "")
Paul's avatar
Paul committed
17
else()
Paul's avatar
Paul committed
18
    set(MIGRAPH_TEST_GDB Off CACHE BOOL "")
Paul's avatar
Paul committed
19
endif()
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
set(SKIP_TESTS)

function(add_test_command NAME EXE)
    if(NAME IN_LIST SKIP_TESTS)
        add_test(NAME ${NAME} COMMAND echo skipped)
        set_tests_properties(${NAME} PROPERTIES DISABLED On)
    elseif(WIN32)
        set(WINPATH)
        foreach(PATH ${CMAKE_FIND_ROOT_PATH})
            list(APPEND WINPATH ${PATH}/bin)
        endforeach()
        file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_${NAME}.cmd"
            CONTENT "set PATH=${WINPATH};%PATH%
                    %1 ${ARGN}")
        add_test(NAME ${NAME} COMMAND ${WINE_CMD} cmd /c "${CMAKE_CURRENT_BINARY_DIR}/test_${NAME}.cmd" $<TARGET_FILE:${EXE}>)
    else()
Paul's avatar
Paul committed
37
38
        if(MIGRAPH_TEST_GDB)
            # add_test(NAME ${NAME} COMMAND ${MIGRAPH_GDB} 
Paul's avatar
Paul committed
39
40
41
42
43
44
            #     --batch
            #     --return-child-result
            #     -ex "set disable-randomization off"
            #     -ex run
            #     -ex backtrace
            #     --args $<TARGET_FILE:${EXE}> ${ARGN})
Paul's avatar
Paul committed
45
46
47
            set(TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}/gdb/test_${NAME})
            file(MAKE_DIRECTORY ${TEST_DIR})
            file(GENERATE OUTPUT "${TEST_DIR}/run.cmake"
Paul's avatar
Paul committed
48
                CONTENT "
Paul's avatar
Paul committed
49
50
                # Remove previous core dump
                file(REMOVE ${TEST_DIR}/core)
Paul's avatar
Paul committed
51
                execute_process(COMMAND $<TARGET_FILE:${EXE}> ${ARGN} WORKING_DIRECTORY ${TEST_DIR} RESULT_VARIABLE RESULT)
Paul's avatar
Paul committed
52
53
                if(NOT RESULT EQUAL 0)
                    # TODO: check for core files based on pid when setting /proc/sys/kernel/core_uses_pid
Paul's avatar
Paul committed
54
                    if(EXISTS ${TEST_DIR}/core)
55
56
                        set(\$ENV{UBSAN_OPTIONS} print_stacktrace=1)
                        set(\$ENV{ASAN_OPTIONS} print_stacktrace=1)
Paul's avatar
Paul committed
57
                        execute_process(COMMAND ${MIGRAPH_GDB} $<TARGET_FILE:${EXE}> ${TEST_DIR}/core -batch -ex bt)
Paul's avatar
Paul committed
58
59
60
61
                    endif()
                    message(FATAL_ERROR \"Test failed\")
                endif()
            ")
Paul's avatar
Paul committed
62
            add_test(NAME ${NAME} COMMAND ${CMAKE_COMMAND} -P "${TEST_DIR}/run.cmake")
Paul's avatar
Paul committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        else()
            add_test(NAME ${NAME} COMMAND ${EXE} ${ARGN})
        endif()
    endif()
endfunction()

function(add_test_executable TEST_NAME)
    add_executable (${TEST_NAME} EXCLUDE_FROM_ALL ${ARGN})
    target_link_libraries(${TEST_NAME} ${CMAKE_THREAD_LIBS_INIT})
    # Cmake does not add flags correctly for gcc
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") 
        set_target_properties(${TEST_NAME} PROPERTIES COMPILE_FLAGS -pthread LINK_FLAGS -pthread)
    endif()
    separate_arguments(MIOPEN_TEST_FLAGS_ARGS UNIX_COMMAND ${MIOPEN_TEST_FLAGS})
    if(MIOPEN_TEST_ALL)
        set(TEST_COMMAND ${TEST_NAME} ${MIOPEN_TEST_FLOAT_ARG} --all ${MIOPEN_TEST_FLAGS_ARGS})
    else()
        set(TEST_COMMAND ${TEST_NAME} ${MIOPEN_TEST_FLOAT_ARG} ${MIOPEN_TEST_FLAGS_ARGS})
    endif()
    add_test_command(${TEST_NAME} ${TEST_COMMAND})
    add_dependencies(tests ${TEST_NAME})
    add_dependencies(check ${TEST_NAME})
    set_tests_properties(${TEST_NAME} PROPERTIES FAIL_REGULAR_EXPRESSION "FAILED")
Scott Thornton's avatar
Scott Thornton committed
86
    target_link_libraries(${TEST_NAME} migraph migraph_cpu migraph_onnx)
Paul's avatar
Paul committed
87
    target_include_directories(${TEST_NAME} PUBLIC include)
Paul's avatar
Paul committed
88
89
90
91
92
93
94
endfunction(add_test_executable)

file(GLOB TESTS *.cpp)

foreach(TEST ${TESTS})
    get_filename_component(BASE_NAME ${TEST} NAME_WE)
    add_test_executable(test_${BASE_NAME} ${TEST})
Paul's avatar
Paul committed
95
    rocm_clang_tidy_check(test_${BASE_NAME})
Paul's avatar
Paul committed
96
endforeach()
Paul's avatar
Paul committed
97

Paul's avatar
Paul committed
98
99
100
if(MIGRAPH_ENABLE_GPU)
    # gpu tests
    file(GLOB GPU_TESTS gpu/*.cpp)
Paul's avatar
Paul committed
101

Paul's avatar
Paul committed
102
    foreach(TEST ${GPU_TESTS})
Paul's avatar
Paul committed
103
        get_filename_component(BASE_NAME ${TEST} NAME_WE)
Paul's avatar
Paul committed
104
        add_test_executable(test_gpu_${BASE_NAME} ${TEST})
Paul's avatar
Paul committed
105
        rocm_clang_tidy_check(test_gpu_${BASE_NAME})
Paul's avatar
Paul committed
106
107
108
109
        set_tests_properties(test_gpu_${BASE_NAME} PROPERTIES 
            COST 10 
            RESOURCE_LOCK gpu
        )
Paul's avatar
Paul committed
110
        target_link_libraries(test_gpu_${BASE_NAME} migraph_gpu)
Paul's avatar
Paul committed
111
112
    endforeach()
endif()
Scott Thornton's avatar
Scott Thornton committed
113

Paul's avatar
Paul committed
114
# Onnx test
Scott Thornton's avatar
Scott Thornton committed
115
add_executable(test_onnx onnx/onnx_test.cpp)
Paul's avatar
Paul committed
116
rocm_clang_tidy_check(test_onnx)
Scott Thornton's avatar
Scott Thornton committed
117
118
119
120
121
target_link_libraries(test_onnx migraph_onnx)
target_include_directories(test_onnx PUBLIC include)
add_test(NAME test_onnx COMMAND $<TARGET_FILE:test_onnx> WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/onnx) 
add_dependencies(tests test_onnx)
add_dependencies(check test_onnx)
Paul's avatar
Paul committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

function(test_header NAME HEADER)

    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/header-main-include-${NAME}.cpp 
        "#include <${HEADER}>\nint main() {}\n"
    )
    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/header-static-include-${NAME}.cpp 
        "#include <${HEADER}>\n"
    )
    add_test_executable(${NAME}
        ${CMAKE_CURRENT_BINARY_DIR}/header-main-include-${NAME}.cpp 
        ${CMAKE_CURRENT_BINARY_DIR}/header-static-include-${NAME}.cpp
    )
endfunction()

137
138
139
140
141
142
143
144
145
146
147
148
149
function(test_headers PREFIX)
    file(GLOB HEADERS ${ARGN})

    foreach(HEADER ${HEADERS})
        file(RELATIVE_PATH HEADER_REL ${CMAKE_SOURCE_DIR} ${HEADER})
        string(MAKE_C_IDENTIFIER ${HEADER_REL} TEST_NAME)
        get_filename_component(BASE_NAME ${HEADER} NAME_WE)
        test_header(header_${TEST_NAME} ${PREFIX}/${BASE_NAME}.hpp)
        if(MIGRAPH_ENABLE_GPU)
            target_link_libraries(header_${TEST_NAME} migraph_gpu)
        endif()
    endforeach()
endfunction()
Paul's avatar
Paul committed
150

151
152
153
154
155
test_headers(migraph ${CMAKE_SOURCE_DIR}/src/include/migraph/*.hpp)
test_headers(migraph/cpu ${CMAKE_SOURCE_DIR}/src/targets/cpu/include/migraph/cpu/*.hpp)
if(MIGRAPH_ENABLE_GPU)
test_headers(migraph/gpu ${CMAKE_SOURCE_DIR}/src/targets/gpu/include/migraph/gpu/*.hpp)
endif()