This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/599-add-rust-bindings
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 0d7c48f0dd71b2c6f91f9dab8cb7a745d94e90bf
Author: Pepijn Noltes <[email protected]>
AuthorDate: Tue Aug 1 23:30:30 2023 +0200

    599: Add celix_bindings rust lib
---
 misc/experimental/rust/CMakeLists.txt              | 11 +++-
 misc/experimental/rust/Cargo.toml                  |  1 +
 .../rust/{ => celix_bindings}/Cargo.toml           | 15 +++--
 misc/experimental/rust/celix_bindings/build.rs     | 65 ++++++++++++++++++++++
 .../rust/celix_bindings/src/celix_bindings.h       | 32 +++++++++++
 misc/experimental/rust/celix_bindings/src/lib.rs   | 41 ++++++++++++++
 6 files changed, 159 insertions(+), 6 deletions(-)

diff --git a/misc/experimental/rust/CMakeLists.txt 
b/misc/experimental/rust/CMakeLists.txt
index b7353468..e2236a1a 100644
--- a/misc/experimental/rust/CMakeLists.txt
+++ b/misc/experimental/rust/CMakeLists.txt
@@ -25,8 +25,16 @@ if (CELIX_RUST_EXPERIMENTAL)
     )
     FetchContent_MakeAvailable(Corrosion)
 
+    #Prepare a list of include paths needed to generating bindings for the 
Apache Celix C API
+    file(GENERATE
+            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include_paths.txt"
+            CONTENT 
"$<TARGET_PROPERTY:framework,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:utils,INTERFACE_INCLUDE_DIRECTORIES>"
+    )
+
     corrosion_import_crate(MANIFEST_PATH Cargo.toml)
-    corrosion_add_target_local_rustflags(rust_bundle_activator  
"-Cprefer-dynamic")
+
+    corrosion_add_target_local_rustflags(rust_bundle_activator 
"-Cprefer-dynamic")
+    corrosion_link_libraries(rust_bundle_activator Celix::framework)
 
     #Note corrosion_import_crate import creates a rust_bundle_activator CMake 
target, but this is a INTERFACE target.
     #Using the INTERFACE_LINK_LIBRARIES property we can get the actual target.
@@ -41,5 +49,4 @@ if (CELIX_RUST_EXPERIMENTAL)
             Celix::shell_tui
             rust_bundle
     )
-
 endif ()
diff --git a/misc/experimental/rust/Cargo.toml 
b/misc/experimental/rust/Cargo.toml
index f0ed38b9..8727be5d 100644
--- a/misc/experimental/rust/Cargo.toml
+++ b/misc/experimental/rust/Cargo.toml
@@ -17,5 +17,6 @@
 
 [workspace]
 members = [
+    "celix_bindings",
     "hello_world_activator",
 ]
diff --git a/misc/experimental/rust/Cargo.toml 
b/misc/experimental/rust/celix_bindings/Cargo.toml
similarity index 82%
copy from misc/experimental/rust/Cargo.toml
copy to misc/experimental/rust/celix_bindings/Cargo.toml
index f0ed38b9..b46868b9 100644
--- a/misc/experimental/rust/Cargo.toml
+++ b/misc/experimental/rust/celix_bindings/Cargo.toml
@@ -15,7 +15,14 @@
 # specific language governing permissions and limitations
 # under the License.
 
-[workspace]
-members = [
-    "hello_world_activator",
-]
+[package]
+name = "celix_bindings"
+version = "0.0.1"
+
+[build-dependencies]
+bindgen = "0.66.1"
+
+[lib]
+name = "celix_bindings"
+path = "src/lib.rs"
+crate-type = ["rlib"]
diff --git a/misc/experimental/rust/celix_bindings/build.rs 
b/misc/experimental/rust/celix_bindings/build.rs
new file mode 100644
index 00000000..8bae728b
--- /dev/null
+++ b/misc/experimental/rust/celix_bindings/build.rs
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+extern crate bindgen;
+
+use std::error::Error;
+use std::path::PathBuf;
+use std::fs::File;
+use std::io::{self, BufRead};
+use std::env;
+
+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");
+
+    //let include_path_file = Path::new("include_paths.txt");
+    let file = File::open(&include_path_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")??;
+    for path in line.split(';') {
+        include_paths.push(path.to_string());
+    }
+    Ok(include_paths)
+}
+
+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");
+
+    // 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 out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
+    bindings
+        .write_to_file(out_path.join("celix_bindings.rs"))
+        .expect("Couldn't write Apache Celix bindings!");
+}
diff --git a/misc/experimental/rust/celix_bindings/src/celix_bindings.h 
b/misc/experimental/rust/celix_bindings/src/celix_bindings.h
new file mode 100644
index 00000000..8495c9a4
--- /dev/null
+++ b/misc/experimental/rust/celix_bindings/src/celix_bindings.h
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @file celix_bindings.h
+ * @brief A header files that includes all Apache Celix headers used to create 
bindings.
+*/
+
+#include "celix_errno.h"
+#include "celix_properties.h"
+#include "celix_filter.h"
+
+#include "celix_bundle_context.h"
+#include "celix_framework.h"
+#include "celix_framework_factory.h"
+#include "celix_framework_utils.h"
diff --git a/misc/experimental/rust/celix_bindings/src/lib.rs 
b/misc/experimental/rust/celix_bindings/src/lib.rs
new file mode 100644
index 00000000..c69c7524
--- /dev/null
+++ b/misc/experimental/rust/celix_bindings/src/lib.rs
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, 
dead_code)]
+ mod bindings {
+      include!(concat!(env!("OUT_DIR"), "/celix_bindings.rs"));
+ }
+pub use bindings::*;
+
+//Note C #defines (compile-time constants) are not all generated in the 
bindings.
+pub const CELIX_SUCCESS: celix_status_t = 0;
+pub const CELIX_BUNDLE_EXCEPTION: celix_status_t = 70001;
+
+// Move to celix_api lib
+pub mod celix {
+
+    #[warn(unused_imports)]
+    pub enum LogLevel {
+        Trace = ::bindings::celix_log_level_CELIX_LOG_LEVEL_TRACE as isize,
+        Debug = ::bindings::celix_log_level_CELIX_LOG_LEVEL_DEBUG as isize,
+        Info = ::bindings::celix_log_level_CELIX_LOG_LEVEL_INFO as isize,
+        Warn = ::bindings::celix_log_level_CELIX_LOG_LEVEL_WARNING as isize,
+        Error = ::bindings::celix_log_level_CELIX_LOG_LEVEL_ERROR as isize,
+    }
+}

Reply via email to