github.com/0xKiwi/rules_go@v0.24.3/tests/core/go_binary/many_deps.bzl (about)

     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  
    15  load(
    16      "@io_bazel_rules_go//go:def.bzl",
    17      "go_binary",
    18      "go_context",
    19      "go_rule",
    20  )
    21  
    22  _PREFIX = "/".join(["abcdefgh"[i] * 100 for i in range(7)]) + "/"
    23  
    24  def _gen_library_impl(ctx):
    25      go = go_context(ctx)
    26      src = go.actions.declare_file(ctx.label.name + ".go")
    27      go.actions.write(src, "package " + ctx.label.name + "\n")
    28      library = go.new_library(go, srcs = [src])
    29      source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
    30      archive = go.archive(go, source)
    31      return [
    32          library,
    33          source,
    34          archive,
    35          DefaultInfo(files = depset([archive.data.file])),
    36      ]
    37  
    38  _gen_library = go_rule(
    39      _gen_library_impl,
    40      attrs = {
    41          "importpath": attr.string(mandatory = True),
    42      },
    43  )
    44  
    45  def _gen_main_src_impl(ctx):
    46      src = ctx.actions.declare_file(ctx.label.name + ".go")
    47      lines = [
    48          "package main",
    49          "",
    50          "import (",
    51      ]
    52      for i in range(ctx.attr.n):
    53          lines.append('\t_ "{}many_deps{}"'.format(_PREFIX, i))
    54      lines.extend([
    55          ")",
    56          "",
    57          "func main() {}",
    58      ])
    59      ctx.actions.write(src, "\n".join(lines))
    60      return [DefaultInfo(files = depset([src]))]
    61  
    62  _gen_main_src = rule(
    63      _gen_main_src_impl,
    64      attrs = {
    65          "n": attr.int(mandatory = True),
    66      },
    67  )
    68  
    69  def many_deps(name, **kwargs):
    70      deps = []
    71      n = 200
    72      for i in range(n):
    73          lib_name = "many_deps" + str(i)
    74          _gen_library(
    75              name = lib_name,
    76              importpath = _PREFIX + lib_name,
    77              visibility = ["//visibility:private"],
    78          )
    79          deps.append(lib_name)
    80      _gen_main_src(
    81          name = "many_deps_src",
    82          n = n,
    83      )
    84      go_binary(
    85          name = name,
    86          srcs = [":many_deps_src"],
    87          deps = deps,
    88          **kwargs
    89      )