diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java index e72d4c3a591e00..1d6da623d70be5 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java @@ -27,6 +27,7 @@ import java.util.Deque; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -110,6 +111,7 @@ private void processCommandlineArgs(Deque argQueue) throws InvalidComman // otherwise we have to do something like adding a "--" // terminator to the passed arguments. collectFlagArguments(javacOpts, argQueue, "--"); + sourcePathFromJavacOpts(); break; case "--direct_dependency": { @@ -220,6 +222,18 @@ private void processCommandlineArgs(Deque argQueue) throws InvalidComman } } + private void sourcePathFromJavacOpts() { + Iterator it = javacOpts.iterator(); + while (it.hasNext()) { + String curr = it.next(); + if (curr.equals("-sourcepath") && it.hasNext()) { + it.remove(); + sourcePath = it.next(); + it.remove(); + } + } + } + private JarOwner parseJarOwner(String line) { List ownerStringParts = SPACE_SPLITTER.splitToList(line); JarOwner owner; diff --git a/src/test/shell/bazel/bazel_java_test.sh b/src/test/shell/bazel/bazel_java_test.sh index 8cbfc12adab1a5..9dc1be797c91e1 100755 --- a/src/test/shell/bazel/bazel_java_test.sh +++ b/src/test/shell/bazel/bazel_java_test.sh @@ -105,6 +105,42 @@ function test_build_hello_world() { bazel build //java/main:main &> $TEST_log || fail "build failed" } +function test_build_with_sourcepath() { + mkdir -p g + cat >g/A.java <<'EOF' +package g; +public class A { + public A() { + new B(); + } +} +EOF + + cat >g/B.java <<'EOF' +package g; +public class B { + public B() { + } +} +EOF + + cat >g/BUILD <<'EOF' +genrule( + name = "stub", + srcs = ["B.java"], + outs = ["B.jar"], + cmd = "zip $@ $(SRCS)", +) +java_library( + name = "test", + srcs = ["A.java"], + javacopts = ["-sourcepath $(GENDIR)/$(location :stub)", "-implicit:none"], + deps = [":stub"] +) +EOF + bazel build //g:test >$TEST_log || fail "Failed to build //g:test" +} + function test_java_common_compile_sourcepath() { # TODO(bazel-team): Enable this for Java 7 when VanillaJavaBuilder supports --sourcepath. JAVA_VERSION="1.$(bazel query --output=build '@bazel_tools//tools/jdk:toolchain' | grep source_version | cut -d '"' -f 2)"