@@ -5,32 +5,32 @@ use regex::Regex;
5
5
pub const HIPCONFIG : & str = "hipconfig" ;
6
6
7
7
/// Retrieve the ROCM_PATH with `hipconfig -R` command.
8
- pub fn get_rocm_path ( ) -> String {
9
- exec_hipconfig ( & [ "-R" ] ) . unwrap ( )
8
+ pub fn get_rocm_path ( ) -> std :: io :: Result < String > {
9
+ exec_hipconfig ( & [ "-R" ] )
10
10
}
11
11
12
12
/// Retrieve the HIP_PATH with `hipconfig -p` command.
13
- pub fn get_hip_path ( ) -> String {
14
- exec_hipconfig ( & [ "-p" ] ) . unwrap ( )
13
+ pub fn get_hip_path ( ) -> std :: io :: Result < String > {
14
+ exec_hipconfig ( & [ "-p" ] )
15
15
}
16
16
17
17
/// Retrieve the HIP patch number from the `hipconfig --version` output
18
- pub fn get_hip_patch_version ( ) -> String {
19
- let hip_version = exec_hipconfig ( & [ "--version" ] ) . unwrap ( ) ;
18
+ pub fn get_hip_patch_version ( ) -> std :: io :: Result < String > {
19
+ let hip_version = exec_hipconfig ( & [ "--version" ] ) ? ;
20
20
parse_hip_patch_number ( & hip_version)
21
21
}
22
22
23
23
/// Return the HIP path suitable for LD_LIBRARY_PATH.
24
- pub fn get_hip_ld_library_path ( ) -> String {
25
- let rocm_path = get_rocm_path ( ) ;
26
- let lib_dir = get_hip_library_directory_name ( & rocm_path) ;
27
- format ! ( "{rocm_path}/{lib_dir}" )
24
+ pub fn get_hip_ld_library_path ( ) -> std :: io :: Result < String > {
25
+ let rocm_path = get_rocm_path ( ) ? ;
26
+ let lib_dir = get_hip_library_directory_name ( & rocm_path) ? ;
27
+ Ok ( format ! ( "{rocm_path}/{lib_dir}" ) )
28
28
}
29
29
30
30
/// Return the include path for HIP
31
- pub fn get_hip_include_path ( ) -> String {
32
- let hip_path = get_hip_path ( ) ;
33
- format ! ( "{hip_path}/include" )
31
+ pub fn get_hip_include_path ( ) -> std :: io :: Result < String > {
32
+ let hip_path = get_hip_path ( ) ? ;
33
+ Ok ( format ! ( "{hip_path}/include" ) )
34
34
}
35
35
36
36
/// Execute hipconfig
@@ -46,8 +46,9 @@ fn exec_hipconfig(args: &[&str]) -> std::io::Result<String> {
46
46
) ;
47
47
}
48
48
}
49
- Err ( ref e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound => {
50
- panic ! ( "Could not find '{HIPCONFIG}' in your PATH. You should install ROCm HIP or ensure '{HIPCONFIG}' is available. Fro more information please visit https://rocm.docs.amd.com/projects/install-on-linux/en/latest/." ) ;
49
+ Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound => {
50
+ println ! ( "cargo::warning=Could not find '{HIPCONFIG}' in your PATH. You should install ROCm HIP or ensure '{HIPCONFIG}' is available. For more information please visit https://rocm.docs.amd.com/projects/install-on-linux/en/latest/." ) ;
51
+ Err ( e)
51
52
}
52
53
Err ( e) => panic ! (
53
54
"Failed to run '{HIPCONFIG}' with args '{args:?}', reason: {}" ,
@@ -57,33 +58,33 @@ fn exec_hipconfig(args: &[&str]) -> std::io::Result<String> {
57
58
}
58
59
59
60
/// extract the HIP patch number from hipconfig version output
60
- fn parse_hip_patch_number ( version : & str ) -> String {
61
+ fn parse_hip_patch_number ( version : & str ) -> std :: io :: Result < String > {
61
62
let re = Regex :: new ( r"\d+\.\d+\.(\d+)-" ) . expect ( "regex should compile" ) ;
62
63
if let Some ( caps) = re. captures ( version) {
63
64
if let Some ( m) = caps. get ( 1 ) {
64
- return m. as_str ( ) . to_string ( ) ;
65
+ return Ok ( m. as_str ( ) . to_string ( ) ) ;
65
66
}
66
67
}
67
68
// cannot parse for the patch number
68
69
panic ! ( "Error retrieving HIP patch number from value '{version}'" )
69
70
}
70
71
71
72
/// Return the library directory using hipconfig
72
- fn get_hip_library_directory_name ( rocm_path : & str ) -> String {
73
- let clang_path = exec_hipconfig ( & [ "-l" ] ) . unwrap ( ) ;
73
+ fn get_hip_library_directory_name ( rocm_path : & str ) -> std :: io :: Result < String > {
74
+ let clang_path = exec_hipconfig ( & [ "-l" ] ) ? ;
74
75
parse_hip_library_directory_name ( rocm_path, & clang_path)
75
76
}
76
77
77
78
/// Parse out the first subdirectory under the ROCm path
78
- fn parse_hip_library_directory_name ( rocm_path : & str , clang_path : & str ) -> String {
79
+ fn parse_hip_library_directory_name ( rocm_path : & str , clang_path : & str ) -> std :: io :: Result < String > {
79
80
let rocm = rocm_path. trim ( ) . trim_end_matches ( '/' ) ;
80
81
let clang = clang_path. trim ( ) ;
81
82
// Build a regex like "^/opt/rocm/([^/]+)"
82
83
let pattern = format ! ( r"^{}\/([^/]+)" , regex:: escape( rocm) ) ;
83
84
let re = Regex :: new ( & pattern) . expect ( "regex should compile" ) ;
84
85
85
86
if let Some ( caps) = re. captures ( clang) {
86
- return caps[ 1 ] . to_string ( ) ;
87
+ return Ok ( caps[ 1 ] . to_string ( ) ) ;
87
88
}
88
89
panic ! ( "Cannot retrieve the name of the HIP library directoy." ) ;
89
90
}
@@ -100,7 +101,7 @@ mod tests {
100
101
#[ case:: missing_hyphen( "6.4.43482" , None ) ]
101
102
#[ case:: completely_invalid( "no numbers" , None ) ]
102
103
fn test_parse_hip_patch_number ( #[ case] input : & str , #[ case] expected : Option < & str > ) {
103
- let result = std :: panic :: catch_unwind ( || parse_hip_patch_number ( input) ) ;
104
+ let result = parse_hip_patch_number ( input) ;
104
105
match expected {
105
106
Some ( expected_str) => {
106
107
let output = result. expect ( "should not panic for valid version" ) ;
@@ -126,7 +127,7 @@ mod tests {
126
127
#[ case] clang : & str ,
127
128
#[ case] expected : Option < & str > ,
128
129
) {
129
- let result = std :: panic :: catch_unwind ( || parse_hip_library_directory_name ( rocm, clang) ) ;
130
+ let result = parse_hip_library_directory_name ( rocm, clang) ;
130
131
match expected {
131
132
Some ( expected_dir) => {
132
133
// For valid inputs, it must not panic and return the directory name
0 commit comments