xref: /llvm-project/utils/bazel/llvm-project-overlay/llvm/lit_test.bzl (revision 5d0957fc23b88ec6d3b743eb2b9ad060e528913d)
1# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2# See https://llvm.org/LICENSE.txt for license information.
3# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4"""Rules for running lit tests."""
5
6load("@bazel_skylib//lib:paths.bzl", "paths")
7load("@rules_python//python:defs.bzl", _py_test = "py_test")
8
9def lit_test(
10        name,
11        srcs,
12        args = None,
13        data = None,
14        deps = None,
15        py_test = _py_test,
16        **kwargs):
17    """Runs a single test file with LLVM's lit tool.
18
19    Args:
20      name: string. the name of the generated test target.
21      srcs: label list. The files on which to run lit.
22      args: string list. Additional arguments to pass to lit.
23        Note that `-v` and the 'srcs' paths are added automatically.
24      data: label list. Additional data dependencies of the test.
25        Note that 'srcs' targets are added automatically.
26      deps: label list. List of targets the test depends on.
27      py_test: function. The py_test rule to use for the underlying test.
28      **kwargs: additional keyword arguments.
29
30    See https://llvm.org/docs/CommandGuide/lit.html for details on lit.
31    """
32
33    args = args or []
34    data = data or []
35    deps = deps or []
36    py_test(
37        name = name,
38        srcs = [Label("//llvm:lit")],
39        main = Label("//llvm:utils/lit/lit.py"),
40        args = args + ["-v"] + ["$(execpath %s)" % src for src in srcs],
41        data = data + srcs,
42        legacy_create_init = False,
43        deps = deps + [Label("//llvm:lit")],
44        **kwargs
45    )
46
47def package_path(label):
48    """Returns the path to the package of 'label'.
49
50    Args:
51      label: label. The label to return the package path of.
52
53    For example, package_path("@foo//bar:BUILD") returns 'external/foo/bar'.
54    """
55    return paths.join(Label(label).workspace_root, Label(label).package)
56