thinlizzy
thinlizzy

Reputation: 708

Making a Bazel genrule that modifies an executable generated by cc_binary

Assuming I have a cc_binary() rule like this:

cc_binary(
    name = "App",
    srcs = [
        "app.cpp",
    ],
)

This will produce App.exe somewhere in bazel-bin. I need to write a genrule that can read App.exe and produce another version of it. How can I do that?

Edit: this is my current attempt for a genrule, but it forces the recompilation of :hello_main with a different configuration and I end up getting errors like "error C2955: 'std::pair': use of class template requires template argument list"

genrule(
    name = "tbds",
    srcs = [
        ":data",
    ],
    outs = ["tbds.exe"],
    exec_tools = ["@libpackfiles//FilePacker"],
    tools = [":hello_main"],
    cmd = "cp $(location :hello_main) $(OUTS) && $(location @libpackfiles//FilePacker) $(OUTS) -files $(SRCS) -append",
    executable = true,
)

Upvotes: 0

Views: 1502

Answers (1)

slsy
slsy

Reputation: 1304

cc_binary(
    name = "hello_main",
    srcs = ["hello_main.cc"],
    deps = [
        ":hello",
    ],
)

genrule(
    name = "foo",
    outs = ["out.txt"],
    cmd = "du -sh  $(location :hello_main)` > $@",
    tools = [":hello_main"],
    visibility = ["//visibility:public"],
)

will create a out.txt file with an output of a du -sh command. You can add an another tool to a tools attribute to run some transforming script on your binary

Upvotes: 1

Related Questions