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