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 <iostream>
27 #include <string>
28 
29 #include "google/protobuf/compiler/plugin.upb.h"
30 #include "upbc/subprocess.h"
31 #include "upbc/upbdev.h"
32 
33 static constexpr char kDefaultPlugin[] = "protoc_dart_plugin";
34 
main()35 int main() {
36   upb_Arena* a = upb_Arena_New();
37   upb_Status status;
38   upb_Status_Clear(&status);
39 
40   // Read (binary) stdin into a string.
41   const std::string input = {std::istreambuf_iterator<char>(std::cin),
42                              std::istreambuf_iterator<char>()};
43 
44   // Parse the request.
45   auto inner_request = google_protobuf_compiler_CodeGeneratorRequest_parse(
46       input.c_str(), input.size(), a);
47 
48   // Check the request for a plugin name.
49   std::string plugin = kDefaultPlugin;
50   if (google_protobuf_compiler_CodeGeneratorRequest_has_parameter(inner_request)) {
51     auto param = google_protobuf_compiler_CodeGeneratorRequest_parameter(inner_request);
52     plugin = std::string(param.data, param.size);
53   }
54 
55   // Wrap the request inside a upbc_CodeGeneratorRequest and JSON-encode it.
56   const upb_StringView sv =
57       upbdev_ProcessInput(input.data(), input.size(), a, &status);
58   if (!upb_Status_IsOk(&status)) {
59     std::cerr << status.msg << std::endl;
60     return -1;
61   }
62 
63   // Launch the subprocess.
64   upbc::Subprocess subprocess;
65   subprocess.Start(plugin, upbc::Subprocess::SEARCH_PATH);
66 
67   // Exchange JSON strings with the subprocess.
68   const std::string json_request = std::string(sv.data, sv.size);
69   std::string json_response, error;
70   const bool ok = subprocess.Communicate(json_request, &json_response, &error);
71   if (!ok) {
72     // Dump the JSON request to stderr if we can't launch the next plugin.
73     std::cerr << json_request << std::endl;
74     return -1;
75   }
76 
77   // Decode, serialize, and write the JSON response.
78   upbdev_ProcessOutput(json_response.data(), json_response.size(), a, &status);
79   if (!upb_Status_IsOk(&status)) {
80     std::cerr << status.msg << std::endl;
81     return -1;
82   }
83 
84   upb_Arena_Free(a);
85   return 0;
86 }
87