build: nicer build script messages

This commit is contained in:
chyyran 2024-09-15 12:43:24 -04:00 committed by Ronny Chan
parent 6e60dd6fa0
commit e0c97f77b4
4 changed files with 95 additions and 23 deletions

20
Cargo.lock generated
View file

@ -403,6 +403,15 @@ dependencies = [
"wayland-client",
]
[[package]]
name = "carlog"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95faf7476605bbef1fdf740eaa3f7f2b97b70fbed0aada1ee0c040cff66c84cf"
dependencies = [
"colored",
]
[[package]]
name = "cbindgen"
version = "0.26.0"
@ -567,6 +576,16 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colored"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
dependencies = [
"lazy_static",
"windows-sys 0.48.0",
]
[[package]]
name = "com"
version = "0.6.0"
@ -1476,6 +1495,7 @@ dependencies = [
name = "librashader-build-script"
version = "0.1.0"
dependencies = [
"carlog",
"cbindgen",
"clap 4.1.0",
]

View file

@ -252,7 +252,7 @@ in both the Rust and C API without an increase to either `LIBRASHADER_CURRENT_VE
When building against nightly Rust, the following MSRV policy is enforced for unstable library features.
* Windows and macOS: **latest** nightly
* Linux: **1.74**
* Linux: **1.76**
A CI job runs weekly to ensure librashader continues to build on nightly.

View file

@ -8,7 +8,7 @@ publish = false
[dependencies]
cbindgen = "0.26.0"
clap = { version = "=4.1.0", features = ["derive"] }
carlog = "0.1.0"
[package.metadata.release]
release = false

View file

@ -1,3 +1,4 @@
use carlog::*;
use clap::Parser;
use std::fs::File;
use std::io::{BufWriter, Write};
@ -29,7 +30,7 @@ pub fn main() -> ExitCode {
let profile = args.profile;
let crate_dir = Path::new("librashader-capi");
println!("INFO: Building librashader C API...");
carlog_info!("Building", "librashader C API");
let mut cmd = Command::new("cargo");
cmd.arg("build");
@ -49,14 +50,25 @@ pub fn main() -> ExitCode {
}
if args.stable {
println!("WARN: Building librashader with stable Rust compatibility. C headers will not be generated.");
carlog_warning!(
"building librashader with stable Rust compatibility"
);
carlog_warning!(
"C headers will not be generated"
);
cmd.args(["--features", "stable"]);
}
if !args.cargoflags.is_empty() {
cmd.args(args.cargoflags);
}
let status = cmd.status().expect("Failed to build librashader-capi");
let Ok(status) = cmd.status().inspect_err(|err| {
carlog_error!("failed to build librashader-capi");
carlog_error!(format!("{err}"));
}) else {
return ExitCode::FAILURE;
};
if !status.success() {
return ExitCode::from(status.code().unwrap_or(1) as u8);
}
@ -66,43 +78,76 @@ pub fn main() -> ExitCode {
output_dir = PathBuf::from(format!("target/{}/{}", target, profile));
}
let output_dir = output_dir
.canonicalize()
.expect("Could not find output directory.");
let Ok(output_dir) = output_dir.canonicalize() else {
carlog_error!("could not find output directory");
println!("help: are you running the build script from the repository root?");
return ExitCode::FAILURE;
};
if args.stable {
println!("WARN: C header generation is not supported when building for stable Rust.");
carlog_warning!("generating C headers is not supported when building for stable Rust");
} else {
println!("INFO: Generating C headers...");
carlog_info!("Generating", "librashader C API headers");
// Create headers.
let mut buf = BufWriter::new(Vec::new());
cbindgen::generate(crate_dir)
.expect("Unable to generate bindings")
.write(&mut buf);
let Ok(bindings) = cbindgen::generate(crate_dir).inspect_err(|err| {
carlog_error!("unable to generate C API headers");
carlog_error!(format!("{err}"));
}) else {
return ExitCode::FAILURE;
};
bindings.write(&mut buf);
let bytes = buf.into_inner().expect("Unable to extract bytes");
let string = String::from_utf8(bytes).expect("Unable to create string");
File::create(output_dir.join("librashader.h"))
.expect("Unable to open file")
.write_all(string.as_bytes())
.expect("Unable to write bindings.");
let Ok(mut file) = File::create(output_dir.join("librashader.h")).inspect_err(|err| {
carlog_error!("unable to open librashader.h");
carlog_error!(format!("{err}"));
}) else {
return ExitCode::FAILURE;
};
let Ok(_) = file.write_all(string.as_bytes()).inspect_err(|err| {
carlog_error!("unable to write to librashader.h");
carlog_error!(format!("{err}"));
}) else {
return ExitCode::FAILURE;
};
}
println!("INFO: Moving artifacts...");
carlog_info!("Moving", "built artifacts");
if cfg!(target_os = "macos") {
let artifacts = &["liblibrashader_capi.dylib", "liblibrashader_capi.a"];
for artifact in artifacts {
let ext = artifact.strip_prefix("lib").unwrap();
let ext = ext.replace("_capi", "");
fs::rename(output_dir.join(artifact), output_dir.join(ext)).unwrap();
let Ok(_) =
fs::rename(output_dir.join(artifact), output_dir.join(&ext)).inspect_err(|err| {
carlog_error!(format!("Unable to rename {artifact} to {}", &ext));
carlog_error!(format!("{err}"));
})
else {
return ExitCode::FAILURE;
};
carlog_ok!("Renamed", format!("{artifact} to {}", &ext));
}
} else if cfg!(target_family = "unix") {
let artifacts = &["liblibrashader_capi.so", "liblibrashader_capi.a"];
for artifact in artifacts {
let ext = artifact.strip_prefix("lib").unwrap();
let ext = ext.replace("_capi", "");
fs::rename(output_dir.join(artifact), output_dir.join(ext)).unwrap();
let Ok(_) =
fs::rename(output_dir.join(artifact), output_dir.join(&ext)).inspect_err(|err| {
carlog_error!(format!("Unable to rename {artifact} to {}", &ext));
carlog_error!(format!("{err}"));
})
else {
return ExitCode::FAILURE;
};
carlog_ok!("Renamed", format!("{artifact} to {}", &ext));
}
}
@ -116,17 +161,24 @@ pub fn main() -> ExitCode {
];
for artifact in artifacts {
let ext = artifact.replace("_capi", "");
println!("INFO: Renaming {artifact} to {ext}");
fs::rename(output_dir.join(artifact), output_dir.join(ext)).unwrap();
let Ok(_) =
fs::rename(output_dir.join(artifact), output_dir.join(&ext)).inspect_err(|err| {
carlog_error!(format!("Unable to rename {artifact} to {}", &ext));
carlog_error!(format!("{err}"));
})
else {
return ExitCode::FAILURE;
};
carlog_ok!("Renamed", format!("{artifact} to {}", &ext));
}
if output_dir.join("librashader_capi.pdb").exists() {
println!("INFO: Renaming librashader_capi.pdb to librashader.pdb");
fs::rename(
output_dir.join("librashader_capi.pdb"),
output_dir.join("librashader.pdb"),
)
.unwrap();
carlog_ok!("Renamed", "librashader_capi.pdb to librashader.pdb");
}
}