github.com/0xKiwi/rules_go@v0.24.3/go/toolchains.rst (about) 1 Go toolchains 2 ============= 3 4 .. _Args: https://docs.bazel.build/versions/master/skylark/lib/Args.html 5 .. _Bazel toolchains: https://docs.bazel.build/versions/master/toolchains.html 6 .. _Go website: https://golang.org/ 7 .. _GoArchive: providers.rst#goarchive 8 .. _GoLibrary: providers.rst#golibrary 9 .. _GoSDK: providers.rst#gosdk 10 .. _GoSource: providers.rst#gosource 11 .. _binary distribution: https://golang.org/dl/ 12 .. _compilation modes: modes.rst#compilation-modes 13 .. _control the version: `Forcing the Go version`_ 14 .. _core: core.bzl 15 .. _forked version of Go: `Registering a custom SDK`_ 16 .. _go assembly: https://golang.org/doc/asm 17 .. _go sdk rules: `The SDK`_ 18 .. _go/platform/list.bzl: platform/list.bzl 19 .. _installed SDK: `Using the installed Go sdk`_ 20 .. _nogo: nogo.rst#nogo 21 .. _register: Registration_ 22 .. _register_toolchains: https://docs.bazel.build/versions/master/skylark/lib/globals.html#register_toolchains 23 24 .. role:: param(kbd) 25 .. role:: type(emphasis) 26 .. role:: value(code) 27 .. |mandatory| replace:: **mandatory value** 28 29 The Go toolchain is at the heart of the Go rules, and is the mechanism used to 30 customize the behavior of the core_ Go rules. 31 32 .. contents:: :depth: 2 33 34 ----- 35 36 Overview 37 -------- 38 39 The Go toolchain consists of three main layers: `the SDK`_, `the toolchain`_, 40 and `the context`_. 41 42 The SDK 43 ~~~~~~~ 44 45 The Go SDK (more commonly known as the Go distribution) is a directory tree 46 containing sources for the Go toolchain and standard library and pre-compiled 47 binaries for the same. You can download this from by visiting the `Go website`_ 48 and downloading a `binary distribution`_. 49 50 There are several Bazel rules for obtaining and configuring a Go SDK: 51 52 * `go_download_sdk`_: downloads a toolchain for a specific version of Go for a 53 specific operating system and architecture. 54 * `go_host_sdk`_: uses the toolchain installed on the system where Bazel is 55 run. The toolchain's location is specified with the ``GOROOT`` or by running 56 ``go env GOROOT``. 57 * `go_local_sdk`_: like `go_host_sdk`_, but uses the toolchain in a specific 58 directory on the host system. 59 * `go_wrap_sdk`_: configures a toolchain downloaded with another Bazel 60 repository rule. 61 62 By default, if none of the above rules are used, the `go_register_toolchains`_ 63 function creates a repository named ``@go_sdk`` using `go_download_sdk`_, using 64 a recent version of Go for the host operating system and architecture. 65 66 SDKs are specific to a host platform (e.g., ``linux_amd64``) and a version of 67 Go. They may target all platforms that Go supports. The Go SDK is naturally 68 cross compiling. 69 70 The toolchain 71 ~~~~~~~~~~~~~ 72 73 The workspace rules above declare `Bazel toolchains`_ with `go_toolchain`_ 74 implementations for each target platform that Go supports. Wrappers around 75 the rules register these toolchains automatically. Bazel will select a 76 registered toolchain automatically based on the execution and target platforms, 77 specified with ``--host_platform`` and ``--platforms``, respectively. 78 79 The toolchain itself should be considered opaque. You should only access 80 its contents through `the context`_. 81 82 The context 83 ~~~~~~~~~~~ 84 85 The context is the type you need if you are writing custom rules that need 86 to be compatible with rules_go. It provides information about the SDK, the 87 toolchain, and the standard library. It also provides a convenient way to 88 declare mode-specific files, and to create actions for compiling, linking, 89 and more. 90 91 Customizing 92 ----------- 93 94 Normal usage 95 ~~~~~~~~~~~~ 96 97 This is an example of normal usage for the other examples to be compared 98 against. This will download and use the latest Go SDK that was available when 99 the version of rules_go you're using was released. 100 101 .. code:: bzl 102 103 # WORKSPACE 104 105 load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") 106 107 go_rules_dependencies() 108 109 go_register_toolchains() 110 111 112 Forcing the Go version 113 ~~~~~~~~~~~~~~~~~~~~~~ 114 115 You can select the version of the Go SDK to use by specifying it when you call 116 `go_register_toolchains`_ but you must use a value that matches a known 117 toolchain. 118 119 .. code:: bzl 120 121 # WORKSPACE 122 123 load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") 124 125 go_rules_dependencies() 126 127 go_register_toolchains(go_version="1.10.3") 128 129 130 Using the installed Go SDK 131 ~~~~~~~~~~~~~~~~~~~~~~~~~~ 132 133 You can use the Go SDK that's installed on the system where Bazel is running. 134 This may result in faster builds, since there's no need to download an SDK, 135 but builds won't be reproducible across systems with different SDKs installed. 136 137 .. code:: bzl 138 139 # WORKSPACE 140 141 load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") 142 143 go_rules_dependencies() 144 145 go_register_toolchains(go_version="host") 146 147 148 Registering a custom SDK 149 ~~~~~~~~~~~~~~~~~~~~~~~~ 150 151 If you download the SDK through another repository rule, you can configure 152 it with ``go_wrap_sdk``. It must still be named ``go_sdk``, but this is a 153 temporary limitation that will be removed in the future. 154 155 .. code:: bzl 156 157 # WORKSPACE 158 159 load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains", "go_wrap_sdk") 160 161 unknown_download_sdk( 162 name = "go", 163 ..., 164 ) 165 166 go_wrap_sdk( 167 name = "go_sdk", 168 root_file = "@go//:README.md", 169 ) 170 171 go_rules_dependencies() 172 173 go_register_toolchains() 174 175 176 Writing new Go rules 177 ~~~~~~~~~~~~~~~~~~~~ 178 179 If you are writing a new Bazel rule that uses the Go toolchain, you need to 180 do several things to ensure you have full access to the toolchain and common 181 dependencies. 182 183 * Declare a dependency on a toolchain of type 184 ``@io_bazel_rules_go//go:toolchain``. Bazel will select an appropriate, 185 registered toolchain automatically. 186 * Declare an implicit attribute named ``_go_context_data`` that defaults to 187 ``@io_bazel_rules_go//:go_context_data``. This target gathers configuration 188 information and several common dependencies. 189 * Use the ``go_context`` function to gain access to `the context`_. This is 190 your main interface to the Go toolchain. 191 192 .. code:: bzl 193 194 load("@io_bazel_rules_go//go:def.bzl", "go_context") 195 196 def _my_rule_impl(ctx): 197 go = go_context(ctx) 198 ... 199 200 my_rule = rule( 201 implementation = _my_rule_impl, 202 attrs = { 203 ... 204 "_go_context_data": attr.label( 205 default = "@io_bazel_rules_go//:go_context_data", 206 ), 207 }, 208 toolchains = ["@io_bazel_rules_go//go:toolchain"], 209 ) 210 211 212 Rules and functions 213 ------------------- 214 215 go_register_toolchains 216 ~~~~~~~~~~~~~~~~~~~~~~ 217 218 Installs the Go toolchains. If :param:`go_version` is specified, it sets the 219 SDK version to use (for example, :value:`"1.10.3"`). By default, the latest 220 SDK will be used. 221 222 +--------------------------------+-----------------------------+-----------------------------------+ 223 | **Name** | **Type** | **Default value** | 224 +--------------------------------+-----------------------------+-----------------------------------+ 225 | :param:`go_version` | :type:`string` | latest release | 226 +--------------------------------+-----------------------------+-----------------------------------+ 227 | This specifies the version of the Go SDK to download. This is only used if | 228 | no SDK has been declared with the name :value:`go_sdk` before the call to | 229 | ``go_register_toolchains``. The default version is the latest version of Go | 230 | that was released at the time the rules_go release you're using was tagged. | 231 +--------------------------------+-----------------------------+-----------------------------------+ 232 | :param:`nogo` | :type:`label` | :value:`None` | 233 +--------------------------------+-----------------------------+-----------------------------------+ 234 | The ``nogo`` attribute refers to a nogo_ rule that builds a binary | 235 | used for static analysis. The ``nogo`` binary will be used alongside the | 236 | Go compiler when building packages. | 237 +--------------------------------+-----------------------------+-----------------------------------+ 238 239 go_download_sdk 240 ~~~~~~~~~~~~~~~ 241 242 This downloads a Go SDK for use in toolchains. 243 244 +--------------------------------+-----------------------------+---------------------------------------------+ 245 | **Name** | **Type** | **Default value** | 246 +--------------------------------+-----------------------------+---------------------------------------------+ 247 | :param:`name` | :type:`string` | |mandatory| | 248 +--------------------------------+-----------------------------+---------------------------------------------+ 249 | A unique name for this SDK. This should almost always be :value:`go_sdk` if you want the SDK | 250 | to be used by toolchains. | 251 +--------------------------------+-----------------------------+---------------------------------------------+ 252 | :param:`goos` | :type:`string` | :value:`None` | 253 +--------------------------------+-----------------------------+---------------------------------------------+ 254 | The operating system the binaries in the SDK are intended to run on. | 255 | By default, this is detected automatically, but if you're building on | 256 | an unusual platform, or if you're using remote execution and the execution | 257 | platform is different than the host, you may need to specify this explictly. | 258 +--------------------------------+-----------------------------+---------------------------------------------+ 259 | :param:`goarch` | :type:`string` | :value:`None` | 260 +--------------------------------+-----------------------------+---------------------------------------------+ 261 | The architecture the binaries in the SDK are intended to run on. | 262 | By default, this is detected automatically, but if you're building on | 263 | an unusual platform, or if you're using remote execution and the execution | 264 | platform is different than the host, you may need to specify this explictly. | 265 +--------------------------------+-----------------------------+---------------------------------------------+ 266 | :param:`version` | :type:`string` | :value:`latest Go version` | 267 +--------------------------------+-----------------------------+---------------------------------------------+ 268 | The version of Go to download, for example ``1.12.5``. If unspecified, | 269 | ``go_download_sdk`` will download the latest version of Go that rules_go | 270 | supports. Go versions that rules_go doesn't support may not be specified, | 271 | since the download SHA-256 sums are not known. | 272 +--------------------------------+-----------------------------+---------------------------------------------+ 273 | :param:`urls` | :type:`string_list` | :value:`[https://dl.google.com/go/{}]` | 274 +--------------------------------+-----------------------------+---------------------------------------------+ 275 | A list of mirror urls to the binary distribution of a Go SDK. These must contain the `{}` | 276 | used to substitute the sdk filename being fetched (using `.format`. | 277 | It defaults to the official repository :value:`"https://dl.google.com/go/{}"`. | 278 | | 279 | This attribute is seldom used. It is only needed for downloading Go from | 280 | an alternative location (for example, an internal mirror). | 281 +--------------------------------+-----------------------------+---------------------------------------------+ 282 | :param:`strip_prefix` | :type:`string` | :value:`"go"` | 283 +--------------------------------+-----------------------------+---------------------------------------------+ 284 | A directory prefix to strip from the extracted files. | 285 | Used with ``urls``. | 286 +--------------------------------+-----------------------------+---------------------------------------------+ 287 | :param:`sdks` | :type:`string_list_dict` | :value:`see description` | 288 +--------------------------------+-----------------------------+---------------------------------------------+ 289 | This consists of a set of mappings from the host platform tuple to a list of filename and | 290 | sha256 for that file. The filename is combined the :param:`urls` to produce the final download | 291 | urls to use. | 292 | | 293 | This option is seldom used. It is only needed for downloading a modified | 294 | Go distribution (with a different SHA-256 sum) or a version of Go | 295 | not supported by rules_go (for example, a beta or release candidate). | 296 +--------------------------------+-----------------------------+---------------------------------------------+ 297 298 **Example**: 299 300 .. code:: bzl 301 302 load( 303 "@io_bazel_rules_go//go:deps.bzl", 304 "go_download_sdk", 305 "go_register_toolchains", 306 "go_rules_dependencies", 307 ) 308 309 go_download_sdk( 310 name = "go_sdk", 311 goos = "linux", 312 goarch = "amd64", 313 version = "1.12.5", 314 ) 315 316 go_rules_dependencies() 317 318 go_register_toolchains() 319 320 go_host_sdk 321 ~~~~~~~~~~~ 322 323 This detects and configures the host Go SDK for use in toolchains. 324 325 If the ``GOROOT`` environment variable is set, the SDK in that directory is 326 used. Otherwise, ``go env GOROOT`` is used. 327 328 +--------------------------------+-----------------------------+-----------------------------------+ 329 | **Name** | **Type** | **Default value** | 330 +--------------------------------+-----------------------------+-----------------------------------+ 331 | :param:`name` | :type:`string` | |mandatory| | 332 +--------------------------------+-----------------------------+-----------------------------------+ 333 | A unique name for this SDK. This should almost always be :value:`go_sdk` if you want the SDK | 334 | to be used by toolchains. | 335 +--------------------------------+-----------------------------+-----------------------------------+ 336 337 338 go_local_sdk 339 ~~~~~~~~~~~~ 340 341 This prepares a local path to use as the Go SDK in toolchains. 342 343 +--------------------------------+-----------------------------+-----------------------------------+ 344 | **Name** | **Type** | **Default value** | 345 +--------------------------------+-----------------------------+-----------------------------------+ 346 | :param:`name` | :type:`string` | |mandatory| | 347 +--------------------------------+-----------------------------+-----------------------------------+ 348 | A unique name for this SDK. This should almost always be :value:`go_sdk` if you want the SDK | 349 | to be used by toolchains. | 350 +--------------------------------+-----------------------------+-----------------------------------+ 351 | :param:`path` | :type:`string` | :value:`""` | 352 +--------------------------------+-----------------------------+-----------------------------------+ 353 | The local path to a pre-installed Go SDK. The path must contain the go binary, the tools it | 354 | invokes and the standard library sources. | 355 +--------------------------------+-----------------------------+-----------------------------------+ 356 357 358 go_wrap_sdk 359 ~~~~~~~~~~~ 360 361 This configures an SDK that was downloaded or located with another repository 362 rule. 363 364 +--------------------------------+-----------------------------+-----------------------------------+ 365 | **Name** | **Type** | **Default value** | 366 +--------------------------------+-----------------------------+-----------------------------------+ 367 | :param:`name` | :type:`string` | |mandatory| | 368 +--------------------------------+-----------------------------+-----------------------------------+ 369 | A unique name for this SDK. This should almost always be :value:`go_sdk` if you want the SDK | 370 | to be used by toolchains. | 371 +--------------------------------+-----------------------------+-----------------------------------+ 372 | :param:`root_file` | :type:`label` | |mandatory| | 373 +--------------------------------+-----------------------------+-----------------------------------+ 374 | A Bazel label referencing a file in the root directory of the SDK. Used to | 375 | determine the GOROOT for the SDK. | 376 +--------------------------------+-----------------------------+-----------------------------------+ 377 378 **Example:** 379 380 .. code:: bzl 381 382 load( 383 "@io_bazel_rules_go//go:deps.bzl", 384 "go_register_toolchains", 385 "go_rules_dependencies", 386 "go_wrap_sdk", 387 ) 388 389 go_wrap_sdk( 390 name = "go_sdk", 391 root_file = "@other_repo//go:README.md", 392 ) 393 394 go_rules_dependencies() 395 396 go_register_toolchains() 397 398 go_toolchain 399 ~~~~~~~~~~~~ 400 401 This declares a toolchain that may be used with toolchain type 402 :value:`"@io_bazel_rules_go//go:toolchain"`. 403 404 Normally, ``go_toolchain`` rules are declared and registered in repositories 405 configured with `go_download_sdk`_, `go_host_sdk`_, `go_local_sdk`_, or 406 `go_wrap_sdk`_. You usually won't need to declare these explicitly. 407 408 +--------------------------------+-----------------------------+-----------------------------------+ 409 | **Name** | **Type** | **Default value** | 410 +--------------------------------+-----------------------------+-----------------------------------+ 411 | :param:`name` | :type:`string` | |mandatory| | 412 +--------------------------------+-----------------------------+-----------------------------------+ 413 | A unique name for the toolchain. | 414 +--------------------------------+-----------------------------+-----------------------------------+ 415 | :param:`goos` | :type:`string` | |mandatory| | 416 +--------------------------------+-----------------------------+-----------------------------------+ 417 | The target operating system. Must be a standard ``GOOS`` value. | 418 +--------------------------------+-----------------------------+-----------------------------------+ 419 | :param:`goarch` | :type:`string` | |mandatory| | 420 +--------------------------------+-----------------------------+-----------------------------------+ 421 | The target architecture. Must be a standard ``GOARCH`` value. | 422 +--------------------------------+-----------------------------+-----------------------------------+ 423 | :param:`sdk` | :type:`label` | |mandatory| | 424 +--------------------------------+-----------------------------+-----------------------------------+ 425 | The SDK this toolchain is based on. The target must provide `GoSDK`_. This is | 426 | usually a `go_sdk`_ rule. | 427 +--------------------------------+-----------------------------+-----------------------------------+ 428 | :param:`link_flags` | :type:`string_list` | :value:`[]` | 429 +--------------------------------+-----------------------------+-----------------------------------+ 430 | Flags passed to the Go external linker. | 431 +--------------------------------+-----------------------------+-----------------------------------+ 432 | :param:`cgo_link_flags` | :type:`string_list` | :value:`[]` | 433 +--------------------------------+-----------------------------+-----------------------------------+ 434 | Flags passed to the external linker (if it is used). | 435 +--------------------------------+-----------------------------+-----------------------------------+ 436 437 go_context 438 ~~~~~~~~~~ 439 440 This collects the information needed to form and return a :type:`GoContext` from 441 a rule ctx. It uses the attributes and the toolchains. It can only be used in 442 the implementation of a rule that has the go toolchain attached and the go 443 context data as an attribute. To do this declare the rule using the go_rule 444 wrapper. 445 446 .. code:: bzl 447 448 def _my_rule_impl(ctx): 449 go = go_context(ctx) 450 ... 451 452 my_rule = go_rule( 453 _my_rule_impl, 454 attrs = { 455 ... 456 }, 457 ) 458 459 460 +--------------------------------+-----------------------------+-----------------------------------+ 461 | **Name** | **Type** | **Default value** | 462 +--------------------------------+-----------------------------+-----------------------------------+ 463 | :param:`ctx` | :type:`ctx` | |mandatory| | 464 +--------------------------------+-----------------------------+-----------------------------------+ 465 | The Bazel ctx object for the current rule. | 466 +--------------------------------+-----------------------------+-----------------------------------+ 467 468 The context object 469 ~~~~~~~~~~~~~~~~~~ 470 471 ``GoContext`` is never returned by a rule, instead you build one using 472 ``go_context(ctx)`` in the top of any custom starlark rule that wants to interact 473 with the go rules. It provides all the information needed to create go actions, 474 and create or interact with the other go providers. 475 476 When you get a ``GoContext`` from a context it exposes a number of fields 477 and methods. 478 479 All methods take the ``GoContext`` as the only positional argument. All other 480 arguments must be passed as keyword arguments. This allows us to re-order and 481 deprecate individual parameters over time. 482 483 Fields 484 ^^^^^^ 485 486 +--------------------------------+-----------------------------------------------------------------+ 487 | **Name** | **Type** | 488 +--------------------------------+-----------------------------------------------------------------+ 489 | :param:`toolchain` | :type:`ToolchainInfo` | 490 +--------------------------------+-----------------------------------------------------------------+ 491 | The underlying toolchain. This should be considered an opaque type subject to change. | 492 +--------------------------------+-----------------------------------------------------------------+ 493 | :param:`sdk` | :type:`GoSDK` | 494 +--------------------------------+-----------------------------------------------------------------+ 495 | The SDK in use. This may be used to access sources, packages, and tools. | 496 +--------------------------------+-----------------------------------------------------------------+ 497 | :param:`mode` | :type:`Mode` | 498 +--------------------------------+-----------------------------------------------------------------+ 499 | Controls the compilation setup affecting things like enabling profilers and sanitizers. | 500 | See `compilation modes`_ for more information about the allowed values. | 501 +--------------------------------+-----------------------------------------------------------------+ 502 | :param:`root` | :type:`string` | 503 +--------------------------------+-----------------------------------------------------------------+ 504 | Path of the effective GOROOT. If :param:`stdlib` is set, this is the same | 505 | as ``go.stdlib.root_file.dirname``. Otherwise, this is the same as | 506 | ``go.sdk.root_file.dirname``. | 507 +--------------------------------+-----------------------------------------------------------------+ 508 | :param:`go` | :type:`File` | 509 +--------------------------------+-----------------------------------------------------------------+ 510 | The main "go" binary used to run go sdk tools. | 511 +--------------------------------+-----------------------------------------------------------------+ 512 | :param:`stdlib` | :type:`GoStdLib` | 513 +--------------------------------+-----------------------------------------------------------------+ 514 | The standard library and tools to use in this build mode. This may be the | 515 | pre-compiled standard library that comes with the SDK, or it may be compiled | 516 | in a different directory for this mode. | 517 +--------------------------------+-----------------------------------------------------------------+ 518 | :param:`actions` | :type:`ctx.actions` | 519 +--------------------------------+-----------------------------------------------------------------+ 520 | The actions structure from the Bazel context, which has all the methods for building new | 521 | bazel actions. | 522 +--------------------------------+-----------------------------------------------------------------+ 523 | :param:`exe_extension` | :type:`string` | 524 +--------------------------------+-----------------------------------------------------------------+ 525 | The suffix to use for all executables in this build mode. Mostly used when generating the output | 526 | filenames of binary rules. | 527 +--------------------------------+-----------------------------------------------------------------+ 528 | :param:`shared_extension` | :type:`string` | 529 +--------------------------------+-----------------------------------------------------------------+ 530 | The suffix to use for shared libraries in this build mode. Mostly used when | 531 | generating output filenames of binary rules. | 532 +--------------------------------+-----------------------------------------------------------------+ 533 | :param:`crosstool` | :type:`list of File` | 534 +--------------------------------+-----------------------------------------------------------------+ 535 | The files you need to add to the inputs of an action in order to use the cc toolchain. | 536 +--------------------------------+-----------------------------------------------------------------+ 537 | :param:`package_list` | :type:`File` | 538 +--------------------------------+-----------------------------------------------------------------+ 539 | A file that contains the package list of the standard library. | 540 +--------------------------------+-----------------------------------------------------------------+ 541 | :param:`env` | :type:`dict of string to string` | 542 +--------------------------------+-----------------------------------------------------------------+ 543 | Environment variables to pass to actions. Includes ``GOARCH``, ``GOOS``, | 544 | ``GOROOT``, ``GOROOT_FINAL``, ``CGO_ENABLED``, and ``PATH``. | 545 +--------------------------------+-----------------------------------------------------------------+ 546 | :param:`tags` | :type:`list of string` | 547 +--------------------------------+-----------------------------------------------------------------+ 548 | List of build tags used to filter source files. | 549 +--------------------------------+-----------------------------------------------------------------+ 550 551 Methods 552 ^^^^^^^ 553 554 * Action generators 555 556 * archive_ 557 * asm_ 558 * binary_ 559 * compile_ 560 * cover_ 561 * link_ 562 * pack_ 563 564 * Helpers 565 566 * args_ 567 * `declare_file`_ 568 * `library_to_source`_ 569 * `new_library`_ 570 571 572 archive 573 +++++++ 574 575 This emits actions to compile Go code into an archive. It supports embedding, 576 cgo dependencies, coverage, and assembling and packing .s files. 577 578 It returns a GoArchive_. 579 580 +--------------------------------+-----------------------------+-----------------------------------+ 581 | **Name** | **Type** | **Default value** | 582 +--------------------------------+-----------------------------+-----------------------------------+ 583 | :param:`go` | :type:`GoContext` | |mandatory| | 584 +--------------------------------+-----------------------------+-----------------------------------+ 585 | This must be the same GoContext object you got this function from. | 586 +--------------------------------+-----------------------------+-----------------------------------+ 587 | :param:`source` | :type:`GoSource` | |mandatory| | 588 +--------------------------------+-----------------------------+-----------------------------------+ 589 | The GoSource_ that should be compiled into an archive. | 590 +--------------------------------+-----------------------------+-----------------------------------+ 591 592 593 asm 594 +++ 595 596 The asm function adds an action that runs ``go tool asm`` on a source file to 597 produce an object, and returns the File of that object. 598 599 +--------------------------------+-----------------------------+-----------------------------------+ 600 | **Name** | **Type** | **Default value** | 601 +--------------------------------+-----------------------------+-----------------------------------+ 602 | :param:`go` | :type:`GoContext` | |mandatory| | 603 +--------------------------------+-----------------------------+-----------------------------------+ 604 | This must be the same GoContext object you got this function from. | 605 +--------------------------------+-----------------------------+-----------------------------------+ 606 | :param:`source` | :type:`File` | |mandatory| | 607 +--------------------------------+-----------------------------+-----------------------------------+ 608 | A source code artifact to assemble. | 609 | This must be a ``.s`` file that contains code in the platform neutral `go assembly`_ language. | 610 +--------------------------------+-----------------------------+-----------------------------------+ 611 | :param:`hdrs` | :type:`File iterable` | :value:`[]` | 612 +--------------------------------+-----------------------------+-----------------------------------+ 613 | The list of .h files that may be included by the source. | 614 +--------------------------------+-----------------------------+-----------------------------------+ 615 616 617 binary 618 ++++++ 619 620 This emits actions to compile and link Go code into a binary. It supports 621 embedding, cgo dependencies, coverage, and assembling and packing .s files. 622 623 It returns a tuple containing GoArchive_, the output executable file, and 624 a ``runfiles`` object. 625 626 +--------------------------------+-----------------------------+-----------------------------------+ 627 | **Name** | **Type** | **Default value** | 628 +--------------------------------+-----------------------------+-----------------------------------+ 629 | :param:`go` | :type:`GoContext` | |mandatory| | 630 +--------------------------------+-----------------------------+-----------------------------------+ 631 | This must be the same GoContext object you got this function from. | 632 +--------------------------------+-----------------------------+-----------------------------------+ 633 | :param:`name` | :type:`string` | :value:`""` | 634 +--------------------------------+-----------------------------+-----------------------------------+ 635 | The base name of the generated binaries. Required if :param:`executable` is not given. | 636 +--------------------------------+-----------------------------+-----------------------------------+ 637 | :param:`source` | :type:`GoSource` | |mandatory| | 638 +--------------------------------+-----------------------------+-----------------------------------+ 639 | The GoSource_ that should be compiled and linked. | 640 +--------------------------------+-----------------------------+-----------------------------------+ 641 | :param:`test_archives` | :type:`list GoArchiveData` | :value:`[]` | 642 +--------------------------------+-----------------------------+-----------------------------------+ 643 | List of archives for libraries under test. See link_. | 644 +--------------------------------+-----------------------------+-----------------------------------+ 645 | :param:`gc_linkopts` | :type:`string_list` | :value:`[]` | 646 +--------------------------------+-----------------------------+-----------------------------------+ 647 | Go link options. | 648 +--------------------------------+-----------------------------+-----------------------------------+ 649 | :param:`version_file` | :type:`File` | :value:`None` | 650 +--------------------------------+-----------------------------+-----------------------------------+ 651 | Version file used for link stamping. See link_. | 652 +--------------------------------+-----------------------------+-----------------------------------+ 653 | :param:`info_file` | :type:`File` | :value:`None` | 654 +--------------------------------+-----------------------------+-----------------------------------+ 655 | Info file used for link stamping. See link_. | 656 +--------------------------------+-----------------------------+-----------------------------------+ 657 | :param:`executable` | :type:`File` | :value:`None` | 658 +--------------------------------+-----------------------------+-----------------------------------+ 659 | Optional output file to write. If not set, ``binary`` will generate an output | 660 | file name based on ``name``, the target platform, and the link mode. | 661 +--------------------------------+-----------------------------+-----------------------------------+ 662 663 compile 664 +++++++ 665 666 The compile function adds an action that compiles a list of source files into 667 a package archive (.a file). 668 669 It does not return anything. 670 671 +--------------------------------+-----------------------------+-----------------------------------+ 672 | **Name** | **Type** | **Default value** | 673 +--------------------------------+-----------------------------+-----------------------------------+ 674 | :param:`go` | :type:`GoContext` | |mandatory| | 675 +--------------------------------+-----------------------------+-----------------------------------+ 676 | This must be the same GoContext object you got this function from. | 677 +--------------------------------+-----------------------------+-----------------------------------+ 678 | :param:`sources` | :type:`File iterable` | |mandatory| | 679 +--------------------------------+-----------------------------+-----------------------------------+ 680 | An iterable of source code artifacts. | 681 | These must be pure .go files, no assembly or cgo is allowed. | 682 +--------------------------------+-----------------------------+-----------------------------------+ 683 | :param:`importpath` | :type:`string` | :value:`""` | 684 +--------------------------------+-----------------------------+-----------------------------------+ 685 | The import path this package represents. This is passed to the -p flag. When the actual import | 686 | path is different than the source import path (i.e., when ``importmap`` is set in a | 687 | ``go_library`` rule), this should be the actual import path. | 688 +--------------------------------+-----------------------------+-----------------------------------+ 689 | :param:`archives` | :type:`GoArchive iterable` | :value:`[]` | 690 +--------------------------------+-----------------------------+-----------------------------------+ 691 | An iterable of all directly imported libraries. | 692 | The action will verify that all directly imported libraries were supplied, not allowing | 693 | transitive dependencies to satisfy imports. It will not check that all supplied libraries were | 694 | used though. | 695 +--------------------------------+-----------------------------+-----------------------------------+ 696 | :param:`out_lib` | :type:`File` | |mandatory| | 697 +--------------------------------+-----------------------------+-----------------------------------+ 698 | The archive file that should be produced. | 699 +--------------------------------+-----------------------------+-----------------------------------+ 700 | :param:`out_export` | :type:`File` | :value:`None` | 701 +--------------------------------+-----------------------------+-----------------------------------+ 702 | File where extra information about the package may be stored. This is used | 703 | by nogo to store serialized facts about definitions. In the future, it may | 704 | be used to store export data (instead of the .a file). | 705 +--------------------------------+-----------------------------+-----------------------------------+ 706 | :param:`gc_goopts` | :type:`string_list` | :value:`[]` | 707 +--------------------------------+-----------------------------+-----------------------------------+ 708 | Additional flags to pass to the compiler. | 709 +--------------------------------+-----------------------------+-----------------------------------+ 710 | :param:`testfilter` | :type:`string` | :value:`"off"` | 711 +--------------------------------+-----------------------------+-----------------------------------+ 712 | Controls how files with a ``_test`` suffix are filtered. | 713 | | 714 | * ``"off"`` - files with and without a ``_test`` suffix are compiled. | 715 | * ``"only"`` - only files with a ``_test`` suffix are compiled. | 716 | * ``"exclude"`` - only files without a ``_test`` suffix are compiled. | 717 +--------------------------------+-----------------------------+-----------------------------------+ 718 | :param:`asmhdr` | :type:`File` | :value:`None` | 719 +--------------------------------+-----------------------------+-----------------------------------+ 720 | If provided, the compiler will write an assembly header to this file. | 721 +--------------------------------+-----------------------------+-----------------------------------+ 722 723 724 cover 725 +++++ 726 727 The cover function adds an action that runs ``go tool cover`` on a set of source 728 files to produce copies with cover instrumentation. 729 730 Returns a covered GoSource_ with the required source files process for coverage. 731 732 Note that this removes most comments, including cgo comments. 733 734 +--------------------------------+-----------------------------+-----------------------------------+ 735 | **Name** | **Type** | **Default value** | 736 +--------------------------------+-----------------------------+-----------------------------------+ 737 | :param:`go` | :type:`GoContext` | |mandatory| | 738 +--------------------------------+-----------------------------+-----------------------------------+ 739 | This must be the same GoContext object you got this function from. | 740 +--------------------------------+-----------------------------+-----------------------------------+ 741 | :param:`source` | :type:`GoSource` | |mandatory| | 742 +--------------------------------+-----------------------------+-----------------------------------+ 743 | The source object to process. Any source files in the object that have been marked as needing | 744 | coverage will be processed and substiuted in the returned GoSource. | 745 +--------------------------------+-----------------------------+-----------------------------------+ 746 747 748 link 749 ++++ 750 751 The link function adds an action that runs ``go tool link`` on a library. 752 753 It does not return anything. 754 755 +--------------------------------+-----------------------------+-----------------------------------+ 756 | **Name** | **Type** | **Default value** | 757 +--------------------------------+-----------------------------+-----------------------------------+ 758 | :param:`go` | :type:`GoContext` | |mandatory| | 759 +--------------------------------+-----------------------------+-----------------------------------+ 760 | This must be the same GoContext object you got this function from. | 761 +--------------------------------+-----------------------------+-----------------------------------+ 762 | :param:`archive` | :type:`GoArchive` | |mandatory| | 763 +--------------------------------+-----------------------------+-----------------------------------+ 764 | The library to link. | 765 +--------------------------------+-----------------------------+-----------------------------------+ 766 | :param:`test_archives` | :type:`GoArchiveData list` | :value:`[]` | 767 +--------------------------------+-----------------------------+-----------------------------------+ 768 | List of archives for libraries under test. These are excluded from linking | 769 | if transitive dependencies of :param:`archive` have the same package paths. | 770 | This is useful for linking external test archives that depend internal test | 771 | archives. | 772 +--------------------------------+-----------------------------+-----------------------------------+ 773 | :param:`executable` | :type:`File` | |mandatory| | 774 +--------------------------------+-----------------------------+-----------------------------------+ 775 | The binary to produce. | 776 +--------------------------------+-----------------------------+-----------------------------------+ 777 | :param:`gc_linkopts` | :type:`string_list` | :value:`[]` | 778 +--------------------------------+-----------------------------+-----------------------------------+ 779 | Basic link options, these may be adjusted by the :param:`mode`. | 780 +--------------------------------+-----------------------------+-----------------------------------+ 781 | :param:`version_file` | :type:`File` | :value:`None` | 782 +--------------------------------+-----------------------------+-----------------------------------+ 783 | Version file used for link stamping. | 784 +--------------------------------+-----------------------------+-----------------------------------+ 785 | :param:`info_file` | :type:`File` | :value:`None` | 786 +--------------------------------+-----------------------------+-----------------------------------+ 787 | Info file used for link stamping. | 788 +--------------------------------+-----------------------------+-----------------------------------+ 789 790 pack 791 ++++ 792 793 The pack function adds an action that produces an archive from a base archive 794 and a collection of additional object files. 795 796 It does not return anything. 797 798 +--------------------------------+-----------------------------+-----------------------------------+ 799 | **Name** | **Type** | **Default value** | 800 +--------------------------------+-----------------------------+-----------------------------------+ 801 | :param:`go` | :type:`GoContext` | |mandatory| | 802 +--------------------------------+-----------------------------+-----------------------------------+ 803 | This must be the same GoContext object you got this function from. | 804 +--------------------------------+-----------------------------+-----------------------------------+ 805 | :param:`in_lib` | :type:`File` | |mandatory| | 806 +--------------------------------+-----------------------------+-----------------------------------+ 807 | The archive that should be copied and appended to. | 808 | This must always be an archive in the common ar form (like that produced by the go compiler). | 809 +--------------------------------+-----------------------------+-----------------------------------+ 810 | :param:`out_lib` | :type:`File` | |mandatory| | 811 +--------------------------------+-----------------------------+-----------------------------------+ 812 | The archive that should be produced. | 813 | This will always be an archive in the common ar form (like that produced by the go compiler). | 814 +--------------------------------+-----------------------------+-----------------------------------+ 815 | :param:`objects` | :type:`File iterable` | :value:`()` | 816 +--------------------------------+-----------------------------+-----------------------------------+ 817 | An iterable of object files to be added to the output archive file. | 818 +--------------------------------+-----------------------------+-----------------------------------+ 819 | :param:`archives` | :type:`list of File` | :value:`[]` | 820 +--------------------------------+-----------------------------+-----------------------------------+ 821 | Additional archives whose objects will be appended to the output. | 822 | These can be ar files in either common form or either the bsd or sysv variations. | 823 +--------------------------------+-----------------------------+-----------------------------------+ 824 825 args 826 ++++ 827 828 This creates a new Args_ object, using the ``ctx.actions.args`` method. The 829 object is pre-populated with standard arguments used by all the go toolchain 830 builders. 831 832 +--------------------------------+-----------------------------+-----------------------------------+ 833 | **Name** | **Type** | **Default value** | 834 +--------------------------------+-----------------------------+-----------------------------------+ 835 | :param:`go` | :type:`GoContext` | |mandatory| | 836 +--------------------------------+-----------------------------+-----------------------------------+ 837 | This must be the same GoContext object you got this function from. | 838 +--------------------------------+-----------------------------+-----------------------------------+ 839 840 declare_file 841 ++++++++++++ 842 843 This is the equivalent of ``ctx.actions.declare_file``. It uses the 844 current build mode to make the filename unique between configurations. 845 846 +--------------------------------+-----------------------------+-----------------------------------+ 847 | **Name** | **Type** | **Default value** | 848 +--------------------------------+-----------------------------+-----------------------------------+ 849 | :param:`go` | :type:`GoContext` | |mandatory| | 850 +--------------------------------+-----------------------------+-----------------------------------+ 851 | This must be the same GoContext object you got this function from. | 852 +--------------------------------+-----------------------------+-----------------------------------+ 853 | :param:`path` | :type:`string` | :value:`""` | 854 +--------------------------------+-----------------------------+-----------------------------------+ 855 | A path for this file, including the basename of the file. | 856 +--------------------------------+-----------------------------+-----------------------------------+ 857 | :param:`ext` | :type:`string` | :value:`""` | 858 +--------------------------------+-----------------------------+-----------------------------------+ 859 | The extension to use for the file. | 860 +--------------------------------+-----------------------------+-----------------------------------+ 861 | :param:`name` | :type:`string` | :value:`""` | 862 +--------------------------------+-----------------------------+-----------------------------------+ 863 | A name to use for this file. If path is not present, this becomes a prefix to the path. | 864 | If this is not set, the current rule name is used in it's place. | 865 +--------------------------------+-----------------------------+-----------------------------------+ 866 867 library_to_source 868 +++++++++++++++++ 869 870 This is used to build a GoSource object for a given GoLibrary in the current 871 build mode. 872 873 +--------------------------------+-----------------------------+-----------------------------------+ 874 | **Name** | **Type** | **Default value** | 875 +--------------------------------+-----------------------------+-----------------------------------+ 876 | :param:`go` | :type:`GoContext` | |mandatory| | 877 +--------------------------------+-----------------------------+-----------------------------------+ 878 | This must be the same GoContext object you got this function from. | 879 +--------------------------------+-----------------------------+-----------------------------------+ 880 | :param:`attr` | :type:`ctx.attr` | |mandatory| | 881 +--------------------------------+-----------------------------+-----------------------------------+ 882 | The attributes of the rule being processed. In a normal rule implementation this would be | 883 | ctx.attr. | 884 +--------------------------------+-----------------------------+-----------------------------------+ 885 | :param:`library` | :type:`GoLibrary` | |mandatory| | 886 +--------------------------------+-----------------------------+-----------------------------------+ 887 | The GoLibrary_ that you want to build a GoSource_ object for in the current build mode. | 888 +--------------------------------+-----------------------------+-----------------------------------+ 889 | :param:`coverage_instrumented` | :type:`bool` | |mandatory| | 890 +--------------------------------+-----------------------------+-----------------------------------+ 891 | This controls whether cover is enabled for this specific library in this mode. | 892 | This should generally be the value of ctx.coverage_instrumented() | 893 +--------------------------------+-----------------------------+-----------------------------------+ 894 895 new_library 896 +++++++++++ 897 898 This creates a new GoLibrary. You can add extra fields to the go library by 899 providing extra named parameters to this function, they will be visible to the 900 resolver when it is invoked. 901 902 +--------------------------------+-----------------------------+-----------------------------------+ 903 | **Name** | **Type** | **Default value** | 904 +--------------------------------+-----------------------------+-----------------------------------+ 905 | :param:`go` | :type:`GoContext` | |mandatory| | 906 +--------------------------------+-----------------------------+-----------------------------------+ 907 | This must be the same GoContext object you got this function from. | 908 +--------------------------------+-----------------------------+-----------------------------------+ 909 | :param:`resolver` | :type:`function` | :value:`None` | 910 +--------------------------------+-----------------------------+-----------------------------------+ 911 | This is the function that gets invoked when converting from a GoLibrary to a GoSource. | 912 | The function's signature must be | 913 | | 914 | .. code:: bzl | 915 | | 916 | def _testmain_library_to_source(go, attr, source, merge) | 917 | | 918 | attr is the attributes of the rule being processed | 919 | source is the dictionary of GoSource fields being generated | 920 | merge is a helper you can call to merge | 921 +--------------------------------+-----------------------------+-----------------------------------+ 922 | :param:`importable` | :type:`bool` | |mandatory| | 923 +--------------------------------+-----------------------------+-----------------------------------+ 924 | This controls whether the GoLibrary_ is supposed to be importable. This is generally only false | 925 | for the "main" libraries that are built just before linking. | 926 +--------------------------------+-----------------------------+-----------------------------------+