From 40eb5116e72f52365a9f202be2c317f24cc73333 Mon Sep 17 00:00:00 2001 From: Konstantin Shcherban Date: Fri, 5 Nov 2021 15:51:32 +0100 Subject: [PATCH] Fix bazel build with changing path in include_bytes. This works well with both cargo and bazel. --- khronos_api/Cargo.toml | 1 + khronos_api/build.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/khronos_api/Cargo.toml b/khronos_api/Cargo.toml index f9b0b35..83a38e1 100644 --- a/khronos_api/Cargo.toml +++ b/khronos_api/Cargo.toml @@ -13,6 +13,7 @@ documentation = "https://docs.rs/khronos_api" homepage = "https://github.com/brendanzab/gl-rs/" repository = "https://github.com/brendanzab/gl-rs/" readme = "README.md" +edition = "2018" # Only include what we need here. The git submodules are quite large, and would # exceed the maximimum crate size if we didn't do this diff --git a/khronos_api/build.rs b/khronos_api/build.rs index 160179b..6da5d31 100644 --- a/khronos_api/build.rs +++ b/khronos_api/build.rs @@ -29,7 +29,12 @@ fn main() { // The absolute path is needed, because we don't know where the output // directory will be, and `include_bytes!(..)` resolves paths relative to the // containing file. - let root = env::current_dir().unwrap().join("api_webgl/extensions"); + let manifest_path = env::var("CARGO_MANIFEST_DIR").unwrap(); + let root_base = Path::new(&manifest_path); + let root = match root_base.join("khronos_api/api_webgl/extensions").exists() { + true => root_base.join("khronos_api/api_webgl/extensions"), + false => root_base.join("api_webgl/extensions"), + }; // Generate a slice literal, looking like this: // `&[&*include_bytes!(..), &*include_bytes!(..), ..]` @@ -38,7 +43,11 @@ fn main() { // The slice will have one entry for each WebGL extension. To find the // extensions we mirror the behaviour of the `api_webgl/extensions/find-exts` // shell script. - let mut paths: Vec<_> = root.read_dir().unwrap().map(|e| e.unwrap().path()).collect(); + let mut paths: Vec<_> = root + .read_dir() + .unwrap() + .map(|e| e.unwrap().path()) + .collect(); // Sort the list of paths in order for the webgl_exts.rs file to be created // deterministically. paths.sort(); @@ -52,8 +61,10 @@ fn main() { // really is an extension. let ext_path = path.join("extension.xml"); if ext_path.is_file() { - // Include the XML file, making sure to use an absolute path. - writeln!(file, "&*include_bytes!({:?}),", ext_path.to_str().unwrap()).unwrap(); + // Fix absolute path for bazel build + let abs_out_path = ext_path.canonicalize().unwrap(); + let abs_out_path = abs_out_path.to_str().unwrap(); + writeln!(file, "&*include_bytes!({:?}),", abs_out_path).unwrap(); } } }