aboutsummaryrefslogtreecommitdiff
path: root/go/private/rules/library.bzl
blob: 6f96c0845af923123a37c88af528ee3e4b962d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright 2014 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.

load(
    "//go/private:common.bzl",
    "asm_exts",
    "cgo_exts",
    "go_exts",
)
load(
    "//go/private:context.bzl",
    "go_context",
)
load(
    "//go/private:go_toolchain.bzl",
    "GO_TOOLCHAIN",
)
load(
    "//go/private:providers.bzl",
    "GoLibrary",
    "INFERRED_PATH",
)
load(
    "//go/private/rules:transition.bzl",
    "non_go_transition",
)

def _go_library_impl(ctx):
    """Implements the go_library() rule."""
    go = go_context(ctx)
    if go.pathtype == INFERRED_PATH:
        fail("importpath must be specified in this library or one of its embedded libraries")
    library = go.new_library(go)
    source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
    archive = go.archive(go, source)

    return [
        library,
        source,
        archive,
        DefaultInfo(
            files = depset([archive.data.file]),
        ),
        coverage_common.instrumented_files_info(
            ctx,
            source_attributes = ["srcs"],
            dependency_attributes = ["data", "deps", "embed", "embedsrcs"],
            extensions = ["go"],
        ),
        OutputGroupInfo(
            cgo_exports = archive.cgo_exports,
            compilation_outputs = [archive.data.file],
        ),
    ]

go_library = rule(
    _go_library_impl,
    attrs = {
        "data": attr.label_list(
            allow_files = True,
            cfg = non_go_transition,
            doc = """
            List of files needed by this rule at run-time.
            This may include data files needed or other programs that may be executed.
            The [bazel] package may be used to locate run files; they may appear in different places
            depending on the operating system and environment. See [data dependencies] for more information on data files.
            """,
        ),
        "srcs": attr.label_list(
            allow_files = go_exts + asm_exts + cgo_exts,
            cfg = non_go_transition,
            doc = """
            The list of Go source files that are compiled to create the package.
            Only `.go` and `.s` files are permitted, unless the `cgo` attribute is set,
            in which case, `.c .cc .cpp .cxx .h .hh .hpp .hxx .inc .m .mm` files are also permitted.
            Files may be filtered at build time using Go [build constraints].
            """,
        ),
        "deps": attr.label_list(
            providers = [GoLibrary],
            doc = """
            List of Go libraries this package imports directly.
            These may be `go_library` rules or compatible rules with the [GoLibrary] provider.
            """,
        ),
        "importpath": attr.string(
            doc = """
            The source import path of this library. Other libraries can import this library using this path.
            This must either be specified in `go_library` or inherited from one of the libraries in `embed`.
            """,
        ),
        "importmap": attr.string(
            doc = """
            The actual import path of this library. By default, this is `importpath`. This is mostly only visible to the compiler and linker,
            but it may also be seen in stack traces. This must be unique among packages passed to the linker.
            It may be set to something different than `importpath` to prevent conflicts between multiple packages
            with the same path (for example, from different vendor directories).
            """,
        ),
        "importpath_aliases": attr.string_list(
        ),  # experimental, undocumented
        "embed": attr.label_list(
            providers = [GoLibrary],
            doc = """
            List of Go libraries whose sources should be compiled together with this package's sources.
            Labels listed here must name `go_library`, `go_proto_library`, or other compatible targets with
            the [GoLibrary] and [GoSource] providers. Embedded libraries must have the same `importpath` as the embedding library.
            At most one embedded library may have `cgo = True`, and the embedding library may not also have `cgo = True`.
            See [Embedding] for more information.
            """,
        ),
        "embedsrcs": attr.label_list(
            allow_files = True,
            cfg = non_go_transition,
            doc = """
            The list of files that may be embedded into the compiled package using `//go:embed`
            directives. All files must be in the same logical directory or a subdirectory as source files.
            All source files containing `//go:embed` directives must be in the same logical directory.
            It's okay to mix static and generated source files and static and generated embeddable files.
            """,
        ),
        "gc_goopts": attr.string_list(
            doc = """
            List of flags to add to the Go compilation command when using the gc compiler.
            Subject to ["Make variable"] substitution and [Bourne shell tokenization].
            """,
        ),
        "x_defs": attr.string_dict(
            doc = """
            Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these.
            """,
        ),
        "cgo": attr.bool(
            doc = """
            If `True`, the package may contain [cgo] code, and `srcs` may contain C, C++, Objective-C, and Objective-C++ files
            and non-Go assembly files. When cgo is enabled, these files will be compiled with the C/C++ toolchain and
            included in the package. Note that this attribute does not force cgo to be enabled. Cgo is enabled for
            non-cross-compiling builds when a C/C++ toolchain is configured.
            """,
        ),
        "cdeps": attr.label_list(
            cfg = non_go_transition,
            doc = """
            List of other libraries that the c code depends on.
            This can be anything that would be allowed in [cc_library deps] Only valid if `cgo = True`.
            """,
        ),
        "cppopts": attr.string_list(
            doc = """
            List of flags to add to the C/C++ preprocessor command.
            Subject to ["Make variable"] substitution and [Bourne shell tokenization].
            Only valid if `cgo = True`.
            """,
        ),
        "copts": attr.string_list(
            doc = """
            List of flags to add to the C compilation command.
            Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if `cgo = True`.
            """,
        ),
        "cxxopts": attr.string_list(
            doc = """
            List of flags to add to the C++ compilation command.
            Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if `cgo = True`.
            """,
        ),
        "clinkopts": attr.string_list(
            doc = """
            List of flags to add to the C link command.
            Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if `cgo = True`.
            """,
        ),
        "_go_context_data": attr.label(default = "//:go_context_data"),
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
    },
    toolchains = [GO_TOOLCHAIN],
    doc = """This builds a Go library from a set of source files that are all part of
    the same package.<br><br>
    ***Note:*** For targets generated by Gazelle, `name` is typically the last component of the path,
    or `go_default_library`, with the old naming convention.<br><br>
    **Providers:**
    <ul>
      <li>[GoLibrary]</li>
      <li>[GoSource]</li>
      <li>[GoArchive]</li>
    </ul>
    """,
)

# See docs/go/core/rules.md#go_library for full documentation.

go_tool_library = rule(
    _go_library_impl,
    attrs = {
        "data": attr.label_list(allow_files = True),
        "srcs": attr.label_list(allow_files = True),
        "deps": attr.label_list(providers = [GoLibrary]),
        "importpath": attr.string(),
        "importmap": attr.string(),
        "embed": attr.label_list(providers = [GoLibrary]),
        "gc_goopts": attr.string_list(),
        "x_defs": attr.string_dict(),
        "_go_config": attr.label(default = "//:go_config"),
        "_cgo_context_data": attr.label(default = "//:cgo_context_data_proxy"),
        "_stdlib": attr.label(default = "//:stdlib"),
    },
    toolchains = [GO_TOOLCHAIN],
)
# This is used instead of `go_library` for dependencies of the `nogo` rule and
# packages that are depended on implicitly by code generated within the Go rules.
# This avoids a bootstrapping problem.

# See docs/go/core/rules.md#go_tool_library for full documentation.