bazel icon indicating copy to clipboard operation
bazel copied to clipboard

extra_action support for rules written in Skylark

Open schroederc opened this issue 11 years ago • 8 comments

Based on src/main/protobuf/extra_actions_base.proto, it seems that extra_action support is hardcoded for native Bazel rules only.

Kythe would find it helpful to support extra_actions for custom rules written in Skylark so that we can extract/analyze compilations from a broader range of languages.

schroederc avatar Apr 15 '15 22:04 schroederc

Spawn actions have built-in support for extra actions; whenever a skylark rule generates a spawn action, it immediately becomes available to kythe. I was wondering if we should actually remove the language-specific extra actions for C++ and Java (at least) and use the spawn action extra actions instead.

On Thu, Apr 16, 2015 at 12:41 AM, Cody Schroeder [email protected] wrote:

Based on src/main/protobuf/extra_actions_base.proto https://github.com/google/bazel/blob/f7afc7ace96c042ec3d1aafd158ae19fcb41446e/src/main/protobuf/extra_actions_base.proto, it seems that extra_action support is hardcoded for native Bazel rules only.

Kythe would find it helpful to support extra_actions for custom rules written in Skylark so that we can extract/analyze compilations from a broader range of languages.

— Reply to this email directly or view it on GitHub https://github.com/google/bazel/issues/124.

ulfbot avatar Apr 17 '15 12:04 ulfbot

I'm trying to do something like this and it doesn't seem to work.

kythe/go/util/tools/BUILD

action_listener(
    name = "print_extra_actions",
    extra_actions = [":printer"],
    mnemonics = [
        "GoCompile",              # the actual mnemonic I'm looking for
        "SpawnAction", "Unknown", # me trying to grep for anything
    ],
)

extra_action(
    name = "printer",
    cmd = "$(location :print_extra_action) $(EXTRA_ACTION_FILE)",
    tools = [":print_extra_action"],
)

go_binary(
    name = "print_extra_action",
    srcs = ["print_extra_action/print_extra_action.go"],
    deps = [
        "//third_party/bazel:extra_actions_base_proto",
        "//third_party/go:protobuf",
    ],
)

go.bzl (highly elided)

def go_library_impl(ctx):
  # ...

  ctx.action(
      inputs = inputs,
      outputs = outs,
      mnemonic = "GoCompile",  # removing this doesn't allow the action to be caught by the "Unknown" mnemonic
      command = cmd,
      use_default_shell_env = True)

  return struct(
    # ...
  )

# ...

go_library = rule(
    go_library_impl,
    attrs = base_attrs + {
        "package": attr.string(),
    },
    outputs = {"archive": "%{name}.a"},
)
bazel build --experimental_action_listener=//kythe/go/util/tools:print_extra_actions //...
# nothing is printed
# however, adding "Javac" or "CppCompile" to "print_extra_actions" ensures that java/c++ actions are printed

schroederc avatar Apr 17 '15 16:04 schroederc

Also, I'm in favor of removing the language-specific extra_actions. As long as all of the current information in JavaCompileInfo/CppCompileInfo is still accessible.

Just as a thought from someone not thoroughly versed on Bazel, I was thinking that is may be useful to embed a Map<string, Any> proto field in the ExtraActionInfo and allow the export of arbitrary information like the JavaCompilationArgs from Java rules and arbitrary lists/sets from Skylark. It could possibly be directly linked to a rule's providers instead of being a different mechanism.

schroederc avatar Apr 17 '15 16:04 schroederc

I don't know what's going on there. Skylark rules should be generating spawn actions, and they should automatically get extra actions.

I don't think we pass any information that isn't in the command-line, but I could be wrong. If I'm right, then you can always parse the command line and get all the information back. In the Java case, you can use the parser from JavaBuilder. However, in the C++ case, you'll need a parser that is compatible with gcc and / or clang. Having a map would avoid requiring a parser in some cases because the data is more structured.

ulfjack avatar Apr 21 '15 19:04 ulfjack

Are there any updates on fixing the extra_action support for Skylark spawn actions?

schroederc avatar Jun 29 '15 18:06 schroederc

We're having this same issue with our own Lisp build rules. My own extra_action is not invoked when using our own mnemonic, LispCompile, while it does get invoked when trying with CppCompile. Can someone please take a look?

jinwoo avatar Jun 29 '15 18:06 jinwoo

I have a fix, to be submitted in a few days.

Sorry for the delay!

laurentlb avatar Jul 10 '15 18:07 laurentlb

I believe this is now fixed.

schroederc avatar Sep 23 '15 16:09 schroederc