This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/599-provide-and-use-c-service-in-rust in repository https://gitbox.apache.org/repos/asf/celix.git
commit 53654a3c758f9a463768b1139e4abc54fccbd796 Author: Pepijn Noltes <[email protected]> AuthorDate: Tue Aug 29 19:24:36 2023 +0200 #599: Add support for CELIX_RUST_INCLUDE_PATHS_FILE env in rust poc --- misc/experimental/rust/celix_bindings/build.rs | 40 +++++++++++++++++--------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/misc/experimental/rust/celix_bindings/build.rs b/misc/experimental/rust/celix_bindings/build.rs index 8bae728b..6f3b1d0a 100644 --- a/misc/experimental/rust/celix_bindings/build.rs +++ b/misc/experimental/rust/celix_bindings/build.rs @@ -19,21 +19,37 @@ extern crate bindgen; +use std::env; use std::error::Error; -use std::path::PathBuf; use std::fs::File; use std::io::{self, BufRead}; -use std::env; +use std::path::PathBuf; -fn print_include_paths() -> Result<Vec<String>, Box<dyn Error>> { - let build_dir = PathBuf::from(env::var("CORROSION_BUILD_DIR").unwrap()); - let include_path_file = build_dir.join("include_paths.txt"); +fn open_include_paths_file() -> Result<File, Box<dyn Error>> { + let file: File; - //let include_path_file = Path::new("include_paths.txt"); - let file = File::open(&include_path_file)?; + let corrosion_build_dir = env::var("CORROSION_BUILD_DIR"); + if corrosion_build_dir.is_ok() { + let build_dir = PathBuf::from(corrosion_build_dir.unwrap()); + let include_path_file = build_dir.join("include_paths.txt"); + file = File::open(&include_path_file)?; + } else { + println!("include_paths.txt not found in CORROSION_BUILD_DIR. Failing back to CELIX_RUST_INCLUDE_PATHS_FILE env value"); + let include_path_file = env::var("CELIX_RUST_INCLUDE_PATHS_FILE")?; + file = File::open(&include_path_file)?; + } + + Ok(file) +} + +fn print_include_paths() -> Result<Vec<String>, Box<dyn Error>> { + let file = open_include_paths_file()?; let reader = io::BufReader::new(file); let mut include_paths = Vec::new(); - let line = reader.lines().next().ok_or("Expected at least one line")??; + let line = reader + .lines() + .next() + .ok_or("Expected at least one line")??; for path in line.split(';') { include_paths.push(path.to_string()); } @@ -44,8 +60,7 @@ fn main() { println!("cargo:info=Start build.rs for celix_bindings"); let include_paths = print_include_paths().unwrap(); - let mut builder = bindgen::Builder::default() - .header("src/celix_bindings.h"); + let mut builder = bindgen::Builder::default().header("src/celix_bindings.h"); // Add framework and utils include paths for path in &include_paths { @@ -53,10 +68,7 @@ fn main() { } // Gen bindings - let bindings = builder - .generate() - .expect("Unable to generate bindings"); - + let bindings = builder.generate().expect("Unable to generate bindings"); let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings
