| 1 |
// Copyright (c) 2009-2022, Google LLC |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are met: |
| 6 |
// * Redistributions of source code must retain the above copyright |
| 7 |
// notice, this list of conditions and the following disclaimer. |
| 8 |
// * Redistributions in binary form must reproduce the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer in the |
| 10 |
// documentation and/or other materials provided with the distribution. |
| 11 |
// * Neither the name of Google LLC nor the |
| 12 |
// names of its contributors may be used to endorse or promote products |
| 13 |
// derived from this software without specific prior written permission. |
| 14 |
// |
| 15 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
// ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, |
| 19 |
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 |
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 |
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 |
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
|
| 26 |
#include <assert.h> |
| 27 |
|
| 28 |
#include <iostream> |
| 29 |
#include <string> |
| 30 |
|
| 31 |
#include "google/protobuf/compiler/plugin.upb.h" |
| 32 |
#include "google/protobuf/compiler/plugin.upbdefs.h" |
| 33 |
#include "upb/json_decode.h" |
| 34 |
#include "upb/json_encode.h" |
| 35 |
#include "upb/mini_descriptor.h" |
| 36 |
#include "upb/upb.h" |
| 37 |
#include "upbc/code_generator_request.h" |
| 38 |
#include "upbc/code_generator_request.upb.h" |
| 39 |
#include "upbc/code_generator_request.upbdefs.h" |
| 40 |
#include "upbc/subprocess.h" |
| 41 |
|
| 42 |
static constexpr char kDefaultPlugin[] = "protoc_dart_plugin"; |
| 43 |
|
| 44 |
static std::string JsonEncode(const upbc_CodeGeneratorRequest* request, |
| 45 |
upb_Arena* a) { |
| 46 |
upb_DefPool* s = upb_DefPool_New(); |
| 47 |
const upb_MessageDef* m = upbc_CodeGeneratorRequest_getmsgdef(s); |
| 48 |
|
| 49 |
upb_Status status; |
| 50 |
upb_Status_Clear(&status); |
| 51 |
|
| 52 |
const size_t json_size = upb_JsonEncode(request, m, s, 0, NULL, 0, &status); |
| 53 |
assert(upb_Status_IsOk(&status)); |
| 54 |
|
| 55 |
char* json_buf = (char*)upb_Arena_Malloc(a, json_size + 1); |
| 56 |
|
| 57 |
(void)upb_JsonEncode(request, m, s, 0, json_buf, json_size + 1, &status); |
| 58 |
assert(upb_Status_IsOk(&status)); |
| 59 |
|
| 60 |
upb_DefPool_Free(s); |
| 61 |
|
| 62 |
return std::string(json_buf, json_size); |
| 63 |
} |
| 64 |
|
| 65 |
static google_protobuf_compiler_CodeGeneratorResponse* JsonDecode( |
| 66 |
const std::string& json, upb_Arena* a) { |
| 67 |
google_protobuf_compiler_CodeGeneratorResponse* response = |
| 68 |
google_protobuf_compiler_CodeGeneratorResponse_new(a); |
| 69 |
|
| 70 |
upb_DefPool* s = upb_DefPool_New(); |
| 71 |
const upb_MessageDef* m = google_protobuf_compiler_CodeGeneratorResponse_getmsgdef(s); |
| 72 |
|
| 73 |
upb_Status status; |
| 74 |
upb_Status_Clear(&status); |
| 75 |
|
| 76 |
(void)upb_JsonDecode(json.c_str(), json.size(), response, m, s, 0, a, |
| 77 |
&status); |
| 78 |
assert(upb_Status_IsOk(&status)); |
| 79 |
|
| 80 |
upb_DefPool_Free(s); |
| 81 |
|
| 82 |
return response; |
| 83 |
} |
| 84 |
|
| 85 |
static std::string Serialize( |
| 86 |
const google_protobuf_compiler_CodeGeneratorResponse* response, upb_Arena* a) { |
| 87 |
size_t len = 0; |
| 88 |
const char* buf = |
| 89 |
google_protobuf_compiler_CodeGeneratorResponse_serialize(response, a, &len); |
| 90 |
return std::string(buf, len); |
| 91 |
} |
| 92 |
|
| 93 |
int main() { |
| 94 |
upb_Arena* a = upb_Arena_New(); |
| 95 |
|
| 96 |
// Read (binary) stdin into a string. |
| 97 |
const std::string input = {std::istreambuf_iterator<char>(std::cin), |
| 98 |
std::istreambuf_iterator<char>()}; |
| 99 |
|
| 100 |
// Parse the request. |
| 101 |
auto inner_request = google_protobuf_compiler_CodeGeneratorRequest_parse( |
| 102 |
input.c_str(), input.size(), a); |
| 103 |
|
| 104 |
// Check the request for a plugin name. |
| 105 |
std::string plugin = kDefaultPlugin; |
| 106 |
if (google_protobuf_compiler_CodeGeneratorRequest_has_parameter(inner_request)) { |
| 107 |
auto param = google_protobuf_compiler_CodeGeneratorRequest_parameter(inner_request); |
| 108 |
plugin = std::string(param.data, param.size); |
| 109 |
} |
| 110 |
|
| 111 |
// Wrap the request inside a upbc_CodeGeneratorRequest. |
| 112 |
upb_Status status; |
| 113 |
upb_Status_Clear(&status); |
| 114 |
auto outer_request = upbc_MakeCodeGeneratorRequest(inner_request, a, &status); |
| 115 |
if (!upb_Status_IsOk(&status)) { |
| 116 |
std::cerr << status.msg << std::endl; |
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
const std::string json_request = JsonEncode(outer_request, a); |
| 121 |
|
| 122 |
// Launch the subprocess. |
| 123 |
upbc::Subprocess subprocess; |
| 124 |
subprocess.Start(plugin, upbc::Subprocess::SEARCH_PATH); |
| 125 |
|
| 126 |
// Exchange JSON strings with the subprocess. |
| 127 |
std::string json_response, error; |
| 128 |
const bool ok = subprocess.Communicate(json_request, &json_response, &error); |
| 129 |
if (!ok) { |
| 130 |
// Dump the JSON request to stderr if we can't launch the next plugin. |
| 131 |
std::cerr << json_request << std::endl; |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
// Decode and serialize the JSON response. |
| 136 |
const auto response = JsonDecode(json_response, a); |
| 137 |
const std::string output = Serialize(response, a); |
| 138 |
|
| 139 |
// Question: Is this sufficient for sending reliably to stdout? |
| 140 |
std::cout << output; |
| 141 |
|
| 142 |
upb_Arena_Free(a); |
| 143 |
return 0; |
| 144 |
} |