|
21 | 21 | #include "absl/strings/string_view.h" |
22 | 22 | #include "absl/strings/substitute.h" |
23 | 23 | #include "google/protobuf/compiler/code_generator.h" |
| 24 | +#include "google/protobuf/compiler/code_generator_lite.h" |
| 25 | +#include "upb/mem/arena.hpp" |
24 | 26 | #include "upb/mini_table/enum.h" |
25 | 27 | #include "upb/mini_table/field.h" |
26 | 28 | #include "upb/mini_table/internal/field.h" |
|
32 | 34 | #include "upb_generator/minitable/fasttable.h" |
33 | 35 | #include "upb_generator/minitable/names.h" |
34 | 36 | #include "upb_generator/minitable/names_internal.h" |
| 37 | +#include "upb_generator/plugin.h" |
35 | 38 |
|
36 | 39 | // Must be last. |
37 | 40 | #include "upb/port/def.inc" |
@@ -468,5 +471,72 @@ void WriteMiniTableMultipleSources( |
468 | 471 | } |
469 | 472 | } |
470 | 473 |
|
| 474 | +std::string SourceFilename(upb::FileDefPtr file) { |
| 475 | + return StripExtension(file.name()) + ".upb_minitable.c"; |
| 476 | +} |
| 477 | + |
| 478 | +void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file, |
| 479 | + const MiniTableOptions& options, |
| 480 | + google::protobuf::compiler::GeneratorContext* context) { |
| 481 | + Output h_output; |
| 482 | + WriteMiniTableHeader(pools, file, options, h_output); |
| 483 | + { |
| 484 | + auto stream = absl::WrapUnique( |
| 485 | + context->Open(MiniTableHeaderFilename(file.name(), false))); |
| 486 | + ABSL_CHECK(stream->WriteCord(absl::Cord(h_output.output()))); |
| 487 | + } |
| 488 | + |
| 489 | + Output c_output; |
| 490 | + WriteMiniTableSource(pools, file, options, c_output); |
| 491 | + { |
| 492 | + auto stream = absl::WrapUnique(context->Open(SourceFilename(file))); |
| 493 | + ABSL_CHECK(stream->WriteCord(absl::Cord(c_output.output()))); |
| 494 | + } |
| 495 | + |
| 496 | + if (options.one_output_per_message) { |
| 497 | + WriteMiniTableMultipleSources(pools, file, options, context); |
| 498 | + } |
| 499 | +} |
| 500 | + |
| 501 | +bool ParseOptions(MiniTableOptions* options, absl::string_view parameter, |
| 502 | + std::string* error) { |
| 503 | + for (const auto& pair : ParseGeneratorParameter(parameter)) { |
| 504 | + if (pair.first == "bootstrap_stage") { |
| 505 | + options->bootstrap = true; |
| 506 | + } else if (pair.first == "experimental_strip_nonfunctional_codegen") { |
| 507 | + options->strip_nonfunctional_codegen = true; |
| 508 | + } else if (pair.first == "one_output_per_message") { |
| 509 | + options->one_output_per_message = true; |
| 510 | + } else { |
| 511 | + *error = absl::Substitute("Unknown parameter: $0", pair.first); |
| 512 | + return false; |
| 513 | + } |
| 514 | + } |
| 515 | + |
| 516 | + return true; |
| 517 | +} |
| 518 | + |
| 519 | +bool MiniTableGenerator::GenerateAll( |
| 520 | + const std::vector<const google::protobuf::FileDescriptor*>& files, |
| 521 | + const std::string& parameter, |
| 522 | + google::protobuf::compiler::GeneratorContext* generator_context, |
| 523 | + std::string* error) const { |
| 524 | + MiniTableOptions options; |
| 525 | + if (!ParseOptions(&options, parameter, error)) { |
| 526 | + return false; |
| 527 | + } |
| 528 | + |
| 529 | + upb::Arena arena; |
| 530 | + DefPoolPair pools; |
| 531 | + absl::flat_hash_set<std::string> files_seen; |
| 532 | + for (const auto* file : files) { |
| 533 | + PopulateDefPool(file, &arena, &pools, &files_seen); |
| 534 | + upb::FileDefPtr upb_file = pools.GetFile(file->name()); |
| 535 | + GenerateFile(pools, upb_file, options, generator_context); |
| 536 | + } |
| 537 | + |
| 538 | + return true; |
| 539 | +} |
| 540 | + |
471 | 541 | } // namespace generator |
472 | 542 | } // namespace upb |
0 commit comments