Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go_binary: support build constraints via build_setting #1351

Closed
justinsb opened this issue Feb 25, 2018 · 2 comments · Fixed by #2414 or #2440
Closed

go_binary: support build constraints via build_setting #1351

justinsb opened this issue Feb 25, 2018 · 2 comments · Fixed by #2414 or #2440

Comments

@justinsb
Copy link

I have a library which requires custom build tags to build it (because there are two modes of operation, and we switch between them using build tags).

Specifically, if building from make I need to do this:

go build -o $@ -tags 'peer_name_alternative peer_name_hash' k8s.io/kops/protokube/cmd/protokube

When I try what I believe to be the equivalent syntax with bazel the tags appear to be ignored - they aren't visible on the command line with bazel build -s for example, and I get a compilation error that suggests the code was ignored.

I'm adding this to go_library: tags = [ "peer_name_alternative", "peer_name_hash" ], # keep. Actual code is here: https://github.com/kubernetes/kops/pull/4514/files

Is there a different place / way I should be passing the tags?

@justinsb justinsb changed the title Passing build tags to go_library not working build tags specified for go_library seem to be ignored Feb 25, 2018
@jayconrod
Copy link
Contributor

Build tags aren't actually supported yet. Our compiler wrapper filters source files using tags, but it doesn't have any way to turn on custom tags. #1340 should add some support for this, plus several other changes. You'll pass --define=gotags=foo,bar,baz on the command line when building.

This assumes that tags pull in alternate files that aren't normally required, e.g., extra debug or profiling code. If you have a library that requires tags to build, maybe we can add a go_tags attribute or something like that.

Note that the tags attribute is common to all Bazel rules. rules_go doesn't interpret that attribute directly. It makes it easier to identify specific rules using bazel query, and some tags like manual have special meaning to Bazel.

@jayconrod jayconrod changed the title build tags specified for go_library seem to be ignored go_binary: support build constraints via build_setting Sep 13, 2019
@jayconrod
Copy link
Contributor

#2219 will provide a framework for supporting this.

jayconrod pushed a commit that referenced this issue Mar 26, 2020
This changes how the goos, goarch, race, msan, static, and pure
attributes of go_binary and go_test are implemented.

Previously, the go_binary and go_test rules used an aspect on the deps
and embed attributes. If any of the mode attributes listed above were
set, the aspect declared additional output files and actions to
generate them for the specified configuration. This approach had two
significant problems. First, the aspect caused the analysis to be
performed twice, wasting time and memory even in the case
where no cross-compilation was needed. Second, the aspect was not
compatible with conditional dependencies specified with select
expressions: aspects run on the configured target graph, but the mode
attributes could not affect Bazel's configuration.

This change deletes the aspect and implements these attributes using a
different mechanism: build settings and configuration transitions.

A new package is added //go/config with a build setting for each
attribute. Most settings are flags that may be set on the
command-line. This is an alternative to --feature flags, which will be
deprecated and eventually removed.

The target //:go_config gathers build settings relevant to Go and
provides a GoContextInfo (private).

The target //:go_context_data depends on //:go_config, //:stdlib,
//:nogo, and a few other standard dependencies. go_binary and go_test
rules no longer need to depend on these targets directly. Go rules
should now only need to depend on //:go_context_data and the Go
toolchain. Unfortunately, neither //:go_context_data nor //:go_config
can be part of the toolchain because the toolchain is always analyzed
for the execution configuration, not the target configuration.

Because of the simplified dependencies, the go_rule function is no
longer needed by most rules. This change makes it possible to
deprecate it.

Fixes #1351
Fixes #2219
Updates #2302
@jayconrod jayconrod added this to the v0.23 milestone Apr 7, 2020
jayconrod pushed a commit that referenced this issue Apr 17, 2020
This changes how the goos, goarch, race, msan, static, and pure
attributes of go_binary and go_test are implemented.

Previously, the go_binary and go_test rules used an aspect on the deps
and embed attributes. If any of the mode attributes listed above were
set, the aspect declared additional output files and actions to
generate them for the specified configuration. This approach had two
significant problems. First, the aspect caused the analysis to be
performed twice, wasting time and memory even in the case
where no cross-compilation was needed. Second, the aspect was not
compatible with conditional dependencies specified with select
expressions: aspects run on the configured target graph, but the mode
attributes could not affect Bazel's configuration.

This change deletes the aspect and implements these attributes using a
different mechanism: build settings and configuration transitions.

A new package is added //go/config with a build setting for each
attribute. Most settings are flags that may be set on the
command-line. This is an alternative to --feature flags, which will be
deprecated and eventually removed.

The target //:go_config gathers build settings relevant to Go and
provides a GoContextInfo (private).

The target //:go_context_data depends on //:go_config, //:stdlib,
//:nogo, and a few other standard dependencies. go_binary and go_test
rules no longer need to depend on these targets directly. Go rules
should now only need to depend on //:go_context_data and the Go
toolchain. Unfortunately, neither //:go_context_data nor //:go_config
can be part of the toolchain because the toolchain is always analyzed
for the execution configuration, not the target configuration.

Because of the simplified dependencies, the go_rule function is no
longer needed by most rules. This change makes it possible to
deprecate it.

Fixes #1351
Fixes #2219
Updates #2302
@jayconrod jayconrod reopened this Apr 17, 2020
jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Apr 17, 2020
The gotags attribute sets which build tags are enabled when evaluting
build constraints. This is equivalent to setting
--@io_bazel_rules_go//go/config:tags on the command line.

Fixes bazel-contrib#1351
jayconrod pushed a commit that referenced this issue Apr 20, 2020
The gotags attribute sets which build tags are enabled when evaluting
build constraints. This is equivalent to setting
--@io_bazel_rules_go//go/config:tags on the command line.

Fixes #1351
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants