Bazel go binary: Difference between revisions
Jump to navigation
Jump to search
Line 5: | Line 5: | ||
=Example= | =Example= | ||
The | |||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
load("//meta/rules:go.bzl", "go_binary", "go_library") | |||
go_library( | |||
name = "lib", | |||
srcs = [ | |||
"aux.go", | |||
"main.go", | |||
], | |||
visibility = ["//visibility:private"], | |||
) | |||
go_binary( | |||
name = "somebin", | |||
embed = [":lib"], | |||
visibility = ["//visibility:public"], | |||
) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
To build the binary: | |||
<syntaxhighlight lang='bash'> | |||
bazel build //somedir/someotherdir/some_bazel_package:somebin | |||
</syntaxhighlight> | |||
To run the binary: | |||
<syntaxhighlight lang='bash'> | |||
bazel run //somedir/someotherdir/some_bazel_package:somebin | |||
</syntaxhighlight> | |||
=A More Complex Example= | =A More Complex Example= |
Revision as of 21:48, 20 September 2024
Internal
Overview
Example
The
load("//meta/rules:go.bzl", "go_binary", "go_library")
go_library(
name = "lib",
srcs = [
"aux.go",
"main.go",
],
visibility = ["//visibility:private"],
)
go_binary(
name = "somebin",
embed = [":lib"],
visibility = ["//visibility:public"],
)
To build the binary:
bazel build //somedir/someotherdir/some_bazel_package:somebin
To run the binary:
bazel run //somedir/someotherdir/some_bazel_package:somebin
A More Complex Example
load("//meta/rules:go.bzl", "go_library", "go_binary")
go_library(
name = "somepkg_lib",
srcs = [
"file1.go",
"file2.go",
],
importpath = "github.com/someorg/somemodule/pkg/somepkg",
visibility = ["//visibility:private"],
deps = [
"//lib/a",
"//lib/b",
...
"@com_github_blang_semver//:semver",
],
)
go_binary(
name = "somebin",
embed = [":somepkg_lib"],
visibility = ["//visibility:public"],
x_defs = {"github.com/someorg/somemodule/version.Version": "{BUILD_VERSION}"},
)
Note: the dependencies can be autogenerated with Gazelle.