/** * Scan all output dirs for artifacts and remove those files (artifacts?) that are not recognized * as such, in the javac_state file. */ public void removeUnidentifiedArtifacts() { Set<File> allKnownArtifacts = new HashSet<>(); for (Package pkg : prev.packages().values()) { for (File f : pkg.artifacts().values()) { allKnownArtifacts.add(f); } } // Do not forget about javac_state.... allKnownArtifacts.add(javacState); for (File f : binArtifacts) { if (!allKnownArtifacts.contains(f) && !options.isUnidentifiedArtifactPermitted(f.getAbsolutePath())) { Log.debug("Removing " + f.getPath() + " since it is unknown to the javac_state."); f.delete(); } } for (File f : headerArtifacts) { if (!allKnownArtifacts.contains(f)) { Log.debug("Removing " + f.getPath() + " since it is unknown to the javac_state."); f.delete(); } } for (File f : gensrcArtifacts) { if (!allKnownArtifacts.contains(f)) { Log.debug("Removing " + f.getPath() + " since it is unknown to the javac_state."); f.delete(); } } }
JavacState(Options op, boolean removeJavacState, PrintStream o, PrintStream e) { options = op; out = o; err = e; numCores = options.getNumCores(); theArgs = options.getStateArgsString(); binDir = Util.pathToFile(options.getDestDir()); gensrcDir = Util.pathToFile(options.getGenSrcDir()); headerDir = Util.pathToFile(options.getHeaderDir()); stateDir = Util.pathToFile(options.getStateDir()); javacState = new File(stateDir, "javac_state"); if (removeJavacState && javacState.exists()) { javacState.delete(); } newJavacState = false; if (!javacState.exists()) { newJavacState = true; // If there is no javac_state then delete the contents of all the artifact dirs! // We do not want to risk building a broken incremental build. // BUT since the makefiles still copy things straight into the bin_dir et al, // we avoid deleting files here, if the option --permit-unidentified-classes was supplied. if (!options.areUnidentifiedArtifactsPermitted()) { deleteContents(binDir); deleteContents(gensrcDir); deleteContents(headerDir); } needsSaving = true; } prev = new BuildState(); now = new BuildState(); taintedPackages = new HashSet<>(); recompiledPackages = new HashSet<>(); packagesWithChangedPublicApis = new HashSet<>(); }
/** Recursively delete a directory and all its contents. */ private void deleteContents(File dir) { if (dir != null && dir.exists()) { for (File f : dir.listFiles()) { if (f.isDirectory()) { deleteContents(f); } if (!options.isUnidentifiedArtifactPermitted(f.getAbsolutePath())) { Log.debug("Removing " + f.getAbsolutePath()); f.delete(); } } } }