From 862cc155bd13ba04972d989265de8b5799a94d4f Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 27 Jan 2025 22:29:45 +0000 Subject: [PATCH] Make symlinks relative --- README.md | 13 +------------ xtask/src/main.rs | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 16fb681..3016924 100644 --- a/README.md +++ b/README.md @@ -51,18 +51,7 @@ Not supported * Git clone the repo (make sure to use `--recursive` option to fetch submodules): `git clone --recursive https://github.com/vosen/ZLUDA.git` * Enter freshly cloned `ZLUDA` directory and build with cargo (this takes a while): -`cargo build --release` - -### Linux - -If you are building on Linux you must also symlink the ZLUDA output binaries after ZLUDA build finishes: -``` -cd target/release -ln -s libnvcuda.so libcuda.so -ln -s libnvcuda.so libcuda.so.1 -ln -s libnvml.so libnvidia-ml.so -ln -s libnvml.so libnvidia-ml.so.1 -``` +`cargo xtask --release` ## Contributing diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 9484979..a970576 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -43,15 +43,25 @@ struct Cargo { struct Project { name: String, + clib_name: Option, meta: ZludaMetadata, } impl Project { fn try_new(p: Package) -> Option { + let name = p.name; + let clib_name = p.targets.into_iter().find_map(|target| { + if target.is_cdylib() { + Some(target.name) + } else { + None + } + }); serde_json::from_value::>(p.metadata) .unwrap() .map(|m| Self { - name: p.name, + name, + clib_name, meta: m.zluda, }) } @@ -139,6 +149,8 @@ fn zip() { #[cfg(unix)] mod os { + use std::path::PathBuf; + pub fn make_symlinks( target_directory: std::path::PathBuf, projects: Vec, @@ -147,15 +159,28 @@ mod os { use std::fs; use std::os::unix::fs as unix_fs; for project in projects.iter() { - let mut target = target_directory.clone(); - target.extend([&*profile, &format!("lib{}.so", project.name)]); + let clib_name = match project.clib_name { + Some(ref l) => l, + None => continue, + }; + let libname = format!("lib{}.so", clib_name); for source in project.meta.linux_symlinks.iter() { + let relative_link = PathBuf::from(source); + let ancestors = relative_link.as_path().ancestors().count(); + let mut target = std::iter::repeat_with(|| "../").take(ancestors - 2).fold( + PathBuf::new(), + |mut buff, segment| { + buff.push(segment); + buff + }, + ); let mut link = target_directory.clone(); link.extend([&*profile, source]); let mut dir = link.clone(); assert!(dir.pop()); fs::create_dir_all(dir).unwrap(); fs::remove_file(&link).ok(); + target.push(&*libname); unix_fs::symlink(&target, link).unwrap(); } }