CMakeLists.txt 8.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#####################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#####################################################################################
Paul's avatar
Paul committed
24
25
26
27
28
29

cmake_policy(SET CMP0057 NEW)

include(CTest)

find_package(Threads REQUIRED)
Paul's avatar
Paul committed
30
31
32
include(ProcessorCount)
ProcessorCount(N)
set(CTEST_PARALLEL_LEVEL ${N} CACHE STRING "CTest parallel level")
Paul Fultz II's avatar
Paul Fultz II committed
33
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -j ${CTEST_PARALLEL_LEVEL} -C ${CMAKE_CFG_INTDIR} --timeout 5000)
Paul's avatar
Paul committed
34
35
add_custom_target(tests)

Paul's avatar
Paul committed
36
find_program(MIGRAPHX_GDB gdb)
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
39
if(MIGRAPHX_GDB)
    set(MIGRAPHX_TEST_GDB On CACHE BOOL "")
Paul's avatar
Paul committed
40
else()
Paul's avatar
Paul committed
41
    set(MIGRAPHX_TEST_GDB Off CACHE BOOL "")
Paul's avatar
Paul committed
42
endif()
Paul's avatar
Paul committed
43

Paul's avatar
Paul committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
60
61
        if(MIGRAPHX_TEST_GDB)
            # add_test(NAME ${NAME} COMMAND ${MIGRAPHX_GDB} 
Paul's avatar
Paul committed
62
63
64
65
66
67
            #     --batch
            #     --return-child-result
            #     -ex "set disable-randomization off"
            #     -ex run
            #     -ex backtrace
            #     --args $<TARGET_FILE:${EXE}> ${ARGN})
Paul's avatar
Paul committed
68
69
            set(TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}/gdb/test_${NAME})
            file(MAKE_DIRECTORY ${TEST_DIR})
70
71
72
            if (NOT EXISTS ${TEST_DIR})
                message(FATAL_ERROR "Failed to create test directory: ${TEST_DIR}")
            endif()
Paul's avatar
Paul committed
73
            file(GENERATE OUTPUT "${TEST_DIR}/run.cmake"
Paul's avatar
Paul committed
74
                CONTENT "
Paul's avatar
Paul committed
75
76
                # Remove previous core dump
                file(REMOVE ${TEST_DIR}/core)
Paul's avatar
Paul committed
77
                execute_process(COMMAND $<TARGET_FILE:${EXE}> ${ARGN} WORKING_DIRECTORY ${TEST_DIR} RESULT_VARIABLE RESULT)
Paul's avatar
Paul committed
78
79
                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
80
                    if(EXISTS ${TEST_DIR}/core)
81
82
                        set(\$ENV{UBSAN_OPTIONS} print_stacktrace=1)
                        set(\$ENV{ASAN_OPTIONS} print_stacktrace=1)
Paul's avatar
Paul committed
83
                        execute_process(COMMAND ${MIGRAPHX_GDB} $<TARGET_FILE:${EXE}> ${TEST_DIR}/core -batch -ex bt)
Paul's avatar
Paul committed
84
85
86
87
                    endif()
                    message(FATAL_ERROR \"Test failed\")
                endif()
            ")
Paul's avatar
Paul committed
88
            add_test(NAME ${NAME} COMMAND ${CMAKE_COMMAND} -P "${TEST_DIR}/run.cmake")
Paul's avatar
Paul committed
89
90
91
92
        else()
            add_test(NAME ${NAME} COMMAND ${EXE} ${ARGN})
        endif()
    endif()
93
    set_tests_properties(${NAME} PROPERTIES FAIL_REGULAR_EXPRESSION "FAILED")
Paul's avatar
Paul committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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})
112
    target_link_libraries(${TEST_NAME} migraphx migraphx_ref migraphx_onnx)
Paul's avatar
Paul committed
113
    target_include_directories(${TEST_NAME} PUBLIC include)
Paul's avatar
Paul committed
114
115
endfunction(add_test_executable)

116
file(GLOB TESTS ${CONFIGURE_DEPENDS} *.cpp)
Paul's avatar
Paul committed
117
118
119
120

foreach(TEST ${TESTS})
    get_filename_component(BASE_NAME ${TEST} NAME_WE)
    add_test_executable(test_${BASE_NAME} ${TEST})
Paul's avatar
Paul committed
121
    rocm_clang_tidy_check(test_${BASE_NAME})
Paul's avatar
Paul committed
122
endforeach()
Paul's avatar
Paul committed
123

Paul's avatar
Paul committed
124
if(MIGRAPHX_ENABLE_GPU)
Paul's avatar
Paul committed
125
    # gpu tests
126
    file(GLOB GPU_TESTS ${CONFIGURE_DEPENDS} gpu/*.cpp)
Paul's avatar
Paul committed
127

Paul's avatar
Paul committed
128
    foreach(TEST ${GPU_TESTS})
Paul's avatar
Paul committed
129
        get_filename_component(BASE_NAME ${TEST} NAME_WE)
Paul's avatar
Paul committed
130
        add_test_executable(test_gpu_${BASE_NAME} ${TEST})
Paul's avatar
Paul committed
131
        rocm_clang_tidy_check(test_gpu_${BASE_NAME})
Paul's avatar
Paul committed
132
133
134
135
        set_tests_properties(test_gpu_${BASE_NAME} PROPERTIES 
            COST 10 
            RESOURCE_LOCK gpu
        )
Paul's avatar
Paul committed
136
        target_link_libraries(test_gpu_${BASE_NAME} migraphx_gpu)
Paul's avatar
Paul committed
137
138
    endforeach()
endif()
Scott Thornton's avatar
Scott Thornton committed
139

varunsh's avatar
varunsh committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
if(MIGRAPHX_ENABLE_FPGA)
    # fpga tests
    file(GLOB FPGA_TESTS ${CONFIGURE_DEPENDS} fpga/*.cpp)

    foreach(TEST ${FPGA_TESTS})
        get_filename_component(BASE_NAME ${TEST} NAME_WE)
        add_test_executable(test_fpga_${BASE_NAME} ${TEST})
        rocm_clang_tidy_check(test_fpga_${BASE_NAME})
        set_tests_properties(test_fpga_${BASE_NAME} PROPERTIES 
            COST 10 
            RESOURCE_LOCK fpga
        )
        target_link_libraries(test_fpga_${BASE_NAME} migraphx_fpga)
    endforeach()
endif()

Paul's avatar
Paul committed
156
# Onnx test
Paul's avatar
Paul committed
157
set(TEST_ONNX_DIR ${CMAKE_CURRENT_SOURCE_DIR}/onnx)
158
159
160
file (GLOB ONNX_TESTS ${TEST_ONNX_DIR}/*.cpp)
foreach(ONNX_TEST ${ONNX_TESTS})
    get_filename_component(BASE_NAME ${ONNX_TEST} NAME_WE)
161
    set(TEST_NAME test_${BASE_NAME})
162
    add_executable(${TEST_NAME} ${ONNX_TEST})
163
    rocm_clang_tidy_check(${TEST_NAME})
164
    target_link_libraries(${TEST_NAME} migraphx_onnx migraphx_ref)
165
    target_include_directories(${TEST_NAME} PUBLIC include)
kahmed10's avatar
kahmed10 committed
166
    add_test(NAME ${TEST_NAME} COMMAND $<TARGET_FILE:${TEST_NAME}> WORKING_DIRECTORY ${TEST_ONNX_DIR}) 
167
168
    add_dependencies(tests ${TEST_NAME})
    add_dependencies(check ${TEST_NAME})
169
endforeach()
Paul's avatar
Paul committed
170

171
# tf test
kahmed10's avatar
kahmed10 committed
172
set(TEST_TF_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tf)
173
174
add_executable(test_tf tf/tf_test.cpp)
rocm_clang_tidy_check(test_tf)
175
target_link_libraries(test_tf migraphx_tf migraphx_ref)
176
target_include_directories(test_tf PUBLIC include)
kahmed10's avatar
kahmed10 committed
177
add_test(NAME test_tf COMMAND $<TARGET_FILE:test_tf> WORKING_DIRECTORY ${TEST_TF_DIR}) 
178
179
180
add_dependencies(tests test_tf)
add_dependencies(check test_tf)

Paul Fultz II's avatar
Paul Fultz II committed
181
add_subdirectory(api)
182
add_subdirectory(verify)
183
if(MIGRAPHX_ENABLE_PYTHON)
Paul's avatar
Paul committed
184
add_subdirectory(py)
185
endif()
Paul's avatar
Paul committed
186

Paul's avatar
Paul committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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()

201
function(test_headers PREFIX)
202
    file(GLOB HEADERS ${CONFIGURE_DEPENDS} ${ARGN})
203
204
205
206
207
208

    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)
Paul's avatar
Paul committed
209
        if(MIGRAPHX_ENABLE_GPU)
Paul's avatar
Paul committed
210
            target_link_libraries(header_${TEST_NAME} migraphx_gpu)
211
212
213
        endif()
    endforeach()
endfunction()
Paul's avatar
Paul committed
214

Paul's avatar
Paul committed
215
test_headers(migraphx ${CMAKE_SOURCE_DIR}/src/include/migraphx/*.hpp)
216
test_headers(migraphx/ref ${CMAKE_SOURCE_DIR}/src/targets/ref/include/migraphx/ref/*.hpp)
Paul's avatar
Paul committed
217
if(MIGRAPHX_ENABLE_GPU)
Paul's avatar
Paul committed
218
test_headers(migraphx/gpu ${CMAKE_SOURCE_DIR}/src/targets/gpu/include/migraphx/gpu/*.hpp)
219
endif()