Skip to content

Commit 8f2d1d1

Browse files
Only add typical CUDA paths to the linker search paths if no explicit path is supplied (#475)
* Only add typical locations to the linker search paths if no explicit path is supplied * Clippy * Apply suggestion from @coreylowman --------- Co-authored-by: Corey Lowman <clowman1993@gmail.com>
1 parent c557c33 commit 8f2d1d1

1 file changed

Lines changed: 35 additions & 18 deletions

File tree

build.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
use std::path::PathBuf;
22

3+
const TYPICAL_CUDA_PATH_ENV_VARS: [&str; 5] = [
4+
"CUDA_HOME",
5+
"CUDA_PATH",
6+
"CUDA_ROOT",
7+
"CUDA_TOOLKIT_ROOT_DIR",
8+
"CUDNN_LIB",
9+
];
10+
311
fn main() {
412
#[cfg(all(
513
not(feature = "dynamic-linking"),
@@ -15,10 +23,10 @@ fn main() {
1523
panic!("Both `dynamic-loading` and `dynamic-linking` features are active, this is a bug");
1624

1725
println!("cargo:rerun-if-changed=build.rs");
18-
println!("cargo:rerun-if-env-changed=CUDA_ROOT");
19-
println!("cargo:rerun-if-env-changed=CUDA_PATH");
20-
println!("cargo:rerun-if-env-changed=CUDA_TOOLKIT_ROOT_DIR");
2126
println!("cargo:rerun-if-env-changed=CUDARC_CUDA_VERSION");
27+
TYPICAL_CUDA_PATH_ENV_VARS
28+
.iter()
29+
.for_each(|var| println!("cargo:rerun-if-env-changed={var}"));
2230

2331
let (major, minor): (usize, usize) = if let Ok(version) = std::env::var("CUDARC_CUDA_VERSION") {
2432
let (major, minor) = match version.as_str() {
@@ -231,18 +239,22 @@ fn static_linking(major: usize, minor: usize) {
231239

232240
#[allow(unused)]
233241
fn link_searches(major: usize, minor: usize) -> Vec<PathBuf> {
234-
let env_vars = [
235-
"CUDA_PATH",
236-
"CUDA_ROOT",
237-
"CUDA_TOOLKIT_ROOT_DIR",
238-
"CUDNN_LIB",
239-
];
240-
let env_vars = env_vars
241-
.into_iter()
242+
let env_vars = TYPICAL_CUDA_PATH_ENV_VARS
243+
.iter()
242244
.map(std::env::var)
243-
.filter_map(Result::ok);
245+
.filter_map(Result::ok)
246+
.collect::<Vec<_>>();
247+
248+
// When building in a Conda-like environment with dynamic linking, if no
249+
// CUDA path is supplied, then it is higly likely that, by defaulting our
250+
// linker search paths to the typical locations below, linker errors will
251+
// occur. Print a warning with some guidance.
252+
#[cfg(feature = "dynamic-linking")]
253+
if env_vars.is_empty() && std::env::var("CONDA_PREFIX").is_ok() {
254+
println!("cargo::warning=Detected $CONDA_PREFIX, but no CUDA path was set through one of: {TYPICAL_CUDA_PATH_ENV_VARS:?}. Linking to system CUDA libraries; linker errors may occur. To use CUDA installed via conda please ensure the environment contains all required dependencies (e.g. the \"cuda-driver-dev\") and retry building with CUDA_HOME=$CONDA_PREFIX.")
255+
}
244256

245-
let standard_locations = [
257+
let typical_locations = [
246258
"/usr",
247259
"/usr/local/cuda",
248260
"/opt/cuda",
@@ -264,13 +276,18 @@ fn link_searches(major: usize, minor: usize) -> Vec<PathBuf> {
264276
"C:/Program Files/NVIDIA/CUDNN/v9.1",
265277
"C:/Program Files/NVIDIA/CUDNN/v9.0",
266278
];
267-
let standard_locations = standard_locations.into_iter().map(Into::into);
279+
280+
let possible_locations = if env_vars.is_empty() {
281+
typical_locations
282+
.into_iter()
283+
.map(Into::<String>::into)
284+
.collect()
285+
} else {
286+
env_vars
287+
};
268288

269289
let mut candidates = Vec::new();
270-
for root in env_vars
271-
.chain(standard_locations)
272-
.map(Into::<PathBuf>::into)
273-
{
290+
for root in possible_locations.into_iter().map(Into::<PathBuf>::into) {
274291
candidates.extend(
275292
[
276293
"lib".into(),

0 commit comments

Comments
 (0)