flatbuffers/docs/source/Compiler.md

232 lines
8.8 KiB
Markdown
Raw Normal View History

Using the schema compiler {#flatbuffers_guide_using_schema_compiler}
=========================
Usage:
flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] FILES...
[ -- FILES...]
The files are read and parsed in order, and can contain either schemas
or data (see below). Data files are processed according to the definitions of
the most recent schema specified.
`--` indicates that the following files are binary files in
FlatBuffer format conforming to the schema indicated before it.
Depending on the flags passed, additional files may
be generated for each file processed:
For any schema input files, one or more generators can be specified:
- `--cpp`, `-c` : Generate a C++ header for all definitions in this file (as
`filename_generated.h`).
- `--java`, `-j` : Generate Java code.
- `--kotlin`, `-k` : Generate Kotlin code.
- `--csharp`, `-n` : Generate C# code.
- `--go`, `-g` : Generate Go code.
- `--python`, `-p`: Generate Python code.
- `--js`, `-s`: Generate JavaScript code.
- `--ts`: Generate TypeScript code.
- `--php`: Generate PHP code.
- `--grpc`: Generate RPC stub code for GRPC.
- `--dart`: Generate Dart code.
- `--lua`: Generate Lua code.
- `--lobster`: Generate Lobster code.
- `--rust`, `-r` : Generate Rust code.
[Swift] Swift implementation 🎉🎉 (#5603) * Implemented the swift version of Flatbuffers Implemented serailzing, reading, and mutating data from object monster Fixes mis-aligned pointer issue Fixes issue when shared strings are removed from table Adds swift enum, structs code gen Fixed namespace issues + started implementing the table gen Added Mutate function to the code generator Generated linux test cases Fixed an issue with bools, and structs readers in table writer Swift docker image added Updated the test cases, and removed a method parameters in swift Fixed createVector api when called with scalars Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift Cleaned up project Added enum vectors, and their readers Refactored code Added swift into the support document Added documentation in docs, and fixed a small issue with Data() not being returned correctly Fixes Lowercase issue, and prevents generating lookups for deprecated keys * Made all the required funcs to have const + removed unneeded code + fix lowercase func * Removed transform from lowercased and moved it to function * Fixes an issue with iOS allocation from read * Refactored cpp code to be more readable * casts position into int for position * Fix enums issue, moves scalar writer code to use memcpy * Removed c_str from struct function * Fixed script to generate new objects when ran on travis ci: fix * Handles deallocating space allocated for structs * Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
- `--swift`: Generate Swift code.
For any data input files:
- `--binary`, `-b` : If data is contained in this file, generate a
`filename.bin` containing the binary flatbuffer (or a different extension
if one is specified in the schema).
- `--json`, `-t` : If data is contained in this file, generate a
`filename.json` representing the data in the flatbuffer.
Additional options:
- `-o PATH` : Output all generated files to PATH (either absolute, or
relative to the current directory). If omitted, PATH will be the
current directory. PATH should end in your systems path separator,
e.g. `/` or `\`.
- `-I PATH` : when encountering `include` statements, attempt to load the
files from this path. Paths will be tried in the order given, and if all
fail (or none are specified) it will try to load relative to the path of
the schema file being parsed.
- `-M` : Print make rules for generated files.
- `--strict-json` : Require & generate strict JSON (field names are enclosed
in quotes, no trailing commas in tables/vectors). By default, no quotes are
required/generated, and trailing commas are allowed.
- `--allow-non-utf8` : Pass non-UTF-8 input through parser and emit nonstandard
\x escapes in JSON. (Default is to raise parse error on non-UTF-8 input.)
- `--natural-utf8` : Output strings with UTF-8 as human-readable strings.
By default, UTF-8 characters are printed as \uXXXX escapes."
- `--defaults-json` : Output fields whose value is equal to the default value
when writing JSON text.
- `--no-prefix` : Don't prefix enum values in generated C++ by their enum
type.
- `--scoped-enums` : Use C++11 style scoped and strongly typed enums in
generated C++. This also implies `--no-prefix`.
- `--no-emit-min-max-enum-values` : Disable generation of MIN and MAX
enumerated values for scoped enums and prefixed enums.
- `--gen-includes` : (deprecated), this is the default behavior.
If the original behavior is required (no include
statements) use `--no-includes.`
2015-06-16 03:26:10 +00:00
- `--no-includes` : Don't generate include statements for included schemas the
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
generated file depends on (C++ / Python).
- `--gen-mutable` : Generate additional non-const accessors for mutating
FlatBuffers in-place.
- `--gen-onefile` : Generate single output file for C#, Go, and Python.
- `--gen-name-strings` : Generate type name functions for C++.
- `--gen-object-api` : Generate an additional object-based API. This API is
more convenient for object construction and mutation than the base API,
at the cost of efficiency (object allocation). Recommended only to be used
if other options are insufficient.
- `--gen-compare` : Generate operator== for object-based API types.
- `--gen-nullable` : Add Clang _Nullable for C++ pointer. or @Nullable for Java.
- `--gen-generated` : Add @Generated annotation for Java.
[Kotlin] Attach JvmStatic annotation to each method in companion object (#6052) * Attach JvmStatic annotation to each method of companion object Kotlin does not have static accessor so companion object used instead of static. It's so natural. But when use kotlin companion object methods on java it is very inconvenient. ```java GeneratedClassByFlatBuffer.Companion.someMethod() ``` If use @JvmStatic annotation it can be shorten like below. ```java GeneratedClassByFlatBuffer.someMethod() ``` * Formatting by Idea Google C++ style * Add comments - Commit for missing cla Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add comments - Commit for missing cla Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Reset code formatting except modified lines Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Pass missing flag to validateVersion method Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add annotations to missing method in companion object * addVector * createVector * endVector * tableCreator And also I tried add compiler option for generate annotation who don't like this operation. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Modify unmatched option name in compiler usage Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add missing operation for finishSizePrefixed and finishStructBuffer method. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add compiled example with --kotlin-gen-jvmstatic option. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Fix Compiler.md Change option name from --gen-jvm-static-annotation to --kotlin-gen-jvmstatic Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Use IDLOptions reference instead of bool parameter. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Change option name - kotlin_gen_jvmstatic to gen_jvmstatic Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Use IDLOptions reference instead of bool parameter and missing process @JvmStatic as suffix. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add code generation for --gen-jvmstatic option Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Miss typo directory for including. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Miss typo variable suffix for including. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Fix camel case to snake case. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Rollback generate code for gen_jvmstatic option. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Delete generated test files. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * add missing new line at end of file. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Remove generated test file by command line. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> Co-authored-by: sam <sam@jennifersoft.com>
2020-08-18 16:44:43 +00:00
- `--gen-jvmstatic` : Add @JvmStatic annotation for Kotlin methods
in companion object for interop from Java to Kotlin.
- `--gen-all` : Generate not just code for the current schema files, but
for all files it includes as well. If the language uses a single file for
output (by default the case for C++ and JS), all code will end up in
this one file.
- `--cpp-include` : Adds an #include in generated file
- `--cpp-ptr-type T` : Set object API pointer type (default std::unique_ptr)
- `--cpp-str-type T` : Set object API string type (default std::string)
[C++] Object API: document custom string type requirements, implement better custom string type constructor alternative for Unpack() and fix bug with vector of custom string types in Pack(). Squashed commit of the following: commit e9519c647ef949b22756ed660839dd3af854881c Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:24:49 2019 +0100 tests: regenerate code, reverts change to CreateVectorOfStrings(). commit 117e3b0679209f2aa55cbee18c4036e7da4bd4b3 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:15:05 2019 +0100 idl_gen_cpp.cpp: move clang-format on/off outside of declaration, so they are kept properly aligned automatically. commit 4791923806965637d5b13f7003329bfbb2fdf18b Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:11:40 2019 +0100 idl_gen_cpp.cpp: full clang-format run with provided Google format file, enforce 80 lines width. commit 2f0402f9ff43b1af0a29806932e08e6d64373345 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:09:32 2019 +0100 CppUsage: address requested changes. idl_gen_cpp.cpp: fix formatting, keep CreateVectorOfStrings for normal string cases. commit 371d4e0b7972a59e5cff418c44e4043c016ce56a Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Fri Mar 1 16:35:29 2019 +0100 Fix compile error with a vector of non-std::strings. CreateVectorOfStrings() expects a vector of std::string types, but that's not always the case. commit 92b90d7f0fbcfc837a94aa06bedccec0f7b4bb62 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Fri Mar 1 16:15:36 2019 +0100 Document requirement for custom string types to implement empty() and be constructible from std::string. Add new option --cpp-str-flex-ctor to construct custom string types not via std::string, but (char * + length). commit 28cb2e92d5b7684b5df5184da3a3fad2d0cda733 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Fri Mar 1 14:31:17 2019 +0100 idl_gen_cpp.cpp: clang-format run, to better separate changes in content from formatting. Change-Id: I4887ba2f2c632b9e7a8c938659b088cd95690870
2019-03-11 16:42:02 +00:00
T::c_str(), T::length() and T::empty() must be supported.
The custom type also needs to be constructible from std::string (see the
--cpp-str-flex-ctor option to change this behavior).
- `--cpp-str-flex-ctor` : Don't construct custom string types by passing
std::string from Flatbuffers, but (char* + length). This allows efficient
construction of custom string types, including zero-copy construction.
- `--no-cpp-direct-copy` : Don't generate direct copy methods for C++
object-based API.
- `--cpp-std CPP_STD` : Generate a C++ code using features of selected C++ standard.
Supported `CPP_STD` values:
* `c++0x` - generate code compatible with old compilers (VS2010),
* `c++11` - use C++11 code generator (default),
* `c++17` - use C++17 features in generated code (experimental).
- `--object-prefix` : Customise class prefix for C++ object-based API.
- `--object-suffix` : Customise class suffix for C++ object-based API.
- `--go-namespace` : Generate the overrided namespace in Golang.
- `--go-import` : Generate the overrided import for flatbuffers in Golang.
(default is "github.com/google/flatbuffers/go").
- `--raw-binary` : Allow binaries without a file_indentifier to be read.
This may crash flatc given a mismatched schema.
- `--size-prefixed` : Input binaries are size prefixed buffers.
- `--proto`: Expect input files to be .proto files (protocol buffers).
Output the corresponding .fbs file.
Currently supports: `package`, `message`, `enum`, nested declarations,
`import` (use `-I` for paths), `extend`, `oneof`, `group`.
Does not support, but will skip without error: `option`, `service`,
`extensions`, and most everything else.
2015-06-16 03:26:10 +00:00
- `--oneof-union` : Translate .proto oneofs to flatbuffer unions.
- `--grpc` : Generate GRPC interfaces for the specified languages.
2015-06-16 03:26:10 +00:00
- `--schema`: Serialize schemas instead of JSON (use with -b). This will
output a binary version of the specified schema that itself corresponds
to the reflection/reflection.fbs schema. Loading this binary file is the
basis for reflection functionality.
- `--bfbs-comments`: Add doc comments to the binary schema files.
- `--conform FILE` : Specify a schema the following schemas should be
an evolution of. Gives errors if not. Useful to check if schema
modifications don't break schema evolution rules.
[C++] Object API: document custom string type requirements, implement better custom string type constructor alternative for Unpack() and fix bug with vector of custom string types in Pack(). Squashed commit of the following: commit e9519c647ef949b22756ed660839dd3af854881c Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:24:49 2019 +0100 tests: regenerate code, reverts change to CreateVectorOfStrings(). commit 117e3b0679209f2aa55cbee18c4036e7da4bd4b3 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:15:05 2019 +0100 idl_gen_cpp.cpp: move clang-format on/off outside of declaration, so they are kept properly aligned automatically. commit 4791923806965637d5b13f7003329bfbb2fdf18b Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:11:40 2019 +0100 idl_gen_cpp.cpp: full clang-format run with provided Google format file, enforce 80 lines width. commit 2f0402f9ff43b1af0a29806932e08e6d64373345 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Tue Mar 5 18:09:32 2019 +0100 CppUsage: address requested changes. idl_gen_cpp.cpp: fix formatting, keep CreateVectorOfStrings for normal string cases. commit 371d4e0b7972a59e5cff418c44e4043c016ce56a Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Fri Mar 1 16:35:29 2019 +0100 Fix compile error with a vector of non-std::strings. CreateVectorOfStrings() expects a vector of std::string types, but that's not always the case. commit 92b90d7f0fbcfc837a94aa06bedccec0f7b4bb62 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Fri Mar 1 16:15:36 2019 +0100 Document requirement for custom string types to implement empty() and be constructible from std::string. Add new option --cpp-str-flex-ctor to construct custom string types not via std::string, but (char * + length). commit 28cb2e92d5b7684b5df5184da3a3fad2d0cda733 Author: Luca Longinotti <luca.longinotti@inivation.com> Date: Fri Mar 1 14:31:17 2019 +0100 idl_gen_cpp.cpp: clang-format run, to better separate changes in content from formatting. Change-Id: I4887ba2f2c632b9e7a8c938659b088cd95690870
2019-03-11 16:42:02 +00:00
- `--conform-includes PATH` : Include path for the schema given with
`--conform PATH`.
- `--filename-suffix SUFFIX` : The suffix appended to the generated
file names. Default is '_generated'.
- `--filename-ext EXTENSION` : The extension appended to the generated
file names. Default is language-specific (e.g. "h" for C++). This
should not be used when multiple languages are specified.
- `--include-prefix PATH` : Prefix this path to any generated include
statements.
- `--keep-prefix` : Keep original prefix of schema include statement.
- `--reflect-types` : Add minimal type reflection to code generation.
- `--reflect-names` : Add minimal type/name reflection.
- `--root-type T` : Select or override the default root_type.
- `--require-explicit-ids` : When parsing schemas, require explicit ids (id: x).
- `--force-defaults` : Emit default values in binary output from JSON.
2018-07-16 23:05:06 +00:00
- `--force-empty` : When serializing from object API representation, force
strings and vectors to empty rather than null.
- `--force-empty-vectors` : When serializing from object API representation, force
vectors to empty rather than null.
- `--flexbuffers` : Used with "binary" and "json" options, it generates
data using schema-less FlexBuffers.
- `--no-warnings` : Inhibit all warning messages.
- `--cs-global-alias` : Prepend `global::` to all user generated csharp classes and structs.
- `--json-nested-bytes` : Allow a nested_flatbuffer field to be parsed as a
vector of bytes in JSON, which is unsafe unless checked by a verifier
afterwards.
NOTE: short-form options for generators are deprecated, use the long form
whenever possible.