xref: /aosp_15_r20/external/flatbuffers/tests/annotated_binary/generate_annotations.py (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*890232f2SAndroid Build Coastguard Worker#
3*890232f2SAndroid Build Coastguard Worker# Copyright 2021 Google Inc. All rights reserved.
4*890232f2SAndroid Build Coastguard Worker#
5*890232f2SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*890232f2SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*890232f2SAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*890232f2SAndroid Build Coastguard Worker#
9*890232f2SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*890232f2SAndroid Build Coastguard Worker#
11*890232f2SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*890232f2SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*890232f2SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*890232f2SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*890232f2SAndroid Build Coastguard Worker# limitations under the License.
16*890232f2SAndroid Build Coastguard Worker
17*890232f2SAndroid Build Coastguard Workerimport platform
18*890232f2SAndroid Build Coastguard Workerimport subprocess
19*890232f2SAndroid Build Coastguard Workerfrom pathlib import Path
20*890232f2SAndroid Build Coastguard Worker
21*890232f2SAndroid Build Coastguard Worker# Get the path where this script is located so we can invoke the script from
22*890232f2SAndroid Build Coastguard Worker# any directory and have the paths work correctly.
23*890232f2SAndroid Build Coastguard Workerscript_path = Path(__file__).parent.resolve()
24*890232f2SAndroid Build Coastguard Worker
25*890232f2SAndroid Build Coastguard Worker# Get the root path as an absolute path, so all derived paths are absolute.
26*890232f2SAndroid Build Coastguard Workerroot_path = script_path.parent.parent.absolute()
27*890232f2SAndroid Build Coastguard Worker
28*890232f2SAndroid Build Coastguard Worker# Get the location of the flatc executable, reading from the first command line
29*890232f2SAndroid Build Coastguard Worker# argument or defaulting to default names.
30*890232f2SAndroid Build Coastguard Workerflatc_exe = Path(
31*890232f2SAndroid Build Coastguard Worker    ("flatc" if not platform.system() == "Windows" else "flatc.exe")
32*890232f2SAndroid Build Coastguard Worker)
33*890232f2SAndroid Build Coastguard Worker
34*890232f2SAndroid Build Coastguard Worker# Find and assert flatc compiler is present.
35*890232f2SAndroid Build Coastguard Workerif root_path in flatc_exe.parents:
36*890232f2SAndroid Build Coastguard Worker    flatc_exe = flatc_exe.relative_to(root_path)
37*890232f2SAndroid Build Coastguard Workerflatc_path = Path(root_path, flatc_exe)
38*890232f2SAndroid Build Coastguard Workerassert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path)
39*890232f2SAndroid Build Coastguard Worker
40*890232f2SAndroid Build Coastguard Worker# Specify the other paths that will be referenced
41*890232f2SAndroid Build Coastguard Workertests_path = Path(script_path, "tests")
42*890232f2SAndroid Build Coastguard Worker
43*890232f2SAndroid Build Coastguard Worker
44*890232f2SAndroid Build Coastguard Workerdef flatc_annotate(schema, file, cwd=script_path):
45*890232f2SAndroid Build Coastguard Worker    cmd = [str(flatc_path), "--annotate", schema, file]
46*890232f2SAndroid Build Coastguard Worker    result = subprocess.run(cmd, cwd=str(cwd), check=True)
47*890232f2SAndroid Build Coastguard Worker
48*890232f2SAndroid Build Coastguard Worker
49*890232f2SAndroid Build Coastguard Workertest_files = [
50*890232f2SAndroid Build Coastguard Worker    "annotated_binary.bin",
51*890232f2SAndroid Build Coastguard Worker    "tests/invalid_root_offset.bin",
52*890232f2SAndroid Build Coastguard Worker    "tests/invalid_root_table_too_short.bin",
53*890232f2SAndroid Build Coastguard Worker    "tests/invalid_root_table_vtable_offset.bin",
54*890232f2SAndroid Build Coastguard Worker    "tests/invalid_string_length.bin",
55*890232f2SAndroid Build Coastguard Worker    "tests/invalid_string_length_cut_short.bin",
56*890232f2SAndroid Build Coastguard Worker    "tests/invalid_struct_array_field_cut_short.bin",
57*890232f2SAndroid Build Coastguard Worker    "tests/invalid_struct_field_cut_short.bin",
58*890232f2SAndroid Build Coastguard Worker    "tests/invalid_table_field_size.bin",
59*890232f2SAndroid Build Coastguard Worker    "tests/invalid_table_field_offset.bin",
60*890232f2SAndroid Build Coastguard Worker    "tests/invalid_union_type_value.bin",
61*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vector_length_cut_short.bin",
62*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vector_scalars_cut_short.bin",
63*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vector_strings_cut_short.bin",
64*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vector_structs_cut_short.bin",
65*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vector_tables_cut_short.bin",
66*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vector_unions_cut_short.bin",
67*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vector_union_type_value.bin",
68*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vtable_ref_table_size_short.bin",
69*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vtable_ref_table_size.bin",
70*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vtable_size_short.bin",
71*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vtable_size.bin",
72*890232f2SAndroid Build Coastguard Worker    "tests/invalid_vtable_field_offset.bin",
73*890232f2SAndroid Build Coastguard Worker]
74*890232f2SAndroid Build Coastguard Worker
75*890232f2SAndroid Build Coastguard Workerfor test_file in test_files:
76*890232f2SAndroid Build Coastguard Worker    flatc_annotate("annotated_binary.fbs", test_file)
77