From 515b3ac67761c6f4e5f8a73068ebe34ec6cb44de Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 5 Jun 2017 14:21:25 -0700 Subject: Add Clutz to TensorBoard build This is so we can get JavaScript protobufs. This CL also improves the web_aspect and makes some peculiar Closure Compiler errors go away relating to externs. PiperOrigin-RevId: 158061198 --- WORKSPACE | 8 +- tensorflow/tensorboard/BUILD | 11 + tensorflow/tensorboard/clutz.d.ts | 19 ++ tensorflow/tensorboard/defs.bzl | 7 + .../tensorboard/vulcanize/Vulcanize.java | 73 ++++-- tensorflow/tensorboard/vulcanize.bzl | 24 +- tensorflow/tensorboard/web.bzl | 273 +++++++++++---------- tensorflow/workspace.bzl | 25 +- third_party/clutz.BUILD | 44 ++++ third_party/clutz.bzl | 77 ++++++ 10 files changed, 390 insertions(+), 171 deletions(-) create mode 100644 tensorflow/tensorboard/clutz.d.ts create mode 100644 third_party/clutz.BUILD create mode 100644 third_party/clutz.bzl diff --git a/WORKSPACE b/WORKSPACE index c9d7b458a9..74ce13f4e8 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -2,11 +2,11 @@ workspace(name = "org_tensorflow") http_archive( name = "io_bazel_rules_closure", - sha256 = "edc91f556b762fc5212d1050d00b12e40dd0b0b1c1d5d96886b59e9a30a6cae4", - strip_prefix = "rules_closure-3f07fb6a58870afbb36051bd5d54da4479561cc6", + sha256 = "bc41b80486413aaa551860fc37471dbc0666e1dbb5236fb6177cb83b0c105846", + strip_prefix = "rules_closure-dec425a4ff3faf09a56c85d082e4eed05d8ce38f", urls = [ - "http://mirror.bazel.build/github.com/bazelbuild/rules_closure/archive/3f07fb6a58870afbb36051bd5d54da4479561cc6.tar.gz", # 2017-05-31 - "https://github.com/bazelbuild/rules_closure/archive/3f07fb6a58870afbb36051bd5d54da4479561cc6.tar.gz", + "http://mirror.bazel.build/github.com/bazelbuild/rules_closure/archive/dec425a4ff3faf09a56c85d082e4eed05d8ce38f.tar.gz", # 2017-06-02 + "https://github.com/bazelbuild/rules_closure/archive/dec425a4ff3faf09a56c85d082e4eed05d8ce38f.tar.gz", ], ) diff --git a/tensorflow/tensorboard/BUILD b/tensorflow/tensorboard/BUILD index 1eb5b12415..caaf1769c0 100644 --- a/tensorflow/tensorboard/BUILD +++ b/tensorflow/tensorboard/BUILD @@ -33,6 +33,17 @@ filegroup( ], ) +filegroup( + name = "ts_web_library_default_typings", + srcs = [ + # Ordering probably matters. + "@com_microsoft_typescript//:lib.es6.d.ts", + "@io_angular_clutz//:src/resources/closure.lib.d.ts", + "clutz.d.ts", + ], + visibility = ["//visibility:public"], +) + filegroup( name = "all_files", srcs = glob( diff --git a/tensorflow/tensorboard/clutz.d.ts b/tensorflow/tensorboard/clutz.d.ts new file mode 100644 index 0000000000..47cf307d26 --- /dev/null +++ b/tensorflow/tensorboard/clutz.d.ts @@ -0,0 +1,19 @@ +// Copyright 2017 The TensorFlow Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// tslint:disable +declare namespace ಠ_ಠ.clutz { + interface IteratorIterable extends Iterator, Iterable {} + interface IIterableResult extends IteratorResult {} +} diff --git a/tensorflow/tensorboard/defs.bzl b/tensorflow/tensorboard/defs.bzl index b3712a8156..94e2d7c540 100644 --- a/tensorflow/tensorboard/defs.bzl +++ b/tensorflow/tensorboard/defs.bzl @@ -15,3 +15,10 @@ def tensorboard_webcomponent_library(**kwargs): """Rules referencing this will be deleted from the codebase soon.""" pass + +def _legacy_js_impl(target, ctx): + return struct() + +legacy_js = aspect( + implementation=_legacy_js_impl, + attr_aspects=["exports"]) diff --git a/tensorflow/tensorboard/java/org/tensorflow/tensorboard/vulcanize/Vulcanize.java b/tensorflow/tensorboard/java/org/tensorflow/tensorboard/vulcanize/Vulcanize.java index 2635f9b12f..533907dd64 100644 --- a/tensorflow/tensorboard/java/org/tensorflow/tensorboard/vulcanize/Vulcanize.java +++ b/tensorflow/tensorboard/java/org/tensorflow/tensorboard/vulcanize/Vulcanize.java @@ -24,7 +24,6 @@ import com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.base.Splitter; import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; @@ -38,6 +37,7 @@ import com.google.javascript.jscomp.DiagnosticGroup; import com.google.javascript.jscomp.DiagnosticGroups; import com.google.javascript.jscomp.DiagnosticType; import com.google.javascript.jscomp.JSError; +import com.google.javascript.jscomp.ModuleIdentifier; import com.google.javascript.jscomp.PropertyRenamingPolicy; import com.google.javascript.jscomp.Result; import com.google.javascript.jscomp.SourceFile; @@ -53,8 +53,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; +import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -63,6 +65,7 @@ import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.jsoup.Jsoup; import org.jsoup.nodes.Attribute; import org.jsoup.nodes.Comment; @@ -92,6 +95,7 @@ public final class Vulcanize { private static final Set legalese = new HashSet<>(); private static final List licenses = new ArrayList<>(); private static final List stack = new ArrayList<>(); + private static final List externs = new ArrayList<>(); private static final List sourcesFromJsLibraries = new ArrayList<>(); private static final Map sourcesFromScriptTags = new LinkedHashMap<>(); private static final Map sourceTags = new LinkedHashMap<>(); @@ -111,7 +115,13 @@ public final class Vulcanize { Path output = Paths.get(args[4]); for (int i = 5; i < args.length; i++) { if (args[i].endsWith(".js")) { - sourcesFromJsLibraries.add(SourceFile.fromFile(args[i])); + String code = new String(Files.readAllBytes(Paths.get(args[i])), UTF_8); + SourceFile sourceFile = SourceFile.fromCode(args[i], code); + if (code.contains("@externs")) { + externs.add(sourceFile); + } else { + sourcesFromJsLibraries.add(sourceFile); + } continue; } if (!args[i].endsWith(".pbtxt")) { @@ -339,12 +349,20 @@ public final class Vulcanize { options.setRemoveUnusedPrototypePropertiesInExterns(false); options.setRemoveUnusedClassProperties(false); - // Closure pass. + // Dependency management. options.setClosurePass(true); options.setManageClosureDependencies(true); options.getDependencyOptions().setDependencyPruning(true); - options.getDependencyOptions().setDependencySorting(false); + options.getDependencyOptions().setDependencySorting(true); options.getDependencyOptions().setMoocherDropping(false); + options.getDependencyOptions() + .setEntryPoints( + sourceTags + .keySet() + .stream() + .map(Webpath::toString) + .map(ModuleIdentifier::forFile) + .collect(Collectors.toList())); // Polymer pass. options.setPolymerVersion(1); @@ -362,6 +380,16 @@ public final class Vulcanize { new WarningsGuard() { @Override public CheckLevel level(JSError error) { + if (error.sourceName == null) { + return null; + } + if (error.sourceName.startsWith("javascript/externs") + || error.sourceName.contains("com_google_javascript_closure_compiler_externs")) { + // TODO(jart): Figure out why these "mismatch of the removeEventListener property on + // type" warnings are showing up. + // https://github.com/google/closure-compiler/pull/1959 + return CheckLevel.OFF; + } if (IGNORE_PATHS_PATTERN.matcher(error.sourceName).matches()) { return CheckLevel.OFF; } @@ -395,42 +423,39 @@ public final class Vulcanize { // Compile everything into a single script. Compiler compiler = new Compiler(); compiler.disableThreads(); - Result result = compiler.compile(ImmutableList.of(), sauce, options); + Result result = compiler.compile(externs, sauce, options); if (!result.success) { System.exit(1); } String jsBlob = compiler.toSource(); // Split apart the JS blob and put it back in the original