Fixes a problem where the compiler did not emit dependency information
for programs with no calls to runAsync.

Review by: kprobst


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@5965 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/core/ext/soyc/impl/DependencyRecorder.java b/dev/core/src/com/google/gwt/core/ext/soyc/impl/DependencyRecorder.java
index bb193f7..e58edce 100644
--- a/dev/core/src/com/google/gwt/core/ext/soyc/impl/DependencyRecorder.java
+++ b/dev/core/src/com/google/gwt/core/ext/soyc/impl/DependencyRecorder.java
@@ -36,7 +36,7 @@
 public class DependencyRecorder implements MultipleDependencyGraphRecorder {
   /**
    * DependencyRecorder is not allowed to throw checked exceptions, because if
-   * it did then {@link CodeSplitter} and {@link ControlFlowAnalyzer} would
+   * it did then {@link com.google.gwt.dev.jjs.impl.CodeSplitter} and {@link ControlFlowAnalyzer} would
    * throw exceptions all over the place. Instead, this class throws
    * NestedIOExceptions that wrap them.
    */
@@ -47,14 +47,11 @@
   }
 
   private final StringBuilder builder = new StringBuilder();
+  private final OutputStream finalOutput;
   private OutputStreamWriter writer;
 
-  public DependencyRecorder(OutputStream out) throws IOException {
-    try {
-      this.writer = new OutputStreamWriter(new GZIPOutputStream(out), "UTF-8");
-    } catch (UnsupportedEncodingException e) {
-      throw new InternalCompilerException("UTF-8 is an unsupported encoding", e);
-    }
+  public DependencyRecorder(OutputStream out) {
+    this.finalOutput = out;
   }
 
   public void close() {
@@ -81,6 +78,15 @@
   }
 
   public void open() {
+    try {
+      this.writer = new OutputStreamWriter(new GZIPOutputStream(finalOutput),
+          "UTF-8");
+    } catch (UnsupportedEncodingException e) {
+      throw new InternalCompilerException("UTF-8 is an unsupported encoding", e);
+    } catch (IOException e) {
+      throw new NestedIOException(e);
+    }
+
     printPre();
   }
 
diff --git a/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java b/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
index 538823d..813f944 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
@@ -56,6 +56,7 @@
 import com.google.gwt.dev.jjs.impl.CastNormalizer;
 import com.google.gwt.dev.jjs.impl.CatchBlockNormalizer;
 import com.google.gwt.dev.jjs.impl.CodeSplitter;
+import com.google.gwt.dev.jjs.impl.ControlFlowAnalyzer;
 import com.google.gwt.dev.jjs.impl.DeadCodeElimination;
 import com.google.gwt.dev.jjs.impl.EqualityNormalizer;
 import com.google.gwt.dev.jjs.impl.Finalizer;
@@ -136,9 +137,9 @@
   private static class PermutationResultImpl implements PermutationResult {
     private final ArtifactSet artifacts = new ArtifactSet();
     private final byte[][] js;
+    private final int permutationId;
     private final byte[] serializedSymbolMap;
     private final StatementRanges[] statementRanges;
-    private final int permutationId;
 
     public PermutationResultImpl(String[] js, SymbolData[] symbolMap,
         StatementRanges[] statementRanges, int permutationId) {
@@ -287,6 +288,9 @@
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         CodeSplitter.exec(logger, jprogram, jsProgram, map,
             chooseDependencyRecorder(options.isSoycEnabled(), baos));
+        if (baos.size() == 0 && options.isSoycEnabled()) {
+          recordNonSplitDependencies(jprogram, baos);
+        }
         if (baos.size() > 0) {
           dependencies = new SoycArtifact("dependencies" + permutationId
               + ".xml.gz", baos.toByteArray());
@@ -995,4 +999,27 @@
       }
     }
   }
+
+  /**
+   * Dependency information is normally recorded during code splitting, and it
+   * results in multiple dependency graphs. If the code splitter doesn't run,
+   * then this method can be used instead to record a single dependency graph
+   * for the whole program.
+   */
+  private static void recordNonSplitDependencies(JProgram program,
+      OutputStream out) {
+    DependencyRecorder deps = new DependencyRecorder(out);
+    deps.open();
+    deps.startDependencyGraph("initial", null);
+
+    ControlFlowAnalyzer cfa = new ControlFlowAnalyzer(program);
+    cfa.setDependencyRecorder(deps);
+    for (List<JMethod> entryList : program.entryMethods) {
+      for (JMethod entry : entryList)
+        cfa.traverseFrom(entry);
+    }
+
+    deps.endDependencyGraph();
+    deps.close();
+  }
 }