Permalink
Browse files

Add a hidden flag for serializing libraryinfo in an human readable fo…

…rmat.

This is useful for our readable example or for debugging purpose.

PiperOrigin-RevId: 226081345
  • Loading branch information...
jDramaix authored and Copybara-Service committed Dec 19, 2018
1 parent 1637e54 commit 92601980c97bdedf4d349e556286771ad38ed51c
@@ -18,6 +18,8 @@ def j2cl_transpile(ctx, java_provider, js_srcs):
args.add("-declarelegacynamespaces")
if ctx.attr.readable_source_maps:
args.add("-readablesourcemaps")
if ctx.attr.readable_library_info:
args.add("-readablelibraryinfo")
if ctx.var.get("GROK_ELLIPSIS_BUILD", None):
args.add("-generatekytheindexingmetadata")
args.add_all(srcs)
@@ -39,6 +41,7 @@ def j2cl_transpile(ctx, java_provider, js_srcs):
# transpiler: J2CL compiler jar to use.
J2CL_TRANSPILE_ATTRS = {
"readable_source_maps": attr.bool(default = False),
"readable_library_info": attr.bool(default = False),
"declare_legacy_namespace": attr.bool(default = False),
"transpiler": attr.label(
cfg = "host",
@@ -102,6 +102,11 @@ java_library(
exports = ["@com_google_testing_compile//jar"],
)

alias(
name = "protobuf_util_json",
actual = "@com_google_protobuf//:protobuf_java_util",
)

alias(
name = "closure_testcase",
actual = "@io_bazel_rules_closure//closure/library/testing:testcase",
@@ -57,11 +57,15 @@ public static String streamToString(Consumer<? super PrintStream> streamOutputer
}

public static void writeToFile(Path outputPath, String content, Problems problems) {
writeToFile(outputPath, content.getBytes(StandardCharsets.UTF_8), problems);
}

public static void writeToFile(Path outputPath, byte[] content, Problems problems) {
try {
// Write using the provided fileSystem (which might be the regular file system or might be a
// zip file.)
createDirectories(outputPath.getParent());
Files.write(outputPath, content.getBytes(StandardCharsets.UTF_8));
Files.write(outputPath, content);
// Wipe entries modification time so that input->output mapping is stable
// regardless of the time of day.
maybeResetAllTimeStamps(outputPath);
@@ -28,8 +28,6 @@
import com.google.j2cl.libraryinfo.LibraryInfoBuilder;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@@ -49,20 +47,23 @@
private final Optional<Path> libraryInfoOutputPath;
private final boolean declareLegacyNamespace;
private final boolean shouldGenerateReadableSourceMaps;
private final boolean shouldGenerateReadableLibraryInfo;
private final boolean generateKytheIndexingMetadata;

public OutputGeneratorStage(
List<FileInfo> nativeJavaScriptFiles,
Path outputPath,
Optional<Path> libraryInfoOutputPath,
boolean declareLegacyNamespace,
boolean shouldGenerateReadableLibraryInfo,
boolean shouldGenerateReadableSourceMaps,
boolean generateKytheIndexingMetadata,
Problems problems) {
this.nativeJavaScriptFiles = nativeJavaScriptFiles;
this.outputPath = outputPath;
this.libraryInfoOutputPath = libraryInfoOutputPath;
this.declareLegacyNamespace = declareLegacyNamespace;
this.shouldGenerateReadableLibraryInfo = shouldGenerateReadableLibraryInfo;
this.shouldGenerateReadableSourceMaps = shouldGenerateReadableSourceMaps;
this.generateKytheIndexingMetadata = generateKytheIndexingMetadata;
this.problems = problems;
@@ -154,7 +155,7 @@ public void generateOutputs(List<CompilationUnit> j2clCompilationUnits) {
J2clUtils.writeToFile(
outputPath.resolve(headerRelativePath), javaScriptHeaderSource, problems);

if (libraryInfoOutputPath.isPresent()) {
if (libraryInfoOutputPath.isPresent() || shouldGenerateReadableLibraryInfo) {
libraryInfo.addType(
LibraryInfoBuilder.build(
type,
@@ -174,11 +175,15 @@ public void generateOutputs(List<CompilationUnit> j2clCompilationUnits) {
}

if (libraryInfoOutputPath.isPresent()) {
try (OutputStream outputStream = Files.newOutputStream(libraryInfoOutputPath.get())) {
libraryInfo.build().writeTo(outputStream);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
}
J2clUtils.writeToFile(
libraryInfoOutputPath.get(), LibraryInfoBuilder.toByteArray(libraryInfo), problems);
}

if (shouldGenerateReadableLibraryInfo) {
J2clUtils.writeToFile(
outputPath.resolve("library_info_debug.json"),
LibraryInfoBuilder.toJson(libraryInfo, problems),
problems);
}

// Error if any of the native implementation files were not used.
@@ -23,6 +23,7 @@ java_library(
":library_info_java_proto",
"//third_party:guava",
"//third_party:jsr305_annotations",
"//third_party:protobuf_util_json",
"//transpiler/java/com/google/j2cl/ast",
"//transpiler/java/com/google/j2cl/common",
],
@@ -34,7 +34,11 @@
import com.google.j2cl.ast.Type;
import com.google.j2cl.ast.TypeDeclaration;
import com.google.j2cl.ast.TypeDescriptor;
import com.google.j2cl.common.Problems;
import com.google.j2cl.common.Problems.FatalError;
import com.google.j2cl.common.SourcePosition;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
@@ -43,6 +47,20 @@

/** Traverse types and gather execution flow information for building call graph. */
public final class LibraryInfoBuilder {
/** Serialize a LibraryInfo object into a JSON string. */
public static String toJson(LibraryInfo.Builder libraryInfo, Problems problems) {
try {
return JsonFormat.printer().print(libraryInfo);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
return null;
}
}

public static byte[] toByteArray(LibraryInfo.Builder libraryInfo) {
return libraryInfo.build().toByteArray();
}

/** Gather information from a Type and create a TypeInfo object used to build the call graph. */
public static TypeInfo build(
Type type,
@@ -64,6 +64,9 @@
usage = "Specifies the file into which to place the call graph.")
protected String libraryInfoOutput;

@Option(name = "-readablelibraryinfo", hidden = true)
protected boolean readableLibraryInfo = false;

@Option(name = "-readablesourcemaps", hidden = true)
protected boolean readableSourceMaps = false;

@@ -122,6 +125,7 @@ private J2clTranspilerOptions createOptions() {
.setClasspaths(getPathEntries(this.classPath))
.setOutput(outputPath)
.setLibraryInfoOutput(libraryInfoOutputPath)
.setEmitReadableLibraryInfo(readableLibraryInfo)
.setEmitReadableSourceMap(this.readableSourceMaps)
.setDeclareLegacyNamespace(this.declareLegacyNamespaces)
.setGenerateKytheIndexingMetadata(this.generateKytheIndexingMetadata)
@@ -111,6 +111,7 @@ private J2clTranspilerOptions createOptions() {
? getZipOutput(this.output, problems)
: getDirOutput(this.output, problems))
.setEmitReadableSourceMap(this.readableSourceMaps)
.setEmitReadableLibraryInfo(false)
.setDeclareLegacyNamespace(this.declareLegacyNamespaces)
.setGenerateKytheIndexingMetadata(this.generateKytheIndexingMetadata)
.build();
@@ -281,6 +281,7 @@ private void generateOutputs(List<CompilationUnit> j2clCompilationUnits) {
options.getOutput(),
options.getLibraryInfoOutput(),
options.getDeclareLegacyNamespace(),
options.getEmitReadableLibraryInfo(),
options.getEmitReadableSourceMap(),
options.getGenerateKytheIndexingMetadata(),
problems)
@@ -37,6 +37,8 @@

public abstract Optional<Path> getLibraryInfoOutput();

public abstract boolean getEmitReadableLibraryInfo();

public abstract boolean getEmitReadableSourceMap();

public abstract boolean getDeclareLegacyNamespace();
@@ -61,6 +63,8 @@ public static Builder newBuilder() {

public abstract Builder setLibraryInfoOutput(Path path);

public abstract Builder setEmitReadableLibraryInfo(boolean b);

public abstract Builder setEmitReadableSourceMap(boolean b);

public abstract Builder setDeclareLegacyNamespace(boolean b);
@@ -73,6 +77,8 @@ public J2clTranspilerOptions build() {
J2clTranspilerOptions options = autoBuild();
checkState(
!options.getEmitReadableSourceMap() || !options.getGenerateKytheIndexingMetadata());
checkState(
!options.getEmitReadableLibraryInfo() || options.getLibraryInfoOutput().isPresent());
return options;
}
}
@@ -57,6 +57,7 @@ def readable_example(
tags = j2cl_library_tags,
_js_deps = js_deps,
_readable_source_maps = True,
_readable_library_info = generate_library_info,
_declare_legacy_namespace = _declare_legacy_namespace,
)

0 comments on commit 9260198

Please sign in to comment.