runfiles library: py_binary can run java_binary

Add "JAVA_RUNFILES" (and "RUNFILES_DIR") to the
dict that Runfiles.EnvVars() returns in the Python
runfiles library, so Python programs that use the
Python runfiles library in @bazel_tools can now
run java_binary data-dependencies as subprocesses
and the latter will find the runfiles.

See https://github.com/bazelbuild/bazel/issues/4460

Change-Id: I0566f6d3c68631a1d99e67964fbe8019ee82324b
PiperOrigin-RevId: 185812948
diff --git a/src/tools/runfiles/runfiles.py b/src/tools/runfiles/runfiles.py
index 8b00539..3e06203 100644
--- a/src/tools/runfiles/runfiles.py
+++ b/src/tools/runfiles/runfiles.py
@@ -175,8 +175,23 @@
             result[tokens[0]] = tokens[1]
     return result
 
+  def _GetRunfilesDir(self):
+    if self._path.endswith("/MANIFEST") or self._path.endswith("\\MANIFEST"):
+      return self._path[:-len("/MANIFEST")]
+    elif self._path.endswith(".runfiles_manifest"):
+      return self._path[:-len("_manifest")]
+    else:
+      return ""
+
   def EnvVars(self):
-    return {"RUNFILES_MANIFEST_FILE": self._path}
+    directory = self._GetRunfilesDir()
+    return {
+        "RUNFILES_MANIFEST_FILE": self._path,
+        "RUNFILES_DIR": directory,
+        # TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
+        # pick up RUNFILES_DIR.
+        "JAVA_RUNFILES": directory,
+    }
 
 
 class _DirectoryBased(object):
@@ -196,4 +211,9 @@
     return posixpath.join(self._runfiles_root, path)
 
   def EnvVars(self):
-    return {"RUNFILES_DIR": self._runfiles_root}
+    return {
+        "RUNFILES_DIR": self._runfiles_root,
+        # TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
+        # pick up RUNFILES_DIR.
+        "JAVA_RUNFILES": self._runfiles_root,
+    }