Skip to content

Commit

Permalink
Simplify khronos_api patch for Bazel build
Browse files Browse the repository at this point in the history
Use relative paths to CARGO_MANIFEST_DIR in build.rs. Patch is
also submitted upstream at brendanzab/gl-rs#549
  • Loading branch information
tronical committed Jan 5, 2025
1 parent ab37bc4 commit db5d906
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 76 deletions.
6 changes: 1 addition & 5 deletions examples/bazel_build/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ crate.spec(package = "slint-build", git = "https://github.com/slint-ui/slint", b

crate.annotation(
crate = "khronos_api",
patches = ["@//external_crate_builds:khronos_apis_fix_build.patch"],
patches = ["@//external_crate_builds:0001-Fix-build-with-Bazel.patch"],
patch_args = ["-p1"],
compile_data = [":webgl_exts_rs"],
gen_build_script = "off",
additive_build_file = "@//external_crate_builds:khronos_api_webgl_exts_rs.BUILD.bazel",
rustc_env = {"WEBGL_EXTS": "$(location :webgl_exts_rs)"},
)
crate.annotation(
crate = "proc-macro-crate",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright © 2025 SixtyFPS GmbH
# SPDX-License-Identifier: MIT

# See https://github.com/brendanzab/gl-rs/pull/549

From ff5fd1fa0a2416ddff942173d047e95979aa6dfd Mon Sep 17 00:00:00 2001
From: Simon Hausmann <[email protected]>
Date: Sun, 5 Jan 2025 10:43:59 +0100
Subject: [PATCH] Fix build with Bazel

When building Rust applications with Bazel instead of cargo, typically a
sandbox is in place, where each process invocation (rustc, build.rs
program, etc.) runs in its own "root", where the view on the filesystem
is the same, but the absolute paths are different. As a consequence,
CARGO_MANIFEST_DIR differs between the invocation of build.rs and rustc.

That means that absolute paths in the generated webgl_exts.rs don't
work. Fortunately, the source files are relative to the
CARGO_MANIFEST_DIR, which, while changing between invocations, remains
valid.
---
khronos_api/build.rs | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/khronos_api/build.rs b/khronos_api/build.rs
index 160179b..f7fc439 100644
--- a/build.rs
+++ b/build.rs
@@ -29,7 +29,8 @@ 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 cargo_manifest_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap());
+ let root = cargo_manifest_dir.join("api_webgl/extensions");

// Generate a slice literal, looking like this:
// `&[&*include_bytes!(..), &*include_bytes!(..), ..]`
@@ -53,7 +54,7 @@ fn main() {
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();
+ writeln!(file, "&*include_bytes!(concat!(env!(\"CARGO_MANIFEST_DIR\"), \"/\", {:?})),", ext_path.strip_prefix(cargo_manifest_dir.clone()).unwrap().to_str().unwrap()).unwrap();
}
}
}
--
2.39.5 (Apple Git-154)

This file was deleted.

This file was deleted.

0 comments on commit db5d906

Please sign in to comment.