cubecl_hip_sys/
build_script.rs

1use std::fmt;
2use std::path::Path;
3
4pub struct Version {
5    pub major: u8,
6    pub minor: u8,
7    pub patch: u32,
8}
9
10impl fmt::Display for Version {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
13    }
14}
15
16/// Reads the header inside the rocm folder that contains the ROCm global version
17pub fn get_rocm_system_version(rocm_path: impl AsRef<Path>) -> std::io::Result<Version> {
18    let version_path = rocm_path.as_ref().join("include/rocm-core/rocm_version.h");
19    let version_file = std::fs::read_to_string(version_path)?;
20    let version_lines = version_file.lines().collect::<Vec<_>>();
21
22    let major = version_lines
23        .iter()
24        .find_map(|line| line.strip_prefix("#define ROCM_VERSION_MAJOR "))
25        .expect("Invalid rocm_version.h file structure: Major version line not found.")
26        .trim()
27        .parse::<u8>()
28        .expect("Invalid rocm_version.h file structure: Couldn't parse major version.");
29    let minor = version_lines
30        .iter()
31        .find_map(|line| line.strip_prefix("#define ROCM_VERSION_MINOR "))
32        .expect("Invalid rocm_version.h file structure: Minor version line not found.")
33        .trim()
34        .parse::<u8>()
35        .expect("Invalid rocm_version.h file structure: Couldn't parse minor version.");
36    let patch = version_lines
37        .iter()
38        .find_map(|line| line.strip_prefix("#define ROCM_VERSION_PATCH "))
39        .expect("Invalid rocm_version.h file structure: Patch version line not found.")
40        .trim()
41        .parse::<u32>()
42        .expect("Invalid rocm_version.h file structure: Couldn't parse patch version.");
43
44    Ok(Version {
45        major,
46        minor,
47        patch,
48    })
49}
50
51/// Reads the HIP header inside the rocm folder that contains the HIP specific version
52pub fn get_hip_system_version(rocm_path: impl AsRef<Path>) -> std::io::Result<Version> {
53    let version_path = rocm_path.as_ref().join("include/hip/hip_version.h");
54    let version_file = std::fs::read_to_string(version_path)?;
55    let version_lines = version_file.lines().collect::<Vec<_>>();
56
57    let major = version_lines
58        .iter()
59        .find_map(|line| line.strip_prefix("#define HIP_VERSION_MAJOR "))
60        .expect("Invalid hip_version.h file structure: Major version line not found.")
61        .trim()
62        .parse::<u8>()
63        .expect("Invalid hip_version.h file structure: Couldn't parse major version.");
64    let minor = version_lines
65        .iter()
66        .find_map(|line| line.strip_prefix("#define HIP_VERSION_MINOR "))
67        .expect("Invalid hip_version.h file structure: Minor version line not found.")
68        .trim()
69        .parse::<u8>()
70        .expect("Invalid hip_version.h file structure: Couldn't parse minor version.");
71    let patch = version_lines
72        .iter()
73        .find_map(|line| line.strip_prefix("#define HIP_VERSION_PATCH "))
74        .expect("Invalid hip_version.h file structure: Patch version line not found.")
75        .trim()
76        .parse::<u32>()
77        .expect("Invalid hip_version.h file structure: Couldn't parse patch version.");
78
79    Ok(Version {
80        major,
81        minor,
82        patch,
83    })
84}