From 3ac310e3c870cd70cef26354ee34b3708584db84 Mon Sep 17 00:00:00 2001 From: cushon Date: Thu, 18 Jan 2018 16:59:20 -0800 Subject: Minimal support for compiling module-infos PiperOrigin-RevId: 182461095 --- .../build/buildjar/javac/BlazeJavacMain.java | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/BlazeJavacMain.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/BlazeJavacMain.java index 74059d2e4e..dd2a14105d 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/BlazeJavacMain.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/BlazeJavacMain.java @@ -14,6 +14,8 @@ package com.google.devtools.build.buildjar.javac; +import static com.google.common.collect.ImmutableList.toImmutableList; +import static com.google.common.collect.Iterables.getOnlyElement; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.annotations.VisibleForTesting; @@ -170,6 +172,10 @@ public class BlazeJavacMain { private static void setLocations(JavacFileManager fileManager, BlazeJavacArguments arguments) { try { fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, arguments.classPath()); + // modular dependencies must be on the module path, not the classpath + fileManager.setLocationFromPaths( + StandardLocation.locationFor("MODULE_PATH"), arguments.classPath()); + fileManager.setLocationFromPaths( StandardLocation.CLASS_OUTPUT, ImmutableList.of(arguments.classOutput())); if (arguments.nativeHeaderOutput() != null) { @@ -177,7 +183,24 @@ public class BlazeJavacMain { StandardLocation.NATIVE_HEADER_OUTPUT, ImmutableList.of(arguments.nativeHeaderOutput())); } - fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, arguments.sourcePath()); + + ImmutableList sourcePath = arguments.sourcePath(); + if (sourcePath.isEmpty()) { + // javac expects a module-info-relative source path to be set when compiling modules, + // otherwise it reports an error: + // "file should be on source path, or on patch path for module" + ImmutableList moduleInfos = + arguments + .sourceFiles() + .stream() + .filter(f -> f.getFileName().toString().equals("module-info.java")) + .collect(toImmutableList()); + if (moduleInfos.size() == 1) { + sourcePath = ImmutableList.of(getOnlyElement(moduleInfos).getParent()); + } + } + fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcePath); + // TODO(cushon): require an explicit bootclasspath Collection bootClassPath = arguments.bootClassPath(); if (!bootClassPath.isEmpty()) { -- cgit v1.2.3