@@ -3,7 +3,118 @@ use std::fs;
33use std:: path;
44use std:: sync;
55
6- use bindgen:: callbacks:: { ItemInfo , ItemKind , MacroParsingBehavior , ParseCallbacks } ;
6+ use bindgen:: callbacks:: { DeriveInfo , ItemInfo , ItemKind , MacroParsingBehavior , ParseCallbacks } ;
7+
8+ /// Enum to handle different callback combinations
9+ #[ derive( Debug ) ]
10+ pub ( crate ) enum BindgenCallbacks {
11+ /// For bindings that need function renaming (driver, runtime, cublas)
12+ WithFunctionRenames {
13+ function_renames : Box < FunctionRenames > ,
14+ cargo_callbacks : bindgen:: CargoCallbacks ,
15+ } ,
16+ /// For bindings that only need comment processing (nvptx, nvvm)
17+ Simple {
18+ cargo_callbacks : bindgen:: CargoCallbacks ,
19+ } ,
20+ }
21+
22+ impl BindgenCallbacks {
23+ pub fn with_function_renames ( function_renames : FunctionRenames ) -> Self {
24+ Self :: WithFunctionRenames {
25+ function_renames : Box :: new ( function_renames) ,
26+ cargo_callbacks : bindgen:: CargoCallbacks :: new ( ) ,
27+ }
28+ }
29+
30+ pub fn simple ( ) -> Self {
31+ Self :: Simple {
32+ cargo_callbacks : bindgen:: CargoCallbacks :: new ( ) ,
33+ }
34+ }
35+ }
36+
37+ impl ParseCallbacks for BindgenCallbacks {
38+ fn process_comment ( & self , comment : & str ) -> Option < String > {
39+ // First replace backslashes with @ to avoid doctest parsing issues
40+ let cleaned = comment. replace ( '\\' , "@" ) ;
41+ // Then transform doxygen syntax to rustdoc
42+ match doxygen_bindgen:: transform ( & cleaned) {
43+ Ok ( res) => Some ( res) ,
44+ Err ( err) => {
45+ println ! (
46+ "cargo:warning=Problem processing doxygen comment: {}\n {}" ,
47+ comment, err
48+ ) ;
49+ None
50+ }
51+ }
52+ }
53+
54+ fn will_parse_macro ( & self , name : & str ) -> MacroParsingBehavior {
55+ match self {
56+ Self :: WithFunctionRenames {
57+ function_renames, ..
58+ } => function_renames. will_parse_macro ( name) ,
59+ Self :: Simple { .. } => MacroParsingBehavior :: Default ,
60+ }
61+ }
62+
63+ fn item_name ( & self , original_item_name : & str ) -> Option < String > {
64+ match self {
65+ Self :: WithFunctionRenames {
66+ function_renames, ..
67+ } => function_renames. item_name ( original_item_name) ,
68+ Self :: Simple { .. } => None ,
69+ }
70+ }
71+
72+ fn add_derives ( & self , info : & DeriveInfo ) -> Vec < String > {
73+ match self {
74+ Self :: WithFunctionRenames {
75+ function_renames, ..
76+ } => ParseCallbacks :: add_derives ( function_renames. as_ref ( ) , info) ,
77+ Self :: Simple { .. } => vec ! [ ] ,
78+ }
79+ }
80+
81+ fn generated_name_override ( & self , item_info : ItemInfo < ' _ > ) -> Option < String > {
82+ match self {
83+ Self :: WithFunctionRenames {
84+ function_renames, ..
85+ } => ParseCallbacks :: generated_name_override ( function_renames. as_ref ( ) , item_info) ,
86+ Self :: Simple { .. } => None ,
87+ }
88+ }
89+
90+ fn generated_link_name_override ( & self , item_info : ItemInfo < ' _ > ) -> Option < String > {
91+ match self {
92+ Self :: WithFunctionRenames {
93+ function_renames, ..
94+ } => ParseCallbacks :: generated_link_name_override ( function_renames. as_ref ( ) , item_info) ,
95+ Self :: Simple { .. } => None ,
96+ }
97+ }
98+
99+ // Delegate cargo callbacks
100+ fn include_file ( & self , filename : & str ) {
101+ match self {
102+ Self :: WithFunctionRenames {
103+ cargo_callbacks, ..
104+ }
105+ | Self :: Simple { cargo_callbacks } => cargo_callbacks. include_file ( filename) ,
106+ }
107+ }
108+
109+ fn read_env_var ( & self , var : & str ) {
110+ match self {
111+ Self :: WithFunctionRenames {
112+ cargo_callbacks, ..
113+ }
114+ | Self :: Simple { cargo_callbacks } => cargo_callbacks. read_env_var ( var) ,
115+ }
116+ }
117+ }
7118
8119/// Struct to handle renaming of functions through macro expansion.
9120#[ derive( Debug ) ]
@@ -123,4 +234,8 @@ impl ParseCallbacks for FunctionRenames {
123234 _ => None ,
124235 }
125236 }
237+
238+ fn add_derives ( & self , _info : & DeriveInfo ) -> Vec < String > {
239+ vec ! [ ]
240+ }
126241}
0 commit comments