Skip to content

Commit

Permalink
Fix bazel build with changing path in include_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shcherban committed Nov 8, 2021
1 parent 1cd256c commit 1a3d96c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions khronos_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions khronos_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ use std::path::*;
fn main() {
// Create and open a file in the output directory to contain our generated rust code
let dest = env::var("OUT_DIR").unwrap();
let manifest_path = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut file = File::create(&Path::new(&dest).join("webgl_exts.rs")).unwrap();

// Find the absolute path to the folder containing the WebGL extensions.
// 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_build = env::current_dir().unwrap();
let root = env::current_dir().unwrap().join("api_webgl/extensions");

// Generate a slice literal, looking like this:
Expand All @@ -38,7 +40,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();
Expand All @@ -52,8 +58,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 = Path::new(&manifest_path)
.join(ext_path.strip_prefix(root_build.to_str().unwrap()).unwrap());
writeln!(file, "&*include_bytes!({:?}),", abs_out_path).unwrap();
}
}
}
Expand Down

0 comments on commit 1a3d96c

Please sign in to comment.