blob: 46f862202ab1234a2019aeecb44cdba28c695063 [file] [log] [blame]
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07001# Copyright 2020 The IREE Authors
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -08002#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07003# Licensed under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -08006
7include(CMakeParseArguments)
8
9# iree_lit_test()
10#
11# Creates a lit test for the specified source file.
12#
13# Mirrors the bzl rule of the same name.
14#
15# Parameters:
16# NAME: Name of the target
17# TEST_FILE: Test file to run with the lit runner.
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080018# TOOLS: Tools that should be included on the PATH
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080019# DATA: Additional data dependencies invoked by the test (e.g. binaries
20# called in the RUN line)
Scott Todd2bef7202024-07-30 08:14:38 -070021# LABELS: Additional labels to apply to the test. Package path and
22# "test-type=lit-test" labels are added automatically.
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080023function(iree_lit_test)
Maksim Levental6d4eea62024-06-03 13:56:04 -070024 if(NOT IREE_BUILD_TESTS)
25 return()
26 endif()
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080027 cmake_parse_arguments(
28 _RULE
29 ""
30 "NAME;TEST_FILE"
Rob Sudermanf900f502022-06-07 12:26:06 -070031 "DATA;TOOLS;LABELS;TIMEOUT"
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080032 ${ARGN}
33 )
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080034
Scott Todd315e2382024-01-30 13:35:51 -080035 if(NOT TARGET FileCheck)
36 # TODO(scotttodd): mark test DISABLED instead of skipping entirely?
37 return()
38 endif()
39
40 # TODO(scotttodd): remove this and make all lit tests "hostonly" implicitly?
Lei Zhangf505f592020-07-01 07:23:43 -040041 if(CMAKE_CROSSCOMPILING AND "hostonly" IN_LIST _RULE_LABELS)
Scott Todd315e2382024-01-30 13:35:51 -080042 # TODO(scotttodd): mark test DISABLED instead of skipping entirely?
Lei Zhangf505f592020-07-01 07:23:43 -040043 return()
44 endif()
45
Ben Vanikb3970152021-04-22 13:26:22 -070046 iree_package_ns(_PACKAGE_NS)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080047 iree_package_name(_PACKAGE_NAME)
48 set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}")
49
50 get_filename_component(_TEST_FILE_PATH ${_RULE_TEST_FILE} ABSOLUTE)
Geoffrey Martin-Nobleaa24d3e2020-02-04 14:38:30 -080051
Ben Vanikb3970152021-04-22 13:26:22 -070052 list(TRANSFORM _RULE_DATA REPLACE "^::" "${_PACKAGE_NS}::")
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080053 list(TRANSFORM _RULE_TOOLS REPLACE "^::" "${_PACKAGE_NS}::")
Ben Vanik57d5e2e2020-03-03 07:14:56 -080054 set(_DATA_DEP_PATHS)
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080055 foreach(_DATA_DEP IN LISTS _RULE_DATA _RULE_TOOLS)
Ben Vanik8a43b8d2021-10-21 11:41:17 -070056 list(APPEND _DATA_DEP_PATHS $<TARGET_FILE:${_DATA_DEP}>)
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080057 endforeach()
58
59 set(_LIT_PATH_ARGS)
60 foreach(_TOOL IN LISTS _RULE_TOOLS)
61 list(APPEND _LIT_PATH_ARGS "--path" "$<TARGET_FILE_DIR:${_TOOL}>")
62 endforeach()
Ben Vanik57d5e2e2020-03-03 07:14:56 -080063
Marius Brehlercc4328c2020-04-02 19:12:51 -070064 iree_package_ns(_PACKAGE_NS)
65 string(REPLACE "::" "/" _PACKAGE_PATH ${_PACKAGE_NS})
Ben Vanik2e9aaa42020-10-10 21:11:02 -070066 set(_NAME_PATH "${_PACKAGE_PATH}/${_RULE_NAME}")
Stella Laurenzo2d82fb72021-10-21 20:52:33 -070067 add_test(
68 NAME
69 ${_NAME_PATH}
Ben Vanik57d5e2e2020-03-03 07:14:56 -080070 COMMAND
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080071 "${Python3_EXECUTABLE}"
Stella Laurenzo96d959e2023-02-19 11:58:14 -080072 "${LLVM_EXTERNAL_LIT}"
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -080073 ${_LIT_PATH_ARGS}
Ben Vanik57d5e2e2020-03-03 07:14:56 -080074 ${_TEST_FILE_PATH}
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080075 )
Stella Laurenzo301e86d2021-08-08 13:59:26 -070076
Stella Laurenzo2d82fb72021-10-21 20:52:33 -070077 list(APPEND _RULE_LABELS "${_PACKAGE_PATH}")
Scott Todd2bef7202024-07-30 08:14:38 -070078 list(APPEND _RULE_LABELS "test-type=lit-test")
Stella Laurenzo2d82fb72021-10-21 20:52:33 -070079 set_property(TEST ${_NAME_PATH} PROPERTY LABELS "${_RULE_LABELS}")
80 set_property(TEST ${_NAME_PATH} PROPERTY REQUIRED_FILES "${_TEST_FILE_PATH}")
Geoffrey Martin-Noble52864162022-01-12 13:08:17 -080081 set_property(TEST ${_NAME_PATH} PROPERTY ENVIRONMENT
Ben Vanik2bc10462022-01-26 18:43:38 -080082 "LIT_OPTS=-v"
Geoffrey Martin-Noble52864162022-01-12 13:08:17 -080083 "FILECHECK_OPTS=--enable-var-scope")
Rob Sudermanf900f502022-06-07 12:26:06 -070084 set_property(TEST ${_NAME_PATH} PROPERTY TIMEOUT ${_RULE_TIMEOUT})
Geoffrey Martin-Noble10f18222022-06-09 16:00:13 -070085 iree_configure_test(${_NAME_PATH})
Stella Laurenzo2d82fb72021-10-21 20:52:33 -070086
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -080087 # TODO(gcmn): Figure out how to indicate a dependency on _RULE_DATA being built
88endfunction()
89
90
91# iree_lit_test_suite()
92#
93# Creates a suite of lit tests for a list of source files.
94#
95# Mirrors the bzl rule of the same name.
96#
97# Parameters:
98# NAME: Name of the target
99# SRCS: List of test files to run with the lit runner. Creates one test per source.
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -0800100# TOOLS: Tools that should be included on the PATH
101# DATA: Additional data dependencies used by the test
Geoffrey Martin-Nobleb5b46cb2020-04-01 12:12:18 -0700102# LABELS: Additional labels to apply to the generated tests. The package path is
103# added automatically.
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800104function(iree_lit_test_suite)
Scott Todd37c17e62020-12-28 11:00:01 -0800105 if(NOT IREE_BUILD_TESTS)
Lei Zhangd454ee42020-06-02 11:48:57 -0700106 return()
107 endif()
108
Scott Todd37c17e62020-12-28 11:00:01 -0800109 # Note: we could check IREE_BUILD_COMPILER here, but cross compilation makes
110 # that a little tricky. Instead, we let iree_check_test handle the checks,
111 # meaning this function may run some configuration but generate no targets.
112
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800113 cmake_parse_arguments(
114 _RULE
115 ""
116 "NAME"
Rob Sudermanf900f502022-06-07 12:26:06 -0700117 "SRCS;DATA;TOOLS;LABELS;TIMEOUT"
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800118 ${ARGN}
119 )
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800120
Rob Sudermanf900f502022-06-07 12:26:06 -0700121 if (NOT DEFINED _RULE_TIMEOUT)
122 set(_RULE_TIMEOUT 60)
123 endif()
124
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800125 foreach(_TEST_FILE ${_RULE_SRCS})
126 get_filename_component(_TEST_BASENAME ${_TEST_FILE} NAME)
127 iree_lit_test(
128 NAME
Geoffrey Martin-Nobleaa24d3e2020-02-04 14:38:30 -0800129 "${_TEST_BASENAME}.test"
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800130 TEST_FILE
131 "${_TEST_FILE}"
132 DATA
133 "${_RULE_DATA}"
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -0800134 TOOLS
135 "${_RULE_TOOLS}"
Geoffrey Martin-Nobleb5b46cb2020-04-01 12:12:18 -0700136 LABELS
137 "${_RULE_LABELS}"
Rob Sudermanf900f502022-06-07 12:26:06 -0700138 TIMEOUT
139 ${_RULE_TIMEOUT}
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800140 )
141 endforeach()
142endfunction()