CMakeLists.txt 5.43 KB
Newer Older
1
include_directories(BEFORE
Chao Liu's avatar
Chao Liu committed
2
3
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/library/include
4
5
)

Chao Liu's avatar
Chao Liu committed
6
add_custom_target(examples)
7

Anthony Chang's avatar
Anthony Chang committed
8
function(add_example_executable EXAMPLE_NAME FILE_NAME)
Chao Liu's avatar
Chao Liu committed
9
    message("adding example ${EXAMPLE_NAME}")
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    set(result 1)
    if(DEFINED DTYPES)
    foreach(source IN LISTS FILE_NAME)
        set(test 0)
        foreach(type IN LISTS DTYPES)
            if(type MATCHES "fp16")
                set(type1 "_f16")
            elseif(type MATCHES "fp32")
                set(type1 "_f32")
            elseif(type MATCHES "fp8")
                set(type1 "_f8")
            elseif(type MATCHES "bf16")
                set(type1 "_b16")
            elseif(type MATCHES "fp64")
                set(type1 "_f64")
            elseif(type MATCHES "int8")
                set(type1 "_i8")
            endif()
            if("${source}" MATCHES "${type}" OR "${source}" MATCHES "${type1}")
                #if filename matches any selected type, exit type loop and do no exclude the file from the list
                set(test 0)
                break()
            elseif((source MATCHES "fp8" OR source MATCHES "fp32" OR source MATCHES "fp64" OR source MATCHES "bf16" OR source MATCHES "int8" OR source MATCHES "fp16" OR
33
                source MATCHES "_f8" OR source MATCHES "_f32" OR source MATCHES "_f64" OR source MATCHES "_i8" OR source MATCHES "_f16" OR source MATCHES "_b16") AND
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
                NOT(source MATCHES type OR source MATCHES type1))
                    #if filename contains a type which doesn't match any selected type, mark it for removal
                    set(test 1)
            endif()
        endforeach()
        if(test EQUAL 1)
            message("removing example source file ${source} ")
            list(REMOVE_ITEM FILE_NAME "${source}")
        endif()
    endforeach()
    endif()
    foreach(source IN LISTS FILE_NAME)
        if(NOT DEFINED DL_KERNELS AND source MATCHES "_dl")
            message("removing dl example ${source} ")
            list(REMOVE_ITEM FILE_NAME "${source}")
        endif()
    endforeach()
    #only continue if there are some source files left on the list
    if(FILE_NAME)
        add_executable(${EXAMPLE_NAME} ${FILE_NAME})
Artur Wojcik's avatar
Artur Wojcik committed
54
        target_link_libraries(${EXAMPLE_NAME} PRIVATE utility getopt::getopt)
55
56
57
58
59
60
61
        add_test(NAME ${EXAMPLE_NAME} COMMAND $<TARGET_FILE:${EXAMPLE_NAME}> ${ARGN})
        add_dependencies(examples ${EXAMPLE_NAME})
        add_dependencies(check ${EXAMPLE_NAME})
        rocm_install(TARGETS ${EXAMPLE_NAME} COMPONENT examples)
        set(result 0)
    endif()
    #message("add_example returns ${result}")
62
    set(result ${result} PARENT_SCOPE)
Artur Wojcik's avatar
Artur Wojcik committed
63
endfunction()
Anthony Chang's avatar
Anthony Chang committed
64

65
66
67
68
69
70
function(add_example_dependencies EXAMPLE_NAME FILE_NAME)
    if(result EQUAL 0)
        add_dependencies(${EXAMPLE_NAME} ${FILE_NAME})
    endif()
endfunction(add_example_dependencies EXAMPLE_NAME)

Anthony Chang's avatar
Anthony Chang committed
71
72
function(add_example_executable_no_testing EXAMPLE_NAME FILE_NAME)
    message("adding example ${EXAMPLE_NAME}")
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    set(result 1)
    if(DEFINED DTYPES)
    foreach(source IN LISTS FILE_NAME)
        set(test 0)
        foreach(type IN LISTS DTYPES)
                if(type MATCHES "fp16")
                    set(type1 "_f16")
                elseif(type MATCHES "fp32")
                    set(type1 "_f32")
                elseif(type MATCHES "fp8")
                    set(type1 "_f8")
                elseif(type MATCHES "bf16")
                    set(type1 "_b16")
                elseif(type MATCHES "fp64")
                    set(type1 "_f64")
                elseif(type MATCHES "int8")
                    set(type1 "_i8")
                endif()
                if("${source}" MATCHES "${type}" OR "${source}" MATCHES "${type1}")
                    #if filename matches any selected type, exit type loop and do no exclude the file from the list
                    set(test 0)
                    break()
                elseif((source MATCHES "fp8" OR source MATCHES "fp32" OR source MATCHES "fp64" OR source MATCHES "bf16" OR source MATCHES "int8" OR source MATCHES "fp16" OR
96
                  source MATCHES "_f8" OR source MATCHES "_f32" OR source MATCHES "_f64" OR source MATCHES "_i8" OR source MATCHES "_f16" OR source MATCHES "_b16") AND
97
98
99
100
101
102
103
104
                  NOT(source MATCHES type OR source MATCHES type1))
                    #if filename contains a type which doesn't match any selected type, mark it for removal
                    set(test 1)
                endif()
        endforeach()
        if(test EQUAL 1)
            message("removing example ${source} ")
            list(REMOVE_ITEM FILE_NAME "${source}")
105
        endif()
106
107
108
109
110
111
112
113
114
115
116
    endforeach()
    endif()
    foreach(source IN LISTS FILE_NAME)
        if(NOT DEFINED DL_KERNELS AND source MATCHES "_dl")
            message("removing dl example ${source} ")
            list(REMOVE_ITEM FILE_NAME "${source}")
        endif()
    endforeach()
    #only continue if there are some source files left on the list
    if(FILE_NAME)
        add_executable(${EXAMPLE_NAME} ${FILE_NAME})
Artur Wojcik's avatar
Artur Wojcik committed
117
        target_link_libraries(${EXAMPLE_NAME} PRIVATE utility getopt::getopt)
118
119
120
121
122
        add_dependencies(examples ${EXAMPLE_NAME})
        rocm_install(TARGETS ${EXAMPLE_NAME} COMPONENT examples)
        set(result 0)
    endif()
    #message("add_example returns ${result}")
123
    set(result ${result} PARENT_SCOPE)
Artur Wojcik's avatar
Artur Wojcik committed
124
endfunction()
125

Shaojie WANG's avatar
Shaojie WANG committed
126
127
128
129
130
131
132
# add all example subdir
file(GLOB dir_list LIST_DIRECTORIES true *)
FOREACH(subdir ${dir_list})
    IF(IS_DIRECTORY "${subdir}")
        add_subdirectory(${subdir})
    ENDIF()
ENDFOREACH()