Commit 89c7bc1f authored by kraynov's avatar kraynov Committed by Commit bot

Skeleton for ProtoZero plugin.

ProtoZero plugin is a custom C++ stubs generator for protobuf compiler.
These stubs are Tracing V2 specific and meet perfomance and memory
requirements being less functional than conventional ones.
This CL has no complete implementation (see crrev.com/2083373002) but
contains all invasive changes to build files.
See crrev.com/2082693002 for background about protobuf plugin support.

BUG=608721

Review-Url: https://codereview.chromium.org/2147613002
Cr-Commit-Position: refs/heads/master@{#405281}
parent 8ca0f9d3
...@@ -877,6 +877,7 @@ ...@@ -877,6 +877,7 @@
'tracing/core/trace_ring_buffer_unittest.cc', 'tracing/core/trace_ring_buffer_unittest.cc',
'tracing/core/scattered_stream_writer_unittest.cc', 'tracing/core/scattered_stream_writer_unittest.cc',
'tracing/test/fake_scattered_buffer.cc', 'tracing/test/fake_scattered_buffer.cc',
'tracing/test/proto_zero_generation_unittest.cc',
], ],
'translate_unittest_sources': [ 'translate_unittest_sources': [
'translate/core/browser/language_state_unittest.cc', 'translate/core/browser/language_state_unittest.cc',
...@@ -1382,6 +1383,7 @@ ...@@ -1382,6 +1383,7 @@
'components.gyp:zoom', 'components.gyp:zoom',
'scheduler/scheduler.gyp:scheduler', 'scheduler/scheduler.gyp:scheduler',
'test_runner/test_runner.gyp:test_runner', 'test_runner/test_runner.gyp:test_runner',
'tracing.gyp:proto_zero_testing_messages',
'tracing.gyp:tracing', 'tracing.gyp:tracing',
'webcrypto/webcrypto.gyp:webcrypto', 'webcrypto/webcrypto.gyp:webcrypto',
'../third_party/boringssl/boringssl.gyp:boringssl', '../third_party/boringssl/boringssl.gyp:boringssl',
......
...@@ -57,5 +57,41 @@ ...@@ -57,5 +57,41 @@
}], }],
] ]
}, },
{
'target_name': 'proto_zero_plugin',
'type': 'executable',
'toolsets': ['host'],
'sources': [
'tracing/tools/proto_zero_plugin/proto_zero_generator.cc',
'tracing/tools/proto_zero_plugin/proto_zero_generator.h',
'tracing/tools/proto_zero_plugin/proto_zero_plugin.cc',
],
'include_dirs': [
'..',
'../third_party/protobuf/src',
],
'dependencies': [
'../third_party/protobuf/protobuf.gyp:protoc_lib',
],
},
{
'target_name': 'proto_zero_testing_messages',
'type': 'static_library',
'variables': {
'proto_in_dir': 'tracing/test',
'proto_out_dir': 'components/tracing/test',
'generator_plugin': 'proto_zero_plugin',
'generator_plugin_suffix': '.pbzero',
'generate_cc': 0,
'generate_python': 0,
},
'sources': [
'tracing/test/example_messages.proto',
],
'dependencies': [
'proto_zero_plugin',
],
'includes': ['../build/protoc.gypi'],
},
], ],
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import("//third_party/protobuf/proto_library.gni")
component("tracing") { component("tracing") {
sources = [ sources = [
"child/child_memory_dump_manager_delegate_impl.cc", "child/child_memory_dump_manager_delegate_impl.cc",
...@@ -52,6 +54,38 @@ component("startup_tracing") { ...@@ -52,6 +54,38 @@ component("startup_tracing") {
] ]
} }
# For unit testing of generated stubs.
proto_library("proto_zero_testing_messages") {
visibility = [ "//components/tracing/*" ]
sources = [
"test/example_messages.proto",
]
generator_plugin_label = "tools/proto_zero_plugin:proto_zero_plugin"
generator_plugin_suffix = ".pbzero"
generate_cc = false
generate_python = false
# TODO(kraynov) Move away this complex path evaluation and dependency
# injection to proto_library.gni. All lines below should be removed.
# The problem is that root_out_dir and root_build_dir may differ in case of
# cross compilation. Also .exe will be appended on Windows.
plugin_host_label = generator_plugin_label + "($host_toolchain)"
generator_plugin =
rebase_path(get_label_info(plugin_host_label, "root_out_dir") + "/" +
get_label_info(plugin_host_label, "name"),
root_build_dir)
if (is_win) {
generator_plugin += ".exe"
}
deps = [
plugin_host_label,
]
}
source_set("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
...@@ -63,9 +97,11 @@ source_set("unit_tests") { ...@@ -63,9 +97,11 @@ source_set("unit_tests") {
"core/scattered_stream_writer_unittest.cc", "core/scattered_stream_writer_unittest.cc",
"core/trace_ring_buffer_unittest.cc", "core/trace_ring_buffer_unittest.cc",
"test/fake_scattered_buffer.cc", "test/fake_scattered_buffer.cc",
"test/proto_zero_generation_unittest.cc",
] ]
deps = [ deps = [
":proto_zero_testing_messages",
":tracing", ":tracing",
"//base/test:test_support", "//base/test:test_support",
"//ipc", "//ipc",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
syntax = "proto2";
package foo.bar;
// This file contains comprehensive set of supported message structures and
// data types. Unit tests depends on the plugin-processed version of this file.
// TODO(kraynov) Populate in the next CL.
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/tracing/test/example_messages.pbzero.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace tracing {
namespace proto {
TEST(ProtoZeroTest, Simple) {
// TODO(kraynov) Put tests in the next CL (crbug.com/608721).
}
} // namespace proto
} // namespace tracing
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
if (current_toolchain == host_toolchain) {
executable("proto_zero_plugin") {
sources = [
"proto_zero_generator.cc",
"proto_zero_generator.h",
"proto_zero_plugin.cc",
]
deps = [
"//third_party/protobuf:protoc_lib",
]
}
}
include_rules = [
"+third_party/protobuf",
]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "proto_zero_generator.h"
#include <memory>
#include <string>
#include "third_party/protobuf/src/google/protobuf/descriptor.h"
#include "third_party/protobuf/src/google/protobuf/io/printer.h"
#include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h"
#include "third_party/protobuf/src/google/protobuf/stubs/strutil.h"
namespace tracing {
namespace proto {
using google::protobuf::FileDescriptor;
using google::protobuf::StripSuffixString;
using google::protobuf::compiler::GeneratorContext;
using google::protobuf::io::Printer;
using google::protobuf::io::ZeroCopyOutputStream;
namespace {
class GeneratorJob {
public:
GeneratorJob(const FileDescriptor *file,
Printer* stub_h_printer,
Printer* stub_cc_printer)
: file_(file),
stub_h_(stub_h_printer),
stub_cc_(stub_cc_printer) {}
bool GenerateStubs() {
stub_h_->Print(
"// Autogenerated. DO NOT EDIT.\n"
"// Generated by: //components/tracing/proto_zero_plugin.\n\n"
"// Package: $package$\n",
"package", file_->package());
stub_cc_->Print(
"// Autogenerated. DO NOT EDIT.\n"
"// Generated by: //components/tracing/proto_zero_plugin.\n\n"
"// This file intentionally left blank.\n");
// TODO(kraynov) Implement in the next CL (crbug.com/608721).
return true;
}
// If generator fails to produce stubs for a particular proto definitions
// it finishes with undefined output and writes the first error occured.
const std::string& GetFirstError() const {
return error_;
}
private:
// Only the first error will be recorded.
void Abort(const std::string& reason) {
if (error_.empty()) {
error_ = reason;
}
}
const FileDescriptor* const file_;
Printer* const stub_h_;
Printer* const stub_cc_;
std::string error_;
};
} // namespace
ProtoZeroGenerator::ProtoZeroGenerator() {
}
ProtoZeroGenerator::~ProtoZeroGenerator() {
}
bool ProtoZeroGenerator::Generate(const FileDescriptor* file,
const std::string& options,
GeneratorContext* context,
std::string* error) const {
const std::string proto_stubs_name =
StripSuffixString(file->name(), ".proto") + ".pbzero";
const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream(
context->Open(proto_stubs_name + ".h"));
const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream(
context->Open(proto_stubs_name + ".cc"));
// Variables are delimited by $.
Printer stub_h_printer(stub_h_file_stream.get(), '$');
Printer stub_cc_printer(stub_cc_file_stream.get(), '$');
GeneratorJob job(file, &stub_h_printer, &stub_cc_printer);
if (!job.GenerateStubs()) {
*error = job.GetFirstError();
return false;
}
return true;
}
} // namespace proto
} // namespace tracing
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_TRACING_PROTO_ZERO_PLUGIN_PROTO_ZERO_GENERATOR_H_
#define COMPONENTS_TRACING_PROTO_ZERO_PLUGIN_PROTO_ZERO_GENERATOR_H_
#include <string>
#include "third_party/protobuf/src/google/protobuf/compiler/code_generator.h"
namespace tracing {
namespace proto {
class ProtoZeroGenerator : public google::protobuf::compiler::CodeGenerator {
public:
explicit ProtoZeroGenerator();
~ProtoZeroGenerator() override;
// CodeGenerator implementation
bool Generate(const google::protobuf::FileDescriptor* file,
const std::string& options,
google::protobuf::compiler::GeneratorContext* context,
std::string* error) const override;
};
} // namesapce proto
} // namespace tracing
#endif // COMPONENTS_TRACING_PROTO_ZERO_PLUGIN_PROTO_ZERO_GENERATOR_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/protobuf/src/google/protobuf/compiler/plugin.h"
#include "proto_zero_generator.h"
int main(int argc, char* argv[]) {
tracing::proto::ProtoZeroGenerator generator;
return google::protobuf::compiler::PluginMain(argc, argv, &generator);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment