-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rust_tonic_compile
rule
#391
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package contains the Ideally, we'd get this rule from an external repo such as We may want to relocate this rule to another repo so it can be used by other Rust proto repos. We may also want to ask the |
||
# Copyright (C) 2022 Vaticle | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
|
||
load("@rules_rust//rust:defs.bzl", "rust_binary") | ||
load("@vaticle_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test") | ||
|
||
rust_binary( | ||
name = "compile", | ||
srcs = ["compile.rs"], | ||
deps = ["@vaticle_dependencies//library/crates:tonic_build"], | ||
visibility = ["//visibility:public"] | ||
) | ||
|
||
checkstyle_test( | ||
name = "checkstyle", | ||
include = glob(["*"]), | ||
license_type = "agpl-header", | ||
size = "small", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rule implementation is quite straightforward:
(The |
||
# Copyright (C) 2022 Vaticle | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
|
||
def _rust_tonic_compile_impl(ctx): | ||
protos = [src[ProtoInfo].direct_sources[0] for src in ctx.attr.srcs] | ||
|
||
inputs = ctx.attr.protoc.files.to_list() + protos | ||
outputs = [ctx.actions.declare_file("{}.rs".format(package)) for package in ctx.attr.packages] | ||
|
||
ctx.actions.run( | ||
inputs = inputs, | ||
outputs = outputs, | ||
executable = ctx.executable._compile_script, | ||
env = { | ||
"OUT_DIR": outputs[0].dirname, | ||
"PROTOC": ctx.attr.protoc.files.to_list()[0].path, | ||
"PROTOS": ";".join([src.path for src in protos]), | ||
"PROTOS_ROOT": ctx.attr.srcs[0][ProtoInfo].proto_source_root, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
mnemonic = "RustTonicCompileAction" | ||
) | ||
|
||
return [DefaultInfo(files = depset(outputs))] | ||
|
||
rust_tonic_compile = rule( | ||
implementation = _rust_tonic_compile_impl, | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
mandatory = True, | ||
doc = "The .proto source files." | ||
), | ||
"packages": attr.string_list( | ||
mandatory = True, | ||
allow_empty = False, | ||
doc = "The Protobuf package names. Each package name corresponds to a single output file." | ||
), | ||
"protoc": attr.label( | ||
default = "@com_google_protobuf//:protoc", | ||
doc = "The protoc executable." | ||
), | ||
"_compile_script": attr.label( | ||
executable = True, | ||
cfg = "host", | ||
default = "@vaticle_dependencies//builder/grpc/rust:compile", | ||
), | ||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We pass in all the |
||
// Copyright (C) 2022 Vaticle | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
|
||
use std::env; | ||
|
||
fn main() -> std::io::Result<()> { | ||
let protos_raw = env::var("PROTOS").expect("PROTOS environment variable is not set"); | ||
let protos: Vec<&str> = protos_raw.split(";").filter(|&str| !str.is_empty()).collect(); | ||
|
||
tonic_build::configure() | ||
.compile(&protos, &[ | ||
env::var("PROTOS_ROOT").expect("PROTOS_ROOT environment variable is not set") | ||
]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update to the latest Rust (fixes build issue in
ortools
, and also is just generally good practice)