...
1# Copyright 2018 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15DEFAULT_NOGO = "@io_bazel_rules_go//:default_nogo"
16NOGO_DEFAULT_INCLUDES = ["@@//:__subpackages__"]
17NOGO_DEFAULT_EXCLUDES = []
18
19# repr(Label(...)) does not emit a canonical label literal.
20def _label_repr(label):
21 return "Label(\"{}\")".format(label)
22
23def _scope_list_repr(scopes):
24 if scopes == ["all"]:
25 return repr(["all"])
26 return "[" + ", ".join([_label_repr(Label(l)) for l in scopes]) + "]"
27
28def _go_register_nogo_impl(ctx):
29 ctx.template(
30 "BUILD.bazel",
31 Label("//go/private:BUILD.nogo.bazel"),
32 substitutions = {
33 "{{nogo}}": ctx.attr.nogo,
34 },
35 executable = False,
36 )
37 ctx.file(
38 "scope.bzl",
39 """
40INCLUDES = {includes}
41EXCLUDES = {excludes}
42""".format(
43 includes = _scope_list_repr(ctx.attr.includes),
44 excludes = _scope_list_repr(ctx.attr.excludes),
45 ),
46 executable = False,
47 )
48
49# go_register_nogo creates a repository with an alias that points
50# to the nogo rule that should be used globally by go rules in the workspace.
51# This may be called automatically by go_rules_dependencies or by
52# go_register_toolchains.
53# With Bzlmod, it is created by the go_sdk extension.
54go_register_nogo = repository_rule(
55 _go_register_nogo_impl,
56 attrs = {
57 "nogo": attr.string(mandatory = True),
58 # Special sentinel value used to let nogo run on all targets when using
59 # WORKSPACE, for backwards compatibility.
60 "includes": attr.string_list(default = ["all"]),
61 "excludes": attr.string_list(),
62 },
63)
64
65def go_register_nogo_wrapper(nogo, includes = NOGO_DEFAULT_INCLUDES, excludes = NOGO_DEFAULT_EXCLUDES):
66 """See go/nogo.rst"""
67 go_register_nogo(
68 name = "io_bazel_rules_nogo",
69 nogo = nogo,
70 includes = includes,
71 excludes = excludes,
72 )
View as plain text