xref: /aosp_15_r20/external/flatbuffers/scripts/check_generate_code.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 Workerimport sys
20*890232f2SAndroid Build Coastguard Workerfrom pathlib import Path
21*890232f2SAndroid Build Coastguard Worker
22*890232f2SAndroid Build Coastguard Worker# Get the path where this script is located so we can invoke the script from
23*890232f2SAndroid Build Coastguard Worker# any directory and have the paths work correctly.
24*890232f2SAndroid Build Coastguard Workerscript_path = Path(__file__).parent.resolve()
25*890232f2SAndroid Build Coastguard Worker
26*890232f2SAndroid Build Coastguard Worker# Get the root path as an absolute path, so all derived paths are absolute.
27*890232f2SAndroid Build Coastguard Workerroot_path = script_path.parent.absolute()
28*890232f2SAndroid Build Coastguard Worker
29*890232f2SAndroid Build Coastguard Workerresult = subprocess.run(["git", "diff", "--quiet"], cwd=root_path)
30*890232f2SAndroid Build Coastguard Worker
31*890232f2SAndroid Build Coastguard Workerif result.returncode != 0:
32*890232f2SAndroid Build Coastguard Worker    print(
33*890232f2SAndroid Build Coastguard Worker        "\n"
34*890232f2SAndroid Build Coastguard Worker        "ERROR: *********************************************************\n"
35*890232f2SAndroid Build Coastguard Worker        "ERROR: * The following differences were found after building.  *\n"
36*890232f2SAndroid Build Coastguard Worker        "ERROR: * Perhaps there is a difference in the flags for the.   *\n"
37*890232f2SAndroid Build Coastguard Worker        "ERROR: * CMakeLists.txt vs the script/generate_code.py script? *\n"
38*890232f2SAndroid Build Coastguard Worker        "ERROR: *********************************************************\n"
39*890232f2SAndroid Build Coastguard Worker    )
40*890232f2SAndroid Build Coastguard Worker    subprocess.run(["git", "diff", "--binary", "--exit-code"], cwd=root_path)
41*890232f2SAndroid Build Coastguard Worker    sys.exit(result.returncode)
42*890232f2SAndroid Build Coastguard Worker
43*890232f2SAndroid Build Coastguard Worker# Rung the generate_code.py script, forwarding arguments
44*890232f2SAndroid Build Coastguard Workergen_cmd = ["scripts/generate_code.py"] + sys.argv[1:]
45*890232f2SAndroid Build Coastguard Workerif platform.system() == "Windows":
46*890232f2SAndroid Build Coastguard Worker    gen_cmd = ["py"] + gen_cmd
47*890232f2SAndroid Build Coastguard Workersubprocess.run(gen_cmd, cwd=root_path)
48*890232f2SAndroid Build Coastguard Worker
49*890232f2SAndroid Build Coastguard Workerresult = subprocess.run(["git", "diff", "--quiet"], cwd=root_path)
50*890232f2SAndroid Build Coastguard Worker
51*890232f2SAndroid Build Coastguard Workerif result.returncode != 0:
52*890232f2SAndroid Build Coastguard Worker    print(
53*890232f2SAndroid Build Coastguard Worker        "\n"
54*890232f2SAndroid Build Coastguard Worker        "ERROR: ********************************************************\n"
55*890232f2SAndroid Build Coastguard Worker        "ERROR: * The following differences were found after running   *\n"
56*890232f2SAndroid Build Coastguard Worker        "ERROR: * the script/generate_code.py script. Maybe you forgot *\n"
57*890232f2SAndroid Build Coastguard Worker        "ERROR: * to run it after making changes in a generator?       *\n"
58*890232f2SAndroid Build Coastguard Worker        "ERROR: ********************************************************\n"
59*890232f2SAndroid Build Coastguard Worker    )
60*890232f2SAndroid Build Coastguard Worker    subprocess.run(["git", "diff", "--binary", "--exit-code"], cwd=root_path)
61*890232f2SAndroid Build Coastguard Worker    sys.exit(result.returncode)
62*890232f2SAndroid Build Coastguard Worker
63*890232f2SAndroid Build Coastguard Workersys.exit(0)
64