1# Copyright 2022 Google Inc. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import argparse 16import platform 17import subprocess 18from pathlib import Path 19 20parser = argparse.ArgumentParser() 21parser.add_argument( 22 "--flatc", 23 help="path of the Flat C compiler relative to the root directory", 24) 25parser.add_argument("--cpp-0x", action="store_true", help="use --cpp-std c++ox") 26parser.add_argument( 27 "--skip-monster-extra", 28 action="store_true", 29 help="skip generating tests involving monster_extra.fbs", 30) 31parser.add_argument( 32 "--skip-gen-reflection", 33 action="store_true", 34 help="skip generating the reflection.fbs files", 35) 36args = parser.parse_args() 37 38# Get the path where this script is located so we can invoke the script from 39# any directory and have the paths work correctly. 40script_path = Path(__file__).parent.resolve() 41 42# Get the root path as an absolute path, so all derived paths are absolute. 43root_path = script_path.parent.absolute() 44tests_path = Path(root_path, "tests") 45 46# Get the location of the flatc executable, reading from the first command line 47# argument or defaulting to default names. 48flatc_exe = Path( 49 ("flatc" if not platform.system() == "Windows" else "flatc.exe") 50 if not args.flatc 51 else args.flatc 52) 53 54# Find and assert flatc compiler is present. 55if root_path in flatc_exe.parents: 56 flatc_exe = flatc_exe.relative_to(root_path) 57flatc_path = Path(root_path, flatc_exe) 58assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path) 59 60# Execute the flatc compiler with the specified parameters 61def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path): 62 cmd = [str(flatc_path)] + options 63 if prefix: 64 cmd += ["-o"] + [prefix] 65 if include: 66 cmd += ["-I"] + [include] 67 cmd += [schema] if isinstance(schema, str) else schema 68 if data: 69 cmd += [data] if isinstance(data, str) else data 70 result = subprocess.run(cmd, cwd=str(cwd), check=True) 71