Skip to content

Commit 64c5bcd

Browse files
fix(build): honor PROTOC when generating protobuf code (NVIDIA#4441)
The core, server, and sandbox-backend build scripts always used the vendored protoc binary and ignored PROTOC, so a distro build could not use its system compiler. When PROTOC is set, use it and let prost resolve the includes (PROTOC_INCLUDE still applies); otherwise behave exactly as before. Contributes to NVIDIA#3510. Signed-off-by: politerealism <[email protected]>
1 parent 8b774e2 commit 64c5bcd

3 files changed

Lines changed: 69 additions & 28 deletions

File tree

‎crates/openshell-core/build.rs‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ mod build_version;
99
const PROTO_REL: &str = "../../proto";
1010

1111
fn main() -> Result<(), Box<dyn std::error::Error>> {
12-
// Preserve the vendored include-tree guarantee even when the caller has
13-
// PROTOC_INCLUDE set: prost validates that ambient path independently of
14-
// explicit includes. Re-enter with a child-only environment override.
15-
if env::var_os("PROTOC_INCLUDE").is_some() {
12+
// A caller-supplied PROTOC (for example a distro package's system
13+
// compiler) is used as-is and prost resolves its own includes. Without it,
14+
// use the vendored compiler and include tree.
15+
println!("cargo:rerun-if-env-changed=PROTOC");
16+
println!("cargo:rerun-if-env-changed=PROTOC_INCLUDE");
17+
let use_vendored_protoc = env::var_os("PROTOC").is_none();
18+
19+
// In vendored mode, preserve the vendored include-tree guarantee even when
20+
// the caller has PROTOC_INCLUDE set: prost validates that ambient path
21+
// independently of explicit includes. Re-enter with a child-only
22+
// environment override.
23+
if use_vendored_protoc && env::var_os("PROTOC_INCLUDE").is_some() {
1624
let status = std::process::Command::new(env::current_exe()?)
1725
.env_remove("PROTOC_INCLUDE")
1826
.status()?;
@@ -40,10 +48,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4048
// Re-run when anything under proto/ changes (including newly added .proto files).
4149
println!("cargo:rerun-if-changed={PROTO_REL}");
4250
// Configure the vendored compiler and well-known includes without changing
43-
// the build process environment.
51+
// the build process environment. With a caller-supplied PROTOC, leave both
52+
// to prost.
4453
let mut proto_config = tonic_prost_build::Config::new();
45-
proto_config.protoc_executable(protoc_bin_vendored::protoc_bin_path()?);
46-
let proto_include = protoc_bin_vendored::include_path()?;
54+
let proto_include = if use_vendored_protoc {
55+
proto_config.protoc_executable(protoc_bin_vendored::protoc_bin_path()?);
56+
Some(protoc_bin_vendored::include_path()?)
57+
} else {
58+
None
59+
};
4760

4861
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
4962
let proto_root = manifest_dir.join(PROTO_REL);
@@ -53,6 +66,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5366

5467
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
5568
let descriptor_path = out_dir.join("openshell_descriptor.bin");
69+
let mut include_dirs = vec![proto_root];
70+
include_dirs.extend(proto_include);
5671

5772
// Configure tonic/prost protobuf code generation.
5873
tonic_prost_build::configure()
@@ -62,7 +77,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6277
// Emit a binary FileDescriptorSet so the server can enumerate every
6378
// RPC at runtime (used by the per-handler auth exhaustiveness test).
6479
.file_descriptor_set_path(&descriptor_path)
65-
.compile_with_config(proto_config, &proto_files, &[proto_root, proto_include])?;
80+
.compile_with_config(proto_config, &proto_files, &include_dirs)?;
6681

6782
println!(
6883
"cargo:rustc-env=OPENSHELL_DESCRIPTOR_PATH={}",

‎crates/openshell-sandbox-backend/build.rs‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
fn main() -> Result<(), Box<dyn std::error::Error>> {
5-
// Preserve the vendored include-tree guarantee even when the caller has
6-
// PROTOC_INCLUDE set: prost validates that ambient path independently of
7-
// explicit includes. Re-enter with a child-only environment override.
8-
if std::env::var_os("PROTOC_INCLUDE").is_some() {
5+
// A caller-supplied PROTOC (for example a distro package's system
6+
// compiler) is used as-is and prost resolves its own includes. Without it,
7+
// use the vendored compiler and include tree.
8+
println!("cargo:rerun-if-env-changed=PROTOC");
9+
println!("cargo:rerun-if-env-changed=PROTOC_INCLUDE");
10+
let use_vendored_protoc = std::env::var_os("PROTOC").is_none();
11+
12+
// In vendored mode, preserve the vendored include-tree guarantee even when
13+
// the caller has PROTOC_INCLUDE set: prost validates that ambient path
14+
// independently of explicit includes. Re-enter with a child-only
15+
// environment override.
16+
if use_vendored_protoc && std::env::var_os("PROTOC_INCLUDE").is_some() {
917
let status = std::process::Command::new(std::env::current_exe()?)
1018
.env_remove("PROTOC_INCLUDE")
1119
.status()?;
@@ -18,18 +26,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1826
println!("cargo:rerun-if-changed=proto/openshell_sandbox.proto");
1927

2028
// Configure the vendored compiler and well-known includes without changing
21-
// the build process environment.
29+
// the build process environment. With a caller-supplied PROTOC, leave both
30+
// to prost.
2231
let mut proto_config = tonic_prost_build::Config::new();
23-
proto_config.protoc_executable(protoc_bin_vendored::protoc_bin_path()?);
24-
let proto_include = protoc_bin_vendored::include_path()?;
32+
let proto_include = if use_vendored_protoc {
33+
proto_config.protoc_executable(protoc_bin_vendored::protoc_bin_path()?);
34+
Some(protoc_bin_vendored::include_path()?)
35+
} else {
36+
None
37+
};
38+
let mut include_dirs = vec![std::path::PathBuf::from("proto")];
39+
include_dirs.extend(proto_include);
2540

2641
tonic_prost_build::configure()
2742
.build_server(true)
2843
.build_client(true)
2944
.compile_with_config(
3045
proto_config,
3146
&[std::path::PathBuf::from("proto/openshell_sandbox.proto")],
32-
&[std::path::PathBuf::from("proto"), proto_include],
47+
&include_dirs,
3348
)?;
3449
Ok(())
3550
}

‎crates/openshell-server/build.rs‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ use std::env;
55
use std::path::PathBuf;
66

77
fn main() -> Result<(), Box<dyn std::error::Error>> {
8-
// Preserve the vendored include-tree guarantee even when the caller has
9-
// PROTOC_INCLUDE set: prost validates that ambient path independently of
10-
// explicit includes. Re-enter with a child-only environment override.
11-
if env::var_os("PROTOC_INCLUDE").is_some() {
8+
// A caller-supplied PROTOC (for example a distro package's system
9+
// compiler) is used as-is and prost resolves its own includes. Without it,
10+
// use the vendored compiler and include tree.
11+
println!("cargo:rerun-if-env-changed=PROTOC");
12+
println!("cargo:rerun-if-env-changed=PROTOC_INCLUDE");
13+
let use_vendored_protoc = env::var_os("PROTOC").is_none();
14+
15+
// In vendored mode, preserve the vendored include-tree guarantee even when
16+
// the caller has PROTOC_INCLUDE set: prost validates that ambient path
17+
// independently of explicit includes. Re-enter with a child-only
18+
// environment override.
19+
if use_vendored_protoc && env::var_os("PROTOC_INCLUDE").is_some() {
1220
let status = std::process::Command::new(env::current_exe()?)
1321
.env_remove("PROTOC_INCLUDE")
1422
.status()?;
@@ -37,12 +45,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3745
}
3846

3947
// Configure the vendored compiler and well-known includes without changing
40-
// the build process environment.
48+
// the build process environment. With a caller-supplied PROTOC, leave both
49+
// to prost.
4150
let mut proto_config = tonic_prost_build::Config::new();
42-
proto_config.protoc_executable(protoc_bin_vendored::protoc_bin_path()?);
43-
let proto_include = protoc_bin_vendored::include_path()?;
51+
let proto_include = if use_vendored_protoc {
52+
proto_config.protoc_executable(protoc_bin_vendored::protoc_bin_path()?);
53+
Some(protoc_bin_vendored::include_path()?)
54+
} else {
55+
None
56+
};
4457

4558
let descriptor_path = PathBuf::from(env::var("OUT_DIR")?).join("storage_descriptor.bin");
59+
let mut include_dirs = vec![storage_proto_dir, public_proto_dir];
60+
include_dirs.extend(proto_include);
4661
tonic_prost_build::configure()
4762
.build_server(false)
4863
.build_client(false)
@@ -56,11 +71,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5671
"::openshell_core::proto::sandbox::v1",
5772
)
5873
.file_descriptor_set_path(&descriptor_path)
59-
.compile_with_config(
60-
proto_config,
61-
&[storage_proto],
62-
&[storage_proto_dir, public_proto_dir, proto_include],
63-
)?;
74+
.compile_with_config(proto_config, &[storage_proto], &include_dirs)?;
6475

6576
Ok(())
6677
}

0 commit comments

Comments
 (0)