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