-
Notifications
You must be signed in to change notification settings - Fork 16k
Migrate proto_descriptor_set
#23369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Migrate proto_descriptor_set
#23369
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| cc_binary( | ||
| name = "file_concat", | ||
| srcs = [ | ||
| "main.cc", | ||
| ], | ||
| visibility = ["//bazel:__subpackages__"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Copyright 2020 The Bazel Authors. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr size_t kBufferSize = 4096; // 4kB | ||
|
|
||
| // Return codes. | ||
| constexpr int kOk = 0; | ||
| constexpr int kUsageError = 1; | ||
| constexpr int kIOError = 2; | ||
|
|
||
| } // namespace | ||
|
|
||
| int main(int argc, const char* argv[]) { | ||
| if (argc < 2) { | ||
| std::cout << "Usage: " << argv[0] << " <output> <inputs...>" << std::endl; | ||
| return kUsageError; | ||
| } | ||
|
|
||
| std::string output_path(argv[1]); | ||
| std::ofstream output(output_path, std::ofstream::binary); | ||
| if (!output) { | ||
| std::cerr << "Could not open output file " << output_path << std::endl; | ||
| return kIOError; | ||
| } | ||
|
|
||
| for (int i = 2; i < argc; i++) { | ||
| std::string input_path(argv[i]); | ||
| std::ifstream input(input_path, std::ifstream::binary); | ||
| if (!input) { | ||
| std::cerr << "Could not open input file " << output_path << std::endl; | ||
| return kIOError; | ||
| } | ||
|
|
||
| char buffer[kBufferSize]; | ||
| while (input) { | ||
| if (!input.read(buffer, kBufferSize) && !input.eof()) { | ||
| std::cerr << "Error reading from " << input_path << std::endl; | ||
| return kIOError; | ||
| } | ||
| if (!output.write(buffer, input.gcount())) { | ||
| std::cerr << "Error writing to " << output_path << std::endl; | ||
| return kIOError; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return kOk; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Copyright 2020 The Bazel Authors. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """A rule for generating a `FileDescriptorSet` with all transitive dependencies. | ||
|
|
||
| This module contains the definition of `proto_descriptor_set`, a rule that | ||
| collects all `FileDescriptorSet`s from its transitive dependencies and generates | ||
| a single `FileDescriptorSet` containing all the `FileDescriptorProto` from them. | ||
| """ | ||
|
|
||
| load("//bazel/common:proto_info.bzl", "ProtoInfo") | ||
|
|
||
| def _proto_descriptor_set_impl(ctx): | ||
| args = ctx.actions.args() | ||
|
|
||
| output = ctx.actions.declare_file("{}.pb".format(ctx.attr.name)) | ||
| args.add(output) | ||
|
|
||
| descriptor_sets = depset( | ||
| transitive = [dep[ProtoInfo].transitive_descriptor_sets for dep in ctx.attr.deps], | ||
| ) | ||
| args.add_all(descriptor_sets) | ||
|
|
||
| ctx.actions.run( | ||
| executable = ctx.executable._file_concat, | ||
| mnemonic = "ConcatFileDescriptorSet", | ||
| inputs = descriptor_sets, | ||
| outputs = [output], | ||
| arguments = [args], | ||
| ) | ||
|
|
||
| return [ | ||
| DefaultInfo( | ||
| files = depset([output]), | ||
| runfiles = ctx.runfiles(files = [output]), | ||
| ), | ||
| ] | ||
|
|
||
| proto_descriptor_set = rule( | ||
| implementation = _proto_descriptor_set_impl, | ||
| attrs = { | ||
| "deps": attr.label_list( | ||
| mandatory = False, | ||
| providers = [ProtoInfo], | ||
| doc = """ | ||
| Sequence of `ProtoInfo`s to collect `FileDescriptorSet`s from. | ||
| """.strip(), | ||
| ), | ||
| "_file_concat": attr.label( | ||
| default = "//bazel/private/file_concat:file_concat", | ||
| executable = True, | ||
| cfg = "exec", | ||
| ), | ||
| }, | ||
| doc = """ | ||
| Collects all `FileDescriptorSet`s from `deps` and combines them into a single | ||
| `FileDescriptorSet` containing all the `FileDescriptorProto`. | ||
| """.strip(), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright 2020 The Bazel Authors. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <algorithm> | ||
| #include <fstream> | ||
| #include <memory> | ||
| #include <set> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "tools/cpp/runfiles/runfiles.h" | ||
| #include "google/protobuf/descriptor.pb.h" | ||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
|
|
||
| using bazel::tools::cpp::runfiles::Runfiles; | ||
| using google::protobuf::FileDescriptorProto; | ||
| using google::protobuf::FileDescriptorSet; | ||
|
|
||
| namespace rulesproto { | ||
| constexpr char kWorkspaceRlocation[] = "protobuf/"; | ||
| constexpr char kWorkspaceRlocationBzlmod[] = "_main/"; | ||
|
|
||
| namespace { | ||
|
|
||
| std::string GetRlocation(const std::string& file) { | ||
| static std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest()); | ||
| std::string path = | ||
| runfiles->Rlocation(rulesproto::kWorkspaceRlocation + file); | ||
| std::ifstream input(path, std::ifstream::binary); | ||
| if (!input) { | ||
| path = runfiles->Rlocation(rulesproto::kWorkspaceRlocationBzlmod + file); | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| template <typename T, typename K> | ||
| bool Contains(const T& container, const K& key) { | ||
| return container.find(key) != container.end(); | ||
| } | ||
|
|
||
| std::vector<std::string> ReadFileDescriptorSet(const std::string& path) { | ||
| std::ifstream input(path, std::ifstream::binary); | ||
| EXPECT_TRUE(input) << "Could not open " << path; | ||
|
|
||
| FileDescriptorSet file_descriptor_set; | ||
| EXPECT_TRUE(file_descriptor_set.ParseFromIstream(&input)); | ||
|
|
||
| std::set<std::string> unordered_proto_files; | ||
| for (FileDescriptorProto file_descriptor : file_descriptor_set.file()) { | ||
| EXPECT_FALSE(Contains(unordered_proto_files, file_descriptor.name())) | ||
| << "Already saw " << file_descriptor.name(); | ||
| unordered_proto_files.insert(file_descriptor.name()); | ||
| } | ||
|
|
||
| std::vector<std::string> proto_files(unordered_proto_files.begin(), | ||
| unordered_proto_files.end()); | ||
| std::sort(proto_files.begin(), proto_files.end()); | ||
| return proto_files; | ||
| } | ||
|
|
||
| void AssertFileDescriptorSetContains( | ||
| const std::string& path, | ||
| const std::vector<std::string>& expected_proto_files) { | ||
| std::vector<std::string> actual_proto_files = | ||
| ReadFileDescriptorSet(GetRlocation(path)); | ||
| EXPECT_THAT(actual_proto_files, | ||
| ::testing::IsSupersetOf(expected_proto_files)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(ProtoDescriptorSetTest, NoProtos) { | ||
| AssertFileDescriptorSetContains( | ||
| "bazel/tests/no_protos.pb", {}); | ||
| } | ||
|
|
||
| TEST(ProtoDescriptorSetTest, WellKnownProtos) { | ||
| AssertFileDescriptorSetContains( | ||
| "bazel/tests/well_known_protos.pb", | ||
| { | ||
| "google/protobuf/any.proto", | ||
| "google/protobuf/api.proto", | ||
| "google/protobuf/descriptor.proto", | ||
| "google/protobuf/duration.proto", | ||
| "google/protobuf/empty.proto", | ||
| "google/protobuf/field_mask.proto", | ||
| "google/protobuf/source_context.proto", | ||
| "google/protobuf/struct.proto", | ||
| "google/protobuf/timestamp.proto", | ||
| "google/protobuf/type.proto", | ||
| "google/protobuf/wrappers.proto", | ||
| }); | ||
| } | ||
|
|
||
| } // namespace rulesproto |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.