blob: 3dba5e1414e58c3daebb1603970748ffc418d371 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
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 Schuh272c6132020-11-14 16:37:52 -080017#ifndef FLATBUFFERS_FLATC_H_
18#define FLATBUFFERS_FLATC_H_
19
Austin Schuhe89fa2d2019-08-14 20:24:23 -070020#include <functional>
21#include <limits>
22#include <string>
Austin Schuh272c6132020-11-14 16:37:52 -080023
James Kuszmaul8e62b022022-03-22 09:33:25 -070024#include "flatbuffers/bfbs_generator.h"
Austin Schuhe89fa2d2019-08-14 20:24:23 -070025#include "flatbuffers/flatbuffers.h"
26#include "flatbuffers/idl.h"
27#include "flatbuffers/util.h"
28
Austin Schuhe89fa2d2019-08-14 20:24:23 -070029namespace flatbuffers {
30
Austin Schuh272c6132020-11-14 16:37:52 -080031extern void LogCompilerWarn(const std::string &warn);
32extern void LogCompilerError(const std::string &err);
33
James Kuszmaul8e62b022022-03-22 09:33:25 -070034struct FlatCOption {
35 std::string short_opt;
36 std::string long_opt;
37 std::string parameter;
38 std::string description;
39};
40
Austin Schuhe89fa2d2019-08-14 20:24:23 -070041class 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 Kuszmaul8e62b022022-03-22 09:33:25 -070052 typedef bool (*ParsingCompletedFn)(const flatbuffers::Parser &parser,
53 const std::string &output_path);
Austin Schuhe89fa2d2019-08-14 20:24:23 -070054
55 GenerateFn generate;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070056 const char *lang_name;
57 bool schema_only;
58 GenerateFn generateGRPC;
59 flatbuffers::IDLOptions::Language lang;
James Kuszmaul8e62b022022-03-22 09:33:25 -070060 FlatCOption option;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070061 MakeRuleFn make_rule;
James Kuszmaul8e62b022022-03-22 09:33:25 -070062 BfbsGenerator *bfbs_generator;
63 ParsingCompletedFn parsing_completed;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070064 };
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 &params) : params_(params) {}
87
88 int Compile(int argc, const char **argv);
89
James Kuszmaul8e62b022022-03-22 09:33:25 -070090 std::string GetShortUsageString(const char *program_name) const;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070091 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 InitParams params_;
107};
108
109} // namespace flatbuffers
110
Austin Schuh272c6132020-11-14 16:37:52 -0800111#endif // FLATBUFFERS_FLATC_H_