xref: /aosp_15_r20/external/flatbuffers/scripts/util.py (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker# Copyright 2022 Google Inc. All rights reserved.
2*890232f2SAndroid Build Coastguard Worker#
3*890232f2SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*890232f2SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*890232f2SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*890232f2SAndroid Build Coastguard Worker#
7*890232f2SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*890232f2SAndroid Build Coastguard Worker#
9*890232f2SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*890232f2SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*890232f2SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*890232f2SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*890232f2SAndroid Build Coastguard Worker# limitations under the License.
14*890232f2SAndroid Build Coastguard Worker
15*890232f2SAndroid Build Coastguard Workerimport platform
16*890232f2SAndroid Build Coastguard Workerimport subprocess
17*890232f2SAndroid Build Coastguard Workerfrom pathlib import Path
18*890232f2SAndroid Build Coastguard Worker
19*890232f2SAndroid Build Coastguard Worker# Get the path where this script is located so we can invoke the script from
20*890232f2SAndroid Build Coastguard Worker# any directory and have the paths work correctly.
21*890232f2SAndroid Build Coastguard Workerscript_path = Path(__file__).parent.resolve()
22*890232f2SAndroid Build Coastguard Worker
23*890232f2SAndroid Build Coastguard Worker# Get the root path as an absolute path, so all derived paths are absolute.
24*890232f2SAndroid Build Coastguard Workerroot_path = script_path.parent.absolute()
25*890232f2SAndroid Build Coastguard Worker
26*890232f2SAndroid Build Coastguard Worker# Get the location of the flatc executable, reading from the first command line
27*890232f2SAndroid Build Coastguard Worker# argument or defaulting to default names.
28*890232f2SAndroid Build Coastguard Workerflatc_exe = Path("flatc" if not platform.system() == "Windows" else "flatc.exe")
29*890232f2SAndroid Build Coastguard Worker
30*890232f2SAndroid Build Coastguard Worker# Find and assert flatc compiler is present.
31*890232f2SAndroid Build Coastguard Workerif root_path in flatc_exe.parents:
32*890232f2SAndroid Build Coastguard Worker    flatc_exe = flatc_exe.relative_to(root_path)
33*890232f2SAndroid Build Coastguard Workerflatc_path = Path(root_path, flatc_exe)
34*890232f2SAndroid Build Coastguard Workerassert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path)
35*890232f2SAndroid Build Coastguard Worker
36*890232f2SAndroid Build Coastguard Worker# Execute the flatc compiler with the specified parameters
37*890232f2SAndroid Build Coastguard Workerdef flatc(options, schema, prefix=None, include=None, data=None, cwd=root_path):
38*890232f2SAndroid Build Coastguard Worker    cmd = [str(flatc_path)] + options
39*890232f2SAndroid Build Coastguard Worker    if prefix:
40*890232f2SAndroid Build Coastguard Worker        cmd += ["-o"] + [prefix]
41*890232f2SAndroid Build Coastguard Worker    if include:
42*890232f2SAndroid Build Coastguard Worker        cmd += ["-I"] + [include]
43*890232f2SAndroid Build Coastguard Worker    if isinstance(schema, Path):
44*890232f2SAndroid Build Coastguard Worker      cmd += [str(schema)]
45*890232f2SAndroid Build Coastguard Worker    elif isinstance(schema, str):
46*890232f2SAndroid Build Coastguard Worker      cmd += [schema]
47*890232f2SAndroid Build Coastguard Worker    else:
48*890232f2SAndroid Build Coastguard Worker      cmd += schema
49*890232f2SAndroid Build Coastguard Worker    if data:
50*890232f2SAndroid Build Coastguard Worker        cmd += [data] if isinstance(data, str) else data
51*890232f2SAndroid Build Coastguard Worker    return subprocess.check_call(cmd, cwd=str(cwd))
52