Skip to content

Commit

Permalink
#599: Add support for CELIX_RUST_INCLUDE_PATHS_FILE env in rust poc
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Aug 29, 2023
1 parent 8b3f943 commit 53654a3
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions misc/experimental/rust/celix_bindings/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -44,19 +60,15 @@ 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 {
builder = builder.clang_arg(format!("-I{}", path));
}

// 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
Expand Down

0 comments on commit 53654a3

Please sign in to comment.