fn build_backend()

in backends/trtllm/build.rs [87:176]


fn build_backend(is_debug: bool, opt_level: &str, out_dir: &PathBuf) -> (PathBuf, PathBuf) {
    // Build the backend implementation through CMake
    let install_path = INSTALL_PREFIX.unwrap_or("/usr/local/tgi");
    let tensorrt_path = TENSORRT_ROOT_DIR.unwrap_or("/usr/local/tensorrt");
    let cuda_arch_list = CUDA_ARCH_LIST.unwrap_or("75-real;80-real;86-real;89-real;90-real");

    let mut install_path = PathBuf::from(install_path);
    if !install_path.is_absolute() {
        install_path = absolute(out_dir).expect("cannot happen").join(install_path);
    }

    let mut config = cmake::Config::new(".");
    config
        .uses_cxx11()
        .generator("Ninja")
        .profile(match is_debug {
            true => "Debug",
            false => "Release",
        })
        .env("OPT_LEVEL", opt_level)
        .define("CMAKE_INSTALL_PREFIX", &install_path)
        .define("CMAKE_CUDA_COMPILER", "/usr/local/cuda/bin/nvcc")
        .define("CMAKE_LIBRARY_ARCHITECTURE", get_library_architecture())
        .define("TGI_TRTLLM_BACKEND_TARGET_CUDA_ARCH_LIST", cuda_arch_list)
        .define(
            "TGI_TRTLLM_BACKEND_DEBUG",
            get_compiler_flag(is_debug, "ON", "OFF"),
        )
        .define("TGI_TRTLLM_BACKEND_TRT_ROOT", tensorrt_path);

    if is_debug || *IS_GHA_BUILD {
        config.define("TGI_TRTLLM_BACKEND_BUILD_TESTS", "ON");
    }

    if option_env!("USE_LLD_LINKER").is_some() {
        println!("cargo:warning=Using lld linker");
        config.define("TGI_TRTLLM_BACKEND_BUILD_USE_LLD", "ON");
    }

    if (is_debug && option_env!("ENABLE_ASAN").is_some()) || *IS_GHA_BUILD {
        println!("cargo:warning=Enabling Address Sanitizer");
        config.define("TGI_TRTLLM_BACKEND_ENABLE_ASAN", "ON");
    }

    if (is_debug && option_env!("ENABLE_UBSAN").is_some()) || *IS_GHA_BUILD {
        println!("cargo:warning=Enabling Undefined Sanitizer");
        config.define("TGI_TRTLLM_BACKEND_ENABLE_UBSAN", "ON");
    }

    if let Some(nvcc_host_compiler) = option_env!("CMAKE_CUDA_HOST_COMPILER") {
        config.define("CMAKE_CUDA_HOST_COMPILER", nvcc_host_compiler);
    }

    if let Some(wrapper) = option_env!("RUSTC_WRAPPER") {
        println!("cargo:warning=Using caching tool: {wrapper}");
        config.define("CMAKE_C_COMPILER_LAUNCHER", wrapper);
        config.define("CMAKE_CXX_COMPILER_LAUNCHER", wrapper);
        config.define("CMAKE_CUDA_COMPILER_LAUNCHER", wrapper);
    }

    // Allow to override which Python to use ...
    if let Some(python3) = option_env!("Python3_EXECUTABLE") {
        config.define("Python3_EXECUTABLE", python3);
    }

    config.build();

    // Additional transitive CMake dependencies
    let deps_folder = out_dir.join("build").join("_deps");
    for dependency in ADDITIONAL_BACKEND_LINK_LIBRARIES {
        let dep_name = match is_debug {
            true => format!("{}d", dependency),
            false => String::from(dependency),
        };
        let dep_path = deps_folder.join(format!("{}-build", dependency));
        println!("cargo:rustc-link-search={}", dep_path.display());
        println!("cargo:rustc-link-lib=static={}", dep_name);
    }

    // Emit linkage information from the artifacts we just built
    for path in ["lib", "lib64"] {
        let install_lib_path = install_path.join(path);
        println!(
            r"cargo:warning=Adding link search path: {}",
            install_lib_path.display()
        );
        println!(r"cargo:rustc-link-search={}", install_lib_path.display());
    }
    (PathBuf::from(install_path), deps_folder)
}