Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 57 additions & 17 deletions src/bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ fn get_nixl_libs() -> Option<Vec<pkg_config::Library>> {
}
}

/// Check if NIXL library is available on the system
fn check_nixl_available() -> bool {
// First try pkg-config
if pkg_config::probe_library("nixl").is_ok() {
return true;
}

// Fall back to checking common library paths
let nixl_root_path =
env::var("NIXL_PREFIX").unwrap_or_else(|_| "/opt/nvidia/nvda_nixl".to_string());
let arch = get_arch();

// Check various possible library locations
let possible_paths = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the check for availability use the same set of paths that would be sent to the linker? We're missing 2 of the 5 right now. And maybe use the same function for both so they are always aligned

    // Add all possible library paths
    println!("cargo:rustc-link-search=native={}", nixl_lib_path);
    println!("cargo:rustc-link-search=native={}", nixl_root_path);
    println!("cargo:rustc-link-search=native={}/lib", nixl_root_path);
    println!("cargo:rustc-link-search=native={}/lib64", nixl_root_path);
    println!("cargo:rustc-link-search=native={}/lib/x86_64-linux-gnu", nixl_root_path);

format!("{}/lib/{}-linux-gnu/libnixl.so", nixl_root_path, arch),
format!("{}/lib64/libnixl.so", nixl_root_path),
format!("{}/lib/libnixl.so", nixl_root_path),
];

for path in &possible_paths {
if std::path::Path::new(path).exists() {
return true;
}
}

false
}

fn build_nixl(cc_builder: &mut cc::Build) -> anyhow::Result<()> {
let nixl_root_path =
env::var("NIXL_PREFIX").unwrap_or_else(|_| "/opt/nvidia/nvda_nixl".to_string());
Expand Down Expand Up @@ -233,25 +261,37 @@ fn create_builder() -> cc::Build {
fn run_build(use_stub_api: bool) {
let mut cc_builder = create_builder();

if !use_stub_api {
let no_fallback = env::var("NIXL_NO_STUBS_FALLBACK")
.map(|v| v == "1")
.unwrap_or(false);

if let Err(e) = build_nixl(&mut cc_builder) {
if !no_fallback {
println!(
"cargo:warning=NIXL build failed: {}, falling back to stub API",
e
);
let mut stub_builder = create_builder();
build_stubs(&mut stub_builder);
} else {
panic!("Failed to build NIXL: {}", e);
}
if use_stub_api {
build_stubs(&mut cc_builder);
return;
}

let no_fallback = env::var("NIXL_NO_STUBS_FALLBACK")
.map(|v| v == "1")
.unwrap_or(false);

// Check if NIXL is available before attempting to build
if !check_nixl_available() {
if no_fallback {
panic!("NIXL library not found and NIXL_NO_STUBS_FALLBACK is set");
}
} else {
println!("cargo:warning=NIXL library not found, building with stub API");
build_stubs(&mut cc_builder);
return;
}

// Try to build with NIXL
if let Err(e) = build_nixl(&mut cc_builder) {
if !no_fallback {
println!(
"cargo:warning=NIXL build failed: {}, falling back to stub API",
e
);
let mut stub_builder = create_builder();
build_stubs(&mut stub_builder);
} else {
panic!("Failed to build NIXL: {}", e);
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/bindings/rust/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@
*/
#pragma once

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// Use built-in compiler types to avoid system header dependencies
typedef __SIZE_TYPE__ size_t;
typedef __UINT64_TYPE__ uint64_t;
typedef __INT64_TYPE__ int64_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __UINTPTR_TYPE__ uintptr_t;

#ifndef __cplusplus
typedef _Bool bool;
#define true 1
#define false 0
#endif

#ifdef __cplusplus
extern "C" {
Expand Down