Skip to content

Commit d4aff03

Browse files
jckingcopybara-github
authored andcommitted
Add minimal and testing descriptor pools
PiperOrigin-RevId: 658448217
1 parent 064ef34 commit d4aff03

11 files changed

+740
-0
lines changed

bazel/BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ java_binary(
77
)
88

99
package(default_visibility = ["//visibility:public"])
10+
11+
cc_binary(
12+
name = "cel_cc_embed",
13+
srcs = ["cel_cc_embed.cc"],
14+
visibility = ["//:__subpackages__"],
15+
deps = [
16+
"@com_google_absl//absl/flags:flag",
17+
"@com_google_absl//absl/flags:parse",
18+
"@com_google_absl//absl/log:absl_check",
19+
"@com_google_absl//absl/log:initialize",
20+
"@com_google_absl//absl/strings",
21+
"@com_google_absl//absl/types:span",
22+
],
23+
)

bazel/cel_cc_embed.bzl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Provides the `cel_cc_embed` build rule.
17+
"""
18+
19+
def cel_cc_embed(name, src, testonly = False):
20+
native.genrule(
21+
name = name,
22+
srcs = [src],
23+
outs = ["{}.inc".format(name)],
24+
cmd = "$(location //bazel:cel_cc_embed) --in=$< --out=$@",
25+
tools = ["//bazel:cel_cc_embed"],
26+
testonly = testonly,
27+
)

bazel/cel_cc_embed.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <cstddef>
16+
#include <cstdint>
17+
#include <cstdlib>
18+
#include <fstream>
19+
#include <string>
20+
#include <vector>
21+
22+
#include "absl/flags/flag.h"
23+
#include "absl/flags/parse.h"
24+
#include "absl/log/absl_check.h"
25+
#include "absl/log/initialize.h"
26+
#include "absl/strings/str_cat.h"
27+
#include "absl/strings/str_join.h"
28+
#include "absl/types/span.h"
29+
30+
ABSL_FLAG(std::string, in, "", "");
31+
ABSL_FLAG(std::string, out, "", "");
32+
33+
namespace {
34+
35+
std::vector<uint8_t> ReadFile(const std::string& path) {
36+
ABSL_CHECK(!path.empty()) << "--in is required";
37+
std::ifstream file(path);
38+
ABSL_CHECK(file.is_open()) << path;
39+
file.seekg(0, file.end);
40+
ABSL_CHECK(file.good());
41+
size_t size = static_cast<size_t>(file.tellg());
42+
file.seekg(0, file.beg);
43+
ABSL_CHECK(file.good());
44+
std::vector<uint8_t> buffer;
45+
buffer.resize(size);
46+
file.read(reinterpret_cast<char*>(buffer.data()), size);
47+
ABSL_CHECK(file.good());
48+
return buffer;
49+
}
50+
51+
void WriteFile(const std::string& path, absl::Span<const char> data) {
52+
ABSL_CHECK(!path.empty()) << "--out is required";
53+
std::ofstream file(path);
54+
ABSL_CHECK(file.is_open()) << path;
55+
file.write(data.data(), data.size());
56+
ABSL_CHECK(file.good());
57+
file.flush();
58+
ABSL_CHECK(file.good());
59+
}
60+
61+
} // namespace
62+
63+
int main(int argc, char** argv) {
64+
{
65+
auto args = absl::ParseCommandLine(argc, argv);
66+
ABSL_CHECK(args.empty() || args.size() == 1)
67+
<< "unexpected positional args: " << absl::StrJoin(args, ", ");
68+
}
69+
absl::InitializeLog();
70+
71+
auto in_buffer = ReadFile(absl::GetFlag(FLAGS_in));
72+
std::string out_buffer;
73+
out_buffer.reserve(in_buffer.size() * 6);
74+
for (const auto& in_byte : in_buffer) {
75+
absl::StrAppend(&out_buffer, "0x",
76+
absl::Hex(in_byte, absl::PadSpec::kZeroPad2), ", ");
77+
}
78+
if (!in_buffer.empty()) {
79+
// Replace last space with newline.
80+
out_buffer.back() = '\n';
81+
}
82+
WriteFile(absl::GetFlag(FLAGS_out), out_buffer);
83+
84+
return EXIT_SUCCESS;
85+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Provides the `cel_proto_transitive_descriptor_set` build rule.
17+
"""
18+
19+
load("@com_google_protobuf//bazel/common:proto_info.bzl", "ProtoInfo")
20+
21+
def _cel_proto_transitive_descriptor_set(ctx):
22+
output = ctx.actions.declare_file(ctx.attr.name + ".binarypb")
23+
transitive_descriptor_sets = depset(transitive = [dep[ProtoInfo].transitive_descriptor_sets for dep in ctx.attr.deps])
24+
args = ctx.actions.args()
25+
args.use_param_file(param_file_arg = "%s", use_always = True)
26+
args.add_all(transitive_descriptor_sets)
27+
ctx.actions.run_shell(
28+
outputs = [output],
29+
inputs = transitive_descriptor_sets,
30+
progress_message = "Joining descriptors.",
31+
command = ("< \"$1\" xargs cat >{output}".format(output = output.path)),
32+
arguments = [args],
33+
)
34+
return DefaultInfo(
35+
files = depset([output]),
36+
runfiles = ctx.runfiles(files = [output]),
37+
)
38+
39+
cel_proto_transitive_descriptor_set = rule(
40+
attrs = {
41+
"deps": attr.label_list(providers = [[ProtoInfo]]),
42+
},
43+
outputs = {
44+
"out": "%{name}.binarypb",
45+
},
46+
implementation = _cel_proto_transitive_descriptor_set,
47+
)

internal/BUILD

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("//bazel:cel_cc_embed.bzl", "cel_cc_embed")
16+
load("//bazel:cel_proto_transitive_descriptor_set.bzl", "cel_proto_transitive_descriptor_set")
17+
1518
package(default_visibility = ["//visibility:public"])
1619

1720
licenses(["notice"])
@@ -517,3 +520,91 @@ cc_test(
517520
":to_address",
518521
],
519522
)
523+
524+
cel_proto_transitive_descriptor_set(
525+
name = "minimal_descriptor_set",
526+
deps = [
527+
"@com_google_protobuf//:any_proto",
528+
"@com_google_protobuf//:duration_proto",
529+
"@com_google_protobuf//:struct_proto",
530+
"@com_google_protobuf//:timestamp_proto",
531+
"@com_google_protobuf//:wrappers_proto",
532+
],
533+
)
534+
535+
cel_cc_embed(
536+
name = "minimal_descriptor_set_embed",
537+
src = ":minimal_descriptor_set",
538+
)
539+
540+
cc_library(
541+
name = "minimal_descriptor_pool",
542+
srcs = ["minimal_descriptor_pool.cc"],
543+
hdrs = ["minimal_descriptor_pool.h"],
544+
textual_hdrs = [":minimal_descriptor_set_embed"],
545+
deps = [
546+
"@com_google_absl//absl/base:core_headers",
547+
"@com_google_absl//absl/base:nullability",
548+
"@com_google_absl//absl/log:absl_check",
549+
"@com_google_protobuf//:protobuf",
550+
],
551+
)
552+
553+
cc_test(
554+
name = "minimal_descriptor_pool_test",
555+
srcs = ["minimal_descriptor_pool_test.cc"],
556+
deps = [
557+
":minimal_descriptor_pool",
558+
":testing",
559+
"@com_google_protobuf//:protobuf",
560+
],
561+
)
562+
563+
cel_proto_transitive_descriptor_set(
564+
name = "testing_descriptor_set",
565+
testonly = True,
566+
deps = [
567+
"@com_google_cel_spec//proto/test/v1/proto2:test_all_types_proto",
568+
"@com_google_cel_spec//proto/test/v1/proto3:test_all_types_proto",
569+
"@com_google_googleapis//google/api/expr/v1alpha1:checked_proto",
570+
"@com_google_googleapis//google/api/expr/v1alpha1:eval_proto",
571+
"@com_google_googleapis//google/api/expr/v1alpha1:explain_proto",
572+
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto",
573+
"@com_google_googleapis//google/api/expr/v1alpha1:value_proto",
574+
"@com_google_protobuf//:any_proto",
575+
"@com_google_protobuf//:duration_proto",
576+
"@com_google_protobuf//:struct_proto",
577+
"@com_google_protobuf//:timestamp_proto",
578+
"@com_google_protobuf//:wrappers_proto",
579+
],
580+
)
581+
582+
cel_cc_embed(
583+
name = "testing_descriptor_set_embed",
584+
testonly = True,
585+
src = ":testing_descriptor_set",
586+
)
587+
588+
cc_library(
589+
name = "testing_descriptor_pool",
590+
testonly = True,
591+
srcs = ["testing_descriptor_pool.cc"],
592+
hdrs = ["testing_descriptor_pool.h"],
593+
textual_hdrs = [":testing_descriptor_set_embed"],
594+
deps = [
595+
"@com_google_absl//absl/base:core_headers",
596+
"@com_google_absl//absl/base:nullability",
597+
"@com_google_absl//absl/log:absl_check",
598+
"@com_google_protobuf//:protobuf",
599+
],
600+
)
601+
602+
cc_test(
603+
name = "testing_descriptor_pool_test",
604+
srcs = ["testing_descriptor_pool_test.cc"],
605+
deps = [
606+
":testing",
607+
":testing_descriptor_pool",
608+
"@com_google_protobuf//:protobuf",
609+
],
610+
)

internal/minimal_descriptor_pool.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "internal/minimal_descriptor_pool.h"
16+
17+
#include <cstdint>
18+
19+
#include "google/protobuf/descriptor.pb.h"
20+
#include "absl/base/attributes.h"
21+
#include "absl/base/macros.h"
22+
#include "absl/base/nullability.h"
23+
#include "absl/log/absl_check.h"
24+
#include "google/protobuf/descriptor.h"
25+
26+
namespace cel::internal {
27+
28+
namespace {
29+
30+
ABSL_CONST_INIT const uint8_t kMinimalDescriptorSet[] = {
31+
#include "internal/minimal_descriptor_set_embed.inc"
32+
};
33+
34+
} // namespace
35+
36+
absl::Nonnull<const google::protobuf::DescriptorPool*> GetMinimalDescriptorPool() {
37+
static absl::Nonnull<const google::protobuf::DescriptorPool* const> pool = []() {
38+
google::protobuf::FileDescriptorSet file_desc_set;
39+
ABSL_CHECK(file_desc_set.ParseFromArray( // Crash OK
40+
kMinimalDescriptorSet, ABSL_ARRAYSIZE(kMinimalDescriptorSet)));
41+
auto* pool = new google::protobuf::DescriptorPool();
42+
for (const auto& file_desc : file_desc_set.file()) {
43+
ABSL_CHECK(pool->BuildFile(file_desc) != nullptr); // Crash OK
44+
}
45+
return pool;
46+
}();
47+
return pool;
48+
}
49+
50+
} // namespace cel::internal

internal/minimal_descriptor_pool.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef THIRD_PARTY_CEL_CPP_INTERNAL_MINIMAL_DESCRIPTOR_POOL_H_
16+
#define THIRD_PARTY_CEL_CPP_INTERNAL_MINIMAL_DESCRIPTOR_POOL_H_
17+
18+
#include "absl/base/nullability.h"
19+
#include "google/protobuf/descriptor.h"
20+
21+
namespace cel::internal {
22+
23+
// GetMinimalDescriptorPool returns a pointer to a `google::protobuf::DescriptorPool`
24+
// which includes has the minimally necessary descriptors required by the Common
25+
// Expression Language. The returning `google::protobuf::DescriptorPool` is valid for the
26+
// lifetime of the process.
27+
//
28+
// This descriptor pool can be used as an underlay for another descriptor pool:
29+
//
30+
// google::protobuf::DescriptorPool my_descriptor_pool(GetMinimalDescriptorPool());
31+
absl::Nonnull<const google::protobuf::DescriptorPool*> GetMinimalDescriptorPool();
32+
33+
} // namespace cel::internal
34+
35+
#endif // THIRD_PARTY_CEL_CPP_INTERNAL_MINIMAL_DESCRIPTOR_POOL_H_

0 commit comments

Comments
 (0)