diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index e714085..f847fb0 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -13,27 +13,40 @@ jobs: runs-on: macos-14 steps: - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: toolchain: stable targets: aarch64-apple-darwin,x86_64-apple-darwin,aarch64-apple-ios,aarch64-apple-ios-sim,x86_64-apple-ios - - run: brew install cbindgen + + - name: Install cbindgen + run: brew install cbindgen - - name: Run cargo build for each target + - name: Run cargo build for each macos target run: make macos + - name: Run cargo build for each ios target + run: make ios + - name: Generate header file - run: cbindgen --config cbindgen.toml --crate munchausen --output include/munchausen.h + run: cbindgen --config cbindgen.toml --crate fiber_ffi --output include/fiber_ffi.h - name: Create binary framework run: | xcodebuild -create-xcframework \ - -library libs/libmunchausen-macos.a \ + -library libs/libfiber_ffi_macos.a \ + -headers ./include/ \ + -library libs/libfiber_ffi_ios_sim.a \ -headers ./include/ \ - -output Munchausen.xcframework + -library libs/libfiber_ffi_ios.a \ + -headers ./include/ \ + -output FiberFFI.xcframework + + - name: Create zipped framework + run: zip -r framework.zip FiberFFI.xcframework - - run: zip -r framework.zip Munchausen.xcframework - - run: openssl dgst -sha256 framework.zip + - name: Generate checksum + run: openssl dgst -sha256 framework.zip - name: Create Release id: create_release @@ -57,10 +70,10 @@ jobs: asset_name: framework.zip asset_content_type: application/zip - - name: Invoke workflow without inputs - uses: peter-evans/repository-dispatch@v3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - repository: szkabaroli/SwiftRusty - event-type: new-release - client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}' \ No newline at end of file + #- name: Invoke workflow without inputs + # uses: peter-evans/repository-dispatch@v3 + # with: + # token: ${{ secrets.GITHUB_TOKEN }} + # repository: szkabaroli/SwiftRusty + # event-type: new-release + # client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}' \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 29cdeff..775fe02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "munchausen" -version = "0.10.0" +name = "fiber_ffi" +version = "0.12.0" diff --git a/Cargo.toml b/Cargo.toml index 73fd951..d6b2701 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "munchausen" +name = "fiber_ffi" version = "0.12.0" authors = [] diff --git a/Makefile b/Makefile index 3ce8558..b2670b4 100644 --- a/Makefile +++ b/Makefile @@ -3,21 +3,21 @@ macos: @cargo build --release --lib --target x86_64-apple-darwin #@cargo +nightly build -Z build-std - release - lib - target aarch64-apple-ios-macabi #@cargo +nightly build -Z build-std - release - lib - target x86_64-apple-ios-macabi - @$(RM) -rf libs/libmunchausen-macos.a + @$(RM) -rf libs/libfiber_ffi_macos.a #@$(RM) -rf libs/libmunchausen-maccatalyst.a - @lipo -create -o libs/libmunchausen-macos.a \ - target/aarch64-apple-darwin/release/libmunchausen.a \ - target/x86_64-apple-darwin/release/libmunchausen.a + @lipo -create -o libs/libfiber_ffi_macos.a \ + target/aarch64-apple-darwin/release/libfiber_ffi.a \ + target/x86_64-apple-darwin/release/libfiber_ffi.a #@lipo -create -output libs/libmunchausen-maccatalyst.a \ # target/aarch64-apple-ios-macabi/release/libmunchausen.a \ # target/x86_64-apple-ios-macabi/release/libmunchausen.a ios: - @cargo build - release - lib - target aarch64-apple-ios - @cargo build - release - lib - target aarch64-apple-ios-sim - @cargo build - release - lib - target x86_64-apple-ios + @cargo build --release --lib --target aarch64-apple-ios + @cargo build --release --lib --target aarch64-apple-ios-sim + @cargo build --release --lib --target x86_64-apple-ios @$(RM) -rf libs/libmunchausen-ios.a @$(RM) -rf libs/libmunchausen-ios-sim.a - # @cp target/aarch64-apple-ios/release/libmunchausen.a libs/libmunchausen-ios.a - # @lipo -create -output libs/libmunchausen-ios-sim.a \ - target/aarch64-apple-ios-sim/release/libmunchausen.a \ - target/x86_64-apple-ios/release/libmunchausen.a \ No newline at end of file + @cp target/aarch64-apple-ios/release/libfiber_ffi.a libs/libfiber_ffi_ios.a + @lipo -create -output libs/libfiber_ffi_ios_sim.a \ + target/aarch64-apple-ios-sim/release/libfiber_ffi.a \ + target/x86_64-apple-ios/release/libfiber_ffi.a \ No newline at end of file diff --git a/include/module.modulemap b/include/module.modulemap index 4892189..9c40642 100644 --- a/include/module.modulemap +++ b/include/module.modulemap @@ -1,4 +1,4 @@ -module Munchausen { - header "munchausen.h" +module FiberFFI { + header "fiber_ffi.h" export * } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 426b2b1..e961f3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,59 +1,73 @@ -use std::ffi; - -type Callback = unsafe extern "C" fn(data: *mut u8, len: usize) -> ffi::c_int; - -#[no_mangle] -pub extern "C" fn test(cb: Callback) { - let mut vec = vec![1, 2, 3, 4]; - vec.shrink_to_fit(); - assert!(vec.len() == vec.capacity()); +macro_rules! multiconst { + ($typename:ident, [$($(#[$attr:meta])* $rawname:ident = $value:expr;)*]) => { + $( + $(#[$attr])* + pub const $rawname: $typename = $value; + )* + } +} - let res = unsafe { cb(vec.as_mut_ptr(), vec.len() as usize) }; +#[allow(non_camel_case_types)] +pub type fx_handle_t = u32; +#[allow(non_camel_case_types)] +pub type fx_status_t = i32; +#[allow(non_camel_case_types)] +pub type fx_obj_type_t = u32; +#[allow(non_camel_case_types)] +pub type fx_rights_t = u32; - println!("{:?}", cb); - println!("{}", res); +#[repr(C)] +#[allow(non_camel_case_types)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub struct fx_handle_info_t { + pub handle: fx_handle_t, + pub ty: fx_obj_type_t, + pub rights: fx_rights_t, + pub unused: u32, } -#[no_mangle] -pub extern "C" fn rust_munchausen_numbers() -> *mut [i32; 4] { - // Pre-caching the power for all of the digits; 0⁰ is initially in the cache array. - let mut cache = [0; 10]; - let mut index = 0; - let mut munchausen_num: [i32; 4] = [0; 4]; - let munchausen_num_ptr: *mut [i32; 4] = &mut munchausen_num; - - for n in 1..=9 { - cache[n] = (n as i32).pow(n as u32); - } +multiconst!(fx_status_t, [ + FX_OK = 0; +]); - // Searching for Munchausen numbers iterating through a long range containing all of them. - for n in 0..500000000 { - if is_munchausen_number(n, &cache) { - munchausen_num[index] = n; - index += 1; - } - } +multiconst!(fx_obj_type_t, [ + FX_OBJ_TYPE_NONE = 0; + FX_OBJ_TYPE_PROCESS = 1; + FX_OBJ_TYPE_CHANNEL = 4; + FX_OBJ_TYPE_EVENT = 5; + FX_OBJ_TYPE_PORT = 6; + FX_OBJ_TYPE_JOB = 17; +]); - munchausen_num_ptr +#[no_mangle] +pub extern "C" fn fx_port_create(options: u32, out: *mut fx_handle_t) -> fx_status_t { + FX_OK } -fn is_munchausen_number(number: i32, cache: &[i32; 10]) -> bool { - let mut current_number = number; - let mut sum = 0; - - // The calculation details: Do until we go through all of the digits. - while current_number > 0 { - // Take the last digit of a number. - let digit = current_number % 10; - // Add the cached power of the digit to the overall sum. - sum += cache[digit as usize]; - - if sum > number { - return false; - } - // "Cut" the last digit - current_number /= 10; - } +#[no_mangle] +pub extern "C" fn fx_channel_read( + handle: fx_handle_t, + options: u32, + bytes: *mut u8, + handles: *mut fx_handle_t, + num_bytes: u32, + num_handles: u32, + actual_bytes: *mut u32, + actual_handles: *mut u32, +) -> fx_status_t { + FX_OK +} - number == sum +#[no_mangle] +pub extern "C" fn fx_channel_read_etc( + handle: fx_handle_t, + options: u32, + bytes: *mut u8, + handles: *mut fx_handle_info_t, + num_bytes: u32, + num_handles: u32, + actual_bytes: *mut u32, + actual_handles: *mut u32, +) -> fx_status_t { + FX_OK }