Keith Mok | 57baaaf | 2023-06-13 22:33:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 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 |
Tyler Trephan | c8a50de | 2024-01-19 22:50:03 +0000 | [diff] [blame] | 21 | ChangeModeForVehicleProperty.java, AccessForVehicleProperty.java, EnumForVehicleProperty.java |
| 22 | UnitsForVehicleProperty.java under generated_lib/java. |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 23 | |
| 24 | Usage: |
| 25 | $ python generate_annotation_enums.py |
| 26 | """ |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 27 | import argparse |
| 28 | import filecmp |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 29 | import os |
| 30 | import re |
| 31 | import sys |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 32 | import tempfile |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 33 | |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 34 | PROP_AIDL_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl_property/android/hardware/' + |
| 35 | 'automotive/vehicle/VehicleProperty.aidl') |
| 36 | CHANGE_MODE_CPP_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/cpp/' + |
| 37 | 'ChangeModeForVehicleProperty.h') |
| 38 | ACCESS_CPP_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/cpp/' + |
| 39 | 'AccessForVehicleProperty.h') |
| 40 | CHANGE_MODE_JAVA_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/java/' + |
| 41 | 'ChangeModeForVehicleProperty.java') |
| 42 | ACCESS_JAVA_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/java/' + |
| 43 | 'AccessForVehicleProperty.java') |
Tyler Trephan | a1828f9 | 2023-10-06 02:39:13 +0000 | [diff] [blame] | 44 | ENUM_JAVA_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/java/' + |
| 45 | 'EnumForVehicleProperty.java') |
Tyler Trephan | c8a50de | 2024-01-19 22:50:03 +0000 | [diff] [blame] | 46 | UNITS_JAVA_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/java/' + |
| 47 | 'UnitsForVehicleProperty.java') |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 48 | VERSION_CPP_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/cpp/' + |
| 49 | 'VersionForVehicleProperty.h') |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 50 | SCRIPT_PATH = 'hardware/interfaces/automotive/vehicle/tools/generate_annotation_enums.py' |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 51 | |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 52 | TAB = ' ' |
| 53 | RE_ENUM_START = re.compile('\s*enum VehicleProperty \{') |
| 54 | RE_ENUM_END = re.compile('\s*\}\;') |
| 55 | RE_COMMENT_BEGIN = re.compile('\s*\/\*\*?') |
| 56 | RE_COMMENT_END = re.compile('\s*\*\/') |
| 57 | RE_CHANGE_MODE = re.compile('\s*\* @change_mode (\S+)\s*') |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 58 | RE_VERSION = re.compile('\s*\* @version (\S+)\s*') |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 59 | RE_ACCESS = re.compile('\s*\* @access (\S+)\s*') |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 60 | RE_DATA_ENUM = re.compile('\s*\* @data_enum (\S+)\s*') |
| 61 | RE_UNIT = re.compile('\s*\* @unit (\S+)\s+') |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 62 | RE_VALUE = re.compile('\s*(\w+)\s*=(.*)') |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 63 | |
| 64 | LICENSE = """/* |
Tyler Trephan | a1828f9 | 2023-10-06 02:39:13 +0000 | [diff] [blame] | 65 | * Copyright (C) 2023 The Android Open Source Project |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 66 | * |
| 67 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 68 | * you may not use this file except in compliance with the License. |
| 69 | * You may obtain a copy of the License at |
| 70 | * |
| 71 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 72 | * |
| 73 | * Unless required by applicable law or agreed to in writing, software |
| 74 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 75 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 76 | * See the License for the specific language governing permissions and |
| 77 | * limitations under the License. |
| 78 | */ |
| 79 | |
| 80 | /** |
| 81 | * DO NOT EDIT MANUALLY!!! |
| 82 | * |
| 83 | * Generated by tools/generate_annotation_enums.py. |
| 84 | */ |
| 85 | |
Aaqib Ismail | 2e8915d | 2023-01-30 13:04:05 -0800 | [diff] [blame] | 86 | // clang-format off |
| 87 | |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 88 | """ |
| 89 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 90 | CHANGE_MODE_CPP_HEADER = """#pragma once |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 91 | |
| 92 | #include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h> |
| 93 | #include <aidl/android/hardware/automotive/vehicle/VehiclePropertyChangeMode.h> |
| 94 | |
| 95 | #include <unordered_map> |
| 96 | |
| 97 | namespace aidl { |
| 98 | namespace android { |
| 99 | namespace hardware { |
| 100 | namespace automotive { |
| 101 | namespace vehicle { |
| 102 | |
| 103 | std::unordered_map<VehicleProperty, VehiclePropertyChangeMode> ChangeModeForVehicleProperty = { |
| 104 | """ |
| 105 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 106 | CPP_FOOTER = """ |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace vehicle |
| 110 | } // namespace automotive |
| 111 | } // namespace hardware |
| 112 | } // namespace android |
| 113 | } // aidl |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 114 | """ |
| 115 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 116 | ACCESS_CPP_HEADER = """#pragma once |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 117 | |
| 118 | #include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h> |
| 119 | #include <aidl/android/hardware/automotive/vehicle/VehiclePropertyAccess.h> |
| 120 | |
| 121 | #include <unordered_map> |
| 122 | |
| 123 | namespace aidl { |
| 124 | namespace android { |
| 125 | namespace hardware { |
| 126 | namespace automotive { |
| 127 | namespace vehicle { |
| 128 | |
| 129 | std::unordered_map<VehicleProperty, VehiclePropertyAccess> AccessForVehicleProperty = { |
| 130 | """ |
| 131 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 132 | VERSION_CPP_HEADER = """#pragma once |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 133 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 134 | #include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h> |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 135 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 136 | #include <unordered_map> |
| 137 | |
| 138 | namespace aidl { |
| 139 | namespace android { |
| 140 | namespace hardware { |
| 141 | namespace automotive { |
| 142 | namespace vehicle { |
| 143 | |
| 144 | std::unordered_map<VehicleProperty, int32_t> VersionForVehicleProperty = { |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 145 | """ |
| 146 | |
| 147 | CHANGE_MODE_JAVA_HEADER = """package android.hardware.automotive.vehicle; |
| 148 | |
| 149 | import java.util.Map; |
| 150 | |
| 151 | public final class ChangeModeForVehicleProperty { |
| 152 | |
| 153 | public static final Map<Integer, Integer> values = Map.ofEntries( |
| 154 | """ |
| 155 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 156 | JAVA_FOOTER = """ |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 157 | ); |
| 158 | |
| 159 | } |
| 160 | """ |
| 161 | |
| 162 | ACCESS_JAVA_HEADER = """package android.hardware.automotive.vehicle; |
| 163 | |
| 164 | import java.util.Map; |
| 165 | |
| 166 | public final class AccessForVehicleProperty { |
| 167 | |
| 168 | public static final Map<Integer, Integer> values = Map.ofEntries( |
| 169 | """ |
| 170 | |
Tyler Trephan | a1828f9 | 2023-10-06 02:39:13 +0000 | [diff] [blame] | 171 | ENUM_JAVA_HEADER = """package android.hardware.automotive.vehicle; |
| 172 | |
| 173 | import java.util.List; |
| 174 | import java.util.Map; |
| 175 | |
| 176 | public final class EnumForVehicleProperty { |
| 177 | |
| 178 | public static final Map<Integer, List<Class<?>>> values = Map.ofEntries( |
| 179 | """ |
| 180 | |
Tyler Trephan | c8a50de | 2024-01-19 22:50:03 +0000 | [diff] [blame] | 181 | UNITS_JAVA_HEADER = """package android.hardware.automotive.vehicle; |
| 182 | |
| 183 | import java.util.Map; |
| 184 | |
| 185 | public final class UnitsForVehicleProperty { |
| 186 | |
| 187 | public static final Map<Integer, Integer> values = Map.ofEntries( |
| 188 | """ |
| 189 | |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 190 | |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 191 | class PropertyConfig: |
| 192 | """Represents one VHAL property definition in VehicleProperty.aidl.""" |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 193 | |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 194 | def __init__(self): |
| 195 | self.name = None |
| 196 | self.description = None |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 197 | self.comment = None |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 198 | self.change_mode = None |
| 199 | self.access_modes = [] |
| 200 | self.enum_types = [] |
| 201 | self.unit_type = None |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 202 | self.version = None |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 203 | |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 204 | def __repr__(self): |
| 205 | return self.__str__() |
| 206 | |
| 207 | def __str__(self): |
| 208 | return ('PropertyConfig{{' + |
| 209 | 'name: {}, description: {}, change_mode: {}, access_modes: {}, enum_types: {}' + |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 210 | ', unit_type: {}, version: {}, comment: {}}}').format(self.name, self.description, |
| 211 | self.change_mode, self.access_modes, self.enum_types, self.unit_type, |
| 212 | self.version, self.comment) |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 213 | |
| 214 | |
| 215 | class FileParser: |
| 216 | |
| 217 | def __init__(self): |
| 218 | self.configs = None |
| 219 | |
| 220 | def parseFile(self, input_file): |
| 221 | """Parses the input VehicleProperty.aidl file into a list of property configs.""" |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 222 | processing = False |
| 223 | in_comment = False |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 224 | configs = [] |
| 225 | config = None |
| 226 | with open(input_file, 'r') as f: |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 227 | for line in f.readlines(): |
| 228 | if RE_ENUM_START.match(line): |
| 229 | processing = True |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 230 | elif RE_ENUM_END.match(line): |
| 231 | processing = False |
| 232 | if not processing: |
| 233 | continue |
| 234 | if RE_COMMENT_BEGIN.match(line): |
| 235 | in_comment = True |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 236 | config = PropertyConfig() |
| 237 | description = '' |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 238 | continue |
| 239 | |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 240 | if RE_COMMENT_END.match(line): |
| 241 | in_comment = False |
| 242 | if in_comment: |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 243 | match = RE_CHANGE_MODE.match(line) |
| 244 | if match: |
| 245 | config.change_mode = match.group(1).replace('VehiclePropertyChangeMode.', '') |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 246 | continue |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 247 | match = RE_ACCESS.match(line) |
| 248 | if match: |
| 249 | config.access_modes.append(match.group(1).replace('VehiclePropertyAccess.', '')) |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 250 | continue |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 251 | match = RE_UNIT.match(line) |
| 252 | if match: |
| 253 | config.unit_type = match.group(1) |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 254 | continue |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 255 | match = RE_DATA_ENUM.match(line) |
| 256 | if match: |
| 257 | config.enum_types.append(match.group(1)) |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 258 | continue |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 259 | match = RE_VERSION.match(line) |
| 260 | if match: |
| 261 | if config.version != None: |
| 262 | raise Exception('Duplicate version annotation for property: ' + prop_name) |
| 263 | config.version = match.group(1) |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 264 | continue |
| 265 | |
| 266 | sline = line.strip() |
| 267 | if sline.startswith('*'): |
| 268 | # Remove the '*'. |
| 269 | sline = sline[1:].strip() |
| 270 | |
| 271 | if not config.description: |
| 272 | # We reach an empty line of comment, the description part is ending. |
| 273 | if sline == '': |
| 274 | config.description = description |
| 275 | else: |
| 276 | if description != '': |
| 277 | description += ' ' |
| 278 | description += sline |
| 279 | else: |
| 280 | if not config.comment: |
| 281 | if sline != '': |
| 282 | # This is the first line for comment. |
| 283 | config.comment = sline |
| 284 | else: |
| 285 | if sline != '': |
| 286 | # Concat this line with the previous line's comment with a space. |
| 287 | config.comment += ' ' + sline |
| 288 | else: |
| 289 | # Treat empty line comment as a new line. |
| 290 | config.comment += '\n' |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 291 | else: |
| 292 | match = RE_VALUE.match(line) |
| 293 | if match: |
| 294 | prop_name = match.group(1) |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 295 | if prop_name == 'INVALID': |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 296 | continue |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 297 | if not config.change_mode: |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 298 | raise Exception( |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 299 | 'No change_mode annotation for property: ' + prop_name) |
| 300 | if not config.access_modes: |
| 301 | raise Exception( |
| 302 | 'No access_mode annotation for property: ' + prop_name) |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 303 | if not config.version: |
| 304 | raise Exception( |
| 305 | 'no version annotation for property: ' + prop_name) |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 306 | config.name = prop_name |
| 307 | configs.append(config) |
| 308 | |
| 309 | self.configs = configs |
| 310 | |
| 311 | def convert(self, output, header, footer, cpp, field): |
| 312 | """Converts the property config file to C++/Java output file.""" |
| 313 | counter = 0 |
| 314 | content = LICENSE + header |
| 315 | for config in self.configs: |
| 316 | if field == 'change_mode': |
| 317 | if cpp: |
| 318 | annotation = "VehiclePropertyChangeMode::" + config.change_mode |
| 319 | else: |
| 320 | annotation = "VehiclePropertyChangeMode." + config.change_mode |
| 321 | elif field == 'access_mode': |
| 322 | if cpp: |
| 323 | annotation = "VehiclePropertyAccess::" + config.access_modes[0] |
| 324 | else: |
| 325 | annotation = "VehiclePropertyAccess." + config.access_modes[0] |
Tyler Trephan | a1828f9 | 2023-10-06 02:39:13 +0000 | [diff] [blame] | 326 | elif field == 'enum_types': |
| 327 | if len(config.enum_types) < 1: |
| 328 | continue; |
| 329 | if not cpp: |
| 330 | annotation = "List.of(" + ', '.join([class_name + ".class" for class_name in config.enum_types]) + ")" |
Tyler Trephan | c8a50de | 2024-01-19 22:50:03 +0000 | [diff] [blame] | 331 | elif field == 'unit_type': |
| 332 | if not config.unit_type: |
| 333 | continue |
| 334 | if not cpp: |
| 335 | annotation = config.unit_type |
| 336 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 337 | elif field == 'version': |
| 338 | if cpp: |
| 339 | annotation = config.version |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 340 | else: |
| 341 | raise Exception('Unknown field: ' + field) |
| 342 | if counter != 0: |
| 343 | content += '\n' |
| 344 | if cpp: |
| 345 | content += (TAB + TAB + '{VehicleProperty::' + config.name + ', ' + |
| 346 | annotation + '},') |
| 347 | else: |
| 348 | content += (TAB + TAB + 'Map.entry(VehicleProperty.' + config.name + ', ' + |
| 349 | annotation + '),') |
| 350 | counter += 1 |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 351 | |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 352 | # Remove the additional ',' at the end for the Java file. |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 353 | if not cpp: |
| 354 | content = content[:-1] |
| 355 | |
| 356 | content += footer |
| 357 | |
| 358 | with open(output, 'w') as f: |
| 359 | f.write(content) |
| 360 | |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 361 | def outputAsCsv(self, output): |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 362 | content = 'name,description,change mode,access mode,enum type,unit type,comment\n' |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 363 | for config in self.configs: |
| 364 | enum_types = None |
| 365 | if not config.enum_types: |
| 366 | enum_types = '/' |
| 367 | else: |
| 368 | enum_types = '/'.join(config.enum_types) |
| 369 | unit_type = config.unit_type |
| 370 | if not unit_type: |
| 371 | unit_type = '/' |
| 372 | access_modes = '' |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 373 | comment = config.comment |
| 374 | if not comment: |
| 375 | comment = '' |
| 376 | content += '"{}","{}","{}","{}","{}","{}", "{}"\n'.format( |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 377 | config.name, |
| 378 | # Need to escape quote as double quote. |
| 379 | config.description.replace('"', '""'), |
| 380 | config.change_mode, |
| 381 | '/'.join(config.access_modes), |
| 382 | enum_types, |
Yu Shan | 0ebf5bb | 2023-12-18 15:42:43 -0800 | [diff] [blame] | 383 | unit_type, |
| 384 | comment.replace('"', '""')) |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 385 | |
| 386 | with open(output, 'w+') as f: |
| 387 | f.write(content) |
| 388 | |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 389 | |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 390 | def createTempFile(): |
| 391 | f = tempfile.NamedTemporaryFile(delete=False); |
| 392 | f.close(); |
| 393 | return f.name |
| 394 | |
| 395 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 396 | class GeneratedFile: |
| 397 | |
| 398 | def __init__(self, type): |
| 399 | self.type = type |
| 400 | self.cpp_file_path = None |
| 401 | self.java_file_path = None |
| 402 | self.cpp_header = None |
| 403 | self.java_header = None |
| 404 | self.cpp_footer = None |
| 405 | self.java_footer = None |
| 406 | self.cpp_output_file = None |
| 407 | self.java_output_file = None |
| 408 | |
| 409 | def setCppFilePath(self, cpp_file_path): |
| 410 | self.cpp_file_path = cpp_file_path |
| 411 | |
| 412 | def setJavaFilePath(self, java_file_path): |
| 413 | self.java_file_path = java_file_path |
| 414 | |
| 415 | def setCppHeader(self, cpp_header): |
| 416 | self.cpp_header = cpp_header |
| 417 | |
| 418 | def setCppFooter(self, cpp_footer): |
| 419 | self.cpp_footer = cpp_footer |
| 420 | |
| 421 | def setJavaHeader(self, java_header): |
| 422 | self.java_header = java_header |
| 423 | |
| 424 | def setJavaFooter(self, java_footer): |
| 425 | self.java_footer = java_footer |
| 426 | |
| 427 | def convert(self, file_parser, check_only, temp_files): |
| 428 | if self.cpp_file_path: |
| 429 | output_file = GeneratedFile._getOutputFile(self.cpp_file_path, check_only, temp_files) |
| 430 | file_parser.convert(output_file, self.cpp_header, self.cpp_footer, True, self.type) |
| 431 | self.cpp_output_file = output_file |
| 432 | |
| 433 | if self.java_file_path: |
| 434 | output_file = GeneratedFile._getOutputFile(self.java_file_path, check_only, temp_files) |
| 435 | file_parser.convert(output_file, self.java_header, self.java_footer, False, self.type) |
| 436 | self.java_output_file = output_file |
| 437 | |
| 438 | def cmp(self): |
| 439 | if self.cpp_file_path: |
| 440 | if not filecmp.cmp(self.cpp_output_file, self.cpp_file_path): |
| 441 | return False |
| 442 | |
| 443 | if self.java_file_path: |
| 444 | if not filecmp.cmp(self.java_output_file, self.java_file_path): |
| 445 | return False |
| 446 | |
| 447 | return True |
| 448 | |
| 449 | @staticmethod |
| 450 | def _getOutputFile(file_path, check_only, temp_files): |
| 451 | if not check_only: |
| 452 | return file_path |
| 453 | |
| 454 | temp_file = createTempFile() |
| 455 | temp_files.append(temp_file) |
| 456 | return temp_file |
| 457 | |
| 458 | |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 459 | def main(): |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 460 | parser = argparse.ArgumentParser( |
| 461 | description='Generate Java and C++ enums based on annotations in VehicleProperty.aidl') |
| 462 | parser.add_argument('--android_build_top', required=False, help='Path to ANDROID_BUILD_TOP') |
Yu Shan | b1fc8c9 | 2023-08-30 11:51:04 -0700 | [diff] [blame] | 463 | parser.add_argument('--preupload_files', nargs='*', required=False, help='modified files') |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 464 | parser.add_argument('--check_only', required=False, action='store_true', |
| 465 | help='only check whether the generated files need update') |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 466 | parser.add_argument('--output_csv', required=False, |
| 467 | help='Path to the parsing result in CSV style, useful for doc generation') |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 468 | args = parser.parse_args(); |
| 469 | android_top = None |
| 470 | output_folder = None |
| 471 | if args.android_build_top: |
| 472 | android_top = args.android_build_top |
| 473 | vehiclePropertyUpdated = False |
| 474 | for preuload_file in args.preupload_files: |
| 475 | if preuload_file.endswith('VehicleProperty.aidl'): |
| 476 | vehiclePropertyUpdated = True |
| 477 | break |
| 478 | if not vehiclePropertyUpdated: |
| 479 | return |
| 480 | else: |
| 481 | android_top = os.environ['ANDROID_BUILD_TOP'] |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 482 | if not android_top: |
Tyler Trephan | a1828f9 | 2023-10-06 02:39:13 +0000 | [diff] [blame] | 483 | print('ANDROID_BUILD_TOP is not in environmental variable, please run source and lunch ' + |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 484 | 'at the android root') |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 485 | |
| 486 | aidl_file = os.path.join(android_top, PROP_AIDL_FILE_PATH) |
Yu Shan | 8ddd65d | 2023-06-28 17:02:29 -0700 | [diff] [blame] | 487 | f = FileParser(); |
| 488 | f.parseFile(aidl_file) |
| 489 | |
| 490 | if args.output_csv: |
| 491 | f.outputAsCsv(args.output_csv) |
| 492 | return |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 493 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 494 | generated_files = [] |
| 495 | |
| 496 | change_mode = GeneratedFile('change_mode') |
| 497 | change_mode.setCppFilePath(os.path.join(android_top, CHANGE_MODE_CPP_FILE_PATH)) |
| 498 | change_mode.setJavaFilePath(os.path.join(android_top, CHANGE_MODE_JAVA_FILE_PATH)) |
| 499 | change_mode.setCppHeader(CHANGE_MODE_CPP_HEADER) |
| 500 | change_mode.setCppFooter(CPP_FOOTER) |
| 501 | change_mode.setJavaHeader(CHANGE_MODE_JAVA_HEADER) |
| 502 | change_mode.setJavaFooter(JAVA_FOOTER) |
| 503 | generated_files.append(change_mode) |
| 504 | |
| 505 | access_mode = GeneratedFile('access_mode') |
| 506 | access_mode.setCppFilePath(os.path.join(android_top, ACCESS_CPP_FILE_PATH)) |
| 507 | access_mode.setJavaFilePath(os.path.join(android_top, ACCESS_JAVA_FILE_PATH)) |
| 508 | access_mode.setCppHeader(ACCESS_CPP_HEADER) |
| 509 | access_mode.setCppFooter(CPP_FOOTER) |
| 510 | access_mode.setJavaHeader(ACCESS_JAVA_HEADER) |
| 511 | access_mode.setJavaFooter(JAVA_FOOTER) |
| 512 | generated_files.append(access_mode) |
| 513 | |
| 514 | enum_types = GeneratedFile('enum_types') |
| 515 | enum_types.setJavaFilePath(os.path.join(android_top, ENUM_JAVA_FILE_PATH)) |
| 516 | enum_types.setJavaHeader(ENUM_JAVA_HEADER) |
| 517 | enum_types.setJavaFooter(JAVA_FOOTER) |
| 518 | generated_files.append(enum_types) |
| 519 | |
Tyler Trephan | c8a50de | 2024-01-19 22:50:03 +0000 | [diff] [blame] | 520 | unit_type = GeneratedFile('unit_type') |
| 521 | unit_type.setJavaFilePath(os.path.join(android_top, UNITS_JAVA_FILE_PATH)) |
| 522 | unit_type.setJavaHeader(UNITS_JAVA_HEADER) |
| 523 | unit_type.setJavaFooter(JAVA_FOOTER) |
| 524 | generated_files.append(unit_type) |
| 525 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 526 | version = GeneratedFile('version') |
| 527 | version.setCppFilePath(os.path.join(android_top, VERSION_CPP_FILE_PATH)) |
| 528 | version.setCppHeader(VERSION_CPP_HEADER) |
| 529 | version.setCppFooter(CPP_FOOTER) |
| 530 | generated_files.append(version) |
| 531 | |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 532 | temp_files = [] |
| 533 | |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 534 | try: |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 535 | for generated_file in generated_files: |
| 536 | generated_file.convert(f, args.check_only, temp_files) |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 537 | |
| 538 | if not args.check_only: |
| 539 | return |
| 540 | |
Yu Shan | 7881e82 | 2023-12-15 13:12:01 -0800 | [diff] [blame] | 541 | for generated_file in generated_files: |
| 542 | if not generated_file.cmp(): |
| 543 | print('The generated enum files for VehicleProperty.aidl requires update, ') |
| 544 | print('Run \npython ' + android_top + '/' + SCRIPT_PATH) |
| 545 | sys.exit(1) |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 546 | except Exception as e: |
| 547 | print('Error parsing VehicleProperty.aidl') |
| 548 | print(e) |
| 549 | sys.exit(1) |
| 550 | finally: |
| 551 | for file in temp_files: |
| 552 | os.remove(file) |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 553 | |
| 554 | |
Yu Shan | 41dd7f1 | 2023-06-07 13:25:43 -0700 | [diff] [blame] | 555 | if __name__ == '__main__': |
Yu Shan | 63e24d7 | 2022-06-24 17:53:32 +0000 | [diff] [blame] | 556 | main() |