Skip to content

Commit cd18716

Browse files
committed
khronos_api: Resolve WebGL extension files relative to CARGO_MANIFEST_DIR
Fixes #536 Fixes #549 Fixes #554 There are not one, not two but three PRs attempting to fix Bazel build support, which seems to containerize or sandbox the build script such that the paths it uses to find the crate source (e.g. `current_dir()`) and XML files within is different than when the generated source is being compiled, leading to files no longer being found. The most simple solution of course is to remove all assumptions and references to the working directory or absolute location of the crate when the build script is being ran. This can only be used to find the files, after which their relative paths (**within** the crate source) will be emitted to the output file with a `concat!()` directive that prepends it with `env!("CARGO_MANIFEST_DIR")` whenever the source is finally being compiled (in a different sandbox).
1 parent a4140fe commit cd18716

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

khronos_api/build.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ fn main() {
2525
let dest = env::var("OUT_DIR").unwrap();
2626
let mut file = File::create(Path::new(&dest).join("webgl_exts.rs")).unwrap();
2727

28-
// Find the absolute path to the folder containing the WebGL extensions.
29-
// The absolute path is needed, because we don't know where the output
30-
// directory will be, and `include_bytes!(..)` resolves paths relative to the
31-
// containing file.
32-
let root = env::current_dir().unwrap().join("api_webgl/extensions");
28+
// The relative path within this crate containing the WebGL extensions.
29+
let root = Path::new("api_webgl/extensions");
3330

3431
// Generate a slice literal, looking like this:
3532
// `&[&*include_bytes!(..), &*include_bytes!(..), ..]`
@@ -56,8 +53,17 @@ fn main() {
5653
// really is an extension.
5754
let ext_path = path.join("extension.xml");
5855
if ext_path.is_file() {
59-
// Include the XML file, making sure to use an absolute path.
60-
writeln!(file, "&*include_bytes!({:?}),", ext_path.to_str().unwrap()).unwrap();
56+
// Include the XML file.
57+
writeln!(
58+
file,
59+
// The path of the crate must be resolved at compile-time of the generated code
60+
// using this environment variable, not at runtime of this build script using
61+
// current_dir() or canonicalization. These will differ on at least Bazel
62+
// when/where the build script is containerized.
63+
r#"&*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/{}")),"#,
64+
ext_path.to_str().unwrap()
65+
)
66+
.unwrap();
6167
}
6268
}
6369
}

0 commit comments

Comments
 (0)