blob: 7111d8d313c96faa16c25f9bd14bd5944ccc2a37 [file] [log] [blame]
James Kuszmaul8e62b022022-03-22 09:33:25 -07001#!/usr/bin/env python3
2#
3# Copyright 2021 Google Inc. All rights reserved.
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
17import argparse
18import filecmp
19import glob
20import platform
21import shutil
22import subprocess
Austin Schuh2dd86a92022-09-14 21:19:23 -070023import generate_grpc_examples
James Kuszmaul8e62b022022-03-22 09:33:25 -070024from pathlib import Path
25
26parser = argparse.ArgumentParser()
27parser.add_argument(
28 "--flatc",
29 help="path of the Flat C compiler relative to the root directory",
30)
31parser.add_argument("--cpp-0x", action="store_true", help="use --cpp-std c++ox")
32parser.add_argument(
33 "--skip-monster-extra",
34 action="store_true",
35 help="skip generating tests involving monster_extra.fbs",
36)
37parser.add_argument(
38 "--skip-gen-reflection",
39 action="store_true",
40 help="skip generating the reflection.fbs files",
41)
42args = parser.parse_args()
43
44# Get the path where this script is located so we can invoke the script from
45# any directory and have the paths work correctly.
46script_path = Path(__file__).parent.resolve()
47
48# Get the root path as an absolute path, so all derived paths are absolute.
49root_path = script_path.parent.absolute()
50
51# Get the location of the flatc executable, reading from the first command line
52# argument or defaulting to default names.
53flatc_exe = Path(
54 ("flatc" if not platform.system() == "Windows" else "flatc.exe")
55 if not args.flatc
56 else args.flatc
57)
58
59# Find and assert flatc compiler is present.
60if root_path in flatc_exe.parents:
61 flatc_exe = flatc_exe.relative_to(root_path)
62flatc_path = Path(root_path, flatc_exe)
63assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path)
64
65# Specify the other paths that will be referenced
66tests_path = Path(root_path, "tests")
Austin Schuh2dd86a92022-09-14 21:19:23 -070067swift_code_gen = Path(root_path, "tests/swift/tests/CodeGenerationTests")
James Kuszmaul8e62b022022-03-22 09:33:25 -070068samples_path = Path(root_path, "samples")
69reflection_path = Path(root_path, "reflection")
70
71# Execute the flatc compiler with the specified parameters
Austin Schuh2dd86a92022-09-14 21:19:23 -070072def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path):
James Kuszmaul8e62b022022-03-22 09:33:25 -070073 cmd = [str(flatc_path)] + options
74 if prefix:
75 cmd += ["-o"] + [prefix]
76 if include:
77 cmd += ["-I"] + [include]
78 cmd += [schema] if isinstance(schema, str) else schema
79 if data:
80 cmd += [data] if isinstance(data, str) else data
81 result = subprocess.run(cmd, cwd=str(cwd), check=True)
82
83
84# Generate the code for flatbuffers reflection schema
85def flatc_reflection(options, location, target):
86 full_options = ["--no-prefix"] + options
87 temp_dir = ".tmp"
88 flatc(
89 full_options,
90 prefix=temp_dir,
91 schema="reflection.fbs",
92 cwd=reflection_path,
93 )
94 new_reflection_path = Path(reflection_path, temp_dir, target)
95 original_reflection_path = Path(root_path, location, target)
96 if not filecmp.cmp(str(new_reflection_path), str(original_reflection_path)):
Austin Schuh2dd86a92022-09-14 21:19:23 -070097 shutil.rmtree(str(original_reflection_path), ignore_errors=True)
James Kuszmaul8e62b022022-03-22 09:33:25 -070098 shutil.move(str(new_reflection_path), str(original_reflection_path))
99 shutil.rmtree(str(Path(reflection_path, temp_dir)))
100
Austin Schuh2dd86a92022-09-14 21:19:23 -0700101def flatc_annotate(schema, file, include=None, cwd=tests_path):
102 cmd = [str(flatc_path)]
103 if include:
104 cmd += ["-I"] + [include]
105 cmd += ["--annotate", schema, file]
106 result = subprocess.run(cmd, cwd=str(cwd), check=True)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700107
108# Glob a pattern relative to file path
109def glob(path, pattern):
110 return [str(p) for p in path.glob(pattern)]
111
112
113# flatc options that are shared
114BASE_OPTS = ["--reflect-names", "--gen-mutable", "--gen-object-api"]
115NO_INCL_OPTS = BASE_OPTS + ["--no-includes"]
116
117# Language specific options
118CS_OPTS = ["--csharp", "--cs-gen-json-serializer"]
119CPP_OPTS = [
120 "--cpp",
121 "--gen-compare",
122 "--cpp-ptr-type",
123 "flatbuffers::unique_ptr",
124] + (["--cpp-std", "c++0x"] if args.cpp_0x else [])
125
126CPP_17_OPTS = NO_INCL_OPTS + [
127 "--cpp",
128 "--cpp-std",
129 "c++17",
130 "--cpp-static-reflection",
131 "--gen-object-api",
132]
Austin Schuh2dd86a92022-09-14 21:19:23 -0700133RUST_OPTS = BASE_OPTS + [
134 "--rust",
135 "--gen-all",
136 "--gen-name-strings",
137 "--rust-module-root-file",
138]
James Kuszmaul8e62b022022-03-22 09:33:25 -0700139RUST_SERIALIZE_OPTS = BASE_OPTS + [
140 "--rust",
141 "--gen-all",
142 "--gen-name-strings",
143 "--rust-serialize",
144 "--rust-module-root-file",
145]
146TS_OPTS = ["--ts", "--gen-name-strings"]
147LOBSTER_OPTS = ["--lobster"]
148SWIFT_OPTS = ["--swift", "--gen-json-emit", "--bfbs-filenames", str(tests_path)]
Austin Schuh2dd86a92022-09-14 21:19:23 -0700149SWIFT_OPTS_CODE_GEN = [
150 "--swift",
151 "--gen-json-emit",
152 "--bfbs-filenames",
153 swift_code_gen
154]
James Kuszmaul8e62b022022-03-22 09:33:25 -0700155JAVA_OPTS = ["--java"]
156KOTLIN_OPTS = ["--kotlin"]
157PHP_OPTS = ["--php"]
158DART_OPTS = ["--dart"]
159PYTHON_OPTS = ["--python"]
160BINARY_OPTS = ["-b", "--schema", "--bfbs-comments", "--bfbs-builtins"]
161
162# Basic Usage
163
164flatc(
165 NO_INCL_OPTS
166 + CPP_OPTS
167 + CS_OPTS
James Kuszmaul8e62b022022-03-22 09:33:25 -0700168 + [
169 "--binary",
170 "--java",
171 "--kotlin",
172 "--dart",
173 "--go",
174 "--lobster",
175 "--php",
176 ],
177 schema="monster_test.fbs",
178 include="include_test",
179 data="monsterdata_test.json",
180)
181
182flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700183 NO_INCL_OPTS
184 + TS_OPTS,
185 schema="monster_test.fbs",
186 prefix="ts",
187 include="include_test",
188 data="monsterdata_test.json",
189)
190
191flatc(
James Kuszmaul8e62b022022-03-22 09:33:25 -0700192 ["--lua", "--bfbs-filenames", str(tests_path)],
193 schema="monster_test.fbs",
194 include="include_test",
195)
196
197flatc(
198 NO_INCL_OPTS + CPP_OPTS + ["--grpc"],
199 schema="monster_test.fbs",
200 include="include_test",
201 data="monsterdata_test.json",
202)
203
204flatc(
205 RUST_OPTS,
206 schema="monster_test.fbs",
207 include="include_test",
208 prefix="monster_test",
209 data="monsterdata_test.json",
210)
211
212flatc(
213 RUST_SERIALIZE_OPTS,
214 schema="monster_test.fbs",
215 include="include_test",
216 prefix="monster_test_serialize",
217 data="monsterdata_test.json",
218)
219
220flatc(
221 options=BASE_OPTS + ["--python"],
222 schema="monster_test.fbs",
223 include="include_test",
224 data="monsterdata_test.json",
225)
226
227flatc(
228 options=BASE_OPTS + ["--python", "--gen-onefile"],
229 schema="monster_test.fbs",
230 include="include_test",
231 data="monsterdata_test.json",
232)
233
234# For Rust we currently generate two independent schemas, with namespace_test2
235# duplicating the types in namespace_test1
236flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700237 RUST_OPTS + CS_OPTS,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700238 prefix="namespace_test",
239 schema=[
240 "namespace_test/namespace_test1.fbs",
241 "namespace_test/namespace_test2.fbs",
242 ],
243)
244
245flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700246 BASE_OPTS + CPP_OPTS + CS_OPTS + JAVA_OPTS + KOTLIN_OPTS + PHP_OPTS,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700247 prefix="union_vector",
248 schema="union_vector/union_vector.fbs",
249)
250
251flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700252 BASE_OPTS + TS_OPTS,
253 prefix="ts/union_vector",
254 schema="union_vector/union_vector.fbs",
255)
256
257flatc(
James Kuszmaul8e62b022022-03-22 09:33:25 -0700258 BASE_OPTS + TS_OPTS + ["--gen-name-strings", "--gen-mutable"],
259 include="include_test",
Austin Schuh2dd86a92022-09-14 21:19:23 -0700260 prefix="ts",
James Kuszmaul8e62b022022-03-22 09:33:25 -0700261 schema="monster_test.fbs",
262)
263
Austin Schuh2dd86a92022-09-14 21:19:23 -0700264# Generate the complete flat file TS of monster.
265flatc(
266 ["--ts", "--gen-all", "--ts-flat-files"],
267 include="include_test",
268 schema="monster_test.fbs",
269 prefix="ts/ts-flat-files"
270)
271
James Kuszmaul8e62b022022-03-22 09:33:25 -0700272flatc(
273 BASE_OPTS + TS_OPTS + ["-b"],
274 include="include_test",
Austin Schuh2dd86a92022-09-14 21:19:23 -0700275 prefix="ts",
James Kuszmaul8e62b022022-03-22 09:33:25 -0700276 schema="monster_test.fbs",
277 data="unicode_test.json",
278)
279
280flatc(
281 BASE_OPTS + TS_OPTS + ["--gen-name-strings"],
Austin Schuh2dd86a92022-09-14 21:19:23 -0700282 prefix="ts/union_vector",
James Kuszmaul8e62b022022-03-22 09:33:25 -0700283 schema="union_vector/union_vector.fbs",
284)
285
286flatc(
287 RUST_OPTS,
288 prefix="include_test1",
289 include="include_test",
290 schema="include_test/include_test1.fbs",
291)
292
293flatc(
294 RUST_OPTS,
295 prefix="include_test2",
296 include="include_test",
297 schema="include_test/sub/include_test2.fbs",
298)
299
300flatc(
301 BINARY_OPTS + ["--bfbs-filenames", str(tests_path)],
302 include="include_test",
303 schema="monster_test.fbs",
304)
305
Austin Schuh2dd86a92022-09-14 21:19:23 -0700306# Generate the annotated binary of the monster_test binary schema.
307flatc_annotate(
308 schema="../reflection/reflection.fbs",
309 file="monster_test.bfbs",
310 include="include_test"
311)
312
313flatc_annotate(
314 schema="monster_test.fbs",
315 file="monsterdata_test.mon",
316 include="include_test"
317)
318
James Kuszmaul8e62b022022-03-22 09:33:25 -0700319flatc(
320 CPP_OPTS
321 + NO_INCL_OPTS
322 + [
323 "--bfbs-comments",
324 "--bfbs-builtins",
325 "--bfbs-gen-embed",
326 "--bfbs-filenames",
327 str(tests_path),
328 ],
329 include="include_test",
330 schema="monster_test.fbs",
331)
332
333flatc(
334 BINARY_OPTS + ["--bfbs-filenames", str(tests_path)],
335 include="include_test",
336 schema="arrays_test.fbs",
337)
338
339flatc(
340 ["--jsonschema", "--schema"],
341 include="include_test",
342 schema="monster_test.fbs",
343)
344
345if not args.skip_monster_extra:
346 flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700347 CPP_OPTS + CS_OPTS + NO_INCL_OPTS + JAVA_OPTS + KOTLIN_OPTS + PYTHON_OPTS,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700348 schema="monster_extra.fbs",
349 data="monsterdata_extra.json",
350 )
351
352 flatc(
353 DART_OPTS + ["--gen-object-api"],
354 schema="monster_extra.fbs",
355 )
356
357flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700358 CPP_OPTS + CS_OPTS + NO_INCL_OPTS + JAVA_OPTS + ["--jsonschema", "--scoped-enums"],
James Kuszmaul8e62b022022-03-22 09:33:25 -0700359 schema="arrays_test.fbs",
360)
361
362flatc(
363 RUST_OPTS,
364 prefix="arrays_test",
365 schema="arrays_test.fbs",
366)
367
368flatc(
369 BASE_OPTS + PYTHON_OPTS,
370 schema="arrays_test.fbs",
371)
372
373
374# Optional Scalars
375optional_scalars_schema = "optional_scalars.fbs"
Austin Schuh2dd86a92022-09-14 21:19:23 -0700376flatc(["--java", "--kotlin", "--lobster"], schema=optional_scalars_schema)
377flatc(TS_OPTS, schema=optional_scalars_schema, prefix="ts")
James Kuszmaul8e62b022022-03-22 09:33:25 -0700378
Austin Schuh2dd86a92022-09-14 21:19:23 -0700379flatc(["--csharp", "--python", "--gen-object-api"], schema=optional_scalars_schema)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700380
381flatc(RUST_OPTS, prefix="optional_scalars", schema=optional_scalars_schema)
382
383flatc(NO_INCL_OPTS + CPP_OPTS, schema=optional_scalars_schema)
384
Austin Schuh2dd86a92022-09-14 21:19:23 -0700385# Type / field collsion
386type_field_collsion_schema = "type_field_collsion.fbs"
387
388flatc(["--csharp", "--gen-object-api"], schema=type_field_collsion_schema)
389
James Kuszmaul8e62b022022-03-22 09:33:25 -0700390# Generate string/vector default code for tests
391flatc(RUST_OPTS, prefix="more_defaults", schema="more_defaults.fbs")
392
393# Generate the schema evolution tests
394flatc(
395 CPP_OPTS + ["--scoped-enums"],
396 prefix="evolution_test",
397 schema=glob(tests_path, "evolution_test/evolution_v*.fbs"),
398)
399
400# Generate the keywords tests
401flatc(BASE_OPTS + CS_OPTS, schema="keyword_test.fbs")
402flatc(RUST_OPTS, prefix="keyword_test", schema="keyword_test.fbs")
403flatc(
404 BASE_OPTS + CS_OPTS + ["--cs-global-alias", "--gen-onefile"],
405 prefix="nested_namespace_test",
406 schema=glob(tests_path, "nested_namespace_test/nested_namespace_test*.fbs"),
407)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700408flatc(BASE_OPTS + DART_OPTS, prefix="../dart/test/", schema="keyword_test.fbs")
409
410# Field key lookup with default value test
411dictionary_lookup_schema = "dictionary_lookup.fbs"
412flatc(["--java", "--kotlin"], schema=dictionary_lookup_schema)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700413
414# Swift Tests
Austin Schuh2dd86a92022-09-14 21:19:23 -0700415swift_prefix = "swift/tests/Tests/FlatBuffers.Test.SwiftTests"
James Kuszmaul8e62b022022-03-22 09:33:25 -0700416flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700417 SWIFT_OPTS + BASE_OPTS + ["--grpc"],
James Kuszmaul8e62b022022-03-22 09:33:25 -0700418 schema="monster_test.fbs",
419 include="include_test",
420 prefix=swift_prefix,
421)
422flatc(
423 SWIFT_OPTS + BASE_OPTS,
424 schema="union_vector/union_vector.fbs",
425 prefix=swift_prefix,
426)
427flatc(SWIFT_OPTS, schema="optional_scalars.fbs", prefix=swift_prefix)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700428flatc(SWIFT_OPTS, schema="vector_has_test.fbs", prefix=swift_prefix)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700429flatc(
430 SWIFT_OPTS + ["--gen-object-api"],
431 schema="more_defaults.fbs",
432 prefix=swift_prefix,
433)
434flatc(
435 SWIFT_OPTS + BASE_OPTS,
436 schema="MutatingBool.fbs",
437 prefix=swift_prefix,
438)
439
Austin Schuh2dd86a92022-09-14 21:19:23 -0700440flatc(
441 SWIFT_OPTS_CODE_GEN + BASE_OPTS + ["--grpc", "--swift-implementation-only"],
442 schema="test_import.fbs",
443 cwd=swift_code_gen
444)
445
446flatc(
447 SWIFT_OPTS_CODE_GEN + NO_INCL_OPTS + ["--grpc"],
448 schema="test_no_include.fbs",
449 cwd=swift_code_gen
450)
451
James Kuszmaul8e62b022022-03-22 09:33:25 -0700452# --filename-suffix and --filename-ext tests
453flatc(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700454 CPP_OPTS + NO_INCL_OPTS + ["--grpc", "--filename-ext", "hpp"],
James Kuszmaul8e62b022022-03-22 09:33:25 -0700455 include="include_test",
Austin Schuh2dd86a92022-09-14 21:19:23 -0700456 prefix="monster_test_suffix/ext_only",
James Kuszmaul8e62b022022-03-22 09:33:25 -0700457 schema="monster_test.fbs",
458)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700459flatc(
460 CPP_OPTS + NO_INCL_OPTS + ["--grpc", "--filename-suffix", "_suffix"],
461 include="include_test",
462 prefix="monster_test_suffix/filesuffix_only",
463 schema="monster_test.fbs",
464)
465flatc(
466 CPP_OPTS + NO_INCL_OPTS + ["--grpc", "--filename-suffix", "_suffix", "--filename-ext", "hpp"],
467 include="include_test",
468 prefix="monster_test_suffix",
469 schema="monster_test.fbs",
470)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700471
472# Flag c++17 requires Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher.
473cpp_17_prefix = "cpp17/generated_cpp17"
474flatc(
475 CPP_17_OPTS,
476 schema="monster_test.fbs",
477 include="include_test",
478 prefix=cpp_17_prefix,
479)
480flatc(
481 CPP_17_OPTS,
482 schema="optional_scalars.fbs",
483 prefix=cpp_17_prefix,
484)
485flatc(
486 CPP_17_OPTS,
487 schema="union_vector/union_vector.fbs",
488 prefix=cpp_17_prefix,
489)
490
Austin Schuh2dd86a92022-09-14 21:19:23 -0700491# Private annotations
492annotations_test_schema = "private_annotation_test.fbs"
493
494flatc(RUST_OPTS + ["--no-leak-private-annotation", "--gen-object-api"], prefix="private_annotation_test", schema=annotations_test_schema)
495
James Kuszmaul8e62b022022-03-22 09:33:25 -0700496# Sample files
497samples_schema = "monster.fbs"
Austin Schuh2dd86a92022-09-14 21:19:23 -0700498flatc(BASE_OPTS + CPP_OPTS + LOBSTER_OPTS, schema=samples_schema, cwd=samples_path)
499flatc(RUST_OPTS, prefix="rust_generated", schema=samples_schema, cwd=samples_path)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700500flatc(
501 BINARY_OPTS + ["--bfbs-filenames", str(samples_path)],
502 schema=samples_schema,
503 cwd=samples_path,
504)
505
506# Reflection
507
508# Skip generating the reflection if told too, as we run this script after
509# building flatc which uses the reflection_generated.h itself.
510if not args.skip_gen_reflection:
Austin Schuh2dd86a92022-09-14 21:19:23 -0700511 # C++ Reflection
512 flatc_reflection(
513 ["-c", "--cpp-std", "c++0x"], "include/flatbuffers", "reflection_generated.h"
514 )
James Kuszmaul8e62b022022-03-22 09:33:25 -0700515
516# Python Reflection
517flatc_reflection(["-p"], "python/flatbuffers", "reflection")
Austin Schuh2dd86a92022-09-14 21:19:23 -0700518
519# Annotation
520
521
522def flatc_annotate(schema, include=None, data=None, cwd=tests_path):
523 cmd = [str(flatc_path)]
524 if include:
525 cmd += ["-I"] + [include]
526 cmd += ["--annotate", schema]
527 if data:
528 cmd += [data] if isinstance(data, str) else data
529 subprocess.run(cmd, cwd=str(cwd), check=True)
530
531
532flatc_annotate(
533 schema="monster_test.fbs", include="include_test", data="monsterdata_test.mon"
534)
535
536# Run the generate_grpc_examples script
537generate_grpc_examples.GenerateGRPCExamples()