Commit ebea797d authored by tqcq's avatar tqcq
Browse files

feat add code generator

parent fdbce08c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ endif()
option(protobuf_BUILD_TESTS "Build tests" OFF)
option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" OFF)
option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries"
       OFF)
+6 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ option(SLED_BUILD_BENCHMARK "Build benchmark" OFF)
option(SLED_BUILD_TESTS "Build tests" OFF)
option(SLED_BUILD_FUZZ "Build fuzzer test" OFF)
option(SLED_LOCATION_PATH "" "sled/src/sled/system/location.cc")
option(SLED_BUILD_PROTOC_PLUGIN "Build protoc plugin" OFF)

set(BUILD_STATIC ON)
set(BUILD_RTTR_DYNAMIC OFF)
@@ -97,6 +98,7 @@ target_sources(
target_link_libraries(
  sled
  PUBLIC rpc_core fmt marl Async++ minilua protobuf::libprotobuf
         # protobuf::libprotoc
  PRIVATE dl
  # protobuf::libprotobuf ${WHOLE_ARCHIVE_WRAPPER_START}
  # tcmalloc_and_profiler_static ${WHOLE_ARCHIVE_WRAPPER_END}
@@ -105,6 +107,10 @@ target_link_libraries(
# set fPIC
set_target_properties(sled PROPERTIES POSITION_INDEPENDENT_CODE ON)

if(SLED_BUILD_PROTOC_PLUGIN)
  add_subdirectory(src/protoc_gen_sled)
endif()

if(SLED_BUILD_BENCHMARK)
  # if(NOT TARGET benchmark) find_package(mbenchmark REQUIRED) endif()

+7 −0
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.10)

project(protoc-gen-sled LANGUAGES C CXX)
add_executable(${PROJECT_NAME} main.cc sled_generator.cc)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE libprotoc sled)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
+17 −0
Original line number Diff line number Diff line
#include "sled_generator.h"
#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/cpp/cpp_generator.h>
#include <google/protobuf/compiler/plugin.h>

int
main(int argc, char *argv[])
{
    google::protobuf::compiler::CommandLineInterface cli;
    // google::protobuf::compiler::cpp::CppGenerator cpp_generator;
    // cli.RegisterGenerator("--cpp_out", &cpp_generator, "Generate C++ source code.");

    sled::SledGenerator sled_generator;
    cli.RegisterGenerator("--sled_out", &sled_generator, "sled cpp rpc generator");

    return google::protobuf::compiler::PluginMain(argc, argv, &sled_generator);
}
+54 −0
Original line number Diff line number Diff line
#include "sled_generator.h"
#include <google/protobuf/compiler/plugin.pb.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <sled/sled.h>
using namespace google::protobuf;
using namespace google::protobuf::compiler;
using namespace google::protobuf::io;

namespace sled {

void
PrintMessage(Printer &printer, const Descriptor *msg)
{
    printer.Print("- $name$\n", "name", msg->name());
    printer.Indent();
    for (int i = 0; i < msg->nested_type_count(); ++i) { PrintMessage(printer, msg->nested_type(i)); }
    printer.Outdent();
}

void
PrintService(Printer &printer, const ServiceDescriptor *service)
{
    inja::json data;
    data["classname"] = service->name();
    data["full_name"] = service->full_name();

    printer.Print("- $name$\n", "name", service->full_name());
    printer.Indent();
    for (int i = 0; i < service->method_count(); i++) {
        const MethodDescriptor *method = service->method(i);
        printer.Print("- $name$\n", "name", method->name());
        printer.Indent();
        printer.Print("request: $name$ $file$\n", "name", method->input_type()->name(), "file", method->file()->name());
        printer
            .Print("response: $name$ $file$\n", "name", method->output_type()->name(), "file", method->file()->name());
        printer.Outdent();
    }
    printer.Outdent();
}

bool
SledGenerator::Generate(const FileDescriptor *file,
                        const std::string &parameter,
                        GeneratorContext *generator_context,
                        std::string *error) const
{
    auto *out_stream = generator_context->Open(file->name() + ".txt");
    Printer printer(out_stream, '$');
    for (int i = 0; i < file->message_type_count(); i++) { PrintMessage(printer, file->message_type(i)); }
    for (int i = 0; i < file->service_count(); i++) { PrintService(printer, file->service(i)); }
    return true;
}
}// namespace sled
Loading