Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions pkgs/development/tools/build-managers/bazel/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ stdenv, lib, fetchurl, fetchpatch, runCommand, makeWrapper
{ stdenv, callPackage, lib, fetchurl, fetchpatch, runCommand, makeWrapper
, jdk, zip, unzip, bash, writeCBin, coreutils
, which, python, perl, gnused, gnugrep, findutils
# Apple dependencies
Expand Down Expand Up @@ -38,6 +38,11 @@ stdenv.mkDerivation rec {
platforms = platforms.linux ++ platforms.darwin;
};

# additional tests that check bazel’s functionality
passthru.tests = {
python_bin_path = callPackage ./python-bin-path-test.nix {};
};

name = "bazel-${version}";

src = fetchurl {
Expand All @@ -47,8 +52,10 @@ stdenv.mkDerivation rec {

sourceRoot = ".";

patches =
lib.optional enableNixHacks ./nix-hacks.patch;
patches = [
(lib.optional enableNixHacks ./nix-hacks.patch)
./python-stub-path-fix.patch
];

# Bazel expects several utils to be available in Bash even without PATH. Hence this hack.

Expand Down Expand Up @@ -118,6 +125,10 @@ stdenv.mkDerivation rec {
'';

genericPatches = ''
# Substitute python's stub shebang to plain python path. (see TODO add pr URL)
substituteInPlace src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt\
--replace "/usr/bin/env python" "${python}/bin/python" \
--replace "NIX_STORE_PYTHON_PATH" "${python}/bin/python" \
# substituteInPlace is rather slow, so prefilter the files with grep
grep -rlZ /bin src/main/java/com/google/devtools | while IFS="" read -r -d "" path; do
# If you add more replacements here, you must change the grep above!
Expand All @@ -137,6 +148,7 @@ stdenv.mkDerivation rec {
--replace '"jvm_opts": JDK9_JVM_OPTS' \
'"jvm_opts": JDK8_JVM_OPTS'


# add nix environment vars to .bazelrc
cat >> .bazelrc <<EOF
build --experimental_distdir=${distDir}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{ stdenv, lib, writeText, runCommandCC, bazel }:

let
WORKSPACE = writeText "WORKSPACE" ''
workspace(name = "our_workspace")
'';

pythonLib = writeText "lib.py" ''
def foo():
return 43
'';

pythonBin = writeText "bin.py" ''
from lib import foo

assert foo() == 43
'';

pythonBUILD = writeText "BUILD" ''
py_library(
name = "lib",
srcs = [ "lib.py" ],
)

py_test(
name = "bin",
srcs = [ "bin.py" ],
deps = [ ":lib" ],
)
'';

runLocal = name: script: runCommandCC name { preferLocalBuild = true; } script;

workspaceDir = runLocal "our_workspace" ''
mkdir $out
cp ${WORKSPACE} $out/WORKSPACE
mkdir $out/python
cp ${pythonLib} $out/python/lib.py
cp ${pythonBin} $out/python/bin.py
cp ${pythonBUILD} $out/python/BUILD.bazel
'';

testBazel = runLocal "bazel-test-builtin-rules" ''
export HOME=$(mktemp -d)
cp -r ${workspaceDir}/* .
${bazel}/bin/bazel --output_base=/tmp/bazel-tests/wd\
test \
--test_output=errors \
--host_javabase='@local_jdk//:jdk' \
//...

touch $out
'';

in testBazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 dac21c9a83..69b11c283f 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
@@ -67,7 +67,7 @@ def FindPythonBinary(module_space):
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)
+ return "NIX_STORE_PYTHON_PATH"

def CreatePythonPathEntries(python_imports, module_space):
parts = python_imports.split(':');