From fa9540fb59e231e32c9fc5f256a718aefafc305b Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 2 Jan 2017 00:42:53 +0000 Subject: [PATCH] build: Support environment variables Only for the include paths. --- README.md | 10 ++++++++++ build.rs | 49 +++++++++++++++++++------------------------------ 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 8a85438..9120168 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,17 @@ # NVIDIA Video Codec SDK bindings +[![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) + It is a simple [binding][1] and safe abstraction over the [nvidia video codec sdk][2]. +## Building + +The bindings are generated using the headers and libraries that ought to be present in the system. + +By default the headers are looked up on `/opt/cuda/include` and `/opt/nvidia-video-codec` and the libraries are assumed to be present in the default path (and provided by the driver). + +It is possible to override the search paths for the headers by setting the environment variables `CUDA_INCLUDE_PATH` and `NVIDIA_VIDEO_CODEC_INCLUDE_PATH`. + ## TODO - [ ] support cuda diff --git a/build.rs b/build.rs index f9ab202..159e5c5 100644 --- a/build.rs +++ b/build.rs @@ -1,9 +1,9 @@ extern crate libbindgen; -// use std::env; -use std::path::Path; +use std::env; use std::fs::OpenOptions; use std::io::Write; +use std::path::PathBuf; fn format_write(builder: libbindgen::Builder, output: &str) { let s = builder.generate() @@ -30,11 +30,17 @@ fn common_builder() -> libbindgen::Builder { .raw_line("#![allow(non_upper_case_globals)]") } +fn find_dir(default: &'static str, env_key: &'static str) -> PathBuf { + match env::var_os(env_key) { + Some(val) => PathBuf::from(&val), + _ => PathBuf::from(default), + } +} + fn main() { - // let out_dir = env::var("OUT_DIR").unwrap(); - // TODO make them params and try to figure them out as default - let cuda_include = "/opt/cuda/include"; - let nvc_include = "/opt/nvidia-video-codec/"; + let cuda_include = find_dir("/opt/cuda/include", "CUDA_INCLUDE_PATH"); + let nvc_include = find_dir("/opt/nvidia-video-codec/", + "NVIDIA_VIDEO_CODEC_INCLUDE_PATH"); // TODO support windows println!("cargo:rustc-link-lib=dylib={}", "cuda"); @@ -43,39 +49,22 @@ fn main() { let cuda_builder = common_builder() - .clang_arg(format!("-I{}", cuda_include)) - .header(Path::new(cuda_include).join("cuda.h").to_str().unwrap()); + .clang_arg(format!("-I{}", cuda_include.to_string_lossy())) + .header(cuda_include.join("cuda.h").to_string_lossy()); // Manually fix the comment so rustdoc won't try to pick them format_write(cuda_builder, "src/ffi/cuda.rs"); let cuvid_builder = common_builder() - .clang_arg(format!("-I{}", nvc_include)) - .clang_arg(format!("-I{}", cuda_include)) - .header(Path::new(nvc_include).join("nvcuvid.h").to_str().unwrap()); - - println!("{:?}", cuvid_builder); + .clang_arg(format!("-I{}", nvc_include.to_string_lossy())) + .clang_arg(format!("-I{}", cuda_include.to_string_lossy())) + .header(nvc_include.join("nvcuvid.h").to_string_lossy()); format_write(cuvid_builder, "src/ffi/cuvid.rs"); let nvenc_builder = common_builder() - .clang_arg(format!("-I{}", nvc_include)) - .header(Path::new(nvc_include).join("nvEncodeAPI.h").to_str().unwrap()); + .clang_arg(format!("-I{}", nvc_include.to_string_lossy())) + .header(nvc_include.join("nvEncodeAPI.h").to_string_lossy()); format_write(nvenc_builder, "src/ffi/nvenc.rs"); } - -// let s = cuda_gen.generate() -// .unwrap() -// .to_string() -// .replace("**", "*"); -// -// let mut file = OpenOptions::new() -// .write(true) -// .truncate(true) -// .create(true) -// .open("src/ffi/cuda.rs") -// .unwrap(); -// -// let _ = file.write(s.as_bytes()); -//