1

I am trying to reference an output of a rule that is nested inside an output directory of another rule in a genrule.

For example, I use rules_foreign_cc to build boost:

boost_build(
    name = "boost",
    lib_source = "@boost//:all",
    linkopts = [
        "-lpthread",
    ],
    shared_libraries = [
        "libboost_chrono.so.1.72.0",
        "libboost_program_options.so.1.72.0",
        "libboost_filesystem.so.1.72.0",
        "libboost_system.so.1.72.0",
        "libboost_thread.so.1.72.0",
        "libboost_timer.so.1.72.0",
    ],
    user_options = [
        "cxxstd=17",
        "--with-chrono",
        "--with-filesystem",
        "--with-program_options",
        "--with-system",
        "--with-thread",
        "--with-timer",
        "-j4",
    ],
)

And when I build it, I see the outputs:

bazel build //:boost
INFO: Invocation ID: 36440de3-15f2-4ca0-8802-0a95f75ed926
INFO: Analyzed target //:boost (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:boost up-to-date:
  bazel-bin/boost/include
  bazel-bin/boost/lib/libboost_chrono.so.1.72.0
  bazel-bin/boost/lib/libboost_program_options.so.1.72.0
  bazel-bin/boost/lib/libboost_filesystem.so.1.72.0
  bazel-bin/boost/lib/libboost_system.so.1.72.0
  bazel-bin/boost/lib/libboost_thread.so.1.72.0
  bazel-bin/boost/lib/libboost_timer.so.1.72.0
  bazel-bin/copy_boost/boost
  bazel-bin/boost/logs/BuildBoost_script.sh
  bazel-bin/boost/logs/BuildBoost.log
  bazel-bin/boost/logs/wrapper_script.sh
INFO: Elapsed time: 0.758s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action

Boost works properly, I can reference it in cc_library targets and binaries run fine.

Now, I would like to reference one of the outputs in a genrule. The file I want to reference is nested inside the boost/lib/ directory. I would expect something like: $(location :boost/lib/libboost_program_options.so.1.72.0), but that doesn't work.

What's the proper way to reference the outputs in the directory?

Dig-Doug
  • 91
  • 8

1 Answers1

0

This is what I did, hope it helps. Inside your genrule, create a variable that hold the output array of your boost rule, use $(locations) instead of $(location). Then loop through the array and find the path you want to use later.

BOOST_OUTPUTS=( $(locations boost) )
LIBBOOST_CHRONO_SO=

for i in "$${{BOOST_OUTPUTS[@]}}"
do
    if [[ "$$i" == *"libboost_chrono.so.1.72.0"* ]]; then
        LIBBOOST_CHRONO_SO="$$i"
    fi
done

# Do something with LIBBOOST_CHRONO_SO

Just note that $$ and {{ and }} are for escaping special characters.