From 8bd3c97d61e6992cc5fd32a50f63a02839fae205 Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Mon, 4 Dec 2017 06:00:23 -0800 Subject: py_binary can now be used as data with custom python. The stub template now looks for the python binary relative to the modules if a relative path was provided. This correctly finds it inside the runfiles folder both when the py_binary is the output, and when the py_binary is called by another binary (ie is data for it). We also now add the binary and dependencies to the runfiles when it is used as data so python is accesible. Change-Id: I3bf6ff17265e72d964614ad66af22933c89f853d PiperOrigin-RevId: 177803641 --- .../build/lib/bazel/rules/python/BazelPythonSemantics.java | 6 +++++- .../build/lib/bazel/rules/python/python_stub_template.txt | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src/main/java/com/google') diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java index 52d839c4d9..701a29327d 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java @@ -74,6 +74,7 @@ public class BazelPythonSemantics implements PythonSemantics { @Override public void collectDefaultRunfilesForBinary(RuleContext ruleContext, Builder builder) { + addRuntime(ruleContext, builder); } @Override @@ -348,7 +349,10 @@ public class BazelPythonSemantics implements PythonSemantics { pythonBinary = provider.interpreterPath(); } else { // checked in Python interpreter in py_runtime - pythonBinary = provider.interpreter().getExecPathString(); + PathFragment workspaceName = + PathFragment.create(ruleContext.getRule().getPackage().getWorkspaceName()); + pythonBinary = + workspaceName.getRelative(provider.interpreter().getRunfilesPath()).getPathString(); } } else { // make use of the Python interpreter in an absolute path diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt index 6adf21f01d..84dd571afa 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt @@ -50,7 +50,7 @@ def IsRunningFromZip(): return %is_zipfile% # Find the real Python binary if it's not a normal absolute path -def FindPythonBinary(): +def FindPythonBinary(module_space): if PYTHON_BINARY.startswith('//'): # Case 1: Path is a label. Not supported yet. raise AssertionError( @@ -59,8 +59,8 @@ def FindPythonBinary(): # Case 2: Absolute path. return PYTHON_BINARY elif '/' in PYTHON_BINARY: - # Case 3: Path is relative to current working directory. - return os.path.join(os.getcwd(), PYTHON_BINARY) + # Case 3: Path is relative to the repo root. + return os.path.join(module_space, PYTHON_BINARY) else: # Case 4: Path has to be looked up in the search path. return SearchPath(PYTHON_BINARY) @@ -140,7 +140,7 @@ def Main(): assert os.access(main_filename, os.R_OK), \ 'Cannot exec() %r: file not readable.' % main_filename - program = python_program = FindPythonBinary() + program = python_program = FindPythonBinary(module_space) if python_program is None: raise AssertionError('Could not find python binary: ' + PYTHON_BINARY) args = [python_program, main_filename] + args -- cgit v1.2.3