diff options
author | John Cater <jcater@google.com> | 2017-04-10 15:59:57 +0000 |
---|---|---|
committer | Jakob Buchgraber <buchgr@google.com> | 2017-04-11 10:50:09 +0200 |
commit | 5dc81a2c9b30f59f143b5e7fd1b1ec6f8ba27fcf (patch) | |
tree | ef115bcd3e30856fe14c65e5de2dd16fcedfeb77 /tools/platforms | |
parent | ba511908f6b53248c42bae22c6eeb110ec5940da (diff) |
Add toolchain_rule to enable rule authors to define toolchains.
Change-Id: I407240708f4aacc89ec5c00bf3e8ff46a1d6d6d6
PiperOrigin-RevId: 152692981
Diffstat (limited to 'tools/platforms')
-rw-r--r-- | tools/platforms/BUILD | 1 | ||||
-rw-r--r-- | tools/platforms/platforms.BUILD | 2 | ||||
-rw-r--r-- | tools/platforms/toolchains.bzl | 63 |
3 files changed, 66 insertions, 0 deletions
diff --git a/tools/platforms/BUILD b/tools/platforms/BUILD index 4e788aea2c..a54e5e6ea9 100644 --- a/tools/platforms/BUILD +++ b/tools/platforms/BUILD @@ -6,6 +6,7 @@ filegroup( name = "package-srcs", srcs = [ "platforms.BUILD", + "toolchains.bzl", ], ) diff --git a/tools/platforms/platforms.BUILD b/tools/platforms/platforms.BUILD index 4e477e72a8..dd12d661f4 100644 --- a/tools/platforms/platforms.BUILD +++ b/tools/platforms/platforms.BUILD @@ -4,6 +4,8 @@ package( default_visibility = ["//visibility:public"], ) +export_files = ["toolchains.bzl"] + # These match values in //src/main/java/com/google/build/lib/util:CPU.java constraint_setting(name = "cpu") diff --git a/tools/platforms/toolchains.bzl b/tools/platforms/toolchains.bzl new file mode 100644 index 0000000000..b931905eeb --- /dev/null +++ b/tools/platforms/toolchains.bzl @@ -0,0 +1,63 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Useful functions and rules for defining toolchains.""" + +_default_toolchain_rule_attrs = { + "exec_compatible_with": attr.label_list( + providers = [platform_common.ConstraintValueInfo]), + "target_compatible_with": attr.label_list( + providers = [platform_common.ConstraintValueInfo]), +} + +def default_toolchain_rule_impl(ctx, override_attrs = {}): + """A default implementation for toolchain_rule. + This implementation creates a toolchain provider and adds all extra + attributes. + + Args: + ctx: The rule context. + override_attrs: Any data in this dict will override the corresponding + attribute from the context. toolchain_rule implementations can use this + to customize the values set in the provider. + + Returns: + The created toolchain provider. + """ + toolchain_data = {} + + # Collect the extra_args from ctx.attrs. + attr_names = ctx.attr._extra_attr_names + for name in attr_names: + if name in override_attrs: + attr = override_attrs[name] + else: + attr = getattr(ctx.attr, name) + toolchain_data[name] = attr + + toolchain = platform_common.toolchain( + exec_compatible_with = ctx.attr.exec_compatible_with, + target_compatible_with = ctx.attr.target_compatible_with, + **toolchain_data) + + return [toolchain] + +def toolchain_rule(implementation = default_toolchain_rule_impl, fragments = [], extra_attrs = {}): + return rule( + implementation = implementation, + attrs = _default_toolchain_rule_attrs + extra_attrs + { + # default_toolchain_rule_impl needs this to know what attributes are extra args. + "_extra_attr_names": attr.string_list(default = extra_attrs.keys()), + }, + fragments = fragments, + ) |