Use a ninja variable for Protoc binary (#2345)

* Use a ninja variable for Protoc binary

* fix whitespace
This commit is contained in:
Mani 2023-01-23 18:44:47 +08:00 committed by GitHub
parent ff59b33c54
commit da7d4dd2fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 19 deletions

View file

@ -1,7 +1,7 @@
[env]
STRINGS_JSON = { value = "out/rslib/i18n/strings.json", relative = true }
# build script will append .exe if necessary
PROTOC = { value = "out/extracted/protoc/bin/protoc", relative = true }
PROTOC_BINARY = { value = "out/extracted/protoc/bin/protoc", relative = true }
PYO3_NO_PYTHON = "1"
MACOSX_DEPLOYMENT_TARGET = "10.13.4"

View file

@ -109,6 +109,7 @@ Zoom <zoomrmc+git@gmail.com>
TRIAEIOU <github.com/TRIAEIOU>
Stefan Kangas <stefankangas@gmail.com>
Fabricio Duarte <fabricio.duarte.jr@gmail.com>
Mani <github.com/krmanik>
********************

View file

@ -1,6 +1,8 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use std::env;
use ninja_gen::archives::download_and_extract;
use ninja_gen::archives::with_exe;
use ninja_gen::glob;
@ -10,8 +12,18 @@ use ninja_gen::protobuf::protoc_archive;
use ninja_gen::protobuf::ClangFormat;
use ninja_gen::Build;
use ninja_gen::Result;
use ninja_gen::Utf8Path;
pub fn download_protoc(build: &mut Build) -> Result<()> {
pub fn setup_protoc(build: &mut Build) -> Result<()> {
let protoc_binary = match env::var("PROTOC_BINARY") {
Ok(path) => {
assert!(
Utf8Path::new(&path).is_absolute(),
"PROTOC_BINARY must be absolute"
);
path.into()
}
Err(_) => {
download_and_extract(
build,
"protoc",
@ -20,6 +32,11 @@ pub fn download_protoc(build: &mut Build) -> Result<()> {
"bin" => [with_exe("bin/protoc")]
},
)?;
inputs![":extract:protoc:bin"]
}
};
let protoc_binary = build.expand_inputs(protoc_binary);
build.variable("protoc_binary", &protoc_binary[0]);
Ok(())
}

View file

@ -178,7 +178,7 @@ impl BuildAction for GenPythonProto {
})
.collect();
build.add_inputs("in", &self.proto_files);
build.add_inputs("protoc", inputs![":extract:protoc:bin"]);
build.add_inputs("protoc", inputs!["$protoc_binary"]);
build.add_inputs("protoc-gen-mypy", inputs![":pyenv:protoc-gen-mypy"]);
build.add_outputs("", python_outputs);
}

View file

@ -14,11 +14,11 @@ use ninja_gen::Build;
use ninja_gen::Result;
use crate::platform::overriden_rust_target_triple;
use crate::proto::download_protoc;
use crate::proto::setup_protoc;
pub fn build_rust(build: &mut Build) -> Result<()> {
prepare_translations(build)?;
download_protoc(build)?;
setup_protoc(build)?;
build_rsbridge(build)
}
@ -79,7 +79,7 @@ fn build_rsbridge(build: &mut Build) -> Result<()> {
CargoBuild {
inputs: inputs![
glob!["{pylib/rsbridge/**,rslib/**,proto/**}"],
":extract:protoc:bin",
"$protoc_binary",
// declare a dependency on i18n so it gets built first, allowing
// things depending on strings.json to build faster, and ensuring
// changes to the ftl files trigger a rebuild

View file

@ -76,9 +76,9 @@ There are a few things to be aware of:
## Packaging considerations
Python and node are downloaded as part of the build. You can optionally define
PYTHON_BINARY as the full path to a Python binary, to use it instead of the downloaded
version. A similar approach could be done with node in the future; a PR would be
Python, node and protoc are downloaded as part of the build. You can optionally define
PYTHON_BINARY and PROTOC_BINARY as the full path to a Python binary and Protoc binary respectively,
to use it instead of the downloaded version. A similar approach could be done with node in the future; a PR would be
welcome.
If rust-toolchain.toml is removed, newer Rust versions can be used. Older versions

View file

@ -125,7 +125,8 @@ pub fn write_backend_proto_rs() {
/// If PROTOC is not defined, and protoc is not on path, use the protoc
/// fetched by Bazel so that Rust Analyzer does not fail.
fn maybe_add_protobuf_to_path() {
if let Ok(protoc) = env::var("PROTOC") {
if let Ok(protoc) = env::var("PROTOC_BINARY") {
env::set_var("PROTOC", &protoc);
if cfg!(windows) && !protoc.ends_with(".exe") {
env::set_var("PROTOC", format!("{protoc}.exe"));
}