Skip to content

Commit

Permalink
Pass through -sourcepath to the JavaBuilder
Browse files Browse the repository at this point in the history
Fix #2606.

--
PiperOrigin-RevId: 149096656
MOS_MIGRATED_REVID=149096656
  • Loading branch information
damienmg authored and hermione521 committed Mar 6, 2017
1 parent 2697dd3 commit 7b295d3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public final class JavaLibraryBuildRequest {
/** Resource files that should be put in the root of the output jar. */
private final ImmutableList<String> rootResourceFiles;

private final String sourcePath;
private final String classPath;
private final String bootClassPath;
private final String extdir;
Expand Down Expand Up @@ -152,6 +153,7 @@ public JavaLibraryBuildRequest(
this.resourceJars = ImmutableList.copyOf(optionsParser.getResourceJars());
this.rootResourceFiles = ImmutableList.copyOf(optionsParser.getRootResourceFiles());
this.classPath = optionsParser.getClassPath();
this.sourcePath = optionsParser.getSourcePath();
this.bootClassPath = optionsParser.getBootClassPath();
this.extdir = optionsParser.getExtdir();
this.processorPath = optionsParser.getProcessorPath();
Expand Down Expand Up @@ -197,6 +199,10 @@ public String getSourceGenDir() {
return sourceGenDir;
}

public String getSourcePath() {
return sourcePath;
}

public String getGeneratedSourcesOutputJar() {
return generatedSourcesOutputJar;
}
Expand Down Expand Up @@ -293,6 +299,7 @@ public BlazeJavacArguments toBlazeJavacArguments(final String classPath) {
.javacOptions(makeJavacArguments())
.sourceFiles(ImmutableList.copyOf(getSourceFiles()))
.processors(null)
.sourcePath(toPaths(getSourcePath()))
.sourceOutput(getSourceGenDir() != null ? Paths.get(getSourceGenDir()) : null)
.processorPath(toPaths(getProcessorPath()))
.plugins(getPlugins())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class OptionsParser {
private final List<String> rootResourceFiles = new ArrayList<>();

private String classPath = "";
private String sourcePath;
private String bootClassPath;
private String extdir;

Expand Down Expand Up @@ -109,6 +110,7 @@ private void processCommandlineArgs(Deque<String> argQueue) throws InvalidComman
// terminator to the passed arguments.
collectFlagArguments(javacOpts, argQueue, "--");
bootClassPathFromJavacOpts();
sourcePathFromJavacOpts();
break;
case "--direct_dependency":
{
Expand Down Expand Up @@ -169,6 +171,10 @@ private void processCommandlineArgs(Deque<String> argQueue) throws InvalidComman
case "--classpath":
classPath = getArgument(argQueue, arg);
break;
// TODO(#970): Consider wether we want to use --sourcepath for resolving of #970.
case "--sourcepath":
sourcePath = getArgument(argQueue, arg);
break;
case "--bootclasspath":
bootClassPath = getArgument(argQueue, arg);
break;
Expand Down Expand Up @@ -332,6 +338,19 @@ private void bootClassPathFromJavacOpts() {
}
}

// TODO(#970): Delete that function (either set --sourcepath from Bazel or just drop support).
private void sourcePathFromJavacOpts() {
Iterator<String> it = javacOpts.iterator();
while (it.hasNext()) {
String curr = it.next();
if (curr.equals("-sourcepath") && it.hasNext()) {
it.remove();
sourcePath = it.next();
it.remove();
}
}
}

public List<String> getJavacOpts() {
return javacOpts;
}
Expand Down Expand Up @@ -408,6 +427,10 @@ public String getBootClassPath() {
return bootClassPath;
}

public String getSourcePath() {
return sourcePath;
}

public String getExtdir() {
return extdir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public abstract class BlazeJavacArguments {
/** The compilation bootclasspath. */
public abstract ImmutableList<Path> bootClassPath();

/** The compilation source path. */
public abstract ImmutableList<Path> sourcePath();

/** The classpath to load processors from. */
public abstract ImmutableList<Path> processorPath();

Expand All @@ -68,6 +71,7 @@ public static Builder builder() {
.bootClassPath(ImmutableList.of())
.javacOptions(ImmutableList.of())
.sourceFiles(ImmutableList.of())
.sourcePath(ImmutableList.of())
.processors(null)
.sourceOutput(null)
.processorPath(ImmutableList.of())
Expand All @@ -85,6 +89,8 @@ public interface Builder {

Builder javacOptions(ImmutableList<String> javacOptions);

Builder sourcePath(ImmutableList<Path> sourcePath);

Builder sourceFiles(ImmutableList<Path> sourceFiles);

Builder processors(ImmutableList<Processor> processors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static void setLocations(JavacFileManager fileManager, BlazeJavacArgumen
fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, arguments.classPath());
fileManager.setLocationFromPaths(
StandardLocation.CLASS_OUTPUT, ImmutableList.of(arguments.classOutput()));
fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, ImmutableList.of());
fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, arguments.sourcePath());
// TODO(cushon): require an explicit bootclasspath
Iterable<Path> bootClassPath = arguments.bootClassPath();
if (!Iterables.isEmpty(bootClassPath)) {
Expand Down
39 changes: 39 additions & 0 deletions src/test/shell/bazel/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,45 @@ function test_build_hello_world() {
bazel build //java/main:main &> $TEST_log || fail "build failed"
}

# Regression test for #2606: support for passing -sourcepath
# TODO(#2606): Update when a final solution is found for #2606.
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"
}

# Runfiles is disabled by default on Windows, but we can test it on Unix by
# adding flag --experimental_enable_runfiles=0
function test_build_and_run_hello_world_without_runfiles() {
Expand Down

0 comments on commit 7b295d3

Please sign in to comment.