|
|
|
// Protocol Buffers - Google's data interchange format
|
|
|
|
// Copyright 2023 Google LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "absl/log/absl_log.h"
|
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
#include "absl/strings/substitute.h"
|
|
|
|
#include "upb/base/status.hpp"
|
|
|
|
#include "upb/base/string_view.h"
|
|
|
|
#include "upb/reflection/def.hpp"
|
|
|
|
#include "upb_generator/common.h"
|
|
|
|
#include "upb_generator/file_layout.h"
|
|
|
|
#include "upb_generator/plugin.h"
|
|
|
|
#include "upb_generator/protoc-gen-upb_minitable.h"
|
|
|
|
|
|
|
|
// Must be last.
|
|
|
|
#include "upb/port/def.inc"
|
|
|
|
|
|
|
|
namespace upb {
|
|
|
|
namespace generator {
|
|
|
|
|
|
|
|
std::string SourceFilename(upb::FileDefPtr file) {
|
|
|
|
return StripExtension(file.name()) + ".upb_minitable.c";
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::string_view ToStringView(upb_StringView str) {
|
|
|
|
return absl::string_view(str.data, str.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file,
|
|
|
|
const MiniTableOptions& options, Plugin* plugin) {
|
|
|
|
Output h_output;
|
Fixed layering check violations once and for all in upb bootstrapping.
Our bootstrapping setup compiles multiple versions of the generated code for `descriptor.proto` and `plugin.proto`, one for each stage of the bootstrap. For source files (`.c`), we can always select the correct version of the file in the BUILD rules, but for header files we need to make sure the correct stage's file is always selected via `#include`.
Previously we used `cc_library(includes=[])` to make it appear as though our bootstrapped headers had the same names as the "real" headers. This allowed a lot of the code to be agnostic to whether a bootstrap header was being used, which simplified things because we did not have to change the code performing the `#include`.
Unfortunately, due to build system limitations, this sometimes led to the incorrect header getting included. This should not have been possible, because we had a clean BUILD graph that should have removed all ambiguity about which header should be available. But in non-sandboxed builds, the compiler was able to find headers that were not actually in `deps=[]`, and worse it preferred those headers over the headers that actually were in `deps=[]`. This led to unintended results and errors about layering check violations.
This CL fixes the problem by removing all use of `includes=[]`. We now spell a full pathname to all bootstrap headers, so this class of errors is no longer possible. Unfortunately this adds some complexity, as we have to hard-code these full paths in several places.
A nice improvement in this CL is that `bootstrap_upb_proto_library()` can now only be used for bootstrapping; it only exposes the `descriptor_bootstrap.h` / `plugin_bootstrap.h` files. Anyone wanting to use the normal `net/proto2/proto/descriptor.upb.h` file should depend on `//net/proto2/proto:descriptor_upb_c_proto` target instead.
PiperOrigin-RevId: 664953196
3 months ago
|
|
|
WriteMiniTableHeader(pools, file, options, h_output);
|
|
|
|
plugin->AddOutputFile(MiniTableHeaderFilename(file, false),
|
|
|
|
h_output.output());
|
|
|
|
|
|
|
|
Output c_output;
|
|
|
|
WriteMiniTableSource(pools, file, options, c_output);
|
|
|
|
plugin->AddOutputFile(SourceFilename(file), c_output.output());
|
|
|
|
|
|
|
|
if (options.one_output_per_message) {
|
|
|
|
WriteMiniTableMultipleSources(pools, file, options, plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseOptions(MiniTableOptions* options, Plugin* plugin) {
|
|
|
|
for (const auto& pair : ParseGeneratorParameter(plugin->parameter())) {
|
Fixed layering check violations once and for all in upb bootstrapping.
Our bootstrapping setup compiles multiple versions of the generated code for `descriptor.proto` and `plugin.proto`, one for each stage of the bootstrap. For source files (`.c`), we can always select the correct version of the file in the BUILD rules, but for header files we need to make sure the correct stage's file is always selected via `#include`.
Previously we used `cc_library(includes=[])` to make it appear as though our bootstrapped headers had the same names as the "real" headers. This allowed a lot of the code to be agnostic to whether a bootstrap header was being used, which simplified things because we did not have to change the code performing the `#include`.
Unfortunately, due to build system limitations, this sometimes led to the incorrect header getting included. This should not have been possible, because we had a clean BUILD graph that should have removed all ambiguity about which header should be available. But in non-sandboxed builds, the compiler was able to find headers that were not actually in `deps=[]`, and worse it preferred those headers over the headers that actually were in `deps=[]`. This led to unintended results and errors about layering check violations.
This CL fixes the problem by removing all use of `includes=[]`. We now spell a full pathname to all bootstrap headers, so this class of errors is no longer possible. Unfortunately this adds some complexity, as we have to hard-code these full paths in several places.
A nice improvement in this CL is that `bootstrap_upb_proto_library()` can now only be used for bootstrapping; it only exposes the `descriptor_bootstrap.h` / `plugin_bootstrap.h` files. Anyone wanting to use the normal `net/proto2/proto/descriptor.upb.h` file should depend on `//net/proto2/proto:descriptor_upb_c_proto` target instead.
PiperOrigin-RevId: 664953196
3 months ago
|
|
|
if (pair.first == "bootstrap_stage") {
|
|
|
|
options->bootstrap = true;
|
|
|
|
} else if (pair.first == "experimental_strip_nonfunctional_codegen") {
|
|
|
|
options->strip_nonfunctional_codegen = true;
|
|
|
|
} else if (pair.first == "one_output_per_message") {
|
|
|
|
options->one_output_per_message = true;
|
|
|
|
} else {
|
|
|
|
plugin->SetError(absl::Substitute("Unknown parameter: $0", pair.first));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PluginMain(int argc, char** argv) {
|
|
|
|
DefPoolPair pools;
|
|
|
|
MiniTableOptions options;
|
|
|
|
Plugin plugin;
|
|
|
|
if (!ParseOptions(&options, &plugin)) return 0;
|
|
|
|
plugin.GenerateFilesRaw(
|
|
|
|
[&](const UPB_DESC(FileDescriptorProto) * file_proto, bool generate) {
|
|
|
|
upb::Status status;
|
|
|
|
upb::FileDefPtr file = pools.AddFile(file_proto, &status);
|
|
|
|
if (!file) {
|
|
|
|
absl::string_view name =
|
|
|
|
ToStringView(UPB_DESC(FileDescriptorProto_name)(file_proto));
|
|
|
|
ABSL_LOG(FATAL) << "Couldn't add file " << name
|
|
|
|
<< " to DefPool: " << status.error_message();
|
|
|
|
}
|
|
|
|
if (generate) GenerateFile(pools, file, options, &plugin);
|
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace generator
|
|
|
|
} // namespace upb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
return upb::generator::PluginMain(argc, argv);
|
|
|
|
}
|