1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
12 #include "absl/log/absl_log.h"
13 #include "absl/strings/string_view.h"
14 #include "absl/strings/substitute.h"
15 #include "google/protobuf/compiler/code_generator_lite.h"
16 #include "upb/base/status.hpp"
17 #include "upb/base/string_view.h"
18 #include "upb/reflection/def.hpp"
19 #include "upb_generator/common.h"
20 #include "upb_generator/common/names.h"
21 #include "upb_generator/file_layout.h"
22 #include "upb_generator/minitable/generator.h"
23 #include "upb_generator/minitable/names_internal.h"
24 #include "upb_generator/plugin.h"
27 #include "upb/port/def.inc"
32 std::string
SourceFilename(upb::FileDefPtr file
) {
33 return StripExtension(file
.name()) + ".upb_minitable.c";
36 absl::string_view
ToStringView(upb_StringView str
) {
37 return absl::string_view(str
.data
, str
.size
);
40 void GenerateFile(const DefPoolPair
& pools
, upb::FileDefPtr file
,
41 const MiniTableOptions
& options
, Plugin
* plugin
) {
43 WriteMiniTableHeader(pools
, file
, options
, h_output
);
44 plugin
->AddOutputFile(MiniTableHeaderFilename(file
.name(), false),
48 WriteMiniTableSource(pools
, file
, options
, c_output
);
49 plugin
->AddOutputFile(SourceFilename(file
), c_output
.output());
51 if (options
.one_output_per_message
) {
52 WriteMiniTableMultipleSources(pools
, file
, options
, plugin
);
56 bool ParseOptions(MiniTableOptions
* options
, Plugin
* plugin
) {
57 for (const auto& pair
: ParseGeneratorParameter(plugin
->parameter())) {
58 if (pair
.first
== "bootstrap_stage") {
59 options
->bootstrap
= true;
60 } else if (pair
.first
== "experimental_strip_nonfunctional_codegen") {
61 options
->strip_nonfunctional_codegen
= true;
62 } else if (pair
.first
== "one_output_per_message") {
63 options
->one_output_per_message
= true;
65 plugin
->SetError(absl::Substitute("Unknown parameter: $0", pair
.first
));
73 int PluginMain(int argc
, char** argv
) {
75 MiniTableOptions options
;
77 if (!ParseOptions(&options
, &plugin
)) return 0;
78 plugin
.GenerateFilesRaw(
79 [&](const UPB_DESC(FileDescriptorProto
) * file_proto
, bool generate
) {
81 upb::FileDefPtr file
= pools
.AddFile(file_proto
, &status
);
83 absl::string_view name
=
84 ToStringView(UPB_DESC(FileDescriptorProto_name
)(file_proto
));
85 ABSL_LOG(FATAL
) << "Couldn't add file " << name
86 << " to DefPool: " << status
.error_message();
88 if (generate
) GenerateFile(pools
, file
, options
, &plugin
);
93 } // namespace generator
96 int main(int argc
, char** argv
) {
97 return upb::generator::PluginMain(argc
, argv
);