diff options
| author | 2024-11-14 12:54:14 -0800 | |
|---|---|---|
| committer | 2024-11-14 13:12:05 -0800 | |
| commit | c513070f526fe3bf0fbff802b6e519ec20d83f0f (patch) | |
| tree | 04ac07c2e773faa15728fc567c93a530092ee417 /backported_fixes/src/java | |
| parent | 03b57b0f15b314ed7bd93535d22528b18f3e236a (diff) | |
Set system property from list of backported fixes
Because the caluculation of a BitSet is
to compicated for make a genrule and java are
used to ro.build.backported_fixes.alias_bitset.long_list to the end of
the system property file.
This part of the API for backported fixes. See go/android-backported-fixes-spec
Bug: 377727445
Test: atest applied_backported_fixes_test backported_fixes_common_test
Change-Id: I2621e2fb52401495f796a5a4db330c5dfafbda40
Diffstat (limited to 'backported_fixes/src/java')
3 files changed, 217 insertions, 0 deletions
diff --git a/backported_fixes/src/java/com/android/build/backportedfixes/Main.java b/backported_fixes/src/java/com/android/build/backportedfixes/Main.java new file mode 100644 index 0000000000..79148cc838 --- /dev/null +++ b/backported_fixes/src/java/com/android/build/backportedfixes/Main.java @@ -0,0 +1,79 @@ + +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.build.backportedfixes; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.android.build.backportedfixes.common.ClosableCollection; +import com.android.build.backportedfixes.common.Parser; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.Parameter; +import com.beust.jcommander.converters.FileConverter; +import com.google.common.io.Files; + +import java.io.File; +import java.io.PrintWriter; +import java.io.Writer; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public final class Main { + @Parameter(description = "BackportedFix proto binary files", converter = FileConverter.class, + required = true) + List<File> fixFiles; + @Parameter(description = "The file to write the property value to.", + names = {"--property_file", "-p"}, converter = FileConverter.class, required = true) + File propertyFile; + + public static void main(String... argv) throws Exception { + Main main = new Main(); + JCommander.newBuilder().addObject(main).build().parse(argv); + main.run(); + } + + Main() { + } + + private void run() throws Exception { + try (var fixStreams = ClosableCollection.wrap(Parser.getFileInputStreams(fixFiles)); + var out = Files.newWriter(propertyFile, UTF_8)) { + var fixes = Parser.parseBackportedFixes(fixStreams.getCollection()); + writeFixesAsAliasBitSet(fixes, out); + } + } + + static void writeFixesAsAliasBitSet(BackportedFixes fixes, Writer out) { + PrintWriter printWriter = new PrintWriter(out); + printWriter.println("# The following backported fixes have been applied"); + for (var f : fixes.getFixesList()) { + printWriter.printf("# https://issuetracker.google.com/issues/%d with alias %d", + f.getKnownIssue(), f.getAlias()); + printWriter.println(); + } + var bsArray = Parser.getBitSetArray( + fixes.getFixesList().stream().mapToInt(BackportedFix::getAlias).toArray()); + String bsString = Arrays.stream(bsArray).mapToObj(Long::toString).collect( + Collectors.joining(",")); + printWriter.printf("ro.build.backported_fixes.alias_bitset.long_list=%s", bsString); + printWriter.println(); + if (printWriter.checkError()) { + throw new RuntimeException("There was an error writing to " + out.toString()); + } + } +} diff --git a/backported_fixes/src/java/com/android/build/backportedfixes/common/ClosableCollection.java b/backported_fixes/src/java/com/android/build/backportedfixes/common/ClosableCollection.java new file mode 100644 index 0000000000..75b6730c88 --- /dev/null +++ b/backported_fixes/src/java/com/android/build/backportedfixes/common/ClosableCollection.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.build.backportedfixes.common; + +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.Collection; + +/** An AutoCloseable holder for a collection of AutoCloseables. */ +public final class ClosableCollection<T extends AutoCloseable, C extends Collection<T>> implements + AutoCloseable { + C source; + + /** Makes the collection AutoCloseable. */ + public static <T extends AutoCloseable, C extends Collection<T>> ClosableCollection<T, C> wrap( + C source) { + return new ClosableCollection<>(source); + } + + private ClosableCollection(C source) { + this.source = source; + } + + /** Get the source collection. */ + public C getCollection() { + return source; + } + + /** + * Closes each item in the collection. + * + * @throws Exception if any close throws an an exception, a new exception is thrown with + * all the exceptions thrown closing the streams added as a suppressed + * exceptions. + */ + @Override + public void close() throws Exception { + var failures = new ArrayList<Exception>(); + for (T t : source) { + try { + t.close(); + } catch (Exception e) { + failures.add(e); + } + } + if (!failures.isEmpty()) { + Exception e = new Exception( + "%d of %d failed while closing".formatted(failures.size(), source.size())); + failures.forEach(e::addSuppressed); + throw e; + } + } +} diff --git a/backported_fixes/src/java/com/android/build/backportedfixes/common/Parser.java b/backported_fixes/src/java/com/android/build/backportedfixes/common/Parser.java new file mode 100644 index 0000000000..6b08b8f3b3 --- /dev/null +++ b/backported_fixes/src/java/com/android/build/backportedfixes/common/Parser.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.build.backportedfixes.common; + +import com.android.build.backportedfixes.BackportedFix; +import com.android.build.backportedfixes.BackportedFixes; + +import com.google.common.collect.ImmutableList; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.BitSet; +import java.util.List; + + +/** Static utilities for working with {@link BackportedFixes}. */ +public final class Parser { + + /** Creates list of FileInputStreams for a list of files. */ + public static ImmutableList<FileInputStream> getFileInputStreams(List<File> fixFiles) throws + FileNotFoundException { + var streams = ImmutableList.<FileInputStream>builder(); + for (var f : fixFiles) { + streams.add(new FileInputStream(f)); + } + return streams.build(); + } + + /** Converts a list of backported fix aliases into a long array representing a {@link BitSet} */ + public static long[] getBitSetArray(int[] aliases) { + BitSet bs = new BitSet(); + for (int a : aliases) { + bs.set(a); + } + return bs.toLongArray(); + } + + /** + * Creates a {@link BackportedFixes} from a list of {@link BackportedFix} binary proto streams. + */ + public static BackportedFixes parseBackportedFixes(List<? extends InputStream> fixStreams) + throws + IOException { + var fixes = BackportedFixes.newBuilder(); + for (var s : fixStreams) { + BackportedFix fix = BackportedFix.parseFrom(s); + fixes.addFixes(fix); + s.close(); + } + return fixes.build(); + } + + private Parser() { + } +} |