• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
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
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
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"
25 
26 // Must be last.
27 #include "upb/port/def.inc"
28 
29 namespace upb {
30 namespace generator {
31 
SourceFilename(upb::FileDefPtr file)32 std::string SourceFilename(upb::FileDefPtr file) {
33   return StripExtension(file.name()) + ".upb_minitable.c";
34 }
35 
ToStringView(upb_StringView str)36 absl::string_view ToStringView(upb_StringView str) {
37   return absl::string_view(str.data, str.size);
38 }
39 
GenerateFile(const DefPoolPair & pools,upb::FileDefPtr file,const MiniTableOptions & options,Plugin * plugin)40 void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file,
41                   const MiniTableOptions& options, Plugin* plugin) {
42   Output h_output;
43   WriteMiniTableHeader(pools, file, options, h_output);
44   plugin->AddOutputFile(MiniTableHeaderFilename(file.name(), false),
45                         h_output.output());
46 
47   Output c_output;
48   WriteMiniTableSource(pools, file, options, c_output);
49   plugin->AddOutputFile(SourceFilename(file), c_output.output());
50 
51   if (options.one_output_per_message) {
52     WriteMiniTableMultipleSources(pools, file, options, plugin);
53   }
54 }
55 
ParseOptions(MiniTableOptions * options,Plugin * 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;
64     } else {
65       plugin->SetError(absl::Substitute("Unknown parameter: $0", pair.first));
66       return false;
67     }
68   }
69 
70   return true;
71 }
72 
PluginMain(int argc,char ** argv)73 int PluginMain(int argc, char** argv) {
74   DefPoolPair pools;
75   MiniTableOptions options;
76   Plugin plugin;
77   if (!ParseOptions(&options, &plugin)) return 0;
78   plugin.GenerateFilesRaw(
79       [&](const UPB_DESC(FileDescriptorProto) * file_proto, bool generate) {
80         upb::Status status;
81         upb::FileDefPtr file = pools.AddFile(file_proto, &status);
82         if (!file) {
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();
87         }
88         if (generate) GenerateFile(pools, file, options, &plugin);
89       });
90   return 0;
91 }
92 
93 }  // namespace generator
94 }  // namespace upb
95 
main(int argc,char ** argv)96 int main(int argc, char** argv) {
97   return upb::generator::PluginMain(argc, argv);
98 }
99