blob: 4ed19d324d983c83ee1e35bfb8698474e1b253d3 [file] [log] [blame]
Keith Mok57baaaf2023-06-13 22:33:10 +00001#!/usr/bin/python3
Yu Shan63e24d72022-06-24 17:53:32 +00002
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 Shan72594ce2024-04-26 14:49:04 -070020 ChangeModeForVehicleProperty.h and AccessForVehicleProperty.h under generated_lib/version/cpp and
Tyler Trephanc8a50de2024-01-19 22:50:03 +000021 ChangeModeForVehicleProperty.java, AccessForVehicleProperty.java, EnumForVehicleProperty.java
Yu Shan72594ce2024-04-26 14:49:04 -070022 UnitsForVehicleProperty.java under generated_lib/version/java.
Yu Shan63e24d72022-06-24 17:53:32 +000023
24 Usage:
25 $ python generate_annotation_enums.py
26"""
Yu Shan41dd7f12023-06-07 13:25:43 -070027import argparse
28import filecmp
Yu Shan63e24d72022-06-24 17:53:32 +000029import os
30import re
31import sys
Yu Shan41dd7f12023-06-07 13:25:43 -070032import tempfile
Yu Shan63e24d72022-06-24 17:53:32 +000033
Yu Shan72594ce2024-04-26 14:49:04 -070034# Keep this updated with the latest in-development property version.
Yu Shanc746fc82024-04-26 15:47:26 -070035PROPERTY_VERSION = '4'
Yu Shan72594ce2024-04-26 14:49:04 -070036
Yu Shan41dd7f12023-06-07 13:25:43 -070037PROP_AIDL_FILE_PATH = ('hardware/interfaces/automotive/vehicle/aidl_property/android/hardware/' +
38 'automotive/vehicle/VehicleProperty.aidl')
Yu Shan72594ce2024-04-26 14:49:04 -070039GENERATED_LIB = ('hardware/interfaces/automotive/vehicle/aidl/generated_lib/' + PROPERTY_VERSION +
40 '/')
41CHANGE_MODE_CPP_FILE_PATH = GENERATED_LIB + '/cpp/ChangeModeForVehicleProperty.h'
42ACCESS_CPP_FILE_PATH = GENERATED_LIB + '/cpp/AccessForVehicleProperty.h'
43CHANGE_MODE_JAVA_FILE_PATH = GENERATED_LIB + '/java/ChangeModeForVehicleProperty.java'
44ACCESS_JAVA_FILE_PATH = GENERATED_LIB + '/java/AccessForVehicleProperty.java'
45ENUM_JAVA_FILE_PATH = GENERATED_LIB + '/java/EnumForVehicleProperty.java'
46UNITS_JAVA_FILE_PATH = GENERATED_LIB + '/java/UnitsForVehicleProperty.java'
47VERSION_CPP_FILE_PATH = GENERATED_LIB + '/cpp/VersionForVehicleProperty.h'
Yu Shan9df12152025-01-14 14:16:40 -080048ANNOTATIONS_CPP_FILE_PATH = GENERATED_LIB + '/cpp/AnnotationsForVehicleProperty.h'
49ANNOTATIONS_JAVA_FILE_PATH = GENERATED_LIB + '/java/AnnotationsForVehicleProperty.java'
Yu Shan41dd7f12023-06-07 13:25:43 -070050SCRIPT_PATH = 'hardware/interfaces/automotive/vehicle/tools/generate_annotation_enums.py'
Yu Shan63e24d72022-06-24 17:53:32 +000051
Yu Shan41dd7f12023-06-07 13:25:43 -070052TAB = ' '
53RE_ENUM_START = re.compile('\s*enum VehicleProperty \{')
54RE_ENUM_END = re.compile('\s*\}\;')
55RE_COMMENT_BEGIN = re.compile('\s*\/\*\*?')
56RE_COMMENT_END = re.compile('\s*\*\/')
57RE_CHANGE_MODE = re.compile('\s*\* @change_mode (\S+)\s*')
Yu Shan7881e822023-12-15 13:12:01 -080058RE_VERSION = re.compile('\s*\* @version (\S+)\s*')
Yu Shan41dd7f12023-06-07 13:25:43 -070059RE_ACCESS = re.compile('\s*\* @access (\S+)\s*')
Yu Shan8ddd65d2023-06-28 17:02:29 -070060RE_DATA_ENUM = re.compile('\s*\* @data_enum (\S+)\s*')
61RE_UNIT = re.compile('\s*\* @unit (\S+)\s+')
Yu Shan41dd7f12023-06-07 13:25:43 -070062RE_VALUE = re.compile('\s*(\w+)\s*=(.*)')
Yu Shan70447692025-01-08 16:56:21 -080063RE_ANNOTATION = re.compile('\s*\* @(\S+)\s*')
64
65SUPPORTED_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.
71ENUM_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 Shan63e24d72022-06-24 17:53:32 +000085
86LICENSE = """/*
Tyler Trephana1828f92023-10-06 02:39:13 +000087 * Copyright (C) 2023 The Android Open Source Project
Yu Shan63e24d72022-06-24 17:53:32 +000088 *
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 Ismail2e8915d2023-01-30 13:04:05 -0800108// clang-format off
109
Yu Shan63e24d72022-06-24 17:53:32 +0000110"""
111
Yu Shan7881e822023-12-15 13:12:01 -0800112CHANGE_MODE_CPP_HEADER = """#pragma once
Yu Shan63e24d72022-06-24 17:53:32 +0000113
114#include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h>
115#include <aidl/android/hardware/automotive/vehicle/VehiclePropertyChangeMode.h>
116
117#include <unordered_map>
118
119namespace aidl {
120namespace android {
121namespace hardware {
122namespace automotive {
123namespace vehicle {
124
125std::unordered_map<VehicleProperty, VehiclePropertyChangeMode> ChangeModeForVehicleProperty = {
126"""
127
Yu Shan7881e822023-12-15 13:12:01 -0800128CPP_FOOTER = """
Yu Shan63e24d72022-06-24 17:53:32 +0000129};
130
131} // namespace vehicle
132} // namespace automotive
133} // namespace hardware
134} // namespace android
135} // aidl
Yu Shan63e24d72022-06-24 17:53:32 +0000136"""
137
Yu Shan7881e822023-12-15 13:12:01 -0800138ACCESS_CPP_HEADER = """#pragma once
Yu Shan63e24d72022-06-24 17:53:32 +0000139
140#include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h>
141#include <aidl/android/hardware/automotive/vehicle/VehiclePropertyAccess.h>
142
143#include <unordered_map>
144
145namespace aidl {
146namespace android {
147namespace hardware {
148namespace automotive {
149namespace vehicle {
150
151std::unordered_map<VehicleProperty, VehiclePropertyAccess> AccessForVehicleProperty = {
152"""
153
Yu Shan7881e822023-12-15 13:12:01 -0800154VERSION_CPP_HEADER = """#pragma once
Yu Shan63e24d72022-06-24 17:53:32 +0000155
Yu Shan7881e822023-12-15 13:12:01 -0800156#include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h>
Yu Shan63e24d72022-06-24 17:53:32 +0000157
Yu Shan7881e822023-12-15 13:12:01 -0800158#include <unordered_map>
159
160namespace aidl {
161namespace android {
162namespace hardware {
163namespace automotive {
164namespace vehicle {
165
166std::unordered_map<VehicleProperty, int32_t> VersionForVehicleProperty = {
Yu Shan63e24d72022-06-24 17:53:32 +0000167"""
168
Yu Shan9df12152025-01-14 14:16:40 -0800169ANNOTATIONS_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
177namespace aidl {
178namespace android {
179namespace hardware {
180namespace automotive {
181namespace vehicle {
182
183std::unordered_map<VehicleProperty, std::unordered_set<std::string>>
184 AnnotationsForVehicleProperty = {
185"""
186
Yu Shan63e24d72022-06-24 17:53:32 +0000187CHANGE_MODE_JAVA_HEADER = """package android.hardware.automotive.vehicle;
188
189import java.util.Map;
190
191public final class ChangeModeForVehicleProperty {
192
193 public static final Map<Integer, Integer> values = Map.ofEntries(
194"""
195
Yu Shan7881e822023-12-15 13:12:01 -0800196JAVA_FOOTER = """
Yu Shan63e24d72022-06-24 17:53:32 +0000197 );
198
199}
200"""
201
202ACCESS_JAVA_HEADER = """package android.hardware.automotive.vehicle;
203
204import java.util.Map;
205
206public final class AccessForVehicleProperty {
207
208 public static final Map<Integer, Integer> values = Map.ofEntries(
209"""
210
Tyler Trephana1828f92023-10-06 02:39:13 +0000211ENUM_JAVA_HEADER = """package android.hardware.automotive.vehicle;
212
213import java.util.List;
214import java.util.Map;
215
216public final class EnumForVehicleProperty {
217
218 public static final Map<Integer, List<Class<?>>> values = Map.ofEntries(
219"""
220
Tyler Trephanc8a50de2024-01-19 22:50:03 +0000221UNITS_JAVA_HEADER = """package android.hardware.automotive.vehicle;
222
223import java.util.Map;
224
225public final class UnitsForVehicleProperty {
226
227 public static final Map<Integer, Integer> values = Map.ofEntries(
228"""
229
Yu Shan9df12152025-01-14 14:16:40 -0800230ANNOTATIONS_JAVA_HEADER = """package android.hardware.automotive.vehicle;
231
232import java.util.Set;
233import java.util.Map;
234
235public final class AnnotationsForVehicleProperty {
236
237 public static final Map<Integer, Set<String>> values = Map.ofEntries(
238"""
239
Yu Shan63e24d72022-06-24 17:53:32 +0000240
Yu Shan8ddd65d2023-06-28 17:02:29 -0700241class PropertyConfig:
242 """Represents one VHAL property definition in VehicleProperty.aidl."""
Yu Shan63e24d72022-06-24 17:53:32 +0000243
Yu Shan8ddd65d2023-06-28 17:02:29 -0700244 def __init__(self):
245 self.name = None
246 self.description = None
Yu Shan0ebf5bb2023-12-18 15:42:43 -0800247 self.comment = None
Yu Shan8ddd65d2023-06-28 17:02:29 -0700248 self.change_mode = None
249 self.access_modes = []
250 self.enum_types = []
251 self.unit_type = None
Yu Shan7881e822023-12-15 13:12:01 -0800252 self.version = None
Yu Shan9df12152025-01-14 14:16:40 -0800253 # Use a set to avoid duplicate annotation.
254 self.annotations = set()
Yu Shan63e24d72022-06-24 17:53:32 +0000255
Yu Shan8ddd65d2023-06-28 17:02:29 -0700256 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 Shan0ebf5bb2023-12-18 15:42:43 -0800262 ', 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 Shan8ddd65d2023-06-28 17:02:29 -0700265
266
267class 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 Shan63e24d72022-06-24 17:53:32 +0000274 processing = False
275 in_comment = False
Yu Shan8ddd65d2023-06-28 17:02:29 -0700276 configs = []
277 config = None
278 with open(input_file, 'r') as f:
Yu Shan63e24d72022-06-24 17:53:32 +0000279 for line in f.readlines():
280 if RE_ENUM_START.match(line):
281 processing = True
Yu Shan63e24d72022-06-24 17:53:32 +0000282 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 Shan8ddd65d2023-06-28 17:02:29 -0700288 config = PropertyConfig()
Yu Shan70447692025-01-08 16:56:21 -0800289 # Use an array so that we could modify the string in parseComment.
290 description = ['']
Yu Shan0ebf5bb2023-12-18 15:42:43 -0800291 continue
292
Yu Shan63e24d72022-06-24 17:53:32 +0000293 if RE_COMMENT_END.match(line):
294 in_comment = False
295 if in_comment:
Yu Shan70447692025-01-08 16:56:21 -0800296 # We will update the string in description in this function.
297 self.parseComment(line, config, description)
Yu Shan63e24d72022-06-24 17:53:32 +0000298 else:
299 match = RE_VALUE.match(line)
300 if match:
301 prop_name = match.group(1)
Yu Shan41dd7f12023-06-07 13:25:43 -0700302 if prop_name == 'INVALID':
Yu Shan63e24d72022-06-24 17:53:32 +0000303 continue
Yu Shan8ddd65d2023-06-28 17:02:29 -0700304 if not config.change_mode:
Yu Shan41dd7f12023-06-07 13:25:43 -0700305 raise Exception(
Yu Shan8ddd65d2023-06-28 17:02:29 -0700306 '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 Shan7881e822023-12-15 13:12:01 -0800310 if not config.version:
311 raise Exception(
Yu Shan70447692025-01-08 16:56:21 -0800312 '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 Shan8ddd65d2023-06-28 17:02:29 -0700324 config.name = prop_name
325 configs.append(config)
326
327 self.configs = configs
328
Yu Shan70447692025-01-08 16:56:21 -0800329 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 Shan9df12152025-01-14 14:16:40 -0800335 config.annotations.add(annotation)
Yu Shan70447692025-01-08 16:56:21 -0800336 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 Shan8ddd65d2023-06-28 17:02:29 -0700385 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 Shan9df12152025-01-14 14:16:40 -0800392 value = "VehiclePropertyChangeMode::" + config.change_mode
Yu Shan8ddd65d2023-06-28 17:02:29 -0700393 else:
Yu Shan9df12152025-01-14 14:16:40 -0800394 value = "VehiclePropertyChangeMode." + config.change_mode
Yu Shan8ddd65d2023-06-28 17:02:29 -0700395 elif field == 'access_mode':
396 if cpp:
Yu Shan9df12152025-01-14 14:16:40 -0800397 value = "VehiclePropertyAccess::" + config.access_modes[0]
Yu Shan8ddd65d2023-06-28 17:02:29 -0700398 else:
Yu Shan9df12152025-01-14 14:16:40 -0800399 value = "VehiclePropertyAccess." + config.access_modes[0]
Tyler Trephana1828f92023-10-06 02:39:13 +0000400 elif field == 'enum_types':
401 if len(config.enum_types) < 1:
Yu Shan9df12152025-01-14 14:16:40 -0800402 continue
Tyler Trephana1828f92023-10-06 02:39:13 +0000403 if not cpp:
Yu Shan9df12152025-01-14 14:16:40 -0800404 value = "List.of(" + ', '.join([class_name + ".class" for class_name in config.enum_types]) + ")"
Tyler Trephanc8a50de2024-01-19 22:50:03 +0000405 elif field == 'unit_type':
406 if not config.unit_type:
407 continue
408 if not cpp:
Yu Shan9df12152025-01-14 14:16:40 -0800409 value = config.unit_type
Yu Shan7881e822023-12-15 13:12:01 -0800410 elif field == 'version':
411 if cpp:
Yu Shan9df12152025-01-14 14:16:40 -0800412 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 Shan8ddd65d2023-06-28 17:02:29 -0700421 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 Shan9df12152025-01-14 14:16:40 -0800427 value + '},')
Yu Shan8ddd65d2023-06-28 17:02:29 -0700428 else:
429 content += (TAB + TAB + 'Map.entry(VehicleProperty.' + config.name + ', ' +
Yu Shan9df12152025-01-14 14:16:40 -0800430 value + '),')
Yu Shan8ddd65d2023-06-28 17:02:29 -0700431 counter += 1
Yu Shan63e24d72022-06-24 17:53:32 +0000432
Yu Shan41dd7f12023-06-07 13:25:43 -0700433 # Remove the additional ',' at the end for the Java file.
Yu Shan63e24d72022-06-24 17:53:32 +0000434 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 Shan8ddd65d2023-06-28 17:02:29 -0700442 def outputAsCsv(self, output):
Yu Shan0ebf5bb2023-12-18 15:42:43 -0800443 content = 'name,description,change mode,access mode,enum type,unit type,comment\n'
Yu Shan8ddd65d2023-06-28 17:02:29 -0700444 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 Shan0ebf5bb2023-12-18 15:42:43 -0800454 comment = config.comment
455 if not comment:
456 comment = ''
457 content += '"{}","{}","{}","{}","{}","{}", "{}"\n'.format(
Yu Shan8ddd65d2023-06-28 17:02:29 -0700458 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 Shan0ebf5bb2023-12-18 15:42:43 -0800464 unit_type,
465 comment.replace('"', '""'))
Yu Shan8ddd65d2023-06-28 17:02:29 -0700466
467 with open(output, 'w+') as f:
468 f.write(content)
469
Yu Shan63e24d72022-06-24 17:53:32 +0000470
Yu Shan41dd7f12023-06-07 13:25:43 -0700471def createTempFile():
472 f = tempfile.NamedTemporaryFile(delete=False);
473 f.close();
474 return f.name
475
476
Yu Shan7881e822023-12-15 13:12:01 -0800477class 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 Shan63e24d72022-06-24 17:53:32 +0000540def main():
Yu Shan41dd7f12023-06-07 13:25:43 -0700541 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 Shanb1fc8c92023-08-30 11:51:04 -0700544 parser.add_argument('--preupload_files', nargs='*', required=False, help='modified files')
Yu Shan41dd7f12023-06-07 13:25:43 -0700545 parser.add_argument('--check_only', required=False, action='store_true',
546 help='only check whether the generated files need update')
Yu Shan8ddd65d2023-06-28 17:02:29 -0700547 parser.add_argument('--output_csv', required=False,
548 help='Path to the parsing result in CSV style, useful for doc generation')
Yu Shan41dd7f12023-06-07 13:25:43 -0700549 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 Shan63e24d72022-06-24 17:53:32 +0000563 if not android_top:
Tyler Trephana1828f92023-10-06 02:39:13 +0000564 print('ANDROID_BUILD_TOP is not in environmental variable, please run source and lunch ' +
Yu Shan41dd7f12023-06-07 13:25:43 -0700565 'at the android root')
Yu Shan63e24d72022-06-24 17:53:32 +0000566
567 aidl_file = os.path.join(android_top, PROP_AIDL_FILE_PATH)
Yu Shan8ddd65d2023-06-28 17:02:29 -0700568 f = FileParser();
569 f.parseFile(aidl_file)
570
571 if args.output_csv:
572 f.outputAsCsv(args.output_csv)
573 return
Yu Shan63e24d72022-06-24 17:53:32 +0000574
Yu Shan7881e822023-12-15 13:12:01 -0800575 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 Trephanc8a50de2024-01-19 22:50:03 +0000601 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 Shan7881e822023-12-15 13:12:01 -0800607 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 Shan9df12152025-01-14 14:16:40 -0800613 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 Shan41dd7f12023-06-07 13:25:43 -0700622 temp_files = []
623
Yu Shan41dd7f12023-06-07 13:25:43 -0700624 try:
Yu Shan7881e822023-12-15 13:12:01 -0800625 for generated_file in generated_files:
626 generated_file.convert(f, args.check_only, temp_files)
Yu Shan41dd7f12023-06-07 13:25:43 -0700627
628 if not args.check_only:
629 return
630
Yu Shan7881e822023-12-15 13:12:01 -0800631 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 Shan41dd7f12023-06-07 13:25:43 -0700636 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 Shan63e24d72022-06-24 17:53:32 +0000643
644
Yu Shan41dd7f12023-06-07 13:25:43 -0700645if __name__ == '__main__':
Yu Shan63e24d72022-06-24 17:53:32 +0000646 main()