| 1 |
// Copyright (C) 2024 Advanced Micro Devices. All rights reserved. |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
// this software and associated documentation files (the "Software"), to deal in |
| 5 |
// the Software without restriction, including without limitation the rights to |
| 6 |
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 7 |
// the Software, and to permit persons to whom the Software is furnished to do so, |
| 8 |
// subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in all |
| 11 |
// copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 |
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 |
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 |
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 |
// |
| 20 |
|
| 21 |
extern crate bindgen; |
| 22 |
use std::env; |
| 23 |
use std::fs; |
| 24 |
use std::io::Result; |
| 25 |
use std::path::{Path, PathBuf}; |
| 26 |
mod callbacks; |
| 27 |
|
| 28 |
fn get_rocm_dir() -> Option<PathBuf> { |
| 29 |
// Look for the latest folder in /opt that begins with "rocm" |
| 30 |
let opt_path = Path::new("/opt"); |
| 31 |
if let Ok(entries) = fs::read_dir(opt_path) { |
| 32 |
let mut rocm_dirs: Vec<PathBuf> = entries |
| 33 |
.filter_map(|entry| entry.ok()) |
| 34 |
.map(|entry| entry.path()) |
| 35 |
.filter(|path| { |
| 36 |
path.is_dir() |
| 37 |
&& path |
| 38 |
.file_name() |
| 39 |
.unwrap_or_default() |
| 40 |
.to_string_lossy() |
| 41 |
.starts_with("rocm") |
| 42 |
}) |
| 43 |
.collect(); |
| 44 |
|
| 45 |
// Sort the directories by name and get the latest one |
| 46 |
rocm_dirs.sort(); |
| 47 |
if let Some(latest_rocm_dir) = rocm_dirs.last() { |
| 48 |
return Some(latest_rocm_dir.clone()); |
| 49 |
} |
| 50 |
} |
| 51 |
None |
| 52 |
} |
| 53 |
|
| 54 |
fn get_amdsmi_lib_dir() -> Result<String> { |
| 55 |
let amdsmi_file = "libamd_smi.so"; |
| 56 |
|
| 57 |
// Check the environment variable AMDSMI_LIB_DIR to get the library directory |
| 58 |
if let Ok(lib_dir) = env::var("AMDSMI_LIB_DIR") { |
| 59 |
if PathBuf::from(lib_dir.clone()).join(amdsmi_file).exists() { |
| 60 |
return Ok(lib_dir); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Check the current directory |
| 65 |
if let Ok(current_dir) = env::current_dir() { |
| 66 |
let current_lib_dir = current_dir.join("lib"); |
| 67 |
if current_lib_dir.join(amdsmi_file).exists() { |
| 68 |
return Ok(current_lib_dir.to_string_lossy().into_owned()); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Check the ROCm directory |
| 73 |
if let Some(lib_dir) = get_rocm_dir() { |
| 74 |
if lib_dir.join("lib").join(amdsmi_file).exists() { |
| 75 |
return Ok(lib_dir.join("lib").to_string_lossy().into_owned()); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
Err(std::io::Error::new( |
| 80 |
std::io::ErrorKind::NotFound, |
| 81 |
"The libamd_smi.so library was not found. Please set the AMDSMI_LIB_DIR environment variable to the directory containing the library.", |
| 82 |
)) |
| 83 |
} |
| 84 |
|
| 85 |
fn get_amdsmi_header_file() -> Result<String> { |
| 86 |
let amdsmi_header_path = "include/amd_smi/amdsmi.h"; |
| 87 |
// Use the project's header as the first priority |
| 88 |
let default_path = PathBuf::from("../").join(amdsmi_header_path); |
| 89 |
if default_path.exists() { |
| 90 |
return Ok(default_path.to_string_lossy().into_owned()); |
| 91 |
} |
| 92 |
|
| 93 |
// Check the current directory |
| 94 |
if let Ok(current_dir) = env::current_dir() { |
| 95 |
let fallback_path = current_dir.join(amdsmi_header_path); |
| 96 |
if fallback_path.exists() { |
| 97 |
return Ok(fallback_path.to_string_lossy().into_owned()); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Check the ROCm directory |
| 102 |
if let Some(rocm_dir) = get_rocm_dir() { |
| 103 |
let fallback_path = rocm_dir.join(amdsmi_header_path); |
| 104 |
if fallback_path.exists() { |
| 105 |
return Ok(fallback_path.to_string_lossy().into_owned()); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
Err(std::io::Error::new( |
| 110 |
std::io::ErrorKind::NotFound, |
| 111 |
"The amdsmi.h header file was not found", |
| 112 |
)) |
| 113 |
} |
| 114 |
|
| 115 |
fn generate_amdsmi_wrapper(amdsmi_header_file: &str) { |
| 116 |
let bindings = bindgen::Builder::default() |
| 117 |
.header(amdsmi_header_file) |
| 118 |
.generate_comments(false) |
| 119 |
.prepend_enum_name(false) |
| 120 |
.rustified_enum("^(.*)$") |
| 121 |
.allowlist_type("^(amdsmi.*)$") |
| 122 |
.allowlist_function("^(amdsmi.*)$") |
| 123 |
.allowlist_var("^(AMDSMI.*)$") |
| 124 |
.parse_callbacks(Box::new(callbacks::UpperCamelCaseCallbacks)) |
| 125 |
.raw_line("// Copyright (C) 2024 Advanced Micro Devices. All rights reserved.") |
| 126 |
.raw_line("//") |
| 127 |
.raw_line( |
| 128 |
"// Permission is hereby granted, free of charge, to any person obtaining a copy of", |
| 129 |
) |
| 130 |
.raw_line( |
| 131 |
"// this software and associated documentation files (the \"Software\"), to deal in", |
| 132 |
) |
| 133 |
.raw_line("// the Software without restriction, including without limitation the rights to") |
| 134 |
.raw_line( |
| 135 |
"// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of", |
| 136 |
) |
| 137 |
.raw_line( |
| 138 |
"// the Software, and to permit persons to whom the Software is furnished to do so,", |
| 139 |
) |
| 140 |
.raw_line("// subject to the following conditions:") |
| 141 |
.raw_line("//") |
| 142 |
.raw_line( |
| 143 |
"// The above copyright notice and this permission notice shall be included in all", |
| 144 |
) |
| 145 |
.raw_line("// copies or substantial portions of the Software.") |
| 146 |
.raw_line("//") |
| 147 |
.raw_line("// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR") |
| 148 |
.raw_line( |
| 149 |
"// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS", |
| 150 |
) |
| 151 |
.raw_line( |
| 152 |
"// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR", |
| 153 |
) |
| 154 |
.raw_line( |
| 155 |
"// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER", |
| 156 |
) |
| 157 |
.raw_line("// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN") |
| 158 |
.raw_line("// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.") |
| 159 |
.raw_line("") |
| 160 |
.raw_line("#![allow(non_upper_case_globals)]") |
| 161 |
.generate() |
| 162 |
.expect("Unable to binding wrapper for amdsmi C interface!"); |
| 163 |
|
| 164 |
let bindings_path = PathBuf::from("./src/amdsmi_wrapper.rs"); |
| 165 |
bindings |
| 166 |
.write_to_file(&bindings_path) |
| 167 |
.expect("Couldn't write binding wrapper for amdsmi C interface!"); |
| 168 |
println!("Wrapper generated at: {:?}", bindings_path); |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
fn main() { |
| 173 |
// Get the amd_smi library directory |
| 174 |
let amdsmi_lib_dir = get_amdsmi_lib_dir().expect("Failed to get the amd_smi library path"); |
| 175 |
|
| 176 |
// Tell cargo to tell rustc to link the AMD-SMI library |
| 177 |
println!("cargo:rustc-link-lib=amd_smi"); |
| 178 |
println!("cargo:rustc-link-search=native={}", amdsmi_lib_dir); |
| 179 |
|
| 180 |
let generate_wrapper = env::var("AMDSMI_GENERATE_RUST_WRAPPER").is_ok(); |
| 181 |
if generate_wrapper { |
| 182 |
// Get the amdsmi.h header file path |
| 183 |
let amdsmi_header_file = get_amdsmi_header_file().expect("Failed to get the amd_smi header file"); |
| 184 |
|
| 185 |
// Generate the amdsmi wrapper |
| 186 |
generate_amdsmi_wrapper(&amdsmi_header_file); |
| 187 |
} |
| 188 |
} |