diff options
author | Yi Jin <jinyithu@google.com> | 2017-10-17 18:29:33 -0700 |
---|---|---|
committer | Yi Jin <jinyithu@google.com> | 2017-10-31 16:54:38 -0700 |
commit | 04625ad4886a478bf74bbfc13937c10fa63eb272 (patch) | |
tree | ba39988adb170b2e6e744ab304de7f8846a4d444 /tools/streaming_proto | |
parent | eb7d335641ec1b9c91c1609a94cb1cbdba1d3987 (diff) |
Refactor incident_helper to use protoutil and cppstream plugin.
1. Split the parsers to its own file to prevent all the parsers in one
gaint file.
2. Completely get rid of protobuf-cpp-full in incident_helper, use
ProtoOutputStream and cppstream instead, the incident_helper binary is
reduced from ~500K to ~113K.
3. Write data to protobuf even its values are zero/default, the reason
is for example we have a repeated int32 orders = 1; and people
explicitly append 0 so the total repeated field has 10 values, if zero
is not written to serialized data, this repeated field will only have 9
values which is not what we want at first place. This also aligns with
the default protobuf serialization behavior in incident_helper_test.
4. Use Android.bp for protoutil lib since it is not able to depend on
libs compiled by .mk file, it works the other way.
5. Add a new custom message option for streaming_proto, if specified,
the cppstream will create extra metadata to get field ids by field name.
A Table class is created in incident_helper to use it.
Bug: 67860303
Test: unit tested as well as on device test
Change-Id: I8e136fd15f343a4a623d20910ec64b622b478a3e
Diffstat (limited to 'tools/streaming_proto')
-rw-r--r-- | tools/streaming_proto/Android.bp | 20 | ||||
-rw-r--r-- | tools/streaming_proto/cpp/main.cpp | 34 | ||||
-rw-r--r-- | tools/streaming_proto/stream.proto | 32 |
3 files changed, 80 insertions, 6 deletions
diff --git a/tools/streaming_proto/Android.bp b/tools/streaming_proto/Android.bp index 756549c5e880..96e060d0fc26 100644 --- a/tools/streaming_proto/Android.bp +++ b/tools/streaming_proto/Android.bp @@ -25,6 +25,25 @@ cc_defaults { ], } +cc_library { + name: "streamingflags", + host_supported: true, + proto: { + export_proto_headers: true, + include_dirs: ["external/protobuf/src"], + }, + + target: { + host: { + proto: { + type: "full", + }, + srcs: [ + "stream.proto", + ], + }, + }, +} cc_binary_host { name: "protoc-gen-javastream", @@ -44,4 +63,5 @@ cc_binary_host { defaults: ["protoc-gen-stream-defaults"], shared_libs: ["libprotoc"], + static_libs: ["streamingflags"], } diff --git a/tools/streaming_proto/cpp/main.cpp b/tools/streaming_proto/cpp/main.cpp index d4e1b7aede92..dc96d5c54c16 100644 --- a/tools/streaming_proto/cpp/main.cpp +++ b/tools/streaming_proto/cpp/main.cpp @@ -1,6 +1,8 @@ #include "Errors.h" #include "string_utils.h" +#include <frameworks/base/tools/streaming_proto/stream.pb.h> + #include "google/protobuf/compiler/plugin.pb.h" #include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/text_format.h" @@ -160,6 +162,12 @@ write_field(stringstream& text, const FieldDescriptorProto& field, const string& text << endl; } +static inline bool +should_generate_fields_mapping(const DescriptorProto& message) +{ + return message.options().GetExtension(stream).enable_fields_mapping(); +} + static void write_message(stringstream& text, const DescriptorProto& message, const string& indent) { @@ -167,8 +175,7 @@ write_message(stringstream& text, const DescriptorProto& message, const string& const string indented = indent + INDENT; text << indent << "// message " << message.name() << endl; - text << indent << "class " << message.name() << " {" << endl; - text << indent << "public:" << endl; + text << indent << "namespace " << message.name() << " {" << endl; // Enums N = message.enum_type_size(); @@ -188,12 +195,27 @@ write_message(stringstream& text, const DescriptorProto& message, const string& write_field(text, message.field(i), indented); } - text << indent << "};" << endl; + if (should_generate_fields_mapping(message)) { + N = message.field_size(); + text << indented << "const int _FIELD_COUNT = " << N << ";" << endl; + text << indented << "const char* _FIELD_NAMES[" << N << "] = {" << endl; + for (int i=0; i<N; i++) { + text << indented << INDENT << "\"" << message.field(i).name() << "\"," << endl; + } + text << indented << "};" << endl; + text << indented << "const uint64_t _FIELD_IDS[" << N << "] = {" << endl; + for (int i=0; i<N; i++) { + text << indented << INDENT << make_constant_name(message.field(i).name()) << "," << endl; + } + text << indented << "};" << endl << endl; + } + + text << indent << "} //" << message.name() << endl; text << endl; } static void -write_cpp_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor) +write_header_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor) { stringstream text; @@ -255,7 +277,7 @@ int main(int argc, char const *argv[]) for (int i=0; i<N; i++) { const FileDescriptorProto& file_descriptor = request.proto_file(i); if (should_generate_for_file(request, file_descriptor.name())) { - write_cpp_file(&response, file_descriptor); + write_header_file(&response, file_descriptor); } } @@ -270,4 +292,4 @@ int main(int argc, char const *argv[]) /* code */ return 0; -}
\ No newline at end of file +} diff --git a/tools/streaming_proto/stream.proto b/tools/streaming_proto/stream.proto new file mode 100644 index 000000000000..123506c03cfd --- /dev/null +++ b/tools/streaming_proto/stream.proto @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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. + */ + +syntax = "proto2"; + +import "google/protobuf/descriptor.proto"; + +package android.stream_proto; + +// This option tells streaming proto plugin to compile .proto files with extra features. +message StreamFlags { + // creates a mapping of field names of the message to its field ids + optional bool enable_fields_mapping = 1; +} + +extend google.protobuf.MessageOptions { + // Flags used by streaming proto plugins + optional StreamFlags stream = 126856794; +} |