| 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 |
|
| 17 |
#ifndef FLATBUFFERS_FLATC_H_ |
| 18 |
#define FLATBUFFERS_FLATC_H_ |
| 19 |
|
| 20 |
#include <functional> |
| 21 |
#include <limits> |
| 22 |
#include <string> |
| 23 |
|
| 24 |
#include "flatbuffers/bfbs_generator.h" |
| 25 |
#include "flatbuffers/flatbuffers.h" |
| 26 |
#include "flatbuffers/idl.h" |
| 27 |
#include "flatbuffers/util.h" |
| 28 |
|
| 29 |
namespace flatbuffers { |
| 30 |
|
| 31 |
extern void LogCompilerWarn(const std::string &warn); |
| 32 |
extern void LogCompilerError(const std::string &err); |
| 33 |
|
| 34 |
struct FlatCOption { |
| 35 |
std::string short_opt; |
| 36 |
std::string long_opt; |
| 37 |
std::string parameter; |
| 38 |
std::string description; |
| 39 |
}; |
| 40 |
|
| 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); |
| 52 |
typedef bool (*ParsingCompletedFn)(const flatbuffers::Parser &parser, |
| 53 |
const std::string &output_path); |
| 54 |
|
| 55 |
GenerateFn generate; |
| 56 |
const char *lang_name; |
| 57 |
bool schema_only; |
| 58 |
GenerateFn generateGRPC; |
| 59 |
flatbuffers::IDLOptions::Language lang; |
| 60 |
FlatCOption option; |
| 61 |
MakeRuleFn make_rule; |
| 62 |
BfbsGenerator *bfbs_generator; |
| 63 |
ParsingCompletedFn parsing_completed; |
| 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 |
|
| 90 |
std::string GetShortUsageString(const char *program_name) const; |
| 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 |
|
| 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 |
|
| 111 |
InitParams params_; |
| 112 |
}; |
| 113 |
|
| 114 |
} // namespace flatbuffers |
| 115 |
|
| 116 |
#endif // FLATBUFFERS_FLATC_H_ |