简体   繁体   中英

How do I get the files in the build directory in another bazel rule

  1. when use the python tool to generate the .cpp/ .hpp code like the protobuf tool, but I don't know how many files will be generated, so it's a little not the same as protbuf tool.
In one genrule:
def __generate_core_ifce_impl(ctx):
    ...
    output_file = ctx.actions.declare_directory(out)
    cmd = """
        mkdir -p {path};
    """.format(path = output_file.path)
    cmd += """
        {tools} -i {src} -o {output_dir}
    """.format(tools = tools, src = ctx.files.srcs, output_dir = output_file.path)

    ctx.actions.run_shell(
        command = cmd,
        inputs = ctx.files.srcs,
        outputs = [output_file]
        )
    return [DefaultInfo(files = depset([output_file])),]

_generate_core_ifce = rule (
    implementation = __generate_core_ifce_impl,
    attrs = {
        "srcs": attr.label_list(mandatory = False, allow_files = True),
        "tools": attr.label_list(mandatory = True, allow_files = True),
        "out": attr.sting(mandatory = True),
    },
)

In output_file directory, there will generate some *.cpp && *.hpp, but i can't know their names

  1. then in another rule, cc_library will use *.cpp && *.hpp which are in output_file directory the questions is: how to write this rule? I can't get the files in the output_file diectory, so I can't write the cc_library?

You should be able to use the name of the target, and the cc_library will use the files that are given in the DefaultInfo, eg:

_generate_core_ifce(
  name = "my_generate_core_ifce_target",
  ...
)

cc_library(
  name = "my_cc_library_target",
  srcs = [":my_generate_core_ifce_target"],
  ...
)

edit: adding an example:

BUILD :

load(":defs.bzl", "my_rule")

my_rule(
  name = "my_target",
)

cc_binary(
  name = "cc",
  srcs = [":my_target"],
)

defs.bzl :

def _impl(ctx):
    output_dir = ctx.actions.declare_directory("my_outputs")
    command = """
mkdir -p {output_dir}
cat > {output_dir}/main.c <<EOF
#include "stdio.h"
#include "mylib.h"
int main() {
  printf("hello world %d\\n", get_num());
  return 0;
}
EOF

cat > {output_dir}/mylib.c <<EOF
int get_num() {
  return 42;
}
EOF

cat > {output_dir}/mylib.h <<EOF
int get_num();
EOF
""".replace("{output_dir}", output_dir.path)

    ctx.actions.run_shell(
        command = command,
        outputs = [output_dir]
    )
    return [DefaultInfo(files = depset([output_dir])),]

my_rule = rule(
    implementation = _impl,
)

usage:

$ bazel run cc
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:cc (15 packages loaded, 57 targets configured).
INFO: Found 1 target...
Target //:cc up-to-date:
  bazel-bin/cc
INFO: Elapsed time: 3.626s, Critical Path: 0.06s
INFO: 8 processes: 4 internal, 4 linux-sandbox.
INFO: Build completed successfully, 8 total actions
INFO: Build completed successfully, 8 total actions
hello world 42

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM