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