-
Notifications
You must be signed in to change notification settings - Fork 95
Feature/599 provide and use c service in rust #627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2c8d89a
2f37f72
3b4ccb9
4c14d8c
21b0027
1654e21
dd13817
99717a6
3ef7b52
b2e8657
5329420
8b3f943
53654a3
88deb83
bf01a26
f227191
e3aa83a
e0f3c4c
c89893f
9fd6da5
766ee3b
dc81c7a
4e91216
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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>") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| 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) | ||
|
|
||
|
|
@@ -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() | ||
| 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"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now it's OK to have static libcelix. Considering its size, 200KB for x64 release build, it might be preferable to built as shared object. Even if we can add |
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not feel right. In the C version, activator is dropped after IMHO, the best fix is to change the corresponding logic in From status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}To status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}So that we can remove |
||
| drop(reclaimed_activator); | ||
| $crate::CELIX_SUCCESS | ||
| } | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.