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