3

I've a c target that always must be compiled for darwin_x86_64, no matter the --cpu set when calling bazel build. It's the only target that always must be compiled for a specific cpu in a big project.

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
)

In the bazel documentation it seems to be possible to do this using transitions. Maybe something like:

def _force_x86_64_impl(settings, attr):
    return {"//command_line_option:cpu": "darwin_x86_64"}

force_x86_64 = transition(
    implementation = _force_x86_64_impl,
    inputs = [],
    outputs = ["//command_line_option:cpu"]
)

But how do I tie these two together? I'm probably missing something obvious, but I can't seem to find the relevant documentation over at bazel.build.

StianE
  • 3,055
  • 20
  • 20

2 Answers2

0

You've got the transition defined, now you need to attach it to a rule.

r = rule(
    implementation = _r_impl,
    attrs = {
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
        "srcs": attr.label(cfg = force_x86_64),
    },
)

def _r_impl(ctx):
    return [DefaultInfo(files = ctx.attr.srcs[0][DefaultInfo].files)]

This defines a rule that attaches the appropriate transition to the srcs you pass it, and then simply forwards the list of files from DefaultInfo. Depending on what you're doing, this might be sufficient, or you might also want to forward the runfiles contained in DefaultInfo and/or other providers.

Brian Silverman
  • 3,085
  • 11
  • 13
  • This is interesting. To complete the example, you'd then create a new rule: `r(label="my_forced_binary", srcs="hello-world")` Is there any way to inject the configuration into `cc_binary` through a macro? Or force the entire build graph to adopt a transition no matter what? – Zendel Aug 22 '23 at 12:14
  • You could definitely wrap that in a macro which creates `r(name=name, srcs=name + "__impl"); cc_binary(name=name + "__impl", **kwargs)`. If you want to apply it to the top-level build targets, just use command line flags (`--cpu=darwin_x86_64`). You can't want to apply it to the entire build graph, things like host transitions will change it further. – Brian Silverman Aug 22 '23 at 20:12
-1

Use target_compatible_with

Example (only compiled on Windows):

cc_library(
    name = "win_driver_lib",
    srcs = ["win_driver_lib.cc"],
    target_compatible_with = [
        "@platforms//cpu:x86_64",
        "@platforms//os:windows",
    ],
)
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90