Skip to content

Commit 7b295d3

Browse files
damienmghermione521
authored andcommitted
Pass through -sourcepath to the JavaBuilder
Fix bazelbuild#2606. -- PiperOrigin-RevId: 149096656 MOS_MIGRATED_REVID=149096656
1 parent 2697dd3 commit 7b295d3

5 files changed

Lines changed: 76 additions & 1 deletion

File tree

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/JavaLibraryBuildRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class JavaLibraryBuildRequest {
5555
/** Resource files that should be put in the root of the output jar. */
5656
private final ImmutableList<String> rootResourceFiles;
5757

58+
private final String sourcePath;
5859
private final String classPath;
5960
private final String bootClassPath;
6061
private final String extdir;
@@ -152,6 +153,7 @@ public JavaLibraryBuildRequest(
152153
this.resourceJars = ImmutableList.copyOf(optionsParser.getResourceJars());
153154
this.rootResourceFiles = ImmutableList.copyOf(optionsParser.getRootResourceFiles());
154155
this.classPath = optionsParser.getClassPath();
156+
this.sourcePath = optionsParser.getSourcePath();
155157
this.bootClassPath = optionsParser.getBootClassPath();
156158
this.extdir = optionsParser.getExtdir();
157159
this.processorPath = optionsParser.getProcessorPath();
@@ -197,6 +199,10 @@ public String getSourceGenDir() {
197199
return sourceGenDir;
198200
}
199201

202+
public String getSourcePath() {
203+
return sourcePath;
204+
}
205+
200206
public String getGeneratedSourcesOutputJar() {
201207
return generatedSourcesOutputJar;
202208
}
@@ -293,6 +299,7 @@ public BlazeJavacArguments toBlazeJavacArguments(final String classPath) {
293299
.javacOptions(makeJavacArguments())
294300
.sourceFiles(ImmutableList.copyOf(getSourceFiles()))
295301
.processors(null)
302+
.sourcePath(toPaths(getSourcePath()))
296303
.sourceOutput(getSourceGenDir() != null ? Paths.get(getSourceGenDir()) : null)
297304
.processorPath(toPaths(getProcessorPath()))
298305
.plugins(getPlugins())

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public final class OptionsParser {
6565
private final List<String> rootResourceFiles = new ArrayList<>();
6666

6767
private String classPath = "";
68+
private String sourcePath;
6869
private String bootClassPath;
6970
private String extdir;
7071

@@ -109,6 +110,7 @@ private void processCommandlineArgs(Deque<String> argQueue) throws InvalidComman
109110
// terminator to the passed arguments.
110111
collectFlagArguments(javacOpts, argQueue, "--");
111112
bootClassPathFromJavacOpts();
113+
sourcePathFromJavacOpts();
112114
break;
113115
case "--direct_dependency":
114116
{
@@ -169,6 +171,10 @@ private void processCommandlineArgs(Deque<String> argQueue) throws InvalidComman
169171
case "--classpath":
170172
classPath = getArgument(argQueue, arg);
171173
break;
174+
// TODO(#970): Consider wether we want to use --sourcepath for resolving of #970.
175+
case "--sourcepath":
176+
sourcePath = getArgument(argQueue, arg);
177+
break;
172178
case "--bootclasspath":
173179
bootClassPath = getArgument(argQueue, arg);
174180
break;
@@ -332,6 +338,19 @@ private void bootClassPathFromJavacOpts() {
332338
}
333339
}
334340

341+
// TODO(#970): Delete that function (either set --sourcepath from Bazel or just drop support).
342+
private void sourcePathFromJavacOpts() {
343+
Iterator<String> it = javacOpts.iterator();
344+
while (it.hasNext()) {
345+
String curr = it.next();
346+
if (curr.equals("-sourcepath") && it.hasNext()) {
347+
it.remove();
348+
sourcePath = it.next();
349+
it.remove();
350+
}
351+
}
352+
}
353+
335354
public List<String> getJavacOpts() {
336355
return javacOpts;
337356
}
@@ -408,6 +427,10 @@ public String getBootClassPath() {
408427
return bootClassPath;
409428
}
410429

430+
public String getSourcePath() {
431+
return sourcePath;
432+
}
433+
411434
public String getExtdir() {
412435
return extdir;
413436
}

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/BlazeJavacArguments.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public abstract class BlazeJavacArguments {
4242
/** The compilation bootclasspath. */
4343
public abstract ImmutableList<Path> bootClassPath();
4444

45+
/** The compilation source path. */
46+
public abstract ImmutableList<Path> sourcePath();
47+
4548
/** The classpath to load processors from. */
4649
public abstract ImmutableList<Path> processorPath();
4750

@@ -68,6 +71,7 @@ public static Builder builder() {
6871
.bootClassPath(ImmutableList.of())
6972
.javacOptions(ImmutableList.of())
7073
.sourceFiles(ImmutableList.of())
74+
.sourcePath(ImmutableList.of())
7175
.processors(null)
7276
.sourceOutput(null)
7377
.processorPath(ImmutableList.of())
@@ -85,6 +89,8 @@ public interface Builder {
8589

8690
Builder javacOptions(ImmutableList<String> javacOptions);
8791

92+
Builder sourcePath(ImmutableList<Path> sourcePath);
93+
8894
Builder sourceFiles(ImmutableList<Path> sourceFiles);
8995

9096
Builder processors(ImmutableList<Processor> processors);

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/BlazeJavacMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private static void setLocations(JavacFileManager fileManager, BlazeJavacArgumen
168168
fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, arguments.classPath());
169169
fileManager.setLocationFromPaths(
170170
StandardLocation.CLASS_OUTPUT, ImmutableList.of(arguments.classOutput()));
171-
fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, ImmutableList.of());
171+
fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, arguments.sourcePath());
172172
// TODO(cushon): require an explicit bootclasspath
173173
Iterable<Path> bootClassPath = arguments.bootClassPath();
174174
if (!Iterables.isEmpty(bootClassPath)) {

src/test/shell/bazel/bazel_java_test.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,45 @@ function test_build_hello_world() {
105105
bazel build //java/main:main &> $TEST_log || fail "build failed"
106106
}
107107

108+
# Regression test for #2606: support for passing -sourcepath
109+
# TODO(#2606): Update when a final solution is found for #2606.
110+
function test_build_with_sourcepath() {
111+
mkdir -p g
112+
cat >g/A.java <<'EOF'
113+
package g;
114+
public class A {
115+
public A() {
116+
new B();
117+
}
118+
}
119+
EOF
120+
121+
cat >g/B.java <<'EOF'
122+
package g;
123+
public class B {
124+
public B() {
125+
}
126+
}
127+
EOF
128+
129+
cat >g/BUILD <<'EOF'
130+
genrule(
131+
name = "stub",
132+
srcs = ["B.java"],
133+
outs = ["B.jar"],
134+
cmd = "zip $@ $(SRCS)",
135+
)
136+
137+
java_library(
138+
name = "test",
139+
srcs = ["A.java"],
140+
javacopts = ["-sourcepath $(GENDIR)/$(location :stub)", "-implicit:none"],
141+
deps = [":stub"]
142+
)
143+
EOF
144+
bazel build //g:test >$TEST_log || fail "Failed to build //g:test"
145+
}
146+
108147
# Runfiles is disabled by default on Windows, but we can test it on Unix by
109148
# adding flag --experimental_enable_runfiles=0
110149
function test_build_and_run_hello_world_without_runfiles() {

0 commit comments

Comments
 (0)