ffi: allows building as static library for all targets

This commit is contained in:
eric committed 2021-04-05 18:21:19 +08:00
1 parent ac82506caf
commit ea3454f0b2
10 files changed
+32 -60

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@
members = [
"leaf",
"leaf-bin",
"leaf-mobile",
"leaf-ffi",
"tun2socks-bin",
]
default-members = ["leaf-bin"]
+8 -4
View File
@@ -1,10 +1,14 @@
ios:
cargo lipo -p leaf-mobile --release --targets aarch64-apple-ios --manifest-path leaf-mobile/Cargo.toml
cbindgen --config leaf-mobile/cbindgen.toml leaf-mobile/src/lib.rs > target/universal/release/leaf.h
cargo lipo -p leaf-ffi --release --targets aarch64-apple-ios --manifest-path leaf-ffi/Cargo.toml
cbindgen --config leaf-ffi/cbindgen.toml leaf-ffi/src/lib.rs > target/universal/release/leaf.h
ios-dev:
cargo lipo -p leaf-mobile --targets aarch64-apple-ios --manifest-path leaf-mobile/Cargo.toml
cbindgen --config leaf-mobile/cbindgen.toml leaf-mobile/src/lib.rs > target/universal/debug/leaf.h
cargo lipo -p leaf-ffi --targets aarch64-apple-ios --manifest-path leaf-ffi/Cargo.toml
cbindgen --config leaf-ffi/cbindgen.toml leaf-ffi/src/lib.rs > target/universal/debug/leaf.h
lib:
cargo build -p leaf-ffi --release
cbindgen --config leaf-ffi/cbindgen.toml leaf-ffi/src/lib.rs > target/release/leaf.h
local:
cargo build -p leaf-bin --release
+1 -3
View File
@@ -127,12 +127,10 @@ fn main() {
log::LevelFilter::Info
};
let mut logger = leaf::common::log::setup_logger(loglevel);
let console_output = fern::Output::stdout("\n");
logger = logger.chain(console_output);
if let Some(log) = config.log.as_ref() {
match log.output {
config::Log_Output::CONSOLE => {
// console output already applied
logger = logger.chain(fern::Output::stdout("\n"));
}
config::Log_Output::FILE => {
let f = fern::log_file(&log.output_file).expect("open log file failed");
@@ -1,5 +1,5 @@
[package]
name = "leaf-mobile"
name = "leaf-ffi"
version = "0.1.0"
authors = ["eycorsican <eric.y.corsican@gmail.com>"]
edition = "2018"
File renamed without changes.
File renamed without changes.
File renamed without changes.
+21 -26
View File
@@ -1,17 +1,15 @@
use std::{ffi::CStr, os::raw::c_char};
use bytes::BytesMut;
use log::*;
use leaf::config;
#[cfg(any(target_os = "ios", target_os = "android"))]
pub mod bindings;
#[cfg(any(target_os = "ios", target_os = "android"))]
mod logger;
use logger::ConsoleWriter;
// This function is only available on iOS 13.0+
// This function is only available on iOS 13.0+, useful for debugging
// memory issues on iOS.
// use ios::os_proc_available_memory;
fn run(rt: &tokio::runtime::Runtime, config: leaf::config::Config) {
@@ -27,12 +25,21 @@ fn run(rt: &tokio::runtime::Runtime, config: leaf::config::Config) {
log::LevelFilter::Info
};
let mut logger = leaf::common::log::setup_logger(loglevel);
let console_output = fern::Output::writer(Box::new(ConsoleWriter(BytesMut::new())), "\n");
logger = logger.chain(console_output);
if let Some(log) = config.log.as_ref() {
match log.output {
config::Log_Output::CONSOLE => {
// console output already applied
#[cfg(any(target_os = "ios", target_os = "android"))]
{
let console_output = fern::Output::writer(
Box::new(logger::ConsoleWriter(BytesMut::new())),
"\n",
);
logger = logger.chain(console_output);
}
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
logger = logger.chain(fern::Output::stdout("\n"));
}
}
config::Log_Output::FILE => {
let f = fern::log_file(&log.output_file).expect("open log file failed");
@@ -46,28 +53,16 @@ fn run(rt: &tokio::runtime::Runtime, config: leaf::config::Config) {
let runners = match leaf::util::create_runners(config) {
Ok(v) => v,
Err(e) => {
error!("create runners fialed: {}", e);
log::error!("create runners fialed: {}", e);
return;
}
};
// let monit_mem = Box::pin(async {
// loop {
// let n = unsafe { os_proc_available_memory() };
// debug!("{} bytes memory available", n);
// tokio::time::delay_for(std::time::Duration::from_secs(1)).await;
// }
// });
rt.block_on(async move {
tokio::select! {
_ = futures::future::join_all(runners) => (),
// _ = monit_mem => (),
}
});
rt.block_on(futures::future::join_all(runners));
}
#[cfg(target_os = "ios")]
// TODO Return meaningful error codes.
#[cfg(not(target_os = "android"))]
#[no_mangle]
pub extern "C" fn run_leaf(config_path: *const c_char) {
let rt = tokio::runtime::Builder::new_current_thread()
@@ -80,7 +75,7 @@ pub extern "C" fn run_leaf(config_path: *const c_char) {
{
run(&rt, config);
} else {
error!("invalid config path or config file");
log::error!("invalid config path or config file");
return;
}
}
@@ -103,7 +98,7 @@ pub extern "C" fn run_leaf(config_path: *const c_char, protect_path: *const c_ch
{
run(&rt, config);
} else {
error!("invalid config path or config file");
log::error!("invalid config path or config file");
return;
}
}
@@ -23,7 +23,6 @@ fn log_out(data: &[u8]) {
std::ptr::null_mut(),
std::ptr::null_mut(),
ASL_LEVEL_NOTICE as i32,
// ffi::CString::new("%s").unwrap().as_c_str().as_ptr(),
s.as_c_str().as_ptr(),
)
};
@@ -44,30 +43,6 @@ fn log_out(data: &[u8]) {
}
}
pub struct ConsoleLogger;
impl log::Log for ConsoleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Debug
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
log_out(
format!(
"[{}] [{}] {}",
record.level(),
record.target(),
record.args()
)
.as_bytes(),
)
}
}
fn flush(&self) {}
}
pub struct ConsoleWriter(pub BytesMut);
unsafe impl Send for ConsoleWriter {}
File renamed without changes.