blob: 26c21bf5547fd83fc76c447570d3fa3fa11589a4 [file]
# Copyright 2019 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "gentbl_filegroup", "td_library")
# All load statements must come first in Starlark.
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load(
"//build_tools/bazel:iree_cc_fuzz.bzl",
_iree_cc_fuzz = "iree_cc_fuzz",
_iree_compiler_cc_fuzz = "iree_compiler_cc_fuzz",
_iree_runtime_cc_fuzz = "iree_runtime_cc_fuzz",
)
"""Common Bazel definitions for IREE."""
# Re-export fuzz rules for external use.
iree_cc_fuzz = _iree_cc_fuzz
iree_compiler_cc_fuzz = _iree_compiler_cc_fuzz
iree_runtime_cc_fuzz = _iree_runtime_cc_fuzz
def defaulting_select(selector):
"""Pass through to select() with special semantics when converting to CMake.
Args:
selector: The selector which is passed through to select(). Must
include a "//conditions:default" branch, which is used by tooling
outside of Bazel when converting to other build systems.
"""
if "//conditions:default" not in selector:
fail("defaulting_select requires a //conditions:default branch")
return select(selector)
def platform_trampoline_deps(basename, path = "runtime/src/iree/base"):
"""Produce a list of deps for the given `basename` platform target.
Example:
"file_mapping" -> ["//iree/base/internal/file_mapping_internal"]
This is used for compatibility with various methods of including the
library in foreign source control systems.
Args:
basename: Library name prefix for a library in [path]/internal.
path: Folder name to work within.
Returns:
A list of dependencies for depending on the library in a platform
sensitive way.
"""
return [
"//%s/internal:%s_internal" % (path, basename),
]
def iree_build_test(name, targets):
"""Dummy rule to ensure that targets build.
This is currently undefined in bazel and is preserved for compatibility.
"""
pass
def iree_cmake_extra_content(content = "", inline = False):
"""Tool for inserting arbitrary content during Bazel->CMake conversion.
This does nothing in Bazel, while the contents are inserted as-is in
converted CMakeLists.txt files.
Args:
content: The text to insert into the converted file.
inline: If true, the content will be inserted inline. Otherwise, it will
be inserted near the top of the converted file.
"""
pass
def iree_cc_library(includes = [], system_includes = [], **kwargs):
"""Base function for all cc_library targets.
This is a pass-through to the native cc_library, which integrators can
customize with additional flags as needed. Prefer to use the compiler
and runtime versions instead.
Note that Bazel does not distinguish between includes and system_includes,
but CMake does. So we allow them to be separate and glom them together
here.
"""
cc_library(
includes = includes + system_includes,
**kwargs
)
def iree_compiler_cc_library(deps = [], **kwargs):
"""Used for cc_library targets within the //compiler tree.
This is a pass-through to the native cc_library which adds specific
compiler specific options and deps.
"""
iree_cc_library(
deps = deps + [
"//compiler/src:defs",
],
**kwargs
)
def iree_compiler_register_plugin(plugin_id, target):
"""Mirror of the CMake iree_compiler_register_plugin function.
Does nothing in bazel currently.
"""
pass
def iree_compiler_cc_test(deps = [], **kwargs):
"""Used for cc_test targets within the //compiler tree.
This is a pass-through to the native cc_test which adds specific
runtime specific options and deps.
"""
cc_test(
deps = deps + [
"//compiler/src:defs",
],
**kwargs
)
def iree_compiler_cc_binary(deps = [], **kwargs):
"""Used for cc_binary targets within the //compiler tree.
This is a pass-through to the native cc_binary which adds specific
runtime specific options and deps.
"""
cc_binary(
deps = deps + [
"//compiler/src:defs",
],
**kwargs
)
def iree_runtime_cc_library(deps = [], **kwargs):
"""Used for cc_library targets within the //runtime tree.
This is a pass-through to the native cc_library which adds specific
runtime specific options and deps.
"""
iree_cc_library(
deps = deps + [
# TODO: Rename to //runtime/src:defs to match compiler.
"//runtime/src:runtime_defines",
],
**kwargs
)
def iree_runtime_cc_test(deps = [], group = None, resource_group = None, **kwargs):
"""Used for cc_test targets within the //runtime tree.
This is a pass-through to the native cc_test which adds specific
runtime specific options and deps.
Args:
deps: Dependencies for the test.
group: Optional test group name.
resource_group: If set, tests sharing the same resource_group name will
not run concurrently. In Bazel this maps to the "exclusive-if-local"
tag (coarser but safe). In CMake this maps to CTest RESOURCE_LOCK
(fine-grained per-group serialization). Use this for tests that
compete for shared system resources (e.g., RLIMIT_MEMLOCK for
io_uring, GPU device access for HIP).
**kwargs: Additional arguments passed to cc_test.
"""
tags = kwargs.pop("tags", [])
if resource_group:
tags = tags + ["exclusive-if-local", "resource_group:" + resource_group]
cc_test(
deps = deps + [
# TODO: Rename to //runtime/src:defs to match compiler.
"//runtime/src:runtime_defines",
],
tags = tags,
**kwargs
)
def iree_runtime_cc_binary(deps = [], **kwargs):
"""Used for cc_binary targets within the //runtime tree.
This is a pass-through to the native cc_binary which adds specific
runtime specific options and deps.
"""
cc_binary(
deps = deps + [
# TODO: Rename to //runtime/src:defs to match compiler.
"//runtime/src:runtime_defines",
],
**kwargs
)
def iree_assert_no_dependency(name, target, dependency, message = "", tags = [], **kwargs):
"""Asserts that target does not transitively depend on dependency.
Creates a genquery + sh_test pair. The test fails if the dependency is
found in the transitive closure of target. Use this to enforce
architectural layering invariants (e.g., the async library must not
depend on pthreads).
No-oped during CMake conversion.
Args:
name: Test target name.
target: Label of the target to check (e.g., "//runtime/src/iree/async").
dependency: Label of the forbidden dependency.
message: Optional message for the failure output.
tags: Additional tags for the test target.
"""
query_name = name + "_query"
native.genquery(
name = query_name,
expression = "deps(%s) intersect %s" % (target, dependency),
scope = [target, dependency],
tags = ["manual"],
**kwargs
)
sh_test(
name = name,
srcs = ["//build_tools/bazel:assert_empty_query.sh"],
args = ["$(location :%s)" % query_name],
data = [":%s" % query_name],
tags = tags,
size = "small",
**kwargs
)
def iree_tablegen_doc(category, includes = [], **kwargs):
"""iree_tablegen_doc() generates documentation from a table definition file.
This is a simple wrapper over gentbl() so we can differentiate between
documentation and others. See gentbl() for details regarding arguments.
"""
gentbl_filegroup(includes = includes + [
"/compiler/src",
], **kwargs)
def iree_gentbl_cc_library(includes = [], **kwargs):
"""IREE version of gentbl_cc_library which sets up includes properly."""
gentbl_cc_library(includes = includes + [
"/compiler/src",
], **kwargs)
def iree_td_library(includes = [], **kwargs):
"""IREE version of td_library."""
td_library(includes = includes + [
"/compiler/src",
], **kwargs)