• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2024 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.android.build.backportedfixes;
18 
19 import static java.nio.charset.StandardCharsets.UTF_8;
20 
21 import com.android.build.backportedfixes.common.Parser;
22 
23 import com.beust.jcommander.JCommander;
24 import com.beust.jcommander.Parameter;
25 import com.beust.jcommander.converters.FileConverter;
26 import com.google.common.io.Files;
27 
28 import java.io.File;
29 import java.io.PrintWriter;
30 import java.io.Writer;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.stream.Collectors;
34 
35 
36 /**
37  * Creates backported fix properties file.
38  *
39  * <p>Writes BitSet of backported fix aliases from a list of BackportedFix proto binary files and
40  * writes the property {@value PROPERTY_NAME} to a file.
41  */
42 public final class WriteBackportedFixesPropFile {
43 
44     private static final String PROPERTY_NAME = "ro.build.backported_fixes.alias_bitset.long_list";
45     @Parameter(description = "BackportedFix proto binary files",
46             converter = FileConverter.class,
47             required = true)
48     List<File> fixFiles;
49     @Parameter(description = "The file to write the property value to.",
50             names = {"--property_file", "-p"},
51             converter = FileConverter.class,
52             required = true)
53     File propertyFile;
54 
main(String... argv)55     public static void main(String... argv) throws Exception {
56         WriteBackportedFixesPropFile main = new WriteBackportedFixesPropFile();
57         JCommander.newBuilder().addObject(main).build().parse(argv);
58         main.run();
59     }
60 
WriteBackportedFixesPropFile()61     WriteBackportedFixesPropFile() {
62     }
63 
run()64     private void run() throws Exception {
65         try (var out = Files.newWriter(propertyFile, UTF_8)) {
66             var fixes = Parser.parseBackportedFixFiles(fixFiles);
67             writeFixesAsAliasBitSet(fixes, out);
68         }
69     }
70 
writeFixesAsAliasBitSet(BackportedFixes fixes, Writer out)71     static void writeFixesAsAliasBitSet(BackportedFixes fixes, Writer out) {
72         PrintWriter printWriter = new PrintWriter(out);
73         printWriter.println("# The following backported fixes have been applied");
74         for (var f : fixes.getFixesList()) {
75             printWriter.printf("# https://issuetracker.google.com/issues/%d with alias %d",
76                     f.getKnownIssue(), f.getAlias());
77             printWriter.println();
78         }
79         var bsArray = Parser.getBitSetArray(
80                 fixes.getFixesList().stream().mapToInt(BackportedFix::getAlias).toArray());
81         String bsString = Arrays.stream(bsArray).mapToObj(Long::toString).collect(
82                 Collectors.joining(","));
83         printWriter.printf("%s=%s", PROPERTY_NAME, bsString);
84         printWriter.println();
85         if (printWriter.checkError()) {
86             throw new RuntimeException("There was an error writing to " + out.toString());
87         }
88     }
89 }
90