Skip to content

fix(cust_raw): Use string macro expansion to record function renames #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
env:
RUSTFLAGS: -Dwarnings
run: cargo clippy --workspace --exclude "optix*" --exclude "path_tracer" --exclude "denoiser" --exclude "ex*" --exclude "cudnn*"
- name: Build all bindings
run: cargo build --all-features -p cust_raw
- name: Build
run: cargo build --workspace --exclude "optix*" --exclude "path_tracer" --exclude "denoiser" --exclude "ex*" --exclude "cudnn*"
- name: Check documentation
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ jobs:
with:
key: ${{ matrix.os }}-${{ matrix.target }}-${{ matrix.cuda }}

- name: Build all bindings
run: cargo build --all-features -p cust_raw

- name: Build
run: cargo build --workspace --exclude "optix*" --exclude "path_tracer" --exclude "denoiser" --exclude "add" --exclude "ex*" --exclude "cudnn*"

Expand Down
19 changes: 14 additions & 5 deletions crates/cust_raw/build/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::fs;
use std::path;
use std::sync;

use bimap;
use bindgen::callbacks::{ItemInfo, ItemKind, MacroParsingBehavior, ParseCallbacks};

/// Struct to handle renaming of functions through macro expansion.
Expand Down Expand Up @@ -40,18 +39,24 @@ impl FunctionRenames {

fn expand(&self) -> &bimap::BiHashMap<String, String> {
self.func_remaps.get_or_init(|| {
if self.macro_names.borrow().is_empty() {
return bimap::BiHashMap::new();
}

let expand_me = self.out_dir.join("expand_macros.c");
let includes = fs::read_to_string(&self.includes)
.expect("Failed to read includes for function renames");

let mut template = format!(
r#"{includes}
#define RENAMED2(from, to) RUST_RENAMED##from##_TO_##to
#define RENAMED(from, to) RENAMED2(from, to)
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define RENAMED(from, to) "RUST_RENAMED" TOSTRING(from) TOSTRING(to)
"#
);

for name in self.macro_names.borrow().iter() {
// Add an underscore to the left so that it won't get expanded.
template.push_str(&format!("RENAMED(_{name}, {name})\n"));
}

Expand All @@ -74,11 +79,15 @@ impl FunctionRenames {

let mut remaps = bimap::BiHashMap::new();
for line in expanded.lines().rev() {
let rename_prefix = "RUST_RENAMED_";
let rename_prefix = "\"RUST_RENAMED\" ";

if let Some((original, expanded)) = line
.strip_prefix(rename_prefix)
.and_then(|s| s.split_once("_TO_"))
.map(|s| s.replace("\"", ""))
.and_then(|s| {
s.split_once(' ')
.map(|(l, r)| (l[1..].to_string(), r.to_string()))
})
.filter(|(l, r)| l != r && !r.is_empty())
{
remaps.insert(original.to_string(), expanded.to_string());
Expand Down
2 changes: 1 addition & 1 deletion crates/cust_raw/src/cublaslt_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/cublaslt_sys.rs"));
include!(concat!(env!("OUT_DIR"), "/cublasLt_sys.rs"));
2 changes: 1 addition & 1 deletion crates/cust_raw/src/cublasxt_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/cublasxt_sys.rs"));
include!(concat!(env!("OUT_DIR"), "/cublasXt_sys.rs"));