Skip to content
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

Add support for Android and iOS #7

Merged
merged 3 commits into from
Mar 15, 2022

Conversation

complexspaces
Copy link
Contributor

@complexspaces complexspaces commented Mar 14, 2022

This PR adds support for compiling and running on Android and iOS devices.

Test Code

[package]
name = "companion"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
android_logger = "0.10"
log = "0.4"

num_threads = { path = "../../github/num_threads" }
use std::time::Duration;

#[no_mangle]
pub extern "C" fn do_something() {
    android_logger::init_once(
        android_logger::Config::default().with_min_level(log::Level::Trace),
    );

    log::info!("trying to get thread number");

    for i in 0..10 {
        log::info!("{:?} threads running", num_threads::num_threads());

        if i % 2 == 0 {
            std::thread::spawn(|| {
                std::thread::sleep(Duration::from_secs(5));
            });
        }
    } 

    std::thread::sleep(Duration::from_secs(60));
    log::info!("{:?} threads running, closing", num_threads::num_threads());
}

Android

This patch is very simple, as the existing Linux implementation already works great inside the Android app sandbox.

This was tested on both a physical Android device and in an emulator by compiling, then loading, a minimal Rust dylib via the JNI.

Output (note the thread count is always high because of the JVM in the process):

2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: trying to get thread number
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(18) threads running
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(19) threads running
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(19) threads running
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(20) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(20) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(21) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(21) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(22) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(22) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(23) threads running
2022-03-14 11:17:47.273 8185-8185/com.library.rustlibtesting I/companion: Some(14) threads running, closing

iOS

The patch for iOS, like Android, is very simple because the MacOS implementation can be reused entirely with no changes.

This was tested inside the iOS simulator and on a physical iPad.

Output (the thread count here is high too because of the Swift runtime):

Trying to get thread number
Some(9) threads running
Some(10) threads running
Some(10) threads running
Some(11) threads running
Some(11) threads running
Some(12) threads running
Some(12) threads running
Some(13) threads running
Some(13) threads running
Some(14) threads running
Some(4) threads running, exiting

@jhpratt
Copy link
Owner

jhpratt commented Mar 14, 2022

Awesome! Diff looks good. Would you mind adding CI runs for this? It should only be a matter of adding targets here, just like there is for FreeBSD.

You might have to rebase because of a commit I just pushed up; I realized while typing this comment that there was some extraneous stuff in the CI file. I don't think you'll have to if you append the new runs, though.

@complexspaces
Copy link
Contributor Author

I'm not sure why the iOS typechecking job is failing. My best guess is that Rust 1.28 didn't support the aarch64-apple-ios target since rustup doesn't appear to recognize it at all.

@jhpratt
Copy link
Owner

jhpratt commented Mar 14, 2022

Hmm…that's probably correct. Let me do some digging to see what version it was introduced in.

@jhpratt
Copy link
Owner

jhpratt commented Mar 14, 2022

I can't immediately determine what version it was introduced in, as it's not mentioned in the release notes. However, it looks like it could only be built on MacOS prior to 1.56.

How familiar are you with GitHub Actions? I think the best way would be to run on macos-latest for this specific target. I can try this if you're not too familiar.

@complexspaces
Copy link
Contributor Author

I can give this a try 👍

@complexspaces
Copy link
Contributor Author

CI looks green to me now. Please let me know if there are tweaks you'd like made to it.

@jhpratt
Copy link
Owner

jhpratt commented Mar 15, 2022

The MacOS check doesn't actually need to be on MacOS, as it works as expected on the Ubuntu host. But that's not a big deal. Thanks!

@jhpratt jhpratt merged commit b8999cc into jhpratt:main Mar 15, 2022
@complexspaces complexspaces deleted the mobile-support branch March 15, 2022 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants