github.com/bazelbuild/rules_go@v0.47.2-0.20240515105122-e7ddb9ea474e/go/private/rules/source.bzl (about)

     1  # Copyright 2017 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      "//go/private:common.bzl",
    17      "GO_TOOLCHAIN",
    18  )
    19  load(
    20      "//go/private:context.bzl",
    21      "go_context",
    22  )
    23  load(
    24      "//go/private:providers.bzl",
    25      "GoLibrary",
    26  )
    27  
    28  def _go_source_impl(ctx):
    29      """Implements the go_source() rule."""
    30      go = go_context(ctx)
    31      library = go.new_library(go)
    32      source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
    33      return [
    34          library,
    35          source,
    36          DefaultInfo(
    37              files = depset(source.srcs),
    38          ),
    39      ]
    40  
    41  go_source = rule(
    42      implementation = _go_source_impl,
    43      attrs = {
    44          "data": attr.label_list(
    45              allow_files = True,
    46              doc = """List of files needed by this rule at run-time. This may include data files
    47              needed or other programs that may be executed. The [bazel] package may be
    48              used to locate run files; they may appear in different places depending on the
    49              operating system and environment. See [data dependencies] for more
    50              information on data files.
    51              """,
    52          ),
    53          "srcs": attr.label_list(
    54              allow_files = True,
    55              doc = """The list of Go source files that are compiled to create the package.
    56              The following file types are permitted: `.go, .c, .s, .S .h`.
    57              The files may contain Go-style [build constraints].
    58              """,
    59          ),
    60          "deps": attr.label_list(
    61              providers = [GoLibrary],
    62              doc = """List of Go libraries this source list imports directly.
    63              These may be go_library rules or compatible rules with the [GoLibrary] provider.
    64              """,
    65          ),
    66          "embed": attr.label_list(
    67              providers = [GoLibrary],
    68              doc = """List of Go libraries whose sources should be compiled together with this
    69              package's sources. Labels listed here must name `go_library`,
    70              `go_proto_library`, or other compatible targets with the [GoLibrary] and
    71              [GoSource] providers. Embedded libraries must have the same `importpath` as
    72              the embedding library. At most one embedded library may have `cgo = True`,
    73              and the embedding library may not also have `cgo = True`. See [Embedding]
    74              for more information.
    75              """,
    76          ),
    77          "gc_goopts": attr.string_list(
    78              doc = """List of flags to add to the Go compilation command when using the gc compiler.
    79              Subject to ["Make variable"] substitution and [Bourne shell tokenization].
    80              """,
    81          ),
    82          "_go_config": attr.label(default = "//:go_config"),
    83          "_cgo_context_data": attr.label(default = "//:cgo_context_data_proxy"),
    84      },
    85      toolchains = [GO_TOOLCHAIN],
    86      doc = """This declares a set of source files and related dependencies that can be embedded into one of the
    87      other rules.
    88      This is used as a way of easily declaring a common set of sources re-used in multiple rules.<br><br>
    89      **Providers:**
    90      <ul>
    91        <li>[GoLibrary]</li>
    92        <li>[GoSource]</li>
    93      </ul>
    94      """,
    95  )
    96  # See docs/go/core/rules.md#go_source for full documentation.