xref: /aosp_15_r20/external/bazelbuild-rules_go/go/private/rules/library.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Copyright 2014 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
15load(
16    "//go/private:common.bzl",
17    "asm_exts",
18    "cgo_exts",
19    "go_exts",
20)
21load(
22    "//go/private:context.bzl",
23    "go_context",
24)
25load(
26    "//go/private:go_toolchain.bzl",
27    "GO_TOOLCHAIN",
28)
29load(
30    "//go/private:providers.bzl",
31    "GoLibrary",
32    "INFERRED_PATH",
33)
34load(
35    "//go/private/rules:transition.bzl",
36    "non_go_transition",
37)
38
39def _go_library_impl(ctx):
40    """Implements the go_library() rule."""
41    go = go_context(ctx)
42    if go.pathtype == INFERRED_PATH:
43        fail("importpath must be specified in this library or one of its embedded libraries")
44    library = go.new_library(go)
45    source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
46    archive = go.archive(go, source)
47
48    return [
49        library,
50        source,
51        archive,
52        DefaultInfo(
53            files = depset([archive.data.file]),
54        ),
55        coverage_common.instrumented_files_info(
56            ctx,
57            source_attributes = ["srcs"],
58            dependency_attributes = ["data", "deps", "embed", "embedsrcs"],
59            extensions = ["go"],
60        ),
61        OutputGroupInfo(
62            cgo_exports = archive.cgo_exports,
63            compilation_outputs = [archive.data.file],
64        ),
65    ]
66
67go_library = rule(
68    _go_library_impl,
69    attrs = {
70        "data": attr.label_list(
71            allow_files = True,
72            cfg = non_go_transition,
73            doc = """
74            List of files needed by this rule at run-time.
75            This may include data files needed or other programs that may be executed.
76            The [bazel] package may be used to locate run files; they may appear in different places
77            depending on the operating system and environment. See [data dependencies] for more information on data files.
78            """,
79        ),
80        "srcs": attr.label_list(
81            allow_files = go_exts + asm_exts + cgo_exts,
82            cfg = non_go_transition,
83            doc = """
84            The list of Go source files that are compiled to create the package.
85            Only `.go` and `.s` files are permitted, unless the `cgo` attribute is set,
86            in which case, `.c .cc .cpp .cxx .h .hh .hpp .hxx .inc .m .mm` files are also permitted.
87            Files may be filtered at build time using Go [build constraints].
88            """,
89        ),
90        "deps": attr.label_list(
91            providers = [GoLibrary],
92            doc = """
93            List of Go libraries this package imports directly.
94            These may be `go_library` rules or compatible rules with the [GoLibrary] provider.
95            """,
96        ),
97        "importpath": attr.string(
98            doc = """
99            The source import path of this library. Other libraries can import this library using this path.
100            This must either be specified in `go_library` or inherited from one of the libraries in `embed`.
101            """,
102        ),
103        "importmap": attr.string(
104            doc = """
105            The actual import path of this library. By default, this is `importpath`. This is mostly only visible to the compiler and linker,
106            but it may also be seen in stack traces. This must be unique among packages passed to the linker.
107            It may be set to something different than `importpath` to prevent conflicts between multiple packages
108            with the same path (for example, from different vendor directories).
109            """,
110        ),
111        "importpath_aliases": attr.string_list(
112        ),  # experimental, undocumented
113        "embed": attr.label_list(
114            providers = [GoLibrary],
115            doc = """
116            List of Go libraries whose sources should be compiled together with this package's sources.
117            Labels listed here must name `go_library`, `go_proto_library`, or other compatible targets with
118            the [GoLibrary] and [GoSource] providers. Embedded libraries must have the same `importpath` as the embedding library.
119            At most one embedded library may have `cgo = True`, and the embedding library may not also have `cgo = True`.
120            See [Embedding] for more information.
121            """,
122        ),
123        "embedsrcs": attr.label_list(
124            allow_files = True,
125            cfg = non_go_transition,
126            doc = """
127            The list of files that may be embedded into the compiled package using `//go:embed`
128            directives. All files must be in the same logical directory or a subdirectory as source files.
129            All source files containing `//go:embed` directives must be in the same logical directory.
130            It's okay to mix static and generated source files and static and generated embeddable files.
131            """,
132        ),
133        "gc_goopts": attr.string_list(
134            doc = """
135            List of flags to add to the Go compilation command when using the gc compiler.
136            Subject to ["Make variable"] substitution and [Bourne shell tokenization].
137            """,
138        ),
139        "x_defs": attr.string_dict(
140            doc = """
141            Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these.
142            """,
143        ),
144        "cgo": attr.bool(
145            doc = """
146            If `True`, the package may contain [cgo] code, and `srcs` may contain C, C++, Objective-C, and Objective-C++ files
147            and non-Go assembly files. When cgo is enabled, these files will be compiled with the C/C++ toolchain and
148            included in the package. Note that this attribute does not force cgo to be enabled. Cgo is enabled for
149            non-cross-compiling builds when a C/C++ toolchain is configured.
150            """,
151        ),
152        "cdeps": attr.label_list(
153            cfg = non_go_transition,
154            doc = """
155            List of other libraries that the c code depends on.
156            This can be anything that would be allowed in [cc_library deps] Only valid if `cgo = True`.
157            """,
158        ),
159        "cppopts": attr.string_list(
160            doc = """
161            List of flags to add to the C/C++ preprocessor command.
162            Subject to ["Make variable"] substitution and [Bourne shell tokenization].
163            Only valid if `cgo = True`.
164            """,
165        ),
166        "copts": attr.string_list(
167            doc = """
168            List of flags to add to the C compilation command.
169            Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if `cgo = True`.
170            """,
171        ),
172        "cxxopts": attr.string_list(
173            doc = """
174            List of flags to add to the C++ compilation command.
175            Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if `cgo = True`.
176            """,
177        ),
178        "clinkopts": attr.string_list(
179            doc = """
180            List of flags to add to the C link command.
181            Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if `cgo = True`.
182            """,
183        ),
184        "_go_context_data": attr.label(default = "//:go_context_data"),
185        "_allowlist_function_transition": attr.label(
186            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
187        ),
188    },
189    toolchains = [GO_TOOLCHAIN],
190    doc = """This builds a Go library from a set of source files that are all part of
191    the same package.<br><br>
192    ***Note:*** For targets generated by Gazelle, `name` is typically the last component of the path,
193    or `go_default_library`, with the old naming convention.<br><br>
194    **Providers:**
195    <ul>
196      <li>[GoLibrary]</li>
197      <li>[GoSource]</li>
198      <li>[GoArchive]</li>
199    </ul>
200    """,
201)
202
203# See docs/go/core/rules.md#go_library for full documentation.
204
205go_tool_library = rule(
206    _go_library_impl,
207    attrs = {
208        "data": attr.label_list(allow_files = True),
209        "srcs": attr.label_list(allow_files = True),
210        "deps": attr.label_list(providers = [GoLibrary]),
211        "importpath": attr.string(),
212        "importmap": attr.string(),
213        "embed": attr.label_list(providers = [GoLibrary]),
214        "gc_goopts": attr.string_list(),
215        "x_defs": attr.string_dict(),
216        "_go_config": attr.label(default = "//:go_config"),
217        "_cgo_context_data": attr.label(default = "//:cgo_context_data_proxy"),
218        "_stdlib": attr.label(default = "//:stdlib"),
219    },
220    toolchains = [GO_TOOLCHAIN],
221)
222# This is used instead of `go_library` for dependencies of the `nogo` rule and
223# packages that are depended on implicitly by code generated within the Go rules.
224# This avoids a bootstrapping problem.
225
226# See docs/go/core/rules.md#go_tool_library for full documentation.
227