Convert nvml project to the new macro

This commit is contained in:
Andrzej Janik
2024-12-09 16:53:33 +00:00
parent c9c7a4d432
commit ed98dca26d
9 changed files with 61 additions and 3183 deletions

View File

@ -6,7 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
quote = "1.0" quote = "1.0"
syn = { version = "2.0", features = ["full", "visit-mut"] } syn = { version = "2.0", features = ["full", "visit-mut", "extra-traits"] }
proc-macro2 = "1.0" proc-macro2 = "1.0"
rustc-hash = "1.1.0" rustc-hash = "1.1.0"

View File

@ -14,6 +14,7 @@ use syn::{
}; };
const CUDA_RS: &'static str = include_str! {"cuda.rs"}; const CUDA_RS: &'static str = include_str! {"cuda.rs"};
const NVML_RS: &'static str = include_str! {"nvml.rs"};
// This macro accepts following arguments: // This macro accepts following arguments:
// * `normal_macro`: ident for a normal macro // * `normal_macro`: ident for a normal macro
@ -31,9 +32,13 @@ const CUDA_RS: &'static str = include_str! {"cuda.rs"};
// Additionally, it does a fixup of CUDA types so they get prefixed with `type_path` // Additionally, it does a fixup of CUDA types so they get prefixed with `type_path`
#[proc_macro] #[proc_macro]
pub fn cuda_function_declarations(tokens: TokenStream) -> TokenStream { pub fn cuda_function_declarations(tokens: TokenStream) -> TokenStream {
function_declarations(tokens, CUDA_RS)
}
fn function_declarations(tokens: TokenStream, module: &str) -> TokenStream {
let input = parse_macro_input!(tokens as FnDeclInput); let input = parse_macro_input!(tokens as FnDeclInput);
let mut cuda_module = syn::parse_str::<File>(module).unwrap();
let mut choose_macro = ChooseMacro::new(input); let mut choose_macro = ChooseMacro::new(input);
let mut cuda_module = syn::parse_str::<File>(CUDA_RS).unwrap();
syn::visit_mut::visit_file_mut(&mut FixFnSignatures, &mut cuda_module); syn::visit_mut::visit_file_mut(&mut FixFnSignatures, &mut cuda_module);
let extern_ = if let Item::ForeignMod(extern_) = cuda_module.items.pop().unwrap() { let extern_ = if let Item::ForeignMod(extern_) = cuda_module.items.pop().unwrap() {
extern_ extern_
@ -68,6 +73,11 @@ pub fn cuda_function_declarations(tokens: TokenStream) -> TokenStream {
} }
result.into() result.into()
} }
#[proc_macro]
pub fn nvml_function_declarations(tokens: TokenStream) -> TokenStream {
function_declarations(tokens, NVML_RS)
}
struct FnDeclInput { struct FnDeclInput {
normal_macro: Path, normal_macro: Path,
overrides: Punctuated<OverrideMacro, Token![,]>, overrides: Punctuated<OverrideMacro, Token![,]>,

View File

@ -1,7 +1,7 @@
pub(crate) mod r#impl; pub(crate) mod r#impl;
macro_rules! unimplemented { macro_rules! unimplemented {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:path;)*) => { ($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
$( $(
#[cfg_attr(not(test), no_mangle)] #[cfg_attr(not(test), no_mangle)]
#[allow(improper_ctypes)] #[allow(improper_ctypes)]
@ -14,7 +14,7 @@ macro_rules! unimplemented {
} }
macro_rules! implemented { macro_rules! implemented {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:path;)*) => { ($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
$( $(
#[cfg_attr(not(test), no_mangle)] #[cfg_attr(not(test), no_mangle)]
#[allow(improper_ctypes)] #[allow(improper_ctypes)]
@ -28,7 +28,7 @@ macro_rules! implemented {
} }
macro_rules! implemented_in_function { macro_rules! implemented_in_function {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:path;)*) => { ($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
$( $(
#[cfg_attr(not(test), no_mangle)] #[cfg_attr(not(test), no_mangle)]
#[allow(improper_ctypes)] #[allow(improper_ctypes)]

View File

@ -9,7 +9,7 @@ extern crate lazy_static;
extern crate cuda_types; extern crate cuda_types;
macro_rules! extern_redirect { macro_rules! extern_redirect {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:path;)*) => { ($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
$( $(
#[no_mangle] #[no_mangle]
#[allow(improper_ctypes_definitions)] #[allow(improper_ctypes_definitions)]
@ -30,7 +30,7 @@ macro_rules! extern_redirect {
} }
macro_rules! extern_redirect_with_post { macro_rules! extern_redirect_with_post {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:path;)*) => { ($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
$( $(
#[no_mangle] #[no_mangle]
#[allow(improper_ctypes_definitions)] #[allow(improper_ctypes_definitions)]

View File

@ -56,7 +56,7 @@ impl CudaDynamicFns {
} }
macro_rules! emit_cuda_fn_table { macro_rules! emit_cuda_fn_table {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:path;)*) => { ($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
#[derive(Default)] #[derive(Default)]
#[allow(improper_ctypes)] #[allow(improper_ctypes)]
#[allow(improper_ctypes_definitions)] #[allow(improper_ctypes_definitions)]

View File

@ -7,3 +7,7 @@ edition = "2021"
[lib] [lib]
name = "nvml" name = "nvml"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies]
cuda_base = { path = "../cuda_base" }
cuda_types = { path = "../cuda_types" }

View File

@ -1,4 +1,4 @@
use crate::nvml::nvmlReturn_t; use cuda_types::nvml::nvmlReturn_t;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
pub(crate) fn unimplemented() -> nvmlReturn_t { pub(crate) fn unimplemented() -> nvmlReturn_t {
@ -9,3 +9,9 @@ pub(crate) fn unimplemented() -> nvmlReturn_t {
pub(crate) fn unimplemented() -> nvmlReturn_t { pub(crate) fn unimplemented() -> nvmlReturn_t {
nvmlReturn_t::NVML_ERROR_NOT_SUPPORTED nvmlReturn_t::NVML_ERROR_NOT_SUPPORTED
} }
pub(crate) fn nvmlErrorString(
_result: cuda_types::nvml::nvmlReturn_t,
) -> *const ::core::ffi::c_char {
c"".as_ptr()
}

View File

@ -1,3 +1,32 @@
pub mod r#impl; mod r#impl;
#[allow(warnings)]
mod nvml; macro_rules! unimplemented_fn {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
$(
#[no_mangle]
#[allow(improper_ctypes_definitions)]
pub extern $abi fn $fn_name ( $( $arg_id : $arg_type),* ) -> $ret_type {
r#impl::unimplemented()
}
)*
};
}
macro_rules! implemented_fn {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
$(
#[no_mangle]
#[allow(improper_ctypes_definitions)]
pub extern $abi fn $fn_name ( $( $arg_id : $arg_type),* ) -> $ret_type {
r#impl::$fn_name($($arg_id),*)
}
)*
};
}
cuda_base::nvml_function_declarations!(
unimplemented_fn,
implemented_fn <= [
nvmlErrorString
]
);

File diff suppressed because it is too large Load Diff