diff --git a/docs/go/core/rules.md b/docs/go/core/rules.md index dc10f686ab..93c0503dc0 100644 --- a/docs/go/core/rules.md +++ b/docs/go/core/rules.md @@ -147,31 +147,31 @@ This builds an executable from a set of source files, | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| basename | The basename of this binary. The binary basename may also be platform-dependent: on Windows, we add an .exe extension. | String | optional | "" | -| cdeps | The 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. | List of labels | optional | [] | -| cgo | 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. | Boolean | optional | False | -| clinkopts | List of flags to add to the C link command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | -| copts | List of flags to add to the C compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | -| cppopts | 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. | List of strings | optional | [] | -| cxxopts | List of flags to add to the C++ compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | -| data | 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. | List of labels | optional | [] | -| deps | List of Go libraries this package imports directly. These may be go_library rules or compatible rules with the [GoLibrary] provider. | List of labels | optional | [] | -| embed | List of Go libraries whose sources should be compiled together with this binary'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 all have the same importpath, which must match the importpath for this go_binary if one is specified. At most one embedded library may have cgo = True, and the embedding binary may not also have cgo = True. See [Embedding] for more information. | List of labels | optional | [] | -| embedsrcs | 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. | List of labels | optional | [] | -| gc_goopts | List of flags to add to the Go compilation command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | -| gc_linkopts | List of flags to add to the Go link command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | -| goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | -| goos | Forces a binary to be cross-compiled for a specific operating system. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | -| gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] | -| importpath | The import path of this binary. Binaries can't actually be imported, but this may be used by [go_path] and other tools to report the location of source files. This may be inferred from embedded libraries. | String | optional | "" | -| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.

| String | optional | "normal" | -| msan | Controls whether code is instrumented for memory sanitization. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:msan. See [mode attributes], specifically [msan]. | String | optional | "auto" | -| out | Sets the output filename for the generated executable. When set, go_binary will write this file without mode-specific directory prefixes, without linkmode-specific prefixes like "lib", and without platform-specific suffixes like ".exe". Note that without a mode-specific directory prefix, the output file (but not its dependencies) will be invalidated in Bazel's cache when changing configurations. | String | optional | "" | -| pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting CGO_ENABLED. May be one of on, off, or auto. If auto, pure mode is enabled when no C/C++ toolchain is configured or when cross-compiling. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:pure. See [mode attributes], specifically [pure]. | String | optional | "auto" | -| race | Controls whether code is instrumented for race detection. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:race. See [mode attributes], specifically [race]. | String | optional | "auto" | -| srcs | 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]. | List of labels | optional | [] | -| static | Controls whether a binary is statically linked. May be one of on, off, or auto. Not available on all platforms or in all modes. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:static. See [mode attributes], specifically [static]. | String | optional | "auto" | -| x_defs | Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these. | Dictionary: String -> String | optional | {} | +| basename | The basename of this binary. The binary basename may also be platform-dependent: on Windows, we add an .exe extension. | String | optional | "" | +| cdeps | The 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. | List of labels | optional | [] | +| cgo | 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. | Boolean | optional | False | +| clinkopts | List of flags to add to the C link command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | +| copts | List of flags to add to the C compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | +| cppopts | 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. | List of strings | optional | [] | +| cxxopts | List of flags to add to the C++ compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | +| data | 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. | List of labels | optional | [] | +| deps | List of Go libraries this package imports directly. These may be go_library rules or compatible rules with the [GoLibrary] provider. | List of labels | optional | [] | +| embed | List of Go libraries whose sources should be compiled together with this binary'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 all have the same importpath, which must match the importpath for this go_binary if one is specified. At most one embedded library may have cgo = True, and the embedding binary may not also have cgo = True. See [Embedding] for more information. | List of labels | optional | [] | +| embedsrcs | 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. | List of labels | optional | [] | +| gc_goopts | List of flags to add to the Go compilation command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | +| gc_linkopts | List of flags to add to the Go link command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | +| goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | +| goos | Forces a binary to be cross-compiled for a specific operating system. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | +| gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] | +| importpath | The import path of this binary. Binaries can't actually be imported, but this may be used by [go_path] and other tools to report the location of source files. This may be inferred from embedded libraries. | String | optional | "" | +| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.

| String | optional | "normal" | +| msan | Controls whether code is instrumented for memory sanitization. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:msan. See [mode attributes], specifically [msan]. | String | optional | "auto" | +| out | Sets the output filename for the generated executable. When set, go_binary will write this file without mode-specific directory prefixes, without linkmode-specific prefixes like "lib", and without platform-specific suffixes like ".exe". Note that without a mode-specific directory prefix, the output file (but not its dependencies) will be invalidated in Bazel's cache when changing configurations. | String | optional | "" | +| pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting CGO_ENABLED. May be one of on, off, or auto. If auto, pure mode is enabled when no C/C++ toolchain is configured or when cross-compiling. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:pure. See [mode attributes], specifically [pure]. | String | optional | "auto" | +| race | Controls whether code is instrumented for race detection. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:race. See [mode attributes], specifically [race]. | String | optional | "auto" | +| srcs | 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]. | List of labels | optional | [] | +| static | Controls whether a binary is statically linked. May be one of on, off, or auto. Not available on all platforms or in all modes. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:static. See [mode attributes], specifically [static]. | String | optional | "auto" | +| x_defs | Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these. | Dictionary: String -> String | optional | {} | @@ -339,30 +339,30 @@ This builds a set of tests that can be run with `bazel test`.

| Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| cdeps | The 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. | List of labels | optional | [] | -| cgo | 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. | Boolean | optional | False | -| clinkopts | List of flags to add to the C link command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | -| copts | List of flags to add to the C compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | -| cppopts | 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. | List of strings | optional | [] | -| cxxopts | List of flags to add to the C++ compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | -| data | 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. | List of labels | optional | [] | -| deps | List of Go libraries this test imports directly. These may be go_library rules or compatible rules with the [GoLibrary] provider. | List of labels | optional | [] | -| embed | 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. | List of labels | optional | [] | -| embedsrcs | 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. | List of labels | optional | [] | -| env | Environment variables to set for the test execution. The values (but not keys) are subject to [location expansion](https://docs.bazel.build/versions/main/skylark/macros.html) but not full [make variable expansion](https://docs.bazel.build/versions/main/be/make-variables.html). | Dictionary: String -> String | optional | {} | -| gc_goopts | List of flags to add to the Go compilation command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | -| gc_linkopts | List of flags to add to the Go link command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | -| goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | -| goos | Forces a binary to be cross-compiled for a specific operating system. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | -| gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] | -| importpath | The import path of this test. Tests can't actually be imported, but this may be used by [go_path] and other tools to report the location of source files. This may be inferred from embedded libraries. | String | optional | "" | -| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.

| String | optional | "normal" | -| msan | Controls whether code is instrumented for memory sanitization. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:msan. See [mode attributes], specifically [msan]. | String | optional | "auto" | -| pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting CGO_ENABLED. May be one of on, off, or auto. If auto, pure mode is enabled when no C/C++ toolchain is configured or when cross-compiling. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:pure. See [mode attributes], specifically [pure]. | String | optional | "auto" | -| race | Controls whether code is instrumented for race detection. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:race. See [mode attributes], specifically [race]. | String | optional | "auto" | -| rundir | A directory to cd to before the test is run. This should be a path relative to the execution dir of the test.

The default behaviour is to change to the workspace relative path, this replicates the normal behaviour of go test so it is easy to write compatible tests.

Setting it to . makes the test behave the normal way for a bazel test.

***Note:*** This defaults to the package path. | String | optional | "" | -| srcs | 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]. | List of labels | optional | [] | -| static | Controls whether a binary is statically linked. May be one of on, off, or auto. Not available on all platforms or in all modes. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:static. See [mode attributes], specifically [static]. | String | optional | "auto" | -| x_defs | Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these. | Dictionary: String -> String | optional | {} | +| cdeps | The 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. | List of labels | optional | [] | +| cgo | 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. | Boolean | optional | False | +| clinkopts | List of flags to add to the C link command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | +| copts | List of flags to add to the C compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | +| cppopts | 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. | List of strings | optional | [] | +| cxxopts | List of flags to add to the C++ compilation command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if cgo = True. | List of strings | optional | [] | +| data | 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. | List of labels | optional | [] | +| deps | List of Go libraries this test imports directly. These may be go_library rules or compatible rules with the [GoLibrary] provider. | List of labels | optional | [] | +| embed | 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. | List of labels | optional | [] | +| embedsrcs | 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. | List of labels | optional | [] | +| env | Environment variables to set for the test execution. The values (but not keys) are subject to [location expansion](https://docs.bazel.build/versions/main/skylark/macros.html) but not full [make variable expansion](https://docs.bazel.build/versions/main/be/make-variables.html). | Dictionary: String -> String | optional | {} | +| gc_goopts | List of flags to add to the Go compilation command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | +| gc_linkopts | List of flags to add to the Go link command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] | +| goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | +| goos | Forces a binary to be cross-compiled for a specific operating system. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | +| gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] | +| importpath | The import path of this test. Tests can't actually be imported, but this may be used by [go_path] and other tools to report the location of source files. This may be inferred from embedded libraries. | String | optional | "" | +| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.

| String | optional | "normal" | +| msan | Controls whether code is instrumented for memory sanitization. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:msan. See [mode attributes], specifically [msan]. | String | optional | "auto" | +| pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting CGO_ENABLED. May be one of on, off, or auto. If auto, pure mode is enabled when no C/C++ toolchain is configured or when cross-compiling. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:pure. See [mode attributes], specifically [pure]. | String | optional | "auto" | +| race | Controls whether code is instrumented for race detection. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:race. See [mode attributes], specifically [race]. | String | optional | "auto" | +| rundir | A directory to cd to before the test is run. This should be a path relative to the execution dir of the test.

The default behaviour is to change to the workspace relative path, this replicates the normal behaviour of go test so it is easy to write compatible tests.

Setting it to . makes the test behave the normal way for a bazel test.

***Note:*** This defaults to the package path. | String | optional | "" | +| srcs | 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]. | List of labels | optional | [] | +| static | Controls whether a binary is statically linked. May be one of on, off, or auto. Not available on all platforms or in all modes. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:static. See [mode attributes], specifically [static]. | String | optional | "auto" | +| x_defs | Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these. | Dictionary: String -> String | optional | {} | diff --git a/go/private/rules/BUILD.bazel b/go/private/rules/BUILD.bazel index 21063d701d..b3ce9b8709 100644 --- a/go/private/rules/BUILD.bazel +++ b/go/private/rules/BUILD.bazel @@ -28,6 +28,7 @@ bzl_library( "//go/private:mode", "//go/private:providers", "//go/private/rules:transition", + "@bazel_skylib//lib:dicts", ], ) @@ -117,6 +118,7 @@ bzl_library( "//go/private:providers", "//go/private/rules:binary", "//go/private/rules:transition", + "@bazel_skylib//lib:dicts", "@bazel_skylib//lib:structs", ], ) @@ -133,6 +135,7 @@ bzl_library( "//go/private:mode", "//go/private:platforms", "//go/private:providers", + "//go/private/skylib/lib:dicts", ], ) diff --git a/go/private/rules/binary.bzl b/go/private/rules/binary.bzl index 61bf1087a7..56f1ac1f95 100644 --- a/go/private/rules/binary.bzl +++ b/go/private/rules/binary.bzl @@ -29,7 +29,10 @@ load( ) load( "//go/private/rules:transition.bzl", - "go_transition_rule", + "ForwardingPastTransitionProvider", + "forward_through_transition_impl", + "go_transition", + "transition_attrs", ) load( "//go/private:mode.bzl", @@ -43,6 +46,10 @@ load( "//go/private:rpath.bzl", "rpath", ) +load( + "@bazel_skylib//lib:dicts.bzl", + "dicts", +) _EMPTY_DEPSET = depset([]) @@ -123,7 +130,7 @@ def _go_binary_impl(ctx): executable = executable, ) - providers = [ + providers_to_forward = [ library, source, archive, @@ -131,11 +138,6 @@ def _go_binary_impl(ctx): cgo_exports = archive.cgo_exports, compilation_outputs = [archive.data.file], ), - DefaultInfo( - files = depset([executable]), - runfiles = runfiles, - executable = executable, - ), ] # If the binary's linkmode is c-archive or c-shared, expose CcInfo @@ -164,9 +166,17 @@ def _go_binary_impl(ctx): ccinfo = cc_common.merge_cc_infos( cc_infos = [ccinfo] + [d[CcInfo] for d in source.cdeps], ) - providers.append(ccinfo) + providers_to_forward.append(ccinfo) - return providers + forwarding_provider = ForwardingPastTransitionProvider(executable = executable, runfiles = runfiles, providers_to_forward = providers_to_forward) + return providers_to_forward + [ + forwarding_provider, + DefaultInfo( + files = depset([executable]), + runfiles = runfiles, + executable = executable, + ), + ] go_binary_attrs = { "srcs": attr.label_list( @@ -390,7 +400,15 @@ _go_binary_kwargs = { } go_binary = rule(**_go_binary_kwargs) -go_transition_binary = go_transition_rule(**_go_binary_kwargs) + +go_transition_binary = rule( + implementation = forward_through_transition_impl, + attrs = dicts.add(transition_attrs, { + "is_windows": attr.bool(), + "transition_dep": attr.label(cfg = go_transition), + }), + executable = True, +) def _go_tool_binary_impl(ctx): sdk = ctx.attr.sdk[GoSDK] diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl index fd134c66cd..fdac00ca55 100644 --- a/go/private/rules/test.bzl +++ b/go/private/rules/test.bzl @@ -39,12 +39,19 @@ load( ) load( "//go/private/rules:transition.bzl", - "go_transition_rule", + "ForwardingPastTransitionProvider", + "forward_through_transition_impl", + "go_transition", + "transition_attrs", ) load( "//go/private:mode.bzl", "LINKMODE_NORMAL", ) +load( + "@bazel_skylib//lib:dicts.bzl", + "dicts", +) load( "@bazel_skylib//lib:structs.bzl", "structs", @@ -53,6 +60,22 @@ load( def _testmain_library_to_source(go, attr, source, merge): source["deps"] = source["deps"] + [attr.library] +go_transition_test = rule( + implementation = forward_through_transition_impl, + attrs = dicts.add(transition_attrs, { + "transition_dep": attr.label(cfg = go_transition), + "is_windows": attr.bool(), + # Workaround for bazelbuild/bazel#6293. See comment in lcov_merger.sh. + "_lcov_merger": attr.label( + executable = True, + default = "//go/tools/builders:lcov_merger", + cfg = "target", + ), + }), + executable = True, + test = True, +) + def _go_test_impl(ctx): """go_test_impl implements go testing. @@ -167,13 +190,8 @@ def _go_test_impl(ctx): # source file is present, Bazel will set the COVERAGE_OUTPUT_FILE # environment variable during tests and will save that file to the build # events + test outputs. - return [ + providers_to_forward = [ test_archive, - DefaultInfo( - files = depset([executable]), - runfiles = runfiles, - executable = executable, - ), OutputGroupInfo( compilation_outputs = [internal_archive.data.file], ), @@ -186,6 +204,21 @@ def _go_test_impl(ctx): testing.TestEnvironment(env), ] + forwarding_provider = ForwardingPastTransitionProvider( + executable = executable, + runfiles = runfiles, + providers_to_forward = providers_to_forward, + ) + + return providers_to_forward + [ + forwarding_provider, + DefaultInfo( + files = depset([executable]), + runfiles = runfiles, + executable = executable, + ), + ] + go_test_attrs = { "data": attr.label_list( allow_files = True, @@ -443,7 +476,6 @@ _go_test_kwargs = { } go_test = rule(**_go_test_kwargs) -go_transition_test = go_transition_rule(**_go_test_kwargs) def _recompile_external_deps(go, external_source, internal_archive, library_labels): """Recompiles some archives in order to split internal and external tests. diff --git a/go/private/rules/transition.bzl b/go/private/rules/transition.bzl index bf9245fbdd..adab4139d0 100644 --- a/go/private/rules/transition.bzl +++ b/go/private/rules/transition.bzl @@ -36,6 +36,10 @@ load( "//go/platform:crosstool.bzl", "platform_from_crosstool", ) +load( + "//go/private/skylib/lib:dicts.bzl", + "dicts", +) def filter_transition_label(label): """Transforms transition labels for the current workspace. @@ -54,7 +58,24 @@ def filter_transition_label(label): else: return str(Label(label)) -def go_transition_wrapper(kind, transition_kind, name, **kwargs): +ForwardingPastTransitionProvider = provider( + """Provider for a go_transition_wrapper to forward information from its dep. + + go_transition_wrapper symlinks outputs from some rule, after performing a + transition on the rule it depends on. It also forwards all of the providers + from that rule. + + This provider allows structured passing of the required data in order to + perform that forwarding. + """, + fields = { + "executable": "File: The executable of the dep, which should be symlinked to.", + "runfiles": "Runfiles: The runfiles of the dep.", + "providers_to_forward": "[Provider]: The providers the transition-wrapping rule should expose.", + }, +) + +def go_transition_wrapper(kind, transition_kind, name, use_basename, keys_to_strip, **kwargs): """Wrapper for rules that may use transitions. This is used in place of instantiating go_binary or go_transition_binary @@ -64,9 +85,34 @@ def go_transition_wrapper(kind, transition_kind, name, **kwargs): configuration identical to the default configuration. """ transition_keys = ("goos", "goarch", "pure", "static", "msan", "race", "gotags", "linkmode") + keys_to_strip = [k for k in keys_to_strip if k not in transition_keys] need_transition = any([key in kwargs for key in transition_keys]) if need_transition: - transition_kind(name = name, **kwargs) + transitioned_name = name + ".transitioned" + + # Strip out attrs that are expected by the underlying rule. + # This means that we strip out things like srcs, because we don't need them, + # but preserve things like tags and exec_compatible_with, + # which are general to all tests rather than specific to go_test specifically, + # and should apply to generated tests and binaries too. + transition_kwargs = dicts.omit(kwargs, keys_to_strip) + transition_kwargs["name"] = name + transition_kwargs["transition_dep"] = transitioned_name + transition_kwargs["is_windows"] = select({ + "@bazel_tools//src/conditions:windows": True, + "//conditions:default": False, + }) + transition_kind(**transition_kwargs) + + tags = kwargs.pop("tags", []) + if "manual" not in tags: + tags += ["manual"] + kwargs["tags"] = tags + + if use_basename and "basename" not in kwargs: + kwargs["basename"] = name + + kind(name = transitioned_name, **kwargs) else: kind(name = name, **kwargs) @@ -105,13 +151,6 @@ transition_attrs = { ), } -def go_transition_rule(**kwargs): - """Like "rule", but adds a transition and mode attributes.""" - kwargs = dict(kwargs) - kwargs["attrs"].update(transition_attrs) - kwargs["cfg"] = go_transition - return rule(**kwargs) - def _go_transition_impl(settings, attr): # NOTE(bazelbuild/bazel#11409): Calling fail here for invalid combinations # of flags reports an error but does not stop the build. @@ -269,7 +308,7 @@ def _go_reset_target_impl(ctx): new_executable = None original_executable = default_info.files_to_run.executable - default_runfiles = default_info.default_runfiles + runfiles_wrapper = default_info if original_executable: # In order for the symlink to have the same basename as the original # executable (important in the case of proto plugins), put it in a @@ -280,13 +319,13 @@ def _go_reset_target_impl(ctx): target_file = original_executable, is_executable = True, ) - default_runfiles = default_runfiles.merge(ctx.runfiles([new_executable])) + runfiles_wrapper = _add_to_runfiles(ctx, runfiles_wrapper, new_executable) providers.append( DefaultInfo( files = default_info.files, - data_runfiles = default_info.data_runfiles, - default_runfiles = default_runfiles, + data_runfiles = runfiles_wrapper.data_runfiles, + default_runfiles = runfiles_wrapper.default_runfiles, executable = new_executable, ), ) @@ -327,3 +366,38 @@ def _set_ternary(settings, attr, name): label = filter_transition_label("@io_bazel_rules_go//go/config:{}".format(name)) settings[label] = value == "on" return value + +def _symlink_file_to_rule_name(ctx, src): + output_file_name = ctx.label.name + (".exe" if ctx.attr.is_windows else "") + dst = ctx.actions.declare_file(paths.join(ctx.label.name, output_file_name)) + ctx.actions.symlink( + output = dst, + target_file = src, + is_executable = True, + ) + return dst + +def _add_to_runfiles(ctx, default_info, file): + if file == None: + return default_info + + data_runfiles = default_info.data_runfiles.merge(ctx.runfiles([file])) + default_runfiles = default_info.data_runfiles.merge(ctx.runfiles([file])) + return struct( + data_runfiles = data_runfiles, + default_runfiles = default_runfiles, + ) + +def forward_through_transition_impl(ctx): + forwarding_provider = ctx.attr.transition_dep[0][ForwardingPastTransitionProvider] + default_info = ctx.attr.transition_dep[0][DefaultInfo] + + copied_executable = _symlink_file_to_rule_name(ctx, forwarding_provider.executable) + runfiles = _add_to_runfiles(ctx, default_info, copied_executable) + + return [DefaultInfo( + executable = copied_executable, + files = default_info.files, + data_runfiles = runfiles.data_runfiles, + default_runfiles = runfiles.default_runfiles, + )] + forwarding_provider.providers_to_forward diff --git a/go/private/rules/wrappers.bzl b/go/private/rules/wrappers.bzl index c7ef6ed97a..132d2f83d2 100644 --- a/go/private/rules/wrappers.bzl +++ b/go/private/rules/wrappers.bzl @@ -24,11 +24,13 @@ load( load( "//go/private/rules:binary.bzl", "go_binary", + "go_binary_attrs", "go_transition_binary", ) load( "//go/private/rules:test.bzl", "go_test", + "go_test_attrs", "go_transition_test", ) load( @@ -48,7 +50,7 @@ def go_library_macro(name, **kwargs): def go_binary_macro(name, **kwargs): """See docs/go/core/rules.md#go_binary for full documentation.""" _cgo(name, kwargs) - go_transition_wrapper(go_binary, go_transition_binary, name = name, **kwargs) + go_transition_wrapper(go_binary, go_transition_binary, name = name, use_basename = True, keys_to_strip = go_binary_attrs.keys(), **kwargs) if kwargs.get("linkmode") in (LINKMODE_C_ARCHIVE, LINKMODE_C_SHARED): # Create an alias to tell users of the `.cc` rule that it is deprecated. native.alias( @@ -62,4 +64,4 @@ def go_binary_macro(name, **kwargs): def go_test_macro(name, **kwargs): """See docs/go/core/rules.md#go_test for full documentation.""" _cgo(name, kwargs) - go_transition_wrapper(go_test, go_transition_test, name = name, **kwargs) + go_transition_wrapper(go_test, go_transition_test, name = name, use_basename = False, keys_to_strip = go_test_attrs.keys(), **kwargs) diff --git a/go/private/skylib/lib/BUILD.bazel b/go/private/skylib/lib/BUILD.bazel index c7cd85c778..f36c887c3c 100644 --- a/go/private/skylib/lib/BUILD.bazel +++ b/go/private/skylib/lib/BUILD.bazel @@ -18,3 +18,9 @@ bzl_library( srcs = ["versions.bzl"], visibility = ["//go:__subpackages__"], ) + +bzl_library( + name = "dicts", + srcs = ["dicts.bzl"], + visibility = ["//go:__subpackages__"], +) diff --git a/go/private/skylib/lib/dicts.bzl b/go/private/skylib/lib/dicts.bzl new file mode 100644 index 0000000000..c0276936a0 --- /dev/null +++ b/go/private/skylib/lib/dicts.bzl @@ -0,0 +1,16 @@ +# This function isn't yet in a released skylib. It's taken from https://github.com/bazelbuild/bazel-skylib/blob/8334f938c1574ef6f1f7a38a03550a31df65274e/lib/dicts.bzl + +def _omit(dictionary, keys): + """Returns a new `dict` that has all the entries of `dictionary` with keys not in `keys`. + Args: + dictionary: A `dict`. + keys: A sequence. + Returns: + A new `dict` that has all the entries of `dictionary` with keys not in `keys`. + """ + keys_set = {k: None for k in keys} + return {k: dictionary[k] for k in dictionary if k not in keys_set} + +dicts = struct( + omit = _omit, +)