-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule go_bootstrap_binary for making Go tools for buck
Summary: > Produces a Go binary for use in prelude. Similar to `python_bootstrap_binary`. It doesn't depend on other Go rules and uses `go build` under the hood. CGo is disabled minimise dependencies. The main purpose is to build tools for Go toolchain, e.q. test-main-gen or embedcfg-gen. However it can be used for other rules as Go wrappers are faster than Python It also may be used for building third-party Go binaries Reviewed By: leoleovich Differential Revision: D59154803 fbshipit-source-id: c3cf5accfb671bd37db7175c553f4ed4a7dd3c66
- Loading branch information
1 parent
6252d88
commit e0a7844
Showing
4 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under both the MIT license found in the | ||
# LICENSE-MIT file in the root directory of this source tree and the Apache | ||
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory | ||
# of this source tree. | ||
|
||
load(":toolchain.bzl", "GoToolchainInfo", "get_toolchain_env_vars") | ||
|
||
def go_bootstrap_binary_impl(ctx: AnalysisContext) -> list[Provider]: | ||
""" | ||
Produces a Go binary for use in prelude. Similar to `python_bootstrap_binary` | ||
It doesn't depend on other Go rules and uses `go build` under the hood. | ||
CGo is disabled minimise dependencies. | ||
""" | ||
go_toolchain = ctx.attrs._go_toolchain[GoToolchainInfo] | ||
|
||
target_is_win = go_toolchain.env_go_os == "windows" | ||
exe_suffix = ".exe" if target_is_win else "" | ||
output = ctx.actions.declare_output(ctx.label.name + exe_suffix) | ||
|
||
# Copy files, becode go:embed doesn't work with symlinks | ||
srcs_dir = ctx.actions.copied_dir( | ||
"__srcs_dir__", | ||
{src.short_path: src for src in ctx.attrs.srcs}, | ||
) | ||
|
||
cmd = cmd_args([ | ||
go_toolchain.go_wrapper, | ||
go_toolchain.go, | ||
["--workdir", srcs_dir], | ||
"build", | ||
["-o", cmd_args(output.as_output(), relative_to = srcs_dir)], | ||
ctx.attrs.entrypoints, | ||
]) | ||
|
||
env = get_toolchain_env_vars(go_toolchain) | ||
env["CGO_ENABLED"] = "0" | ||
|
||
ctx.actions.run(cmd, env = env, category = "go_bootstrap_binary") | ||
|
||
return [ | ||
DefaultInfo(default_output = output), | ||
RunInfo(args = [output]), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters