Skip to content

Commit e39ea57

Browse files
committed
simics-api-sys: add fallback to detect python headers/lib (1033)
1 parent 5afb179 commit e39ea57

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed

simics-api-sys/build.rs

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,31 @@ pub mod common {
450450

451451
let bindings = Builder::default()
452452
.clang_arg(
453-
subdir(base_dir_path.as_ref().join(HOST_DIRNAME).join("include")).and_then(
454-
|p| {
453+
subdir(base_dir_path.as_ref().join(HOST_DIRNAME).join("include"))
454+
.and_then(|p| {
455455
p.to_str()
456456
.map(|s| format!("-I{}", s))
457457
.ok_or_else(|| anyhow!("Could not convert path to string"))
458-
},
459-
)?,
458+
})
459+
.or_else(|_| {
460+
// Fallback for Simics 7.28.0+ where Python headers are in separate package (1033)
461+
println!("cargo:warning=Traditional Python include path not found, trying Simics Python package fallback");
462+
let parent_dir = base_dir_path.as_ref().parent().unwrap();
463+
let python_include_path = parent_dir
464+
.join("simics-python-7.10.0")
465+
.join(HOST_DIRNAME)
466+
.join("include");
467+
if python_include_path.exists() {
468+
subdir(&python_include_path)
469+
.and_then(|p| {
470+
p.to_str()
471+
.map(|s| format!("-I{}", s))
472+
.ok_or_else(|| anyhow!("Could not convert path to string"))
473+
})
474+
} else {
475+
bail!("Python include directory not found at {}", python_include_path.display())
476+
}
477+
})?,
460478
)
461479
.clang_arg(format!("-I{}", &wrapper_include_path))
462480
.clang_arg("-fretain-comments-from-system-headers")
@@ -774,37 +792,52 @@ pub mod common {
774792

775793
let libvtutils = bin_dir.join("libvtutils.so").canonicalize()?;
776794

777-
let sys_lib_dir = base_dir_path
778-
.join(HOST_DIRNAME)
779-
.join("sys")
780-
.join("lib")
781-
.canonicalize()?;
795+
// Try both traditional and fallback sys/lib directories
796+
let sys_lib_dirs = vec![
797+
base_dir_path.join(HOST_DIRNAME).join("sys").join("lib"),
798+
base_dir_path.parent().unwrap().join("simics-python-7.10.0").join(HOST_DIRNAME).join("sys").join("lib")
799+
];
800+
801+
let mut libpython_found = None;
802+
let mut used_sys_lib_dir = None;
803+
804+
for sys_lib_path in &sys_lib_dirs {
805+
println!("cargo:warning=Trying libpython path: {}", sys_lib_path.display());
806+
if let Ok(sys_lib_dir) = sys_lib_path.canonicalize() {
807+
if let Some(libpython_path) = read_dir(&sys_lib_dir)
808+
.ok()
809+
.and_then(|entries| entries
810+
.filter_map(|p| p.ok())
811+
.filter(|p| p.path().is_file())
812+
.filter(|p| {
813+
let path = p.path();
814+
let Some(file_name) = path.file_name() else {
815+
return false;
816+
};
817+
let Some(file_name) = file_name.to_str() else {
818+
return false;
819+
};
820+
file_name.starts_with("libpython")
821+
&& file_name.contains(".so")
822+
&& file_name != "libpython3.so"
823+
})
824+
.map(|p| p.path())
825+
.next())
826+
{
827+
println!("cargo:warning=Found libpython in: {}", sys_lib_dir.display());
828+
libpython_found = Some(libpython_path);
829+
used_sys_lib_dir = Some(sys_lib_dir);
830+
break;
831+
}
832+
}
833+
}
782834

783-
let libpython = sys_lib_dir.join(
784-
read_dir(&sys_lib_dir)?
785-
.filter_map(|p| p.ok())
786-
.filter(|p| p.path().is_file())
787-
.filter(|p| {
788-
let path = p.path();
789-
790-
let Some(file_name) = path.file_name() else {
791-
return false;
792-
};
793-
794-
let Some(file_name) = file_name.to_str() else {
795-
return false;
796-
};
797-
798-
file_name.starts_with("libpython")
799-
&& file_name.contains(".so")
800-
&& file_name != "libpython3.so"
801-
})
802-
.map(|p| p.path())
803-
.next()
804-
.ok_or_else(|| {
805-
anyhow!("No libpythonX.XX.so.X.X found in {}", sys_lib_dir.display())
806-
})?,
807-
);
835+
let (sys_lib_dir, libpython_path) = match (used_sys_lib_dir, libpython_found) {
836+
(Some(dir), Some(path)) => (dir, path),
837+
_ => return Err(anyhow!("No libpythonX.XX.so.X.X found in any sys/lib directory")),
838+
};
839+
840+
let libpython = sys_lib_dir.join(libpython_path);
808841

809842
println!(
810843
"cargo:rustc-link-lib=dylib:+verbatim={}",

0 commit comments

Comments
 (0)