CMakeLists.txt 8.43 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

cmake_policy(SET CMP0057 NEW)

Paul's avatar
Paul committed
27
include(ROCMTest)
Paul's avatar
Paul committed
28

Paul's avatar
Paul committed
29
find_program(MIGRAPHX_GDB gdb)
Paul's avatar
Paul committed
30

Paul's avatar
Paul committed
31
32
if(MIGRAPHX_GDB)
    set(MIGRAPHX_TEST_GDB On CACHE BOOL "")
Paul's avatar
Paul committed
33
else()
Paul's avatar
Paul committed
34
    set(MIGRAPHX_TEST_GDB Off CACHE BOOL "")
Paul's avatar
Paul committed
35
endif()
Paul's avatar
Paul committed
36

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

Paul's avatar
Paul committed
109
110
111
rocm_test_link_libraries(migraphx migraphx_ref migraphx_onnx migraphx_tf)
rocm_test_include_directories(include)

112
file(GLOB TESTS ${CONFIGURE_DEPENDS} *.cpp)
Paul's avatar
Paul committed
113
114
115

foreach(TEST ${TESTS})
    get_filename_component(BASE_NAME ${TEST} NAME_WE)
Paul's avatar
Paul committed
116
    rocm_add_test_executable(test_${BASE_NAME} ${TEST})
Paul's avatar
Paul committed
117
    rocm_clang_tidy_check(test_${BASE_NAME})
Paul's avatar
Paul committed
118
endforeach()
Paul's avatar
Paul committed
119

Paul's avatar
Paul committed
120
if(MIGRAPHX_ENABLE_GPU)
Paul's avatar
Paul committed
121
    # gpu tests
122
    file(GLOB GPU_TESTS ${CONFIGURE_DEPENDS} gpu/*.cpp)
Paul's avatar
Paul committed
123

Paul's avatar
Paul committed
124
    foreach(TEST ${GPU_TESTS})
Paul's avatar
Paul committed
125
        get_filename_component(BASE_NAME ${TEST} NAME_WE)
Paul's avatar
Paul committed
126
        rocm_add_test_executable(test_gpu_${BASE_NAME} ${TEST})
Paul's avatar
Paul committed
127
        rocm_clang_tidy_check(test_gpu_${BASE_NAME})
Paul's avatar
Paul committed
128
129
130
131
        set_tests_properties(test_gpu_${BASE_NAME} PROPERTIES 
            COST 10 
            RESOURCE_LOCK gpu
        )
Paul's avatar
Paul committed
132
        target_link_libraries(test_gpu_${BASE_NAME} migraphx_gpu)
Paul's avatar
Paul committed
133
134
    endforeach()
endif()
Scott Thornton's avatar
Scott Thornton committed
135

varunsh's avatar
varunsh committed
136
137
138
139
140
141
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)
Paul's avatar
Paul committed
142
        rocm_add_test_executable(test_fpga_${BASE_NAME} ${TEST})
varunsh's avatar
varunsh committed
143
144
145
146
147
148
149
150
151
        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
152
# Onnx test
Paul's avatar
Paul committed
153
set(TEST_ONNX_DIR ${CMAKE_CURRENT_SOURCE_DIR}/onnx)
154
155
156
file (GLOB ONNX_TESTS ${TEST_ONNX_DIR}/*.cpp)
foreach(ONNX_TEST ${ONNX_TESTS})
    get_filename_component(BASE_NAME ${ONNX_TEST} NAME_WE)
157
    set(TEST_NAME test_${BASE_NAME})
158
    add_executable(${TEST_NAME} ${ONNX_TEST})
159
    rocm_clang_tidy_check(${TEST_NAME})
160
    target_link_libraries(${TEST_NAME} migraphx_onnx migraphx_ref)
161
    target_include_directories(${TEST_NAME} PUBLIC include)
kahmed10's avatar
kahmed10 committed
162
    add_test(NAME ${TEST_NAME} COMMAND $<TARGET_FILE:${TEST_NAME}> WORKING_DIRECTORY ${TEST_ONNX_DIR}) 
Paul's avatar
Paul committed
163
    rocm_mark_as_test(${TEST_NAME})
164
endforeach()
Paul's avatar
Paul committed
165

166
# tf test
kahmed10's avatar
kahmed10 committed
167
set(TEST_TF_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tf)
168
169
add_executable(test_tf tf/tf_test.cpp)
rocm_clang_tidy_check(test_tf)
170
target_link_libraries(test_tf migraphx_tf migraphx_ref)
171
target_include_directories(test_tf PUBLIC include)
kahmed10's avatar
kahmed10 committed
172
add_test(NAME test_tf COMMAND $<TARGET_FILE:test_tf> WORKING_DIRECTORY ${TEST_TF_DIR}) 
Paul's avatar
Paul committed
173
rocm_mark_as_test(test_tf)
174

Paul Fultz II's avatar
Paul Fultz II committed
175
add_subdirectory(api)
176
add_subdirectory(verify)
177
if(MIGRAPHX_ENABLE_PYTHON)
Paul's avatar
Paul committed
178
add_subdirectory(py)
179
endif()
Paul's avatar
Paul committed
180

Paul's avatar
Paul committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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()

195
function(test_headers PREFIX)
196
    file(GLOB HEADERS ${CONFIGURE_DEPENDS} ${ARGN})
197
198
199
200
201
202

    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
203
        if(MIGRAPHX_ENABLE_GPU)
Paul's avatar
Paul committed
204
            target_link_libraries(header_${TEST_NAME} migraphx_gpu)
205
206
207
        endif()
    endforeach()
endfunction()
Paul's avatar
Paul committed
208

Paul's avatar
Paul committed
209
test_headers(migraphx ${CMAKE_SOURCE_DIR}/src/include/migraphx/*.hpp)
210
test_headers(migraphx/ref ${CMAKE_SOURCE_DIR}/src/targets/ref/include/migraphx/ref/*.hpp)
Paul's avatar
Paul committed
211
if(MIGRAPHX_ENABLE_GPU)
Paul's avatar
Paul committed
212
test_headers(migraphx/gpu ${CMAKE_SOURCE_DIR}/src/targets/gpu/include/migraphx/gpu/*.hpp)
213
endif()