Skip to content

Commit

Permalink
rust/build.rs: provide a way to control the choice between CUDA and R…
Browse files Browse the repository at this point in the history
…OCm.
  • Loading branch information
dot-asm committed Nov 1, 2024
1 parent 7901b27 commit 0a41eb5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ include = [
[build-dependencies]
cc = "^1.0.70"
which = "^4.0"

[features]
# coerse either as an alternative to auto-detection

This comment has been minimized.

Copy link
@nazar-pc

nazar-pc Nov 12, 2024

Rust features are not for coercion, they are inherently additive and should be opt-in.
Right now it is impossible to disable both CUDA and ROCm with default-features = false and default feature selected is non-deterministic (depends on compilers present on the host system rather than features user specified or not specified).

cuda = []
rocm = []
23 changes: 20 additions & 3 deletions rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,39 @@ fn main() {
let all_gpus = base_dir.join("util/all_gpus.cpp");

println!("cargo:rerun-if-env-changed=NVCC");
let nvcc = match env::var("NVCC") {
let mut nvcc = match env::var("NVCC") {
Ok(var) => which::which(var),
Err(_) => which::which("nvcc"),
};

println!("cargo:rerun-if-env-changed=HIPCC");
let hipcc = match env::var("HIPCC") {
let mut hipcc = match env::var("HIPCC") {
Ok(var) => which::which(var),
Err(_) => which::which("hipcc"),
};

match (cfg!(feature = "cuda"), cfg!(feature = "rocm")) {
(true, true) => panic!("mutually exclusive features"),
(true, false) => {
if nvcc.is_err() {
panic!("`nvcc` is not available");
}
hipcc = Err(which::Error::CannotFindBinaryPath);
},
(false, true) => {
if hipcc.is_err() {
panic!("`hipcc` is not available");
}
nvcc = Err(which::Error::CannotFindBinaryPath);
},
(false, false) => (),
}

// Detect if there is CUDA compiler and engage "cuda" feature accordingly,
// even if there is no Nvidia card, but unless there is ROCm compiler.
// In other words if you have both Nvidia and AMD cards installed, Nvidia
// will be preferred. To suppress it, set the NVCC environment variable
// to "off".
// to "off" [or engage "rocm" feature].
if let Ok(nvcc) = nvcc {
let cuda_version = Command::new(nvcc)
.arg("--version")
Expand Down

0 comments on commit 0a41eb5

Please sign in to comment.