What's the preferred way to get run_files directory?

5,074 views
Skip to first unread message

c...@nuro.ai

unread,
Jul 6, 2018, 3:35:17 PM7/6/18
to bazel-discuss
Is using $0.runfiles/path the preferred way to get the runfile for a cc_binary?


I have a py_binary which executes another cc_binary, and I put the cc_binary as data dep of the py_binary.


cc_binary(
name = "main",
srcs = ["main.cc"],
data = [":runfile.txt"]
)

py_binary(
name = "py_main",
srcs = ["py_main.py"],
data = [":main"],
)


Inspecting the output directory hierarchy, I noticed the runfiles of py_binary contains the data needed for both, but there's no main.runfiles directory or symlink to the root of runfiles.

~/bazeltest (master) $ ls -R bazel-bin/py_main.runfiles
bazel-bin/py_main.runfiles:
__main__ MANIFEST

bazel-bin/py_main.runfiles/__main__:
main py_main py_main.py runfile.txt

That means, if I invoke the cc binary in the py, it cannot use $0.runfiles/runfile.txt because that will point to
bazel-bin/py_main.runfiles/__main__/main.runfiles/runfile.txt, which doesn't exist.


What's the best way for main to get the runfile, and works in both cases where it's directly executed or it's executed as part of py_binary?

László Csomor

unread,
Jul 9, 2018, 2:54:00 AM7/9/18
to c...@nuro.ai, bazel-discuss
Use the runfiles libraries built into Bazel:

See the comments I linked for full usage information, including the use case you described.


--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/49f25cb3-7d23-4ca8-8171-1e3af8597abf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

petros.es...@gmail.com

unread,
Jul 19, 2018, 10:15:47 PM7/19/18
to bazel-discuss
László, are there runfiles libraries for java binaries?

petros.es...@gmail.com

unread,
Jul 19, 2018, 10:40:54 PM7/19/18
to bazel-discuss
My apologies on the last message. I may have misinterpreted the intent
of the initial post (and also included a bunch of cruft).

Nevertheless, what I wanted to know was there an officially supported way to
obtain a path to the runfiles for a java_binary from a skylark rule?

Currently, I am leveraging an implementation detail in the java_binary
wrapper script to set a number of system properties, relative to the
runfiles path.

Concretely, in the macro that creates a java_binary, one of the
jvm_flags I use is

-Dsome.company.required.config=$(readlink -f ${RUNPATH:-.})/<path>

After the java_binary is compiled, the bazel-bin wrapper script contains
the above as a JVM_FLAG. This causes no issues since it explicitly defines
the RUNPATH variable earlier in the script [0].

The fundamental reason, I am doing all of this is that I need to define
a set of java system properties that require the paths of key configuration
files.

[0]https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt#L245

László Csomor

unread,
Jul 20, 2018, 2:53:38 AM7/20/18
to petros.es...@gmail.com, bazel-discuss
Hey Petros,

No worries! Yes, for the record there's a runfiles library for Java too, usage info is here [1].
My understanding is, you need this info in a *macro* (you said *rule* at some point).
There's a canonical way to solve the problem, without using the RUNPATH-based hackery: `java_binary.jvm_flags` supports make variable expansion [2], so you can use:
  jvm_flags=["-Dsome.company.required.config=$(location //path/to:data)"]

Does that help?


--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages