langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/JavacState.java
author alundblad
Tue, 09 Jun 2015 15:57:45 +0200
changeset 31115 8d8e98052d5d
parent 27852 2e6ad0e4fe20
child 32335 7df616378cf3
permissions -rw-r--r--
8054717: SJavac should track changes in the public apis of classpath classes! Summary: Added functionality for tracking changes in public APIs of classpath classes. Reviewed-by: jlahoda, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     1
/*
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     4
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    10
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    15
 * accompanied this code).
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    16
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    20
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    23
 * questions.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    24
 */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    25
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    26
package com.sun.tools.sjavac;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    27
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    28
import java.io.BufferedReader;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    29
import java.io.File;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    30
import java.io.FileNotFoundException;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    31
import java.io.FileReader;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    32
import java.io.FileWriter;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    33
import java.io.IOException;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    34
import java.io.PrintStream;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    35
import java.net.URI;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    36
import java.nio.file.NoSuchFileException;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    37
import java.text.SimpleDateFormat;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    38
import java.util.Collection;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    39
import java.util.Collections;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    40
import java.util.Date;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    41
import java.util.HashMap;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    42
import java.util.HashSet;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    43
import java.util.List;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    44
import java.util.Map;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    45
import java.util.Set;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    46
import java.util.stream.Collectors;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    47
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
    48
import com.sun.tools.sjavac.options.Options;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
    49
import com.sun.tools.sjavac.pubapi.PubApi;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
    50
import com.sun.tools.sjavac.server.Sjavac;
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
    51
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    52
/**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    53
 * The javac state class maintains the previous (prev) and the current (now)
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    54
 * build states and everything else that goes into the javac_state file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    55
 *
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
    56
 *  <p><b>This is NOT part of any supported API.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
    57
 *  If you write code that depends on this, you do so at your own risk.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
    58
 *  This code and its internal interfaces are subject to change or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
    59
 *  deletion without notice.</b>
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    60
 */
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
    61
public class JavacState {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    62
    // The arguments to the compile. If not identical, then it cannot
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    63
    // be an incremental build!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    64
    String theArgs;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    65
    // The number of cores limits how many threads are used for heavy concurrent work.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    66
    int numCores;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    67
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    68
    // The bin_dir/javac_state
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    69
    private File javacState;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    70
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    71
    // The previous build state is loaded from javac_state
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    72
    private BuildState prev;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    73
    // The current build state is constructed during the build,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    74
    // then saved as the new javac_state.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    75
    private BuildState now;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    76
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    77
    // Something has changed in the javac_state. It needs to be saved!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    78
    private boolean needsSaving;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    79
    // If this is a new javac_state file, then do not print unnecessary messages.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    80
    private boolean newJavacState;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    81
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    82
    // These are packages where something has changed and the package
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    83
    // needs to be recompiled. Actions that trigger recompilation:
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    84
    // * source belonging to the package has changed
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    85
    // * artifact belonging to the package is lost, or its timestamp has been changed.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    86
    // * an unknown artifact has appeared, we simply delete it, but we also trigger a recompilation.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    87
    // * a package that is tainted, taints all packages that depend on it.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    88
    private Set<String> taintedPackages;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    89
    // After a compile, the pubapis are compared with the pubapis stored in the javac state file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    90
    // Any packages where the pubapi differ are added to this set.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    91
    // Later we use this set and the dependency information to taint dependent packages.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    92
    private Set<String> packagesWithChangedPublicApis;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    93
    // When a module-info.java file is changed, taint the module,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    94
    // then taint all modules that depend on that that module.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    95
    // A module dependency can occur directly through a require, or
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    96
    // indirectly through a module that does a public export for the first tainted module.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    97
    // When all modules are tainted, then taint all packages belonging to these modules.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    98
    // Then rebuild. It is perhaps possible (and valuable?) to do a more finegrained examination of the
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    99
    // change in module-info.java, but that will have to wait.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   100
    private Set<String> taintedModules;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   101
    // The set of all packages that has been recompiled.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   102
    // Copy over the javac_state for the packages that did not need recompilation,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   103
    // verbatim from the previous (prev) to the new (now) build state.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   104
    private Set<String> recompiledPackages;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   105
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   106
    // The output directories filled with tasty artifacts.
26088
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25299
diff changeset
   107
    private File binDir, gensrcDir, headerDir, stateDir;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   108
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   109
    // The current status of the file system.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   110
    private Set<File> binArtifacts;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   111
    private Set<File> gensrcArtifacts;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   112
    private Set<File> headerArtifacts;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   113
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   114
    // The status of the sources.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   115
    Set<Source> removedSources = null;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   116
    Set<Source> addedSources = null;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   117
    Set<Source> modifiedSources = null;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   118
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   119
    // Visible sources for linking. These are the only
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   120
    // ones that -sourcepath is allowed to see.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   121
    Set<URI> visibleSrcs;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   122
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   123
    // Visible classes for linking. These are the only
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   124
    // ones that -classpath is allowed to see.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   125
    // It maps from a classpath root to the set of visible classes for that root.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   126
    // If the set is empty, then all classes are visible for that root.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   127
    // It can also map from a jar file to the set of visible classes for that jar file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   128
    Map<URI,Set<String>> visibleClasses;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   129
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   130
    // Setup transform that always exist.
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   131
    private CompileJavaPackages compileJavaPackages = new CompileJavaPackages();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   132
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   133
    // Where to send stdout and stderr.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   134
    private PrintStream out, err;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   135
26089
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   136
    // Command line options.
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   137
    private Options options;
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   138
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   139
    JavacState(Options op, boolean removeJavacState, PrintStream o, PrintStream e) {
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   140
        options = op;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   141
        out = o;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   142
        err = e;
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   143
        numCores = options.getNumCores();
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   144
        theArgs = options.getStateArgsString();
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   145
        binDir = Util.pathToFile(options.getDestDir());
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   146
        gensrcDir = Util.pathToFile(options.getGenSrcDir());
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   147
        headerDir = Util.pathToFile(options.getHeaderDir());
26088
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25299
diff changeset
   148
        stateDir = Util.pathToFile(options.getStateDir());
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25299
diff changeset
   149
        javacState = new File(stateDir, "javac_state");
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   150
        if (removeJavacState && javacState.exists()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   151
            javacState.delete();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   152
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   153
        newJavacState = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   154
        if (!javacState.exists()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   155
            newJavacState = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   156
            // If there is no javac_state then delete the contents of all the artifact dirs!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   157
            // We do not want to risk building a broken incremental build.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   158
            // BUT since the makefiles still copy things straight into the bin_dir et al,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   159
            // we avoid deleting files here, if the option --permit-unidentified-classes was supplied.
26089
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   160
            if (!options.areUnidentifiedArtifactsPermitted()) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   161
                deleteContents(binDir);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   162
                deleteContents(gensrcDir);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   163
                deleteContents(headerDir);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   164
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   165
            needsSaving = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   166
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   167
        prev = new BuildState();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   168
        now = new BuildState();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   169
        taintedPackages = new HashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   170
        recompiledPackages = new HashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   171
        packagesWithChangedPublicApis = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   172
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   173
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   174
    public BuildState prev() { return prev; }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   175
    public BuildState now() { return now; }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   176
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   177
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   178
     * Remove args not affecting the state.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   179
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   180
    static String[] removeArgsNotAffectingState(String[] args) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   181
        String[] out = new String[args.length];
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   182
        int j = 0;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   183
        for (int i = 0; i<args.length; ++i) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   184
            if (args[i].equals("-j")) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   185
                // Just skip it and skip following value
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   186
                i++;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   187
            } else if (args[i].startsWith("--server:")) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   188
                // Just skip it.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   189
            } else if (args[i].startsWith("--log=")) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   190
                // Just skip it.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   191
            } else if (args[i].equals("--compare-found-sources")) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   192
                // Just skip it and skip verify file name
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   193
                i++;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   194
            } else {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   195
                // Copy argument.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   196
                out[j] = args[i];
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   197
                j++;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   198
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   199
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   200
        String[] ret = new String[j];
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   201
        System.arraycopy(out, 0, ret, 0, j);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   202
        return ret;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   203
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   204
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   205
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   206
     * Specify which sources are visible to the compiler through -sourcepath.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   207
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   208
    public void setVisibleSources(Map<String,Source> vs) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   209
        visibleSrcs = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   210
        for (String s : vs.keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   211
            Source src = vs.get(s);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   212
            visibleSrcs.add(src.file().toURI());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   213
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   214
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   215
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   216
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   217
     * Specify which classes are visible to the compiler through -classpath.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   218
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   219
    public void setVisibleClasses(Map<String,Source> vs) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   220
        visibleSrcs = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   221
        for (String s : vs.keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   222
            Source src = vs.get(s);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   223
            visibleSrcs.add(src.file().toURI());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   224
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   225
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   226
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   227
     * Returns true if this is an incremental build.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   228
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   229
    public boolean isIncremental() {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   230
        return !prev.sources().isEmpty();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   231
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   232
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   233
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   234
     * Find all artifacts that exists on disk.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   235
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   236
    public void findAllArtifacts() {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   237
        binArtifacts = findAllFiles(binDir);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   238
        gensrcArtifacts = findAllFiles(gensrcDir);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   239
        headerArtifacts = findAllFiles(headerDir);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   240
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   241
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   242
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   243
     * Lookup the artifacts generated for this package in the previous build.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   244
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   245
    private Map<String,File> fetchPrevArtifacts(String pkg) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   246
        Package p = prev.packages().get(pkg);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   247
        if (p != null) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   248
            return p.artifacts();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   249
        }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   250
        return new HashMap<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   251
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   252
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   253
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   254
     * Delete all prev artifacts in the currently tainted packages.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   255
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   256
    public void deleteClassArtifactsInTaintedPackages() {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   257
        for (String pkg : taintedPackages) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   258
            Map<String,File> arts = fetchPrevArtifacts(pkg);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   259
            for (File f : arts.values()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   260
                if (f.exists() && f.getName().endsWith(".class")) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   261
                    f.delete();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   262
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   263
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   264
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   265
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   266
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   267
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   268
     * Mark the javac_state file to be in need of saving and as a side effect,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   269
     * it gets a new timestamp.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   270
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   271
    private void needsSaving() {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   272
        needsSaving = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   273
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   274
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   275
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   276
     * Save the javac_state file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   277
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   278
    public void save() throws IOException {
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   279
        if (!needsSaving)
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   280
            return;
26088
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25299
diff changeset
   281
        try (FileWriter out = new FileWriter(javacState)) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   282
            StringBuilder b = new StringBuilder();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   283
            long millisNow = System.currentTimeMillis();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   284
            Date d = new Date(millisNow);
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   285
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   286
            b.append("# javac_state ver 0.4 generated "+millisNow+" "+df.format(d)+"\n");
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   287
            b.append("# This format might change at any time. Please do not depend on it.\n");
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   288
            b.append("# R arguments\n");
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   289
            b.append("# M module\n");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   290
            b.append("# P package\n");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   291
            b.append("# S C source_tobe_compiled timestamp\n");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   292
            b.append("# S L link_only_source timestamp\n");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   293
            b.append("# G C generated_source timestamp\n");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   294
            b.append("# A artifact timestamp\n");
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   295
            b.append("# D S dependant -> source dependency\n");
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   296
            b.append("# D C dependant -> classpath dependency\n");
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   297
            b.append("# I pubapi\n");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   298
            b.append("R ").append(theArgs).append("\n");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   299
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   300
            // Copy over the javac_state for the packages that did not need recompilation.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   301
            now.copyPackagesExcept(prev, recompiledPackages, new HashSet<String>());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   302
            // Save the packages, ie package names, dependencies, pubapis and artifacts!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   303
            // I.e. the lot.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   304
            Module.saveModules(now.modules(), b);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   305
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   306
            String s = b.toString();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   307
            out.write(s, 0, s.length());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   308
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   309
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   310
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   311
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   312
     * Load a javac_state file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   313
     */
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   314
    public static JavacState load(Options options, PrintStream out, PrintStream err) {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   315
        JavacState db = new JavacState(options, false, out, err);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   316
        Module  lastModule = null;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   317
        Package lastPackage = null;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   318
        Source  lastSource = null;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   319
        boolean noFileFound = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   320
        boolean foundCorrectVerNr = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   321
        boolean newCommandLine = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   322
        boolean syntaxError = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   323
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   324
        Log.debug("Loading javac state file: " + db.javacState);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   325
26088
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25299
diff changeset
   326
        try (BufferedReader in = new BufferedReader(new FileReader(db.javacState))) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   327
            for (;;) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   328
                String l = in.readLine();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   329
                if (l==null) break;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   330
                if (l.length()>=3 && l.charAt(1) == ' ') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   331
                    char c = l.charAt(0);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   332
                    if (c == 'M') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   333
                        lastModule = db.prev.loadModule(l);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   334
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   335
                    if (c == 'P') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   336
                        if (lastModule == null) { syntaxError = true; break; }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   337
                        lastPackage = db.prev.loadPackage(lastModule, l);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   338
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   339
                    if (c == 'D') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   340
                        if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   341
                        char depType = l.charAt(2);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   342
                        if (depType != 'S' && depType != 'C')
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   343
                            throw new RuntimeException("Bad dependency string: " + l);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   344
                        lastPackage.parseAndAddDependency(l.substring(4), depType == 'C');
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   345
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   346
                    if (c == 'I') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   347
                        if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   348
                        lastPackage.getPubApi().appendItem(l.substring(2)); // Strip "I "
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   349
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   350
                    if (c == 'A') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   351
                        if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   352
                        lastPackage.loadArtifact(l);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   353
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   354
                    if (c == 'S') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   355
                        if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   356
                        lastSource = db.prev.loadSource(lastPackage, l, false);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   357
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   358
                    if (c == 'G') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   359
                        if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   360
                        lastSource = db.prev.loadSource(lastPackage, l, true);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   361
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   362
                    if (c == 'R') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   363
                        String ncmdl = "R "+db.theArgs;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   364
                        if (!l.equals(ncmdl)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   365
                            newCommandLine = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   366
                        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   367
                    } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   368
                         if (c == '#') {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   369
                        if (l.startsWith("# javac_state ver ")) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   370
                            int sp = l.indexOf(" ", 18);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   371
                            if (sp != -1) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   372
                                String ver = l.substring(18,sp);
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   373
                                if (!ver.equals("0.4")) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   374
                    break;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   375
                                 }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   376
                foundCorrectVerNr = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   377
                            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   378
                        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   379
                    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   380
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   381
            }
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   382
        } catch (FileNotFoundException | NoSuchFileException e) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   383
            // Silently create a new javac_state file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   384
            noFileFound = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   385
        } catch (IOException e) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   386
            Log.info("Dropping old javac_state because of errors when reading it.");
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   387
            db = new JavacState(options, true, out, err);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   388
            foundCorrectVerNr = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   389
            newCommandLine = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   390
            syntaxError = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   391
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   392
        if (foundCorrectVerNr == false && !noFileFound) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   393
            Log.info("Dropping old javac_state since it is of an old version.");
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   394
            db = new JavacState(options, true, out, err);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   395
        } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   396
        if (newCommandLine == true && !noFileFound) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   397
            Log.info("Dropping old javac_state since a new command line is used!");
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   398
            db = new JavacState(options, true, out, err);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   399
        } else
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   400
        if (syntaxError == true) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   401
            Log.info("Dropping old javac_state since it contains syntax errors.");
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   402
            db = new JavacState(options, true, out, err);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   403
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   404
        db.prev.calculateDependents();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   405
        return db;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   406
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   407
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   408
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   409
     * Mark a java package as tainted, ie it needs recompilation.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   410
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   411
    public void taintPackage(String name, String because) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   412
        if (!taintedPackages.contains(name)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   413
            if (because != null) Log.debug("Tainting "+Util.justPackageName(name)+" because "+because);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   414
            // It has not been tainted before.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   415
            taintedPackages.add(name);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   416
            needsSaving();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   417
            Package nowp = now.packages().get(name);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   418
            if (nowp != null) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   419
                for (String d : nowp.dependents()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   420
                    taintPackage(d, because);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   421
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   422
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   423
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   424
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   425
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   426
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   427
     * This packages need recompilation.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   428
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   429
    public Set<String> taintedPackages() {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   430
        return taintedPackages;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   431
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   432
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   433
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   434
     * Clean out the tainted package set, used after the first round of compiles,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   435
     * prior to propagating dependencies.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   436
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   437
    public void clearTaintedPackages() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   438
        taintedPackages = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   439
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   440
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   441
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   442
     * Go through all sources and check which have been removed, added or modified
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   443
     * and taint the corresponding packages.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   444
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   445
    public void checkSourceStatus(boolean check_gensrc) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   446
        removedSources = calculateRemovedSources();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   447
        for (Source s : removedSources) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   448
            if (!s.isGenerated() || check_gensrc) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   449
                taintPackage(s.pkg().name(), "source "+s.name()+" was removed");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   450
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   451
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   452
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   453
        addedSources = calculateAddedSources();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   454
        for (Source s : addedSources) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   455
            String msg = null;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   456
            if (isIncremental()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   457
                // When building from scratch, there is no point
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   458
                // printing "was added" for every file since all files are added.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   459
                // However for an incremental build it makes sense.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   460
                msg = "source "+s.name()+" was added";
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   461
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   462
            if (!s.isGenerated() || check_gensrc) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   463
                taintPackage(s.pkg().name(), msg);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   464
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   465
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   466
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   467
        modifiedSources = calculateModifiedSources();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   468
        for (Source s : modifiedSources) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   469
            if (!s.isGenerated() || check_gensrc) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   470
                taintPackage(s.pkg().name(), "source "+s.name()+" was modified");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   471
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   472
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   473
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   474
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   475
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   476
     * Acquire the compile_java_packages suffix rule for .java files.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   477
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   478
    public Map<String,Transformer> getJavaSuffixRule() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   479
        Map<String,Transformer> sr = new HashMap<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   480
        sr.put(".java", compileJavaPackages);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   481
        return sr;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   482
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   483
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   484
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   485
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   486
     * If artifacts have gone missing, force a recompile of the packages
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   487
     * they belong to.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   488
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   489
    public void taintPackagesThatMissArtifacts() {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   490
        for (Package pkg : prev.packages().values()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   491
            for (File f : pkg.artifacts().values()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   492
                if (!f.exists()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   493
                    // Hmm, the artifact on disk does not exist! Someone has removed it....
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   494
                    // Lets rebuild the package.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   495
                    taintPackage(pkg.name(), ""+f+" is missing.");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   496
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   497
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   498
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   499
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   500
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   501
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   502
     * Propagate recompilation through the dependency chains.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   503
     * Avoid re-tainting packages that have already been compiled.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   504
     */
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   505
    public void taintPackagesDependingOnChangedPackages(Set<String> pkgsWithChangedPubApi, Set<String> recentlyCompiled) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   506
        // For each to-be-recompiled-candidates...
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   507
        for (Package pkg : new HashSet<>(prev.packages().values())) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   508
            // Find out what it depends upon...
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   509
            Set<String> deps = pkg.typeDependencies()
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   510
                                  .values()
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   511
                                  .stream()
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   512
                                  .flatMap(s -> s.stream())
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   513
                                  .collect(Collectors.toSet());
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   514
            for (String dep : deps) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   515
                String depPkg = ":" + dep.substring(0, dep.lastIndexOf('.'));
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   516
                if (depPkg.equals(pkg.name()))
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   517
                    continue;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   518
                // Checking if that dependency has changed
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   519
                if (pkgsWithChangedPubApi.contains(depPkg) && !recentlyCompiled.contains(pkg.name())) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   520
                    taintPackage(pkg.name(), "its depending on " + depPkg);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   521
                }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   522
            }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   523
        }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   524
    }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   525
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   526
    /**
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   527
     * Compare the javac_state recorded public apis of packages on the classpath
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   528
     * with the actual public apis on the classpath.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   529
     */
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   530
    public void taintPackagesDependingOnChangedClasspathPackages() {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   531
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   532
        // 1. Collect fully qualified names of all interesting classpath dependencies
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   533
        Set<String> fqDependencies = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   534
        for (Package pkg : prev.packages().values()) {
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   535
            // Check if this package was compiled. If it's presence is recorded
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   536
            // because it was on the class path and we needed to save it's
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   537
            // public api, it's not a candidate for tainting.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   538
            if (pkg.sources().isEmpty())
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   539
                continue;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   540
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   541
            pkg.typeClasspathDependencies().values().forEach(fqDependencies::addAll);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   542
        }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   543
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   544
        // 2. Extract the public APIs from the on disk .class files
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   545
        // (Reason for doing step 1 in a separate phase is to avoid extracting
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   546
        // public APIs of the same class twice.)
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   547
        PubApiExtractor pubApiExtractor = new PubApiExtractor(options);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   548
        Map<String, PubApi> onDiskPubApi = new HashMap<>();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   549
        for (String cpDep : fqDependencies) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   550
            onDiskPubApi.put(cpDep, pubApiExtractor.getPubApi(cpDep));
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   551
        }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   552
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   553
        // 3. Compare them with the public APIs as of last compilation (loaded from javac_state)
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   554
        nextPkg:
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   555
        for (Package pkg : prev.packages().values()) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   556
            // Check if this package was compiled. If it's presence is recorded
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   557
            // because it was on the class path and we needed to save it's
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   558
            // public api, it's not a candidate for tainting.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   559
            if (pkg.sources().isEmpty())
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   560
                continue;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   561
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   562
            Set<String> cpDepsOfThisPkg = new HashSet<>();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   563
            for (Set<String> cpDeps : pkg.typeClasspathDependencies().values())
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   564
                cpDepsOfThisPkg.addAll(cpDeps);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   565
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   566
            for (String fqDep : cpDepsOfThisPkg) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   567
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   568
                String depPkg = ":" + fqDep.substring(0, fqDep.lastIndexOf('.'));
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   569
                PubApi prevPkgApi = prev.packages().get(depPkg).getPubApi();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   570
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   571
                // This PubApi directly lists the members of the class,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   572
                // i.e. [ MEMBER1, MEMBER2, ... ]
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   573
                PubApi prevDepApi = prevPkgApi.types.get(fqDep).pubApi;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   574
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   575
                // In order to dive *into* the class, we need to add
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   576
                // .types.get(fqDep).pubApi below.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   577
                PubApi currentDepApi = onDiskPubApi.get(fqDep).types.get(fqDep).pubApi;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   578
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   579
                if (!currentDepApi.isBackwardCompatibleWith(prevDepApi)) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   580
                    List<String> apiDiff = currentDepApi.diff(prevDepApi);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   581
                    taintPackage(pkg.name(), "depends on classpath "
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   582
                                + "package which has an updated package api: "
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   583
                                + String.join("\n", apiDiff));
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   584
                    //Log.debug("========================================");
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   585
                    //Log.debug("------ PREV API ------------------------");
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   586
                    //prevDepApi.asListOfStrings().forEach(Log::debug);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   587
                    //Log.debug("------ CURRENT API ---------------------");
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   588
                    //currentDepApi.asListOfStrings().forEach(Log::debug);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   589
                    //Log.debug("========================================");
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   590
                    continue nextPkg;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   591
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   592
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   593
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   594
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   595
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   596
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   597
     * Scan all output dirs for artifacts and remove those files (artifacts?)
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   598
     * that are not recognized as such, in the javac_state file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   599
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   600
    public void removeUnidentifiedArtifacts() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   601
        Set<File> allKnownArtifacts = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   602
        for (Package pkg : prev.packages().values()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   603
            for (File f : pkg.artifacts().values()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   604
                allKnownArtifacts.add(f);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   605
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   606
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   607
        // Do not forget about javac_state....
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   608
        allKnownArtifacts.add(javacState);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   609
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   610
        for (File f : binArtifacts) {
26089
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   611
            if (!allKnownArtifacts.contains(f) &&
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   612
                !options.isUnidentifiedArtifactPermitted(f.getAbsolutePath())) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   613
                Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   614
                f.delete();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   615
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   616
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   617
        for (File f : headerArtifacts) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   618
            if (!allKnownArtifacts.contains(f)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   619
                Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   620
                f.delete();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   621
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   622
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   623
        for (File f : gensrcArtifacts) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   624
            if (!allKnownArtifacts.contains(f)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   625
                Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   626
                f.delete();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   627
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   628
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   629
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   630
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   631
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   632
     * Remove artifacts that are no longer produced when compiling!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   633
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   634
    public void removeSuperfluousArtifacts(Set<String> recentlyCompiled) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   635
        // Nothing to do, if nothing was recompiled.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   636
        if (recentlyCompiled.size() == 0) return;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   637
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   638
        for (String pkg : now.packages().keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   639
            // If this package has not been recompiled, skip the check.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   640
            if (!recentlyCompiled.contains(pkg)) continue;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   641
            Collection<File> arts = now.artifacts().values();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   642
            for (File f : fetchPrevArtifacts(pkg).values()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   643
                if (!arts.contains(f)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   644
                    Log.debug("Removing "+f.getPath()+" since it is now superfluous!");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   645
                    if (f.exists()) f.delete();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   646
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   647
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   648
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   649
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   650
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   651
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   652
     * Return those files belonging to prev, but not now.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   653
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   654
    private Set<Source> calculateRemovedSources() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   655
        Set<Source> removed = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   656
        for (String src : prev.sources().keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   657
            if (now.sources().get(src) == null) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   658
                removed.add(prev.sources().get(src));
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   659
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   660
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   661
        return removed;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   662
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   663
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   664
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   665
     * Return those files belonging to now, but not prev.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   666
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   667
    private Set<Source> calculateAddedSources() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   668
        Set<Source> added = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   669
        for (String src : now.sources().keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   670
            if (prev.sources().get(src) == null) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   671
                added.add(now.sources().get(src));
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   672
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   673
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   674
        return added;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   675
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   676
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   677
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   678
     * Return those files where the timestamp is newer.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   679
     * If a source file timestamp suddenly is older than what is known
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   680
     * about it in javac_state, then consider it modified, but print
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   681
     * a warning!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   682
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   683
    private Set<Source> calculateModifiedSources() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   684
        Set<Source> modified = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   685
        for (String src : now.sources().keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   686
            Source n = now.sources().get(src);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   687
            Source t = prev.sources().get(src);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   688
            if (prev.sources().get(src) != null) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   689
                if (t != null) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   690
                    if (n.lastModified() > t.lastModified()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   691
                        modified.add(n);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   692
                    } else if (n.lastModified() < t.lastModified()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   693
                        modified.add(n);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   694
                        Log.warn("The source file "+n.name()+" timestamp has moved backwards in time.");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   695
                    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   696
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   697
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   698
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   699
        return modified;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   700
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   701
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   702
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   703
     * Recursively delete a directory and all its contents.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   704
     */
26089
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   705
    private void deleteContents(File dir) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   706
        if (dir != null && dir.exists()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   707
            for (File f : dir.listFiles()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   708
                if (f.isDirectory()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   709
                    deleteContents(f);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   710
                }
26089
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   711
                if (!options.isUnidentifiedArtifactPermitted(f.getAbsolutePath())) {
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   712
                    Log.debug("Removing "+f.getAbsolutePath());
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   713
                    f.delete();
196b7a50a266 8054465: Add --permit-artifact=bar to sjavac
ohrstrom
parents: 26088
diff changeset
   714
                }
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   715
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   716
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   717
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   718
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   719
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   720
     * Run the copy translator only.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   721
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   722
    public void performCopying(File binDir, Map<String,Transformer> suffixRules) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   723
        Map<String,Transformer> sr = new HashMap<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   724
        for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   725
            if (e.getValue().getClass().equals(CopyFile.class)) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   726
                sr.put(e.getKey(), e.getValue());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   727
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   728
        }
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents: 24067
diff changeset
   729
        perform(null, binDir, sr);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   730
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   731
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   732
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   733
     * Run all the translators that translate into java source code.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   734
     * I.e. all translators that are not copy nor compile_java_source.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   735
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   736
    public void performTranslation(File gensrcDir, Map<String,Transformer> suffixRules) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   737
        Map<String,Transformer> sr = new HashMap<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   738
        for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   739
            Class<?> trClass = e.getValue().getClass();
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   740
            if (trClass == CompileJavaPackages.class || trClass == CopyFile.class)
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   741
                continue;
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   742
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   743
            sr.put(e.getKey(), e.getValue());
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   744
        }
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents: 24067
diff changeset
   745
        perform(null, gensrcDir, sr);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   746
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   747
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   748
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   749
     * Compile all the java sources. Return true, if it needs to be called again!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   750
     */
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
   751
    public boolean performJavaCompilations(Sjavac sjavac,
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents: 24067
diff changeset
   752
                                           Options args,
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   753
                                           Set<String> recentlyCompiled,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   754
                                           boolean[] rcValue) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   755
        Map<String,Transformer> suffixRules = new HashMap<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   756
        suffixRules.put(".java", compileJavaPackages);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   757
        compileJavaPackages.setExtra(args);
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
   758
        rcValue[0] = perform(sjavac, binDir, suffixRules);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   759
        recentlyCompiled.addAll(taintedPackages());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   760
        clearTaintedPackages();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   761
        boolean again = !packagesWithChangedPublicApis.isEmpty();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   762
        taintPackagesDependingOnChangedPackages(packagesWithChangedPublicApis, recentlyCompiled);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   763
        packagesWithChangedPublicApis = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   764
        return again && rcValue[0];
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   765
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   766
        // TODO: Figure out why 'again' checks packagesWithChangedPublicAPis.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   767
        // (It shouldn't matter if packages had changed pub apis as long as no
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   768
        // one depends on them. Wouldn't it make more sense to let 'again'
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   769
        // depend on taintedPackages?)
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   770
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   771
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   772
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   773
     * Store the source into the set of sources belonging to the given transform.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   774
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   775
    private void addFileToTransform(Map<Transformer,Map<String,Set<URI>>> gs, Transformer t, Source s) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   776
        Map<String,Set<URI>> fs = gs.get(t);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   777
        if (fs == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   778
            fs = new HashMap<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   779
            gs.put(t, fs);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   780
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   781
        Set<URI> ss = fs.get(s.pkg().name());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   782
        if (ss == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   783
            ss = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   784
            fs.put(s.pkg().name(), ss);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   785
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   786
        ss.add(s.file().toURI());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   787
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   788
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   789
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   790
     * For all packages, find all sources belonging to the package, group the sources
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   791
     * based on their transformers and apply the transformers on each source code group.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   792
     */
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
   793
    private boolean perform(Sjavac sjavac,
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents: 24067
diff changeset
   794
                            File outputDir,
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26089
diff changeset
   795
                            Map<String,Transformer> suffixRules) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   796
        boolean rc = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   797
        // Group sources based on transforms. A source file can only belong to a single transform.
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   798
        Map<Transformer,Map<String,Set<URI>>> groupedSources = new HashMap<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   799
        for (Source src : now.sources().values()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   800
            Transformer t = suffixRules.get(src.suffix());
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   801
            if (t != null) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   802
                if (taintedPackages.contains(src.pkg().name()) && !src.isLinkedOnly()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   803
                    addFileToTransform(groupedSources, t, src);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   804
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   805
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   806
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   807
        // Go through the transforms and transform them.
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   808
        for (Map.Entry<Transformer, Map<String, Set<URI>>> e : groupedSources.entrySet()) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   809
            Transformer t = e.getKey();
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   810
            Map<String, Set<URI>> srcs = e.getValue();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   811
            // These maps need to be synchronized since multiple threads will be
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   812
            // writing results into them.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   813
            Map<String, Set<URI>> packageArtifacts = Collections.synchronizedMap(new HashMap<>());
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   814
            Map<String, Map<String, Set<String>>> packageDependencies = Collections.synchronizedMap(new HashMap<>());
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   815
            Map<String, Map<String, Set<String>>> packageCpDependencies = Collections.synchronizedMap(new HashMap<>());
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   816
            Map<String, PubApi> packagePublicApis = Collections.synchronizedMap(new HashMap<>());
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   817
            Map<String, PubApi> dependencyPublicApis = Collections.synchronizedMap(new HashMap<>());
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   818
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   819
            boolean r = t.transform(sjavac,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   820
                                    srcs,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   821
                                    visibleSrcs,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   822
                                    visibleClasses,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   823
                                    prev.dependents(),
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   824
                                    outputDir.toURI(),
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   825
                                    packageArtifacts,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   826
                                    packageDependencies,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   827
                                    packageCpDependencies,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   828
                                    packagePublicApis,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   829
                                    dependencyPublicApis,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   830
                                    0,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   831
                                    isIncremental(),
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   832
                                    numCores,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   833
                                    out,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   834
                                    err);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   835
            if (!r)
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   836
                rc = false;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   837
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   838
            for (String p : srcs.keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   839
                recompiledPackages.add(p);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   840
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   841
            // The transform is done! Extract all the artifacts and store the info into the Package objects.
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   842
            for (Map.Entry<String, Set<URI>> a : packageArtifacts.entrySet()) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   843
                Module mnow = now.findModuleFromPackageName(a.getKey());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   844
                mnow.addArtifacts(a.getKey(), a.getValue());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   845
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   846
            // Extract all the dependencies and store the info into the Package objects.
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   847
            for (Map.Entry<String, Map<String, Set<String>>> a : packageDependencies.entrySet()) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   848
                Map<String, Set<String>> deps = a.getValue();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   849
                Module mnow = now.findModuleFromPackageName(a.getKey());
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   850
                mnow.setDependencies(a.getKey(), deps, false);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   851
            }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   852
            for (Map.Entry<String, Map<String, Set<String>>> a : packageCpDependencies.entrySet()) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   853
                Map<String, Set<String>> deps = a.getValue();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   854
                Module mnow = now.findModuleFromPackageName(a.getKey());
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   855
                mnow.setDependencies(a.getKey(), deps, true);
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   856
            }
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   857
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   858
            // This map contains the public api of the types that this
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   859
            // compilation depended upon. This means that it may not contain
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   860
            // full packages. In other words, we shouldn't remove knowledge of
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   861
            // public apis but merge these with what we already have.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   862
            for (Map.Entry<String, PubApi> a : dependencyPublicApis.entrySet()) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   863
                String pkg = a.getKey();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   864
                PubApi packagePartialPubApi = a.getValue();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   865
                Package pkgNow = now.findModuleFromPackageName(pkg).lookupPackage(pkg);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   866
                PubApi currentPubApi = pkgNow.getPubApi();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   867
                PubApi newPubApi = PubApi.mergeTypes(currentPubApi, packagePartialPubApi);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   868
                pkgNow.setPubapi(newPubApi);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   869
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   870
                // See JDK-8071904
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   871
                if (now.packages().containsKey(pkg))
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   872
                    now.packages().get(pkg).setPubapi(newPubApi);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   873
                else
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   874
                    now.packages().put(pkg, pkgNow);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   875
            }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   876
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   877
            // The packagePublicApis cover entire packages (since sjavac compiles
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   878
            // stuff on package level). This means that if a type is missing
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   879
            // in the public api of a given package, it means that it has been
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   880
            // removed. In other words, we should *set* the pubapi to whatever
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   881
            // this map contains, and not merge it with what we already have.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   882
            for (Map.Entry<String, PubApi> a : packagePublicApis.entrySet()) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   883
                String pkg = a.getKey();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   884
                PubApi newPubApi = a.getValue();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   885
                Module mprev = prev.findModuleFromPackageName(pkg);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   886
                Module mnow = now.findModuleFromPackageName(pkg);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   887
                mnow.setPubapi(pkg, newPubApi);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   888
                if (mprev.hasPubapiChanged(pkg, newPubApi)) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   889
                    // Aha! The pubapi of this package has changed!
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   890
                    // It can also be a new compile from scratch.
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   891
                    if (mprev.lookupPackage(pkg).existsInJavacState()) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   892
                        // This is an incremental compile! The pubapi
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   893
                        // did change. Trigger recompilation of dependents.
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   894
                        packagesWithChangedPublicApis.add(pkg);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   895
                        Log.info("The API of " + Util.justPackageName(pkg) + " has changed!");
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   896
                    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   897
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   898
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   899
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   900
        return rc;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   901
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   902
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   903
    /**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   904
     * Utility method to recursively find all files below a directory.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   905
     */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   906
    private static Set<File> findAllFiles(File dir) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   907
        Set<File> foundFiles = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   908
        if (dir == null) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   909
            return foundFiles;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   910
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   911
        recurse(dir, foundFiles);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   912
        return foundFiles;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   913
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   914
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   915
    private static void recurse(File dir, Set<File> foundFiles) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   916
        for (File f : dir.listFiles()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   917
            if (f.isFile()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   918
                foundFiles.add(f);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   919
            } else if (f.isDirectory()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   920
                recurse(f, foundFiles);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   921
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   922
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   923
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   924
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   925
    /**
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   926
     * Compare the calculate source list, with an explicit list, usually
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   927
     * supplied from the makefile. Used to detect bugs where the makefile and
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   928
     * sjavac have different opinions on which files should be compiled.
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   929
     */
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   930
    public void compareWithMakefileList(File makefileSourceList)
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   931
            throws ProblemException {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   932
        // If we are building on win32 using for example cygwin the paths in the
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   933
        // makefile source list
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   934
        // might be /cygdrive/c/.... which does not match c:\....
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   935
        // We need to adjust our calculated sources to be identical, if
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   936
        // necessary.
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   937
        boolean mightNeedRewriting = File.pathSeparatorChar == ';';
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   938
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   939
        if (makefileSourceList == null)
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27852
diff changeset
   940
            return;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   941
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   942
        Set<String> calculatedSources = new HashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19500
diff changeset
   943
        Set<String> listedSources = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   944
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   945
        // Create a set of filenames with full paths.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   946
        for (Source s : now.sources().values()) {
19500
26d88d483764 8015145: Smartjavac needs more flexibility with linking to sources
erikj
parents: 15368
diff changeset
   947
            // Don't include link only sources when comparing sources to compile
26d88d483764 8015145: Smartjavac needs more flexibility with linking to sources
erikj
parents: 15368
diff changeset
   948
            if (!s.isLinkedOnly()) {
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   949
                String path = s.file().getPath();
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   950
                if (mightNeedRewriting)
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   951
                    path = Util.normalizeDriveLetter(path);
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
   952
                calculatedSources.add(path);
19500
26d88d483764 8015145: Smartjavac needs more flexibility with linking to sources
erikj
parents: 15368
diff changeset
   953
            }
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   954
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   955
        // Read in the file and create another set of filenames with full paths.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   956
        try {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   957
            BufferedReader in = new BufferedReader(new FileReader(makefileSourceList));
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   958
            for (;;) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   959
                String l = in.readLine();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   960
                if (l==null) break;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   961
                l = l.trim();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   962
                if (mightNeedRewriting) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   963
                    if (l.indexOf(":") == 1 && l.indexOf("\\") == 2) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   964
                        // Everything a-ok, the format is already C:\foo\bar
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   965
                    } else if (l.indexOf(":") == 1 && l.indexOf("/") == 2) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   966
                        // The format is C:/foo/bar, rewrite into the above format.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   967
                        l = l.replaceAll("/","\\\\");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   968
                    } else if (l.charAt(0) == '/' && l.indexOf("/",1) != -1) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   969
                        // The format might be: /cygdrive/c/foo/bar, rewrite into the above format.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   970
                        // Do not hardcode the name cygdrive here.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   971
                        int slash = l.indexOf("/",1);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   972
                        l = l.replaceAll("/","\\\\");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   973
                        l = ""+l.charAt(slash+1)+":"+l.substring(slash+2);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   974
                    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   975
                    if (Character.isLowerCase(l.charAt(0))) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   976
                        l = Character.toUpperCase(l.charAt(0))+l.substring(1);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   977
                    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   978
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   979
                listedSources.add(l);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   980
            }
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   981
        } catch (FileNotFoundException | NoSuchFileException e) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   982
            throw new ProblemException("Could not open "+makefileSourceList.getPath()+" since it does not exist!");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   983
        } catch (IOException e) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   984
            throw new ProblemException("Could not read "+makefileSourceList.getPath());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   985
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   986
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   987
        for (String s : listedSources) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   988
            if (!calculatedSources.contains(s)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   989
                 throw new ProblemException("The makefile listed source "+s+" was not calculated by the smart javac wrapper!");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   990
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   991
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   992
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   993
        for (String s : calculatedSources) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   994
            if (!listedSources.contains(s)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   995
                throw new ProblemException("The smart javac wrapper calculated source "+s+" was not listed by the makefiles!");
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   996
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   997
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   998
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   999
}