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