diff --git a/tools/streaming_proto/Android.bp b/tools/streaming_proto/Android.bp index 24068e9ffe92f..756549c5e880c 100644 --- a/tools/streaming_proto/Android.bp +++ b/tools/streaming_proto/Android.bp @@ -17,13 +17,31 @@ // ========================================================== // Build the host executable: protoc-gen-javastream // ========================================================== -cc_binary_host { - name: "protoc-gen-javastream", +cc_defaults { + name: "protoc-gen-stream-defaults", srcs: [ "Errors.cpp", "string_utils.cpp", - "main.cpp", + ], +} + + +cc_binary_host { + name: "protoc-gen-javastream", + srcs: [ + "java/main.cpp", ], + defaults: ["protoc-gen-stream-defaults"], + shared_libs: ["libprotoc"], +} + +cc_binary_host { + name: "protoc-gen-cppstream", + srcs: [ + "cpp/main.cpp", + ], + + defaults: ["protoc-gen-stream-defaults"], shared_libs: ["libprotoc"], } diff --git a/tools/streaming_proto/Errors.cpp b/tools/streaming_proto/Errors.cpp index 91c6b9245de09..0cd9037dcb555 100644 --- a/tools/streaming_proto/Errors.cpp +++ b/tools/streaming_proto/Errors.cpp @@ -3,7 +3,7 @@ #include namespace android { -namespace javastream_proto { +namespace stream_proto { Errors ERRORS; @@ -82,6 +82,6 @@ Errors::HasErrors() const return m_errors.size() > 0; } -} // namespace javastream_proto +} // namespace stream_proto } // namespace android diff --git a/tools/streaming_proto/Errors.h b/tools/streaming_proto/Errors.h index 109195a20b060..f14bbfd55b5fd 100644 --- a/tools/streaming_proto/Errors.h +++ b/tools/streaming_proto/Errors.h @@ -4,7 +4,7 @@ #include namespace android { -namespace javastream_proto { +namespace stream_proto { using namespace std; @@ -44,5 +44,5 @@ extern const string UNKNOWN_FILE; extern const int UNKNOWN_LINE; -} // namespace javastream_proto +} // namespace stream_proto } // namespace android diff --git a/tools/streaming_proto/cpp/main.cpp b/tools/streaming_proto/cpp/main.cpp new file mode 100644 index 0000000000000..d4e1b7aede92f --- /dev/null +++ b/tools/streaming_proto/cpp/main.cpp @@ -0,0 +1,273 @@ +#include "Errors.h" +#include "string_utils.h" + +#include "google/protobuf/compiler/plugin.pb.h" +#include "google/protobuf/io/zero_copy_stream_impl.h" +#include "google/protobuf/text_format.h" + +#include +#include +#include + +using namespace android::stream_proto; +using namespace google::protobuf; +using namespace google::protobuf::compiler; +using namespace google::protobuf::io; +using namespace std; + +/** + * Position of the field type in a (long long) fieldId. + */ +const uint64_t FIELD_TYPE_SHIFT = 32; + +// +// FieldId flags for whether the field is single, repeated or packed. +// TODO: packed is not supported yet. +// +const uint64_t FIELD_COUNT_SHIFT = 40; +const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT; +const uint64_t FIELD_COUNT_UNKNOWN = 0; +const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT; +const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT; +const uint64_t FIELD_COUNT_PACKED = 4ULL << FIELD_COUNT_SHIFT; + +// Indent +const string INDENT = " "; + +/** + * See if this is the file for this request, and not one of the imported ones. + */ +static bool +should_generate_for_file(const CodeGeneratorRequest& request, const string& file) +{ + const int N = request.file_to_generate_size(); + for (int i=0; i"; + case FieldDescriptorProto::TYPE_MESSAGE: + return field.type_name(); + case FieldDescriptorProto::TYPE_BYTES: + return "bytes"; + case FieldDescriptorProto::TYPE_UINT32: + return "uint32"; + case FieldDescriptorProto::TYPE_ENUM: + return field.type_name(); + case FieldDescriptorProto::TYPE_SFIXED32: + return "sfixed32"; + case FieldDescriptorProto::TYPE_SFIXED64: + return "sfixed64"; + case FieldDescriptorProto::TYPE_SINT32: + return "sint32"; + case FieldDescriptorProto::TYPE_SINT64: + return "sint64"; + default: + // won't happen + return "void"; + } +} + +static void +write_enum(stringstream& text, const EnumDescriptorProto& enu, const string& indent) +{ + const int N = enu.value_size(); + text << indent << "// enum " << enu.name() << endl; + for (int i=0; i namespaces = split(file_descriptor.package(), '.'); + for (vector::iterator it = namespaces.begin(); it != namespaces.end(); it++) { + text << "namespace " << *it << " {" << endl; + } + text << endl; + + size_t N; + N = file_descriptor.enum_type_size(); + for (size_t i=0; i::iterator it = namespaces.begin(); it != namespaces.end(); it++) { + text << "} // " << *it << endl; + } + + text << endl; + text << "#endif // " << header << endl; + + CodeGeneratorResponse::File* file_response = response->add_file(); + file_response->set_name(make_filename(file_descriptor)); + file_response->set_content(text.str()); +} + +int main(int argc, char const *argv[]) +{ + (void)argc; + (void)argv; + + GOOGLE_PROTOBUF_VERIFY_VERSION; + + CodeGeneratorRequest request; + CodeGeneratorResponse response; + + // Read the request + request.ParseFromIstream(&cin); + + // Build the files we need. + const int N = request.proto_file_size(); + for (int i=0; i #include -using namespace android::javastream_proto; +using namespace android::stream_proto; using namespace google::protobuf; using namespace google::protobuf::compiler; using namespace google::protobuf::io; diff --git a/tools/streaming_proto/string_utils.cpp b/tools/streaming_proto/string_utils.cpp index cc738c4c108e5..bd34ab7aa44d7 100644 --- a/tools/streaming_proto/string_utils.cpp +++ b/tools/streaming_proto/string_utils.cpp @@ -3,7 +3,7 @@ #include namespace android { -namespace javastream_proto { +namespace stream_proto { using namespace std; @@ -89,7 +89,26 @@ replace_string(const string& str, const char replace, const char with) return result; } -} // namespace javastream_proto +vector +split(const string& str, const char delimiter) +{ + vector result; + size_t base = 0, found = 0; + while (true) { + found = str.find_first_of(delimiter, base); + if (found != base) { + string part = str.substr(base, found - base); + if (!part.empty()) { + result.push_back(part); + } + } + if (found == str.npos) break; + base = found + 1; + } + return result; +} + +} // namespace stream_proto } // namespace android diff --git a/tools/streaming_proto/string_utils.h b/tools/streaming_proto/string_utils.h index ffe83ca997041..03284d16e1be1 100644 --- a/tools/streaming_proto/string_utils.h +++ b/tools/streaming_proto/string_utils.h @@ -1,7 +1,8 @@ #include +#include namespace android { -namespace javastream_proto { +namespace stream_proto { using namespace std; @@ -26,7 +27,11 @@ string file_base_name(const string& str); */ string replace_string(const string& str, const char replace, const char with); +/** + * Split a string to parts by delimiter. + */ +vector split(const string& str, const char delimiter); -} // namespace javastream_proto +} // namespace stream_proto } // namespace android