From 89d6b74dea804995488980c44d3f53855e926d69 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 8 Feb 2023 09:05:07 -0800 Subject: [PATCH] Create skeleton for a Rust backend. This code is experimental and should not be expected to emit working code, and callers are liable to break without warning. It is being released now so that development can occur in the open, but users should not expect this to be supported any time soon. PiperOrigin-RevId: 508095929 --- WORKSPACE | 10 +++ rust/BUILD.bazel | 10 +++ rust/lib.rs | 33 +++++++++ src/google/protobuf/compiler/rust/BUILD.bazel | 22 ++++++ src/google/protobuf/compiler/rust/generator.h | 72 +++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 rust/BUILD.bazel create mode 100644 rust/lib.rs create mode 100644 src/google/protobuf/compiler/rust/BUILD.bazel create mode 100644 src/google/protobuf/compiler/rust/generator.h diff --git a/WORKSPACE b/WORKSPACE index e68cd88b25..2301093274 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -127,3 +127,13 @@ bind( name = "python_headers", actual = "@system_python//:python_headers", ) + +http_archive( + name = "rules_rust", + sha256 = "d125fb75432dc3b20e9b5a19347b45ec607fabe75f98c6c4ba9badaab9c193ce", + urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.17.0/rules_rust-v0.17.0.tar.gz"], +) + +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") +rules_rust_dependencies() +rust_register_toolchains() diff --git a/rust/BUILD.bazel b/rust/BUILD.bazel new file mode 100644 index 0000000000..c8dbca76ec --- /dev/null +++ b/rust/BUILD.bazel @@ -0,0 +1,10 @@ +# Protobuf Rust runtime packages. + +load("@rules_rust//:defs.bzl", "rust_library") + +package(default_visibility = "//src/google/protobuf:__subpackages__") + +rust_library( + name = "protobuf", + srcs = ["lib.rs"], +) diff --git a/rust/lib.rs b/rust/lib.rs new file mode 100644 index 0000000000..8974efae3b --- /dev/null +++ b/rust/lib.rs @@ -0,0 +1,33 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +//! Rust Protobuf Runtime + +// Not yet implemented. diff --git a/src/google/protobuf/compiler/rust/BUILD.bazel b/src/google/protobuf/compiler/rust/BUILD.bazel new file mode 100644 index 0000000000..2a26dd5b62 --- /dev/null +++ b/src/google/protobuf/compiler/rust/BUILD.bazel @@ -0,0 +1,22 @@ +################################################################################ +# Protocol Buffers Compiler - Rust code generator +################################################################################ + +load("@rules_cc//cc:defs.bzl", "cc_library") +load("//build_defs:cpp_opts.bzl", "COPTS") + +cc_library( + name = "rust", + hdrs = ["generator.h"], + copts = COPTS, + include_prefix = "google/protobuf/compiler/rust", + visibility = [ + "//pkg:__pkg__", + "//src/google/protobuf/compiler:__pkg__", + ], + deps = [ + "//src/google/protobuf:protobuf_nowkt", + "//src/google/protobuf/compiler:code_generator", + "@com_google_absl//absl/log:absl_check", + ], +) diff --git a/src/google/protobuf/compiler/rust/generator.h b/src/google/protobuf/compiler/rust/generator.h new file mode 100644 index 0000000000..a01c6aead3 --- /dev/null +++ b/src/google/protobuf/compiler/rust/generator.h @@ -0,0 +1,72 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef GOOGLE_PROTOBUF_COMPILER_RUST_GENERATOR_H__ +#define GOOGLE_PROTOBUF_COMPILER_RUST_GENERATOR_H__ + +#include + +#include "absl/log/absl_check.h" +#include "google/protobuf/compiler/code_generator.h" + +// Must be included last. +#include "google/protobuf/port_def.inc" + +namespace google { +namespace protobuf { +namespace compiler { +namespace rust { + +class RustGenerator final : public google::protobuf::compiler::CodeGenerator { + public: + RustGenerator() = default; + RustGenerator(const RustGenerator&) = delete; + RustGenerator& operator=(const RustGenerator&) = delete; + ~RustGenerator() override = default; + + bool Generate(const FileDescriptor* file, const std::string& parameter, + GeneratorContext* generator_context, + std::string* error) const override { + ABSL_CHECK(false) << "not yet implemented"; + return false; + } + + uint64_t GetSupportedFeatures() const override { + return FEATURE_PROTO3_OPTIONAL; + } +}; + +} // namespace rust +} // namespace compiler +} // namespace protobuf +} // namespace google + +#include "google/protobuf/port_undef.inc" +#endif // GOOGLE_PROTOBUF_COMPILER_RUST_GENERATOR_H__