Skip to content

Commit

Permalink
Override -race via configuration modifiers
Browse files Browse the repository at this point in the history
Summary:
This solution looks a bit hacky, let me know if I can do better 😀
We need to override `race` option of go_binary/etc with a CLI flag (configuration modifier)
- Option `race` controls a configuration transition
- We add a "fake" option `_race` which controlled by a configuration constraint

Reviewed By: awalterschulze

Differential Revision: D53921064

fbshipit-source-id: 22f927b0ac4925a4bdbfe1ba4c3e4144c2e02667
  • Loading branch information
podtserkovskiy authored and facebook-github-bot committed Feb 20, 2024
1 parent eb9ccb3 commit 892fb0a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions prelude/go/go_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def go_binary_impl(ctx: AnalysisContext) -> list[Provider]:
get_filtered_srcs(ctx, ctx.attrs.srcs),
deps = ctx.attrs.deps,
compile_flags = ctx.attrs.compiler_flags,
race = ctx.attrs.race,
race = ctx.attrs._race,
)
(bin, runtime_files, external_debug_info) = link(
ctx,
Expand All @@ -35,7 +35,7 @@ def go_binary_impl(ctx: AnalysisContext) -> list[Provider]:
link_style = value_or(map_val(LinkStyle, ctx.attrs.link_style), LinkStyle("static")),
linker_flags = ctx.attrs.linker_flags,
link_mode = ctx.attrs.link_mode,
race = ctx.attrs.race,
race = ctx.attrs._race,
)

# runtime_files are all the artifacts that must be present in order for this
Expand Down
4 changes: 2 additions & 2 deletions prelude/go/go_exported_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def go_exported_library_impl(ctx: AnalysisContext) -> list[Provider]:
deps = ctx.attrs.deps,
compile_flags = ctx.attrs.compiler_flags,
shared = True,
race = ctx.attrs.race,
race = ctx.attrs._race,
)
(bin, runtime_files, _external_debug_info) = link(
ctx,
Expand All @@ -36,7 +36,7 @@ def go_exported_library_impl(ctx: AnalysisContext) -> list[Provider]:
linker_flags = ctx.attrs.linker_flags,
external_linker_flags = ctx.attrs.external_linker_flags,
shared = True,
race = ctx.attrs.race,
race = ctx.attrs._race,
)
return [
DefaultInfo(
Expand Down
6 changes: 3 additions & 3 deletions prelude/go/go_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ def go_test_impl(ctx: AnalysisContext) -> list[Provider]:
pkgs = pkgs,
compile_flags = ctx.attrs.compiler_flags,
coverage_mode = coverage_mode,
race = ctx.attrs.race,
race = ctx.attrs._race,
)

# Generate a main function which runs the tests and build that into another
# package.
gen_main = _gen_test_main(ctx, pkg_name, coverage_mode, coverage_vars, srcs)
pkgs[pkg_name] = tests
main = compile(ctx, "main", cmd_args(gen_main), pkgs = pkgs, coverage_mode = coverage_mode, race = ctx.attrs.race)
main = compile(ctx, "main", cmd_args(gen_main), pkgs = pkgs, coverage_mode = coverage_mode, race = ctx.attrs._race)

# Link the above into a Go binary.
(bin, runtime_files, external_debug_info) = link(
Expand All @@ -115,7 +115,7 @@ def go_test_impl(ctx: AnalysisContext) -> list[Provider]:
linker_flags = ctx.attrs.linker_flags,
shared = False,
coverage_mode = coverage_mode,
race = ctx.attrs.race,
race = ctx.attrs._race,
)

run_cmd = cmd_args(bin).hidden(runtime_files, external_debug_info)
Expand Down
3 changes: 3 additions & 0 deletions prelude/rules_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,14 @@ inlined_extra_attributes = {
"_exec_os_type": buck.exec_os_type_arg(),
"_go_stdlib": attrs.default_only(attrs.dep(default = "prelude//go/tools:stdlib")),
"_go_toolchain": toolchains_common.go(),
"_race": race_attr,
},
"go_exported_library": {
"embedcfg": attrs.option(attrs.source(allow_directory = False), default = None),
"_exec_os_type": buck.exec_os_type_arg(),
"_go_stdlib": attrs.default_only(attrs.dep(default = "prelude//go/tools:stdlib")),
"_go_toolchain": toolchains_common.go(),
"_race": race_attr,
},
"go_library": {
"embedcfg": attrs.option(attrs.source(allow_directory = False), default = None),
Expand All @@ -461,6 +463,7 @@ inlined_extra_attributes = {
"_exec_os_type": buck.exec_os_type_arg(),
"_go_stdlib": attrs.default_only(attrs.dep(default = "prelude//go/tools:stdlib")),
"_go_toolchain": toolchains_common.go(),
"_race": race_attr,
"_testmaingen": attrs.default_only(attrs.exec_dep(default = "prelude//go/tools:testmaingen")),
},

Expand Down

0 comments on commit 892fb0a

Please sign in to comment.