Build improvements (#206)

* Allow to create .zip package on Windows
* Allow to create .tar.gz package on Linux
* Add configuration for post-build Github CI
This commit is contained in:
Andrzej Janik 2024-04-28 01:22:43 +02:00 committed by GitHub
parent 5d5f7cca75
commit 995bc95174
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 2891 additions and 140 deletions

67
.github/workflows/rust.yml vendored Normal file
View file

@ -0,0 +1,67 @@
name: Rust
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
CARGO_TERM_COLOR: always
ROCM_VERSION: "5.7.3"
jobs:
build_lin:
name: Build and publish (Linux)
runs-on: ubuntu-20.04
steps:
- uses: jlumbroso/free-disk-space@main
- name: Install ROCm
run: |
sudo mkdir --parents --mode=0755 /etc/apt/keyrings
sudo sh -c 'wget https://repo.radeon.com/rocm/rocm.gpg.key -O - | gpg --dearmor | tee /etc/apt/keyrings/rocm.gpg > /dev/null'
sudo sh -c 'echo deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/rocm/apt/${{ env.ROCM_VERSION }} focal main > /etc/apt/sources.list.d/rocm.list'
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends rocm-smi-lib hip-runtime-amd comgr hipblaslt-dev hipfft-dev rocblas-dev rocsolver-dev rocsparse-dev miopen-hip-dev rocm-device-libs
echo 'export PATH="$PATH:/opt/rocm/bin"' | sudo tee /etc/profile.d/rocm.sh
echo '/opt/rocm/lib' | sudo tee /etc/ld.so.conf.d/rocm.conf
sudo ldconfig
- uses: actions/checkout@v4
with:
submodules: true
- uses: Swatinem/rust-cache@v2
- name: Build
# We use tar to unpack .tar.gz we've created because Github actions/upload-artifact
# is broken and will _always_ zip your artifact (even if it is a single file).
# See here: https://github.com/actions/upload-artifact/issues/39
# and here: https://github.com/actions/upload-artifact/issues/109
run: |
cargo xtask zip -r
tar -xzf target/release/zluda.tar.gz -C target/release
# https://stackoverflow.com/a/64195658
- name: Set revision hash
run: echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Upload
uses: actions/upload-artifact@v4
with:
name: zluda-linux-${{ env.SHORT_SHA }}
path: target/release/zluda
build_win:
name: Build and publish (Windows)
runs-on: windows-2019
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: Swatinem/rust-cache@v2
- name: Build
run: |
cargo xtask zip -r
Expand-Archive -Path target/release/zluda.zip -DestinationPath target/release
# https://stackoverflow.com/a/74033027
- name: Set revision hash
run: echo "SHORT_SHA=$("${{ github.sha }}".SubString(0, 7))" >> $env:GITHUB_ENV
- name: Upload
uses: actions/upload-artifact@v4
with:
name: zluda-windows-${{ env.SHORT_SHA }}
path: target/release/zluda

7
.gitignore vendored
View file

@ -1,5 +1,4 @@
target/
Cargo.lock
.vscode/
target/
.vscode/
.idea/

2561
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -58,4 +58,4 @@ opt-level = 3
opt-level = 3
[profile.dev.package.xtask]
opt-level = 3
opt-level = 2

View file

@ -1,57 +0,0 @@
[config]
default_to_workspace = false
skip_core_tasks = true
[tasks.build]
run_task = [
{ name = "build-windows", condition = { platforms = ["windows"] } },
{ name = "build-linux", condition = { platforms = ["linux"] } },
]
[tasks.build-windows]
command = "cargo"
args = [
"build",
"-p", "offline_compiler",
"-p", "zluda_dump",
"-p", "zluda_inject",
"-p", "zluda_lib",
"-p", "zluda_ml",
"-p", "zluda_redirect",
]
[tasks.build-linux]
command = "cargo"
args = [
"build",
"-p", "offline_compiler",
"-p", "zluda_blas",
"-p", "zluda_blaslt",
"-p", "zluda_ccl",
"-p", "zluda_dnn",
"-p", "zluda_dump",
"-p", "zluda_fft",
"-p", "zluda_lib",
"-p", "zluda_ml",
"-p", "zluda_sparse",
]
[tasks.build-release]
command = "cargo"
args = [
"build",
"--release",
"-p", "offline_compiler",
"-p", "zluda_blas",
"-p", "zluda_blaslt",
"-p", "zluda_ccl",
"-p", "zluda_dnn",
"-p", "zluda_dump",
"-p", "zluda_fft",
"-p", "zluda_lib",
"-p", "zluda_ml",
"-p", "zluda_sparse",
]
[tasks.default]
alias = "build"

View file

@ -92,13 +92,16 @@ If you are dumping original CUDA use:
### Linux
Known bug: when dumping from original CUDA you should remove (or rename) all the files in `<ZLUDA_DIRECTORY>/dump` except `libcuda.so` and `libcuda.so.1`.
Use it like this:
If dumping from ZLUDA use it like this:
```
LD_LIBRARY_PATH="<ZLUDA_DIRECTORY>/dump:$LD_LIBRARY_PATH" <APPLICATION> <APPLICATION_ARGUMENTS>
```
If dumping from NVIDIA CUDA use it like this:
```
LD_LIBRARY_PATH="<ZLUDA_DIRECTORY>/dump_nvidia:$LD_LIBRARY_PATH" <APPLICATION> <APPLICATION_ARGUMENTS>
```
### Result
If all went well you should see lines like this in the console output and in the log file specified by `ZLUDA_DUMP_DIR`:

View file

@ -18,3 +18,4 @@ features = [
[package.metadata.zluda]
debug_only = true
skip_zip = true

View file

@ -11,4 +11,12 @@ cargo_metadata = "=0.17.0"
# cargo-platform is a cargo_metadata, version 0.1.6 requires rust 1.70 or higher
cargo-platform = "=0.1.5"
serde = "1.0.193"
serde_json = "1.0.108"
serde_json = "1.0.108"
time = { version = "=0.3.23", features = ["local-offset"] }
[target.'cfg(windows)'.dependencies]
zip = { version = "0.6.6", features = ["deflate", "time"], default-features = false }
[target.'cfg(unix)'.dependencies]
flate2 = { version = "1.0.28", features = ["cloudflare_zlib"], default-features = false }
tar = "0.4"

View file

@ -1,4 +1,5 @@
use argh::{EarlyExit, FromArgs, TopLevelCommand};
use cargo_metadata::camino::Utf8PathBuf;
use serde::Deserialize;
use std::{
env,
@ -60,7 +61,7 @@ struct BuildCommand {
}
#[derive(FromArgs)]
/// Package build artifacts into an archive (.zip or .tar.gz)
/// Compile ZLUDA and package binaries into an archive (.zip or .tar.gz)
#[argh(subcommand, name = "zip")]
struct ZipCommand {
/// use artifacts from release mode
@ -73,10 +74,15 @@ fn main() -> Result<(), DynError> {
let args: Arguments = argh::from_env();
std::process::exit(match args.command {
Subcommand::Build(BuildCommand { release }) => build(!release)?,
Subcommand::Zip(_) => panic!(),
Subcommand::Zip(ZipCommand { release }) => build_and_zip(!release),
})
}
fn build_and_zip(is_debug: bool) -> i32 {
let workspace = build_impl(is_debug).unwrap();
os::zip(workspace)
}
#[derive(Deserialize)]
struct ZludaMetadata {
zluda: Project,
@ -92,8 +98,6 @@ struct Project {
#[serde(skip_deserializing)]
kind: TargetKind,
#[serde(default)]
top_level: bool,
#[serde(default)]
windows_only: bool,
#[serde(default)]
linux_only: bool,
@ -104,9 +108,13 @@ struct Project {
#[serde(default)]
skip_dump_link: bool,
#[serde(default)]
skip_zip: bool,
#[serde(default)]
linux_names: Vec<String>,
#[serde(default)]
dump_names: Vec<String>,
#[serde(default)]
dump_nvidia_names: Vec<String>,
}
#[derive(Clone, Copy, Default, PartialEq, Debug)]
@ -116,14 +124,56 @@ enum TargetKind {
Cdylib,
}
struct Workspace {
pub cargo: String,
pub project_root: PathBuf,
pub projects: Vec<Project>,
pub target_directory: Utf8PathBuf,
}
impl Workspace {
fn open(is_debug: bool) -> Result<Self, DynError> {
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let project_root = Self::project_root()?;
let mut cmd = cargo_metadata::MetadataCommand::new();
cmd.cargo_path(&cargo).current_dir(&project_root).no_deps();
let cargo_metadata = cmd.exec()?;
let projects = cargo_metadata
.packages
.into_iter()
.filter_map(Project::new)
.filter(|p| !p.skip_build(is_debug))
.collect::<Vec<_>>();
let mut target_directory = cargo_metadata.target_directory;
target_directory.push(if is_debug { "debug" } else { "release" });
Ok(Workspace {
cargo,
project_root,
projects,
target_directory,
})
}
fn project_root() -> Result<PathBuf, DynError> {
Ok(Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(1)
.ok_or::<DynError>("CARGO_MANIFEST_DIR".into())?
.to_path_buf())
}
fn cargo_command(&self) -> Command {
let mut command = Command::new(&self.cargo);
command.current_dir(&self.project_root);
command
}
}
impl Project {
fn new(json_pkg: cargo_metadata::Package) -> Self {
let mut project = serde_json::from_value::<Option<ZludaMetadata>>(json_pkg.metadata)
.unwrap()
.map_or(Default::default(), |x| x.zluda);
if project != Default::default() {
project.top_level = true;
}
fn new(json_pkg: cargo_metadata::Package) -> Option<Self> {
let project_metadata =
serde_json::from_value::<Option<ZludaMetadata>>(json_pkg.metadata).unwrap()?;
let mut project = project_metadata.zluda;
project.name = json_pkg.name;
if let Some((target_name, kind)) = json_pkg.targets.into_iter().find_map(|t| {
match t.kind.first().map(std::ops::Deref::deref) {
@ -135,13 +185,10 @@ impl Project {
project.target_name = target_name;
project.kind = kind;
}
project
Some(project)
}
fn skip_build(&self, is_debug: bool) -> bool {
if !self.top_level {
return true;
}
if self.broken {
return true;
}
@ -159,67 +206,76 @@ impl Project {
}
fn build(is_debug: bool) -> Result<i32, DynError> {
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let project_root = project_root()?;
let mut cmd = cargo_metadata::MetadataCommand::new();
cmd.cargo_path(&cargo).current_dir(&project_root).no_deps();
let metadata = cmd.exec()?;
let projects = metadata
.packages
.into_iter()
.map(Project::new)
.filter(|p| !p.skip_build(is_debug))
.collect::<Vec<_>>();
let mut command = Command::new(&cargo);
command.current_dir(&project_root).arg("build");
projects.iter().fold(&mut command, |command, proj| {
command.args(["-p", &proj.name])
});
build_impl(is_debug)?;
Ok(0)
}
fn build_impl(is_debug: bool) -> Result<Workspace, DynError> {
let workspace = Workspace::open(is_debug)?;
let mut command = workspace.cargo_command();
command.arg("build");
workspace
.projects
.iter()
.fold(&mut command, |command, proj| {
command.args(["-p", &proj.name])
});
if !is_debug {
command.arg("--release");
}
let build_result = command.status()?.code().unwrap();
if build_result != 0 {
return Ok(build_result);
return Err(format!("{command:?} failed with exit code {build_result}").into());
}
os::create_dump_dir_and_symlinks(is_debug, metadata.target_directory, projects);
Ok(0)
os::create_dump_dir_and_symlinks(&workspace);
Ok(workspace)
}
fn project_root() -> Result<PathBuf, DynError> {
Ok(Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(1)
.ok_or::<DynError>("CARGO_MANIFEST_DIR".into())?
.to_path_buf())
}
impl TargetKind {
#[cfg(unix)]
fn prefix(self) -> &'static str {
match self {
TargetKind::Binary => "",
TargetKind::Cdylib => "lib",
}
}
#[cfg(not(unix))]
mod os {
use super::Project;
use cargo_metadata::camino::Utf8PathBuf;
#[cfg(unix)]
fn suffix(self) -> &'static str {
match self {
TargetKind::Binary => "",
TargetKind::Cdylib => ".so",
}
}
// This is 100% intentional, we don't want symlinks on Windows since
// we use completely different scheme for injections here
pub(crate) fn create_dump_dir_and_symlinks(_: bool, _: Utf8PathBuf, _: Vec<Project>) {}
#[cfg(windows)]
fn suffix(self) -> &'static str {
match self {
TargetKind::Binary => ".exe",
TargetKind::Cdylib => ".dll",
}
}
}
#[cfg(unix)]
mod os {
use super::{Project, TargetKind};
use crate::Workspace;
use cargo_metadata::camino::Utf8PathBuf;
use flate2::{write::GzEncoder, Compression};
use std::{
fs::File,
time::{Duration, SystemTime},
};
pub(crate) fn create_dump_dir_and_symlinks(
is_debug: bool,
mut target_directory: Utf8PathBuf,
projects: Vec<Project>,
) {
pub(crate) fn create_dump_dir_and_symlinks(workspace: &Workspace) {
use std::fs;
target_directory.push(if is_debug { "debug" } else { "release" });
let mut dump_dir = target_directory.clone();
let mut dump_dir = workspace.target_directory.clone();
dump_dir.push("dump");
fs::create_dir_all(&dump_dir).unwrap();
for project in projects {
let mut dump_nvidia_dir = dump_dir.clone();
dump_nvidia_dir.set_file_name("dump_nvidia");
fs::create_dir_all(&dump_nvidia_dir).unwrap();
for project in workspace.projects.iter() {
let dst = format!(
"{}{}{}",
project.kind.prefix(),
@ -227,15 +283,18 @@ mod os {
project.kind.suffix()
);
let dump_dst = format!("../{}", dst);
for src_file in project.linux_names {
force_symlink(&dst, &target_directory, &src_file);
for src_file in project.linux_names.iter() {
force_symlink(&dst, &workspace.target_directory, src_file);
if project.skip_dump_link {
continue;
}
force_symlink(&dump_dst, &dump_dir, &src_file);
force_symlink(&dump_dst, &dump_dir, src_file);
}
for src_file in project.dump_names {
force_symlink(&dump_dst, &dump_dir, &src_file);
for src_file in project.dump_names.iter() {
force_symlink(&dump_dst, &dump_dir, src_file);
}
for src_file in project.dump_nvidia_names.iter() {
force_symlink(&dump_dst, &dump_nvidia_dir, src_file);
}
}
}
@ -263,19 +322,128 @@ mod os {
}
}
impl TargetKind {
fn prefix(self) -> &'static str {
match self {
TargetKind::Binary => "",
TargetKind::Cdylib => "lib",
pub fn zip(workspace: Workspace) -> i32 {
let mut target_file = workspace.target_directory.clone();
target_file.push("zluda.tar.gz");
let gz_file = File::create(target_file).unwrap();
let gz = GzEncoder::new(gz_file, Compression::default());
let mut tar = tar::Builder::new(gz);
let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or(Duration::ZERO);
for project in workspace.projects {
if project.skip_zip {
continue;
}
let mut src_file = File::open(format!(
"{}/{}{}{}",
&workspace.target_directory,
project.kind.prefix(),
project.target_name,
project.kind.suffix()
))
.unwrap();
let file_name = format!(
"{}{}{}",
project.kind.prefix(),
project.target_name,
project.kind.suffix()
);
tar.append_file(format!("zluda/{file_name}"), &mut src_file)
.unwrap();
for linux_name in project.linux_names.iter() {
let mut header = tar_header_symlink(time);
tar.append_link(&mut header, format!("zluda/{}", linux_name), &file_name)
.unwrap();
if project.skip_dump_link {
continue;
}
let mut header = tar_header_symlink(time);
tar.append_link(
&mut header,
format!("zluda/dump/{}", linux_name),
format!("../{file_name}"),
)
.unwrap();
}
for dump_name in project.dump_names.iter() {
let mut header = tar_header_symlink(time);
tar.append_link(
&mut header,
format!("zluda/dump/{}", dump_name),
format!("../{file_name}"),
)
.unwrap();
}
for dump_name in project.dump_nvidia_names.iter() {
let mut header = tar_header_symlink(time);
tar.append_link(
&mut header,
format!("zluda/dump_nvidia/{}", dump_name),
format!("../{file_name}"),
)
.unwrap();
}
}
tar.finish().unwrap();
0
}
fn suffix(self) -> &'static str {
match self {
TargetKind::Binary => "",
TargetKind::Cdylib => ".so",
}
}
fn tar_header_symlink(time: Duration) -> tar::Header {
let mut header = tar::Header::new_gnu();
header.set_mtime(time.as_secs());
header.set_entry_type(tar::EntryType::Symlink);
header
}
}
#[cfg(windows)]
mod os {
use crate::Workspace;
use std::{convert::TryFrom, fs::File};
// This is 100% intentional, we don't want symlinks on Windows since
// we use a completely different scheme for injections there
pub(crate) fn create_dump_dir_and_symlinks(_: &Workspace) {}
pub(crate) fn zip(workspace: Workspace) -> i32 {
fn get_zip_entry_options(
f: &File,
time_offset: time::UtcOffset,
) -> Option<zip::write::FileOptions> {
let time = f.metadata().unwrap().modified().unwrap();
let time = time::OffsetDateTime::from(time).to_offset(time_offset);
Some(
zip::write::FileOptions::default()
.last_modified_time(zip::DateTime::try_from(time).unwrap()),
)
}
let mut target_file = workspace.target_directory.clone();
target_file.push("zluda.zip");
let zip_archive = File::create(target_file).unwrap();
let mut zip_writer = zip::write::ZipWriter::new(zip_archive);
let time_offset = time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);
for p in workspace.projects {
if p.skip_zip {
continue;
}
let mut src_file = File::open(format!(
"{}/{}{}",
&workspace.target_directory,
p.target_name,
p.kind.suffix()
))
.unwrap();
zip_writer
.start_file(
format!("zluda/{}{}", p.target_name, p.kind.suffix()),
get_zip_entry_options(&src_file, time_offset)
.unwrap_or(zip::write::FileOptions::default()),
)
.unwrap();
std::io::copy(&mut src_file, &mut zip_writer).unwrap();
}
zip_writer.finish().unwrap();
0
}
}

View file

@ -26,3 +26,4 @@ features = [
[package.metadata.zluda]
debug_only = true
windows_only = true
skip_zip = true

View file

@ -44,3 +44,4 @@ rand = "0.8.5"
# Nominally debug_only, but useful for power users
[package.metadata.zluda]
dump_names = ["libcuda.so", "libcuda.so.1"]
dump_nvidia_names = ["libcuda.so", "libcuda.so.1"]

View file

@ -15,5 +15,4 @@ atiadlxx-sys = { path = "../atiadlxx-sys" }
rocm_smi-sys = { path = "../rocm_smi-sys" }
[package.metadata.zluda]
top_level = true
linux_names = ["libnvidia-ml.so", "libnvidia-ml.so.1"]