1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #include <flatbuffers/flatc.h> // @manual=fbsource//third-party/flatbuffers:flatc_library
10 #include <flatbuffers/idl.h> // @manual=fbsource//third-party/flatbuffers:flatc_library
11 #include <pybind11/pybind11.h> // @manual=fbsource//third-party/pybind11:pybind11
12 #include <pybind11/stl.h> // @manual=fbsource//third-party/pybind11:pybind11
13
14 namespace exir {
15 namespace {
Warn(const flatbuffers::FlatCompiler *,const std::string & warn,bool)16 void Warn(
17 const flatbuffers::FlatCompiler* /* flatc */,
18 const std::string& warn,
19 bool /* show_exe_name */) {
20 printf("flatc compiler warning: %s\n", warn.c_str());
21 }
22
Error(const flatbuffers::FlatCompiler *,const std::string & err,bool,bool)23 void Error(
24 const flatbuffers::FlatCompiler* /* flatc */,
25 const std::string& err,
26 bool /* usage */,
27 bool /* show_exe_name */) {
28 throw std::runtime_error("Caught error in flatc compiler: " + err);
29 }
30
31 } // namespace
32
PYBIND11_MODULE(_bindings,m)33 PYBIND11_MODULE(_bindings, m) {
34 m.def(
35 "flatc_compile",
36 [&](const std::string& outputPath,
37 const std::string& schemaPath,
38 const std::string& jsonPath) {
39 static const flatbuffers::FlatCompiler::Generator generators[] = {
40 {flatbuffers::GenerateBinary,
41 "-b",
42 "--binary",
43 "binary",
44 false,
45 nullptr,
46 flatbuffers::IDLOptions::kBinary,
47 "Generate wire format binaries for any data definitions",
48 flatbuffers::BinaryMakeRule}};
49
50 flatbuffers::FlatCompiler::InitParams params;
51 params.generators = generators;
52 params.num_generators = sizeof(generators) / sizeof(generators[0]);
53 params.warn_fn = Warn;
54 params.error_fn = Error;
55
56 flatbuffers::FlatCompiler flatc(params);
57 std::array<const char*, 5> argv = {
58 "--binary",
59 "-o",
60 outputPath.c_str(),
61 schemaPath.c_str(),
62 jsonPath.c_str()};
63 return flatc.Compile(argv.size(), argv.data());
64 })
65 .def(
66 "flatc_decompile",
67 [&](const std::string& outputPath,
68 const std::string& schemaPath,
69 const std::string& binPath) {
70 static const flatbuffers::FlatCompiler::Generator generators[] = {
71 {flatbuffers::GenerateTextFile,
72 "-t",
73 "--json",
74 "text",
75 false,
76 nullptr,
77 flatbuffers::IDLOptions::kJson,
78 "Generate text output for any data definitions",
79 flatbuffers::TextMakeRule}};
80
81 flatbuffers::FlatCompiler::InitParams params;
82 params.generators = generators;
83 params.num_generators = sizeof(generators) / sizeof(generators[0]);
84 params.warn_fn = Warn;
85 params.error_fn = Error;
86
87 flatbuffers::FlatCompiler flatc(params);
88
89 std::array<const char*, 8> argv = {
90 "--json",
91 "--defaults-json",
92 "--strict-json",
93 "-o",
94 outputPath.c_str(),
95 schemaPath.c_str(),
96 "--",
97 binPath.c_str()};
98 return flatc.Compile(argv.size(), argv.data());
99 });
100 }
101
102 } // namespace exir
103