Skip to content

Commit

Permalink
Merge 4e91216 into e1d7fc1
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Sep 17, 2023
2 parents e1d7fc1 + 4e91216 commit 23bcf27
Show file tree
Hide file tree
Showing 19 changed files with 1,430 additions and 120 deletions.
33 changes: 24 additions & 9 deletions misc/experimental/rust/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.

option(CELIX_RUST_EXPERIMENTAL "Enable experimental rust bundle" OFF)
if (CELIX_RUST_EXPERIMENTAL)
if (CELIX_RUST_EXPERIMENTAL AND TARGET Celix::shell_tui AND TARGET Celix::shell AND TARGET Celix::log_admin)
include(FetchContent)
FetchContent_Declare(
Corrosion
Expand All @@ -26,13 +26,12 @@ 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>"
)
#Note for now this includes framework, utils and shell_api maybe this should be separated in the future.
file(GENERATE OUTPUT
"${CMAKE_CURRENT_BINARY_DIR}/include_paths.txt" CONTENT
"$<TARGET_PROPERTY:framework,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:utils,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:shell_api,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:Celix::log_service_api,INTERFACE_INCLUDE_DIRECTORIES>")

corrosion_import_crate(MANIFEST_PATH Cargo.toml)

corrosion_add_target_local_rustflags(rust_bundle_activator "-Cprefer-dynamic")
corrosion_link_libraries(rust_bundle_activator Celix::framework)

Expand All @@ -41,12 +40,28 @@ if (CELIX_RUST_EXPERIMENTAL)
get_target_property(ACTUAL_LIB_TARGET rust_bundle_activator INTERFACE_LINK_LIBRARIES)
add_celix_bundle(rust_bundle ACTIVATOR ${ACTUAL_LIB_TARGET})
add_dependencies(rust_bundle rust_bundle_activator)
corrosion_add_target_local_rustflags(rust_shell_command_activator "-Cprefer-dynamic")
corrosion_link_libraries(rust_shell_command_activator
Celix::framework
)

add_celix_container(rust_container
NO_COPY
get_target_property(ACTUAL_LIB_TARGET rust_shell_command_activator INTERFACE_LINK_LIBRARIES)
add_celix_bundle(rust_shell_command ACTIVATOR ${ACTUAL_LIB_TARGET})
add_dependencies(rust_shell_command rust_shell_command_activator)

add_celix_container(rust_container NO_COPY
BUNDLES
Celix::shell
Celix::shell_tui
rust_bundle
)
endif ()

add_celix_container(rust_shell_cnt NO_COPY
BUNDLES
Celix::shell
Celix::shell_tui
Celix::log_admin
rust_shell_command
)

endif()
4 changes: 4 additions & 0 deletions misc/experimental/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
[workspace]
members = [
"celix_bindings",
"celix",
"hello_world_activator",
"rust_shell_api",
"shell_command_bundle",
]
resolver = "2"
29 changes: 29 additions & 0 deletions misc/experimental/rust/celix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.

[package]
name = "celix"
version = "0.0.1"
edition = '2021'

[dependencies]
celix_bindings = { path = "../celix_bindings" }

[lib]
name = "celix"
path = "src/lib.rs"
crate-type = ["rlib"]
88 changes: 88 additions & 0 deletions misc/experimental/rust/celix/src/bundle_activator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.
*/

use std::sync::Arc;

use super::BundleContext;
use super::Error;

pub trait BundleActivator {
fn new(ctx: Arc<BundleContext>) -> Self;
fn start(&mut self) -> Result<(), Error> {
/* Default implementation */
Ok(())
}
fn stop(&mut self) -> Result<(), Error> {
/* Default implementation */
Ok(())
}
}

#[macro_export]
macro_rules! generate_bundle_activator {
($activator:ty) => {
#[no_mangle]
pub unsafe extern "C" fn celix_bundleActivator_create(
ctx: *mut $crate::details::CBundleContext,
out: *mut *mut ::std::ffi::c_void,
) -> $crate::details::CStatus {
let boxed_context = $crate::bundle_context_new(ctx);
let mut arc_context = Arc::from(boxed_context);
let activator = <$activator>::new(arc_context);
*out = Box::into_raw(Box::new(activator)) as *mut ::std::ffi::c_void;
$crate::CELIX_SUCCESS
}

#[no_mangle]
pub unsafe extern "C" fn celix_bundleActivator_start(
handle: *mut ::std::ffi::c_void,
ctx: *mut $crate::details::CBundleContext,
) -> $crate::details::CStatus {
let activator = &mut *(handle as *mut $activator);
let result = activator.start();
match result {
Ok(_) => $crate::CELIX_SUCCESS,
Err(e) => e.into(),
}
}

#[no_mangle]
pub unsafe extern "C" fn celix_bundleActivator_stop(
handle: *mut ::std::ffi::c_void,
ctx: *mut $crate::details::CBundleContext,
) -> $crate::details::CStatus {
let activator = &mut *(handle as *mut $activator);
let result = activator.stop();
match result {
Ok(_) => $crate::CELIX_SUCCESS,
Err(e) => e.into(),
}
}

#[no_mangle]
pub unsafe extern "C" fn celix_bundleActivator_destroy(
handle: *mut ::std::ffi::c_void,
_ctx: *mut $crate::details::CBundleContext,
) -> $crate::details::CStatus {
let reclaimed_activator = Box::from_raw(handle as *mut $activator);
drop(reclaimed_activator);
$crate::CELIX_SUCCESS
}
};
}
Loading

0 comments on commit 23bcf27

Please sign in to comment.