Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (C) 2022 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 | """A script to generate Java files and CPP header files based on annotations in VehicleProperty.aidl |
| 18 | |
| 19 | Need ANDROID_BUILD_TOP environmental variable to be set. This script will update |
| 20 | ChangeModeForVehicleProperty.h and AccessForVehicleProperty.h under generated_lib/cpp and |
| 21 | ChangeModeForVehicleProperty.java and AccessForVehicleProperty.java under generated_lib/java. |
| 22 | |
| 23 | Usage: |
| 24 | $ python generate_annotation_enums.py |
| 25 | """ |
| 26 | import os |
| 27 | import re |
| 28 | import sys |
| 29 | |
shrikar | f139105 | 2023-01-27 18:47:14 +0000 | [diff] [blame] | 30 | PROP_AIDL_FILE_PATH = ("hardware/interfaces/automotive/vehicle/aidl_property/android/hardware/" + |
| 31 | "automotive/vehicle/VehicleProperty.aidl") |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 32 | CHANGE_MODE_CPP_FILE_PATH = ("hardware/interfaces/automotive/vehicle/aidl/generated_lib/cpp/" + |
| 33 | "ChangeModeForVehicleProperty.h") |
| 34 | ACCESS_CPP_FILE_PATH = ("hardware/interfaces/automotive/vehicle/aidl/generated_lib/cpp/" + |
| 35 | "AccessForVehicleProperty.h") |
| 36 | CHANGE_MODE_JAVA_FILE_PATH = ("hardware/interfaces/automotive/vehicle/aidl/generated_lib/java/" + |
| 37 | "ChangeModeForVehicleProperty.java") |
| 38 | ACCESS_JAVA_FILE_PATH = ("hardware/interfaces/automotive/vehicle/aidl/generated_lib/java/" + |
| 39 | "AccessForVehicleProperty.java") |
| 40 | |
| 41 | TAB = " " |
| 42 | RE_ENUM_START = re.compile("\s*enum VehicleProperty \{") |
| 43 | RE_ENUM_END = re.compile("\s*\}\;") |
Yu Shan | 7b7f666 | 2022-07-14 00:32:15 +0000 | [diff] [blame] | 44 | RE_COMMENT_BEGIN = re.compile("\s*\/\*\*?") |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 45 | RE_COMMENT_END = re.compile("\s*\*\/") |
| 46 | RE_CHANGE_MODE = re.compile("\s*\* @change_mode (\S+)\s*") |
| 47 | RE_ACCESS = re.compile("\s*\* @access (\S+)\s*") |
| 48 | RE_VALUE = re.compile("\s*(\w+)\s*=(.*)") |
| 49 | |
| 50 | LICENSE = """/* |
| 51 | * Copyright (C) 2022 The Android Open Source Project |
| 52 | * |
| 53 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 54 | * you may not use this file except in compliance with the License. |
| 55 | * You may obtain a copy of the License at |
| 56 | * |
| 57 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 58 | * |
| 59 | * Unless required by applicable law or agreed to in writing, software |
| 60 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 61 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 62 | * See the License for the specific language governing permissions and |
| 63 | * limitations under the License. |
| 64 | */ |
| 65 | |
| 66 | /** |
| 67 | * DO NOT EDIT MANUALLY!!! |
| 68 | * |
| 69 | * Generated by tools/generate_annotation_enums.py. |
| 70 | */ |
| 71 | |
| 72 | """ |
| 73 | |
| 74 | CHANGE_MODE_CPP_HEADER = """#ifndef android_hardware_automotive_vehicle_aidl_generated_lib_ChangeModeForVehicleProperty_H_ |
| 75 | #define android_hardware_automotive_vehicle_aidl_generated_lib_ChangeModeForVehicleProperty_H_ |
| 76 | |
| 77 | #include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h> |
| 78 | #include <aidl/android/hardware/automotive/vehicle/VehiclePropertyChangeMode.h> |
| 79 | |
| 80 | #include <unordered_map> |
| 81 | |
| 82 | namespace aidl { |
| 83 | namespace android { |
| 84 | namespace hardware { |
| 85 | namespace automotive { |
| 86 | namespace vehicle { |
| 87 | |
| 88 | std::unordered_map<VehicleProperty, VehiclePropertyChangeMode> ChangeModeForVehicleProperty = { |
| 89 | """ |
| 90 | |
| 91 | CHANGE_MODE_CPP_FOOTER = """ |
| 92 | }; |
| 93 | |
| 94 | } // namespace vehicle |
| 95 | } // namespace automotive |
| 96 | } // namespace hardware |
| 97 | } // namespace android |
| 98 | } // aidl |
| 99 | |
| 100 | #endif // android_hardware_automotive_vehicle_aidl_generated_lib_ChangeModeForVehicleProperty_H_ |
| 101 | """ |
| 102 | |
| 103 | ACCESS_CPP_HEADER = """#ifndef android_hardware_automotive_vehicle_aidl_generated_lib_AccessForVehicleProperty_H_ |
| 104 | #define android_hardware_automotive_vehicle_aidl_generated_lib_AccessForVehicleProperty_H_ |
| 105 | |
| 106 | #include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h> |
| 107 | #include <aidl/android/hardware/automotive/vehicle/VehiclePropertyAccess.h> |
| 108 | |
| 109 | #include <unordered_map> |
| 110 | |
| 111 | namespace aidl { |
| 112 | namespace android { |
| 113 | namespace hardware { |
| 114 | namespace automotive { |
| 115 | namespace vehicle { |
| 116 | |
| 117 | std::unordered_map<VehicleProperty, VehiclePropertyAccess> AccessForVehicleProperty = { |
| 118 | """ |
| 119 | |
| 120 | ACCESS_CPP_FOOTER = """ |
| 121 | }; |
| 122 | |
| 123 | } // namespace vehicle |
| 124 | } // namespace automotive |
| 125 | } // namespace hardware |
| 126 | } // namespace android |
| 127 | } // aidl |
| 128 | |
| 129 | #endif // android_hardware_automotive_vehicle_aidl_generated_lib_AccessForVehicleProperty_H_ |
| 130 | """ |
| 131 | |
| 132 | CHANGE_MODE_JAVA_HEADER = """package android.hardware.automotive.vehicle; |
| 133 | |
| 134 | import java.util.Map; |
| 135 | |
| 136 | public final class ChangeModeForVehicleProperty { |
| 137 | |
| 138 | public static final Map<Integer, Integer> values = Map.ofEntries( |
| 139 | """ |
| 140 | |
| 141 | CHANGE_MODE_JAVA_FOOTER = """ |
| 142 | ); |
| 143 | |
| 144 | } |
| 145 | """ |
| 146 | |
| 147 | ACCESS_JAVA_HEADER = """package android.hardware.automotive.vehicle; |
| 148 | |
| 149 | import java.util.Map; |
| 150 | |
| 151 | public final class AccessForVehicleProperty { |
| 152 | |
| 153 | public static final Map<Integer, Integer> values = Map.ofEntries( |
| 154 | """ |
| 155 | |
| 156 | ACCESS_JAVA_FOOTER = """ |
| 157 | ); |
| 158 | |
| 159 | } |
| 160 | """ |
| 161 | |
| 162 | |
| 163 | class Converter: |
| 164 | |
| 165 | def __init__(self, name, annotation_re): |
| 166 | self.name = name |
| 167 | self.annotation_re = annotation_re |
| 168 | |
| 169 | def convert(self, input, output, header, footer, cpp): |
| 170 | processing = False |
| 171 | in_comment = False |
| 172 | content = LICENSE + header |
| 173 | annotation = None |
| 174 | id = 0 |
| 175 | with open(input, 'r') as f: |
| 176 | for line in f.readlines(): |
| 177 | if RE_ENUM_START.match(line): |
| 178 | processing = True |
| 179 | annotation = None |
| 180 | elif RE_ENUM_END.match(line): |
| 181 | processing = False |
| 182 | if not processing: |
| 183 | continue |
| 184 | if RE_COMMENT_BEGIN.match(line): |
| 185 | in_comment = True |
| 186 | if RE_COMMENT_END.match(line): |
| 187 | in_comment = False |
| 188 | if in_comment: |
| 189 | match = self.annotation_re.match(line) |
| 190 | if match: |
| 191 | annotation = match.group(1) |
| 192 | else: |
| 193 | match = RE_VALUE.match(line) |
| 194 | if match: |
| 195 | prop_name = match.group(1) |
| 196 | if prop_name == "INVALID": |
| 197 | continue |
| 198 | if not annotation: |
| 199 | print("No @" + self.name + " annotation for property: " + prop_name) |
| 200 | sys.exit(1) |
| 201 | if id != 0: |
| 202 | content += "\n" |
| 203 | if cpp: |
| 204 | annotation = annotation.replace(".", "::") |
| 205 | content += (TAB + TAB + "{VehicleProperty::" + prop_name + ", " + |
| 206 | annotation + "},") |
| 207 | else: |
| 208 | content += (TAB + TAB + "Map.entry(VehicleProperty." + prop_name + ", " + |
| 209 | annotation + "),") |
| 210 | id += 1 |
| 211 | |
| 212 | # Remove the additional "," at the end for the Java file. |
| 213 | if not cpp: |
| 214 | content = content[:-1] |
| 215 | |
| 216 | content += footer |
| 217 | |
| 218 | with open(output, 'w') as f: |
| 219 | f.write(content) |
| 220 | |
| 221 | |
| 222 | def main(): |
| 223 | android_top = os.environ['ANDROID_BUILD_TOP'] |
| 224 | if not android_top: |
| 225 | print("ANDROID_BUILD_TOP is not in envorinmental variable, please run source and lunch " + |
| 226 | "at the android root") |
| 227 | |
| 228 | aidl_file = os.path.join(android_top, PROP_AIDL_FILE_PATH) |
| 229 | change_mode_cpp_output = os.path.join(android_top, CHANGE_MODE_CPP_FILE_PATH); |
| 230 | access_cpp_output = os.path.join(android_top, ACCESS_CPP_FILE_PATH); |
| 231 | change_mode_java_output = os.path.join(android_top, CHANGE_MODE_JAVA_FILE_PATH); |
| 232 | access_java_output = os.path.join(android_top, ACCESS_JAVA_FILE_PATH); |
| 233 | |
| 234 | c = Converter("change_mode", RE_CHANGE_MODE); |
| 235 | c.convert(aidl_file, change_mode_cpp_output, CHANGE_MODE_CPP_HEADER, CHANGE_MODE_CPP_FOOTER, True) |
| 236 | c.convert(aidl_file, change_mode_java_output, CHANGE_MODE_JAVA_HEADER, CHANGE_MODE_JAVA_FOOTER, False) |
| 237 | c = Converter("access", RE_ACCESS) |
| 238 | c.convert(aidl_file, access_cpp_output, ACCESS_CPP_HEADER, ACCESS_CPP_FOOTER, True) |
| 239 | c.convert(aidl_file, access_java_output, ACCESS_JAVA_HEADER, ACCESS_JAVA_FOOTER, False) |
| 240 | |
| 241 | |
| 242 | if __name__ == "__main__": |
| 243 | main() |