From ea3454f0b2af60fe4a3db573ea67400ed178a967 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 5 Apr 2021 18:21:19 +0800 Subject: [PATCH] ffi: allows building as static library for all targets --- Cargo.toml | 2 +- Makefile | 12 ++++-- leaf-bin/src/main.rs | 4 +- {leaf-mobile => leaf-ffi}/Cargo.toml | 2 +- {leaf-mobile => leaf-ffi}/build.rs | 0 {leaf-mobile => leaf-ffi}/cbindgen.toml | 0 {leaf-mobile => leaf-ffi}/src/bindings.rs | 0 {leaf-mobile => leaf-ffi}/src/lib.rs | 47 ++++++++++------------- {leaf-mobile => leaf-ffi}/src/logger.rs | 25 ------------ {leaf-mobile => leaf-ffi}/wrapper.h | 0 10 files changed, 32 insertions(+), 60 deletions(-) rename {leaf-mobile => leaf-ffi}/Cargo.toml (96%) rename {leaf-mobile => leaf-ffi}/build.rs (100%) rename {leaf-mobile => leaf-ffi}/cbindgen.toml (100%) rename {leaf-mobile => leaf-ffi}/src/bindings.rs (100%) rename {leaf-mobile => leaf-ffi}/src/lib.rs (71%) rename {leaf-mobile => leaf-ffi}/src/logger.rs (71%) rename {leaf-mobile => leaf-ffi}/wrapper.h (100%) diff --git a/Cargo.toml b/Cargo.toml index 9b8b6e1..07986b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = [ "leaf", "leaf-bin", - "leaf-mobile", + "leaf-ffi", "tun2socks-bin", ] default-members = ["leaf-bin"] diff --git a/Makefile b/Makefile index fcae53c..9f382d5 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/leaf-bin/src/main.rs b/leaf-bin/src/main.rs index 6d2ae9c..fb87bd2 100644 --- a/leaf-bin/src/main.rs +++ b/leaf-bin/src/main.rs @@ -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"); diff --git a/leaf-mobile/Cargo.toml b/leaf-ffi/Cargo.toml similarity index 96% rename from leaf-mobile/Cargo.toml rename to leaf-ffi/Cargo.toml index 809cb3c..e8afe3d 100644 --- a/leaf-mobile/Cargo.toml +++ b/leaf-ffi/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "leaf-mobile" +name = "leaf-ffi" version = "0.1.0" authors = ["eycorsican "] edition = "2018" diff --git a/leaf-mobile/build.rs b/leaf-ffi/build.rs similarity index 100% rename from leaf-mobile/build.rs rename to leaf-ffi/build.rs diff --git a/leaf-mobile/cbindgen.toml b/leaf-ffi/cbindgen.toml similarity index 100% rename from leaf-mobile/cbindgen.toml rename to leaf-ffi/cbindgen.toml diff --git a/leaf-mobile/src/bindings.rs b/leaf-ffi/src/bindings.rs similarity index 100% rename from leaf-mobile/src/bindings.rs rename to leaf-ffi/src/bindings.rs diff --git a/leaf-mobile/src/lib.rs b/leaf-ffi/src/lib.rs similarity index 71% rename from leaf-mobile/src/lib.rs rename to leaf-ffi/src/lib.rs index c6da4c3..2d633bd 100644 --- a/leaf-mobile/src/lib.rs +++ b/leaf-ffi/src/lib.rs @@ -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; } } diff --git a/leaf-mobile/src/logger.rs b/leaf-ffi/src/logger.rs similarity index 71% rename from leaf-mobile/src/logger.rs rename to leaf-ffi/src/logger.rs index c5ca39d..e916df6 100644 --- a/leaf-mobile/src/logger.rs +++ b/leaf-ffi/src/logger.rs @@ -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 {} diff --git a/leaf-mobile/wrapper.h b/leaf-ffi/wrapper.h similarity index 100% rename from leaf-mobile/wrapper.h rename to leaf-ffi/wrapper.h