Start working on build script

This commit is contained in:
Andrzej Janik 2025-01-27 17:56:19 +01:00
parent 9c0747a5f7
commit ea1e8ed028
11 changed files with 1334 additions and 1 deletions

View file

@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"

1197
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -18,6 +18,7 @@ members = [
"ptx_parser",
"ptx_parser_macros",
"ptx_parser_macros_impl",
"xtask",
"zluda_bindgen",
]
@ -27,3 +28,6 @@ default-members = ["zluda", "zluda_ml", "zluda_inject", "zluda_redirect"]
inherits = "release"
codegen-units = 1
lto = true
[profile.dev.package.xtask]
opt-level = 2

11
xtask/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "xtask"
version = "0.0.0"
authors = ["Andrzej Janik <vosen@vosen.pl>"]
edition = "2021"
[dependencies]
bpaf = { version = "0.9.15", features = ["derive"] }
cargo_metadata = "0.19.1"
serde = "1.0.217"
serde_json = "1.0.137"

106
xtask/src/main.rs Normal file
View file

@ -0,0 +1,106 @@
use bpaf::{Args, Bpaf, Parser};
use cargo_metadata::{MetadataCommand, Package};
use serde::Deserialize;
use std::{env, ffi::OsString, process::Command};
#[derive(Debug, Clone, Bpaf)]
struct Build {
#[bpaf(any("CARGO", not_help), many)]
/// Arguments to pass to cargo, e.g. `--release` for release build
cargo_arguments: Vec<OsString>,
}
fn not_help(s: OsString) -> Option<OsString> {
if s == "-h" || s == "--help" {
None
} else {
Some(s)
}
}
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
enum Options {
#[bpaf(command)]
/// Compile ZLUDA (default command)
Build(#[bpaf(external(build))] Build),
#[bpaf(command)]
/// Build ZLUDA package
Zip,
}
struct Project {
name: String,
meta: ZludaMetadata,
}
impl Project {
fn try_new(p: Package) -> Option<Project> {
serde_json::from_value::<Option<Metadata>>(p.metadata)
.unwrap()
.map(|m| Self {
name: p.name,
meta: m.zluda,
})
}
}
#[derive(Deserialize)]
struct Metadata {
zluda: ZludaMetadata,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ZludaMetadata {
#[serde(default)]
windows_only: bool,
#[serde(default)]
debug_only: bool,
}
fn main() {
let options = match options().run_inner(Args::current_args()) {
Ok(b) => b,
Err(err) => match build().to_options().run_inner(Args::current_args()) {
Ok(b) => Options::Build(b),
Err(_) => {
err.print_message(100);
std::process::exit(err.exit_code());
}
},
};
match options {
Options::Build(b) => compile(b),
Options::Zip => zip(),
}
}
fn compile(b: Build) {
let meta = MetadataCommand::new().no_deps().exec().unwrap();
let is_release = b
.cargo_arguments
.iter()
.any(|a| a == "-r" || a == "--release");
let projects = meta.packages.into_iter().filter_map(Project::try_new);
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let mut command = Command::new(&cargo);
command.arg("build");
command.arg("--locked");
for project in projects {
if project.meta.windows_only && cfg!(not(windows)) {
continue;
}
if project.meta.debug_only && is_release {
continue;
}
command.arg("--package");
command.arg(&project.name);
}
command.args(b.cargo_arguments);
command.status().unwrap();
}
fn zip() {
todo!()
}

View file

@ -24,3 +24,5 @@ rustc-hash = "1.1"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["heapapi", "std"] }
[package.metadata.zluda]

View file

@ -29,3 +29,6 @@ detours-sys = { path = "../detours-sys" }
[target.'cfg(not(windows))'.dependencies]
libc = "0.2"
[package.metadata.zluda]
debug_only = true

View file

@ -10,7 +10,7 @@ use winapi::{
um::libloaderapi::{GetProcAddress, LoadLibraryW},
};
use cuda_types::CUuuid;
use cuda_types::cuda::CUuuid;
pub(crate) const LIBCUDA_DEFAULT_PATH: &'static str = "C:\\Windows\\System32\\nvcuda.dll";
const LOAD_LIBRARY_NO_REDIRECT: &'static [u8] = b"ZludaLoadLibraryW_NoRedirect\0";

View file

@ -19,3 +19,6 @@ detours-sys = { path = "../detours-sys" }
zluda_redirect = { path = "../zluda_redirect" }
zluda_dump = { path = "../zluda_dump" }
zluda_ml = { path = "../zluda_ml" }
[package.metadata.zluda]
windows_only = true

View file

@ -11,3 +11,5 @@ crate-type = ["cdylib"]
[dependencies]
cuda_base = { path = "../cuda_base" }
cuda_types = { path = "../cuda_types" }
[package.metadata.zluda]

View file

@ -11,3 +11,6 @@ crate-type = ["cdylib"]
detours-sys = { path = "../detours-sys" }
wchar = "0.6"
winapi = { version = "0.3", features = [ "sysinfoapi", "memoryapi", "processthreadsapi", "winbase", "winnt", "winerror", "libloaderapi", "tlhelp32", "handleapi", "std"] }
[package.metadata.zluda]
windows_only = true