Bazel go binary: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 6: Line 6:
=Example=
=Example=
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
load("//meta/rules:go.bzl", "go_library", "go_binary")
load("//meta/rules:go.bzl", "go_binary", "go_library")


go_library(
go_library(
     name = "somepkg_lib",
     name = "lib",
     srcs = [
     srcs = [
         "file1.go",
         "aux.go",
         "file2.go",
         "main.go",
     ],
     ],
    importpath = "github.com/someorg/somemodule/pkg/somepkg",
     visibility = ["//visibility:private"],
     visibility = ["//visibility:private"],
    deps = [
        "//lib/a",
        "//lib/b",
        ...
        "@com_github_blang_semver//:semver",
    ],
)
)


go_binary(
go_binary(
     name = "somebin",
     name = "somebin",
     embed = [":somepkg_lib"],
     embed = [":lib"],
     visibility = ["//visibility:public"],
     visibility = ["//visibility:public"],
    x_defs = {"github.com/someorg/somemodule/version.Version": "{BUILD_VERSION}"},
)
)
</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>
To add dependencies, see: {{Internal|Bazel_BUILD_Files#Dependency_Management|BUILD Files Dependency Management}}
=A More Complex Example=
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
load("//meta/rules:go.bzl", "go_library")
load("//meta/rules:go.bzl", "go_library", "go_binary")


go_library(
go_library(
     name = "somepkg",
     name = "somepkg_lib",
     srcs = [
     srcs = [
         "file1.go",
         "file1.go",
Line 42: Line 47:
     ],
     ],
     importpath = "github.com/someorg/somemodule/pkg/somepkg",
     importpath = "github.com/someorg/somemodule/pkg/somepkg",
     visibility = ["//visibility:public"], # private if this is a library that becomes part of an executable
     visibility = ["//visibility:private"],
     deps = [
     deps = [
         "//lib/a",
         "//lib/a",
Line 49: Line 54:
         "@com_github_blang_semver//:semver",
         "@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}"},
)
)
</syntaxhighlight>
</syntaxhighlight>
To add dependencies, see: {{Internal|Bazel_BUILD_Files#Dependency_Management|BUILD Files Dependency Management}}

Latest revision as of 23:18, 20 September 2024

Internal

Overview

Example

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

To add dependencies, see:

BUILD Files Dependency Management

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}"},
)

To add dependencies, see:

BUILD Files Dependency Management