1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tools.r8wrappers.utils; 17 18 import com.android.tools.r8.InputDependencyGraphConsumer; 19 import com.android.tools.r8.origin.Origin; 20 import java.io.IOException; 21 import java.io.Writer; 22 import java.nio.charset.StandardCharsets; 23 import java.nio.file.Files; 24 import java.nio.file.Path; 25 import java.nio.file.Paths; 26 import java.nio.file.StandardOpenOption; 27 import java.util.ArrayList; 28 import java.util.HashSet; 29 import java.util.List; 30 import java.util.Set; 31 32 public class DepsFileWriter implements InputDependencyGraphConsumer { 33 34 private final Path dependentFile; 35 private final String dependencyOutput; 36 private final Set<Path> dependencies = new HashSet<>(); 37 DepsFileWriter(Path dependentFile, String dependencyOutput)38 public DepsFileWriter(Path dependentFile, String dependencyOutput) { 39 this.dependentFile = dependentFile; 40 this.dependencyOutput = dependencyOutput; 41 } 42 43 @Override accept(Origin dependent, Path dependency)44 public void accept(Origin dependent, Path dependency) { 45 dependencies.add(dependency); 46 } 47 48 @Override finished()49 public void finished() { 50 List<Path> sorted = new ArrayList<>(dependencies); 51 sorted.sort(Path::compareTo); 52 Path output = Paths.get(dependencyOutput); 53 try (Writer writer = 54 Files.newBufferedWriter( 55 output, 56 StandardCharsets.UTF_8, 57 StandardOpenOption.CREATE, 58 StandardOpenOption.TRUNCATE_EXISTING)) { 59 writer.write(escape(dependentFile.toString())); 60 writer.write(":"); 61 for (Path path : sorted) { 62 writer.write(" "); 63 writer.write(escape(path.toString())); 64 } 65 writer.write("\n"); 66 } catch (IOException e) { 67 throw new RuntimeException(e); 68 } 69 } 70 escape(String filepath)71 private static String escape(String filepath) { 72 return filepath.replace(" ", "\\ "); 73 } 74 } 75