Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 17 | #ifndef FLATBUFFERS_FLATC_H_ |
| 18 | #define FLATBUFFERS_FLATC_H_ |
| 19 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <limits> |
| 22 | #include <string> |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 23 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 24 | #include "flatbuffers/bfbs_generator.h" |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 25 | #include "flatbuffers/flatbuffers.h" |
| 26 | #include "flatbuffers/idl.h" |
| 27 | #include "flatbuffers/util.h" |
| 28 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 29 | namespace flatbuffers { |
| 30 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 31 | extern void LogCompilerWarn(const std::string &warn); |
| 32 | extern void LogCompilerError(const std::string &err); |
| 33 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 34 | struct FlatCOption { |
| 35 | std::string short_opt; |
| 36 | std::string long_opt; |
| 37 | std::string parameter; |
| 38 | std::string description; |
| 39 | }; |
| 40 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 41 | class FlatCompiler { |
| 42 | public: |
| 43 | // Output generator for the various programming languages and formats we |
| 44 | // support. |
| 45 | struct Generator { |
| 46 | typedef bool (*GenerateFn)(const flatbuffers::Parser &parser, |
| 47 | const std::string &path, |
| 48 | const std::string &file_name); |
| 49 | typedef std::string (*MakeRuleFn)(const flatbuffers::Parser &parser, |
| 50 | const std::string &path, |
| 51 | const std::string &file_name); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 52 | typedef bool (*ParsingCompletedFn)(const flatbuffers::Parser &parser, |
| 53 | const std::string &output_path); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 54 | |
| 55 | GenerateFn generate; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 56 | const char *lang_name; |
| 57 | bool schema_only; |
| 58 | GenerateFn generateGRPC; |
| 59 | flatbuffers::IDLOptions::Language lang; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 60 | FlatCOption option; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 61 | MakeRuleFn make_rule; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 62 | BfbsGenerator *bfbs_generator; |
| 63 | ParsingCompletedFn parsing_completed; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn, |
| 67 | bool show_exe_name); |
| 68 | |
| 69 | typedef void (*ErrorFn)(const FlatCompiler *flatc, const std::string &err, |
| 70 | bool usage, bool show_exe_name); |
| 71 | |
| 72 | // Parameters required to initialize the FlatCompiler. |
| 73 | struct InitParams { |
| 74 | InitParams() |
| 75 | : generators(nullptr), |
| 76 | num_generators(0), |
| 77 | warn_fn(nullptr), |
| 78 | error_fn(nullptr) {} |
| 79 | |
| 80 | const Generator *generators; |
| 81 | size_t num_generators; |
| 82 | WarnFn warn_fn; |
| 83 | ErrorFn error_fn; |
| 84 | }; |
| 85 | |
| 86 | explicit FlatCompiler(const InitParams ¶ms) : params_(params) {} |
| 87 | |
| 88 | int Compile(int argc, const char **argv); |
| 89 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 90 | std::string GetShortUsageString(const char *program_name) const; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 91 | std::string GetUsageString(const char *program_name) const; |
| 92 | |
| 93 | private: |
| 94 | void ParseFile(flatbuffers::Parser &parser, const std::string &filename, |
| 95 | const std::string &contents, |
| 96 | std::vector<const char *> &include_directories) const; |
| 97 | |
| 98 | void LoadBinarySchema(Parser &parser, const std::string &filename, |
| 99 | const std::string &contents); |
| 100 | |
| 101 | void Warn(const std::string &warn, bool show_exe_name = true) const; |
| 102 | |
| 103 | void Error(const std::string &err, bool usage = true, |
| 104 | bool show_exe_name = true) const; |
| 105 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 106 | void AnnotateBinaries(const uint8_t *binary_schema, |
| 107 | uint64_t binary_schema_size, |
| 108 | const std::string & schema_filename, |
| 109 | const std::vector<std::string> &binary_files); |
| 110 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 111 | InitParams params_; |
| 112 | }; |
| 113 | |
| 114 | } // namespace flatbuffers |
| 115 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 116 | #endif // FLATBUFFERS_FLATC_H_ |