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

Allow specifying debian package architecture via a file #391

Closed
tnarg opened this issue Jul 26, 2021 · 4 comments
Closed

Allow specifying debian package architecture via a file #391

tnarg opened this issue Jul 26, 2021 · 4 comments

Comments

@tnarg
Copy link
Contributor

tnarg commented Jul 26, 2021

pkg_deb allows specifying the several arguments via the contents of a file, e.g. version_file. However, architecture may only be specified by a static string.

In my particular case, the architecture of the binary generated is controlled by the rules_go toolchain. I can generate a file containing the contents of GOARCH which i would then like to pass to the pkg_deb as architecture_file.

@tnarg
Copy link
Contributor Author

tnarg commented Jul 26, 2021

As an example, here is a simple rule I'd like to use to provide the GOARCH to pkg_deb.

load("@io_bazel_rules_go//go:def.bzl", "go_context")
load("@rules_pkg//:providers.bzl", "PackageVariablesInfo")

def _pkg_variables_go_impl(ctx):
    go = go_context(ctx)

    values = {
        "GOARCH": go.env["GOARCH"],
        "GOOS": go.env["GOOS"],
    }

    out = ctx.actions.declare_file(ctx.label.name + ".goarch")
    ctx.actions.write(
        output = out,
        content = go.env["GOARCH"],
    )

    return [
        DefaultInfo(files = depset([out])),
        PackageVariablesInfo(values = values),
    ]

#
# A rule to inject variables from the go context into package names.
#
pkg_variables_go = rule(
    implementation = _pkg_variables_go_impl,
    attrs = {
        "_go_context_data": attr.label(
            default = "@io_bazel_rules_go//:go_context_data",
        ),
    },
    toolchains = ["@io_bazel_rules_go//go:toolchain"],
    provides = [DefaultInfo, PackageVariablesInfo],
)

@tnarg
Copy link
Contributor Author

tnarg commented Jul 26, 2021

You could also write a rule to generate an architecture_file based on --cpu, e.g.

def _cpu_architecture_impl(ctx):
    arch = "all"
    if ctx.env["TARGET_CPU"] == "k8":
        arch = "amd64"
    elif ctx.env["TARGET_CPU"] == "x86":
        arch = "i386"
    elif ctx.env["TARGET_CPU"] == "arm64":
        arch = "arm64"

    out = ctx.actions.declare_file(ctx.label.name)
    ctx.actions.write(
        output = out,
        content = arch,
    )

    return DefaultInfo(files = depset([out]))

cpu_architecture = rule(
    implementation = _cpu_architecture_impl,
)

@aiuto
Copy link
Collaborator

aiuto commented Jul 30, 2021

Let's see if I understand.

  • Go uses their own architecture names that do not match Bazel (no harm in that. there is no right or wrong in naming those)
  • The traditional way to set it is with GOARCH in the environment
  • Together, that makes getting the value from environment to a file is better than using command line flags to bazel.

Sounds like your PR is the most reasonable path then. Thanks.

@aiuto
Copy link
Collaborator

aiuto commented Aug 12, 2021

Closed by #390

@aiuto aiuto closed this as completed Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants