From 26009f376c440837e0c1c5b32850546c79f8a372 Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Sun, 28 Apr 2024 11:58:21 +0200 Subject: [PATCH] Generate Android builds --- .gitignore | 3 +- Cargo.lock | 138 +++++++++++++++++++++++++++++--- Cargo.toml | 4 +- Dockerfile | 1 - docker-compose.yml | 34 ++++---- robo-identities-java/.gitignore | 2 + robo-identities-java/Cargo.toml | 11 +++ robo-identities-java/src/lib.rs | 61 ++++++++++++++ robohash/Cargo.toml | 1 + robohash/src/lib.rs | 49 ++++++++++++ robonames/Cargo.toml | 1 + robonames/src/lib.rs | 31 +++++++ 12 files changed, 306 insertions(+), 30 deletions(-) create mode 100644 robo-identities-java/.gitignore create mode 100644 robo-identities-java/Cargo.toml create mode 100644 robo-identities-java/src/lib.rs diff --git a/.gitignore b/.gitignore index 2d12d3b..eb58189 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/builds \ No newline at end of file +/builds +/target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 2220cb7..5a9a9e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,6 +132,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" + [[package]] name = "cast" version = "0.3.0" @@ -144,6 +150,12 @@ version = "1.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + [[package]] name = "cfg-if" version = "1.0.0" @@ -208,6 +220,16 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -510,7 +532,7 @@ checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ "hermit-abi", "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -528,6 +550,28 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "jni" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" +dependencies = [ + "cesu8", + "cfg-if", + "combine", + "jni-sys", + "log", + "thiserror", + "walkdir", + "windows-sys 0.45.0", +] + +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + [[package]] name = "jpeg-decoder" version = "0.3.1" @@ -920,6 +964,14 @@ dependencies = [ "winapi", ] +[[package]] +name = "robo-identities-java" +version = "0.1.0" +dependencies = [ + "jni", + "robonames", +] + [[package]] name = "robo-identities-wasm" version = "0.1.0" @@ -944,6 +996,7 @@ dependencies = [ "data-encoding", "image", "imageproc", + "jni", "prefer-dynamic", "ring", "strum", @@ -957,6 +1010,7 @@ name = "robonames" version = "0.1.0" dependencies = [ "data-encoding", + "jni", "num", "prefer-dynamic", "ring", @@ -1388,13 +1442,37 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.4", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -1403,51 +1481,93 @@ version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.52.4", + "windows_aarch64_msvc 0.52.4", + "windows_i686_gnu 0.52.4", + "windows_i686_msvc 0.52.4", + "windows_x86_64_gnu 0.52.4", + "windows_x86_64_gnullvm 0.52.4", + "windows_x86_64_msvc 0.52.4", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.52.4" diff --git a/Cargo.toml b/Cargo.toml index 59af6e2..9cd628f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [workspace] - members = [ "robohash", "robonames", - "robo-identities-wasm" + "robo-identities-wasm", + "robo-identities-java" ] [profile.release] diff --git a/Dockerfile b/Dockerfile index 66b44f0..eb11eb9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -63,7 +63,6 @@ RUN rustup target add \ ENV PATH="${PATH}:${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin" - # install cargo tools RUN cargo install cargo-ndk diff --git a/docker-compose.yml b/docker-compose.yml index f9bf1ee..3999138 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,26 +1,26 @@ version: '3.4' services: - rust_wasm: - build: - context: . - dockerfile: ./robo-identities-wasm/Dockerfile - volumes: - - ./builds/wasm/:/app/robo-identities-wasm/pkg/:rw - command: wasm-pack build - rust_java_robonames: - build: - context: . - dockerfile: ./Dockerfile - target: robonames - volumes: - - ./builds/robonames/:/root/target/:rw - command: cargo ndk -t armeabi-v7a -t arm64-v8a -o ./target build --release + # rust_wasm: + # build: + # context: . + # dockerfile: ./robo-identities-wasm/Dockerfile + # volumes: + # - ./builds/wasm/:/app/robo-identities-wasm/pkg/:rw + # command: wasm-pack build + # rust_java_robonames: + # build: + # context: . + # dockerfile: ./Dockerfile + # target: robonames + # volumes: + # - ./builds/java/robonames:/root/target/:rw + # command: cargo ndk -t armeabi-v7a -t arm64-v8a -o ./target build --release rust_java_robohash: build: context: . dockerfile: ./Dockerfile target: robohash volumes: - - ./builds/robohash/:/root/target/:rw - command: cargo ndk -t armeabi-v7a -t arm64-v8a -o ./target build --release \ No newline at end of file + - ./builds/java/robohash:/root/target/:rw + command: cargo ndk -t armeabi-v7a -t arm64-v8a -o ./target build --release diff --git a/robo-identities-java/.gitignore b/robo-identities-java/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/robo-identities-java/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/robo-identities-java/Cargo.toml b/robo-identities-java/Cargo.toml new file mode 100644 index 0000000..b8720d1 --- /dev/null +++ b/robo-identities-java/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "robo-identities-java" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["rlib", "dylib"] + +[dependencies] +jni = "0.21.1" +robonames = { path = "../robonames" } diff --git a/robo-identities-java/src/lib.rs b/robo-identities-java/src/lib.rs new file mode 100644 index 0000000..be7355e --- /dev/null +++ b/robo-identities-java/src/lib.rs @@ -0,0 +1,61 @@ +#[allow(non_snake_case)] +pub mod android { + use jni::JNIEnv; + use jni::objects::{JClass, JString}; + use jni::sys::jstring; + + use robonames::generate_short_nickname; + + #[no_mangle] + pub extern "system" fn Java_com_robosats_RoboIdentities_nativeHello<'local>(mut env: JNIEnv<'local>, + // This is the class that owns our static method. It's not going to be used, + // but still must be present to match the expected signature of a static + // native method. + _class: JClass<'local>, + input: JString<'local>) + -> jstring { + // First, we have to get the string out of Java. Check jstring `strings` + // module for more info on how this works. + let input: String = + env.get_string(&input).expect("Couldn't get java string!").into(); + + // Then we have to create a new Java string to return. Again, more info + // in the `strings` module. + let output = env.new_string(format!("Hello, {}!", input)) + .expect("Couldn't create java string!"); + + // Finally, extract the raw pointer to return. + output.into_raw() + } + + #[no_mangle] + pub extern "system" fn Java_com_robosats_RoboIdentities_nativeGenerateRoboname<'local>(mut env: JNIEnv<'local>, + // This is the class that owns our static method. It's not going to be used, + // but still must be present to match the expected signature of a static + // native method. + _class: JClass<'local>, + initial_string: JString<'local>) + -> jstring { + + let initial_string: String = + env.get_string(&initial_string).expect("Couldn't get java string!").into(); + + + match initial_string.split_once(';') { + Some((_initial_string, size)) => { + // Generate Robot Nickname synchronousl. Returns a nickname string. + let nickname = generate_short_nickname(initial_string); + match nickname { + Ok(nick) => { + let output = env.new_string(nickname) + .expect("Couldn't create java string!"); + // Finally, extract the raw pointer to return. + output.into_raw() + }, + Err(_) => todo!(), + } + } + None => todo!(), + } + } +} \ No newline at end of file diff --git a/robohash/Cargo.toml b/robohash/Cargo.toml index 6becd1d..293af4c 100644 --- a/robohash/Cargo.toml +++ b/robohash/Cargo.toml @@ -18,6 +18,7 @@ path = "src/main.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +jni = "0.21.1" ring = "0.16.20" anyhow = "1.0.66" data-encoding = "2.3.2" diff --git a/robohash/src/lib.rs b/robohash/src/lib.rs index 9577df6..3edd1f7 100644 --- a/robohash/src/lib.rs +++ b/robohash/src/lib.rs @@ -126,6 +126,55 @@ fn select_hue_rotation(hash_array: &[i64]) -> Option { Some(hue) } +pub mod android { + use jni::objects::{JClass, JString}; + use jni::sys::jstring; + use jni::JNIEnv; + + use crate::RoboHashBuilder; + + #[no_mangle] + pub extern "system" fn Java_com_robosats_RoboIdentities_nativeGenerateRobohash<'local>( + mut env: JNIEnv<'local>, + + _class: JClass<'local>, + initial_string: JString<'local>, + ) -> jstring { + let initial_string: String = env + .get_string(&initial_string) + .expect("Couldn't get java string!") + .into(); + + match initial_string.split_once(';') { + Some((hash, size_str)) => { + match size_str.parse::() { + Ok(size) => { + let robohash = RoboHashBuilder::new(hash) + .with_background(&true) + .with_size(size as u32, size as u32) + .build(); + + match robohash { + Ok(robo) => match robo.assemble_base64() { + Ok(base64_string) => { + let output = env + .new_string(base64_string) + .expect("Couldn't create java string!"); + output.into_raw() + } + Err(_text) => todo!(), + }, + Err(_text) => todo!(), + } + } + Err(_err) => todo!(), + } + } + None => todo!(), + } + } +} + #[cfg(test)] mod tests { use std::fs::File; diff --git a/robonames/Cargo.toml b/robonames/Cargo.toml index 665845d..d2445f9 100644 --- a/robonames/Cargo.toml +++ b/robonames/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +jni = "0.21.1" data-encoding = "2.4.0" num = "0.4.1" ring = "0.16.20" diff --git a/robonames/src/lib.rs b/robonames/src/lib.rs index 777a50f..ee7f9e6 100644 --- a/robonames/src/lib.rs +++ b/robonames/src/lib.rs @@ -86,6 +86,37 @@ pub fn generate_short_nickname(hex_str: &str) -> Result { Ok(String::from("")) } +pub mod android { + use jni::objects::{JClass, JString}; + use jni::sys::jstring; + use jni::JNIEnv; + + use crate::generate_short_nickname; + + #[no_mangle] + pub extern "system" fn Java_com_robosats_RoboIdentities_nativeGenerateRoboname<'local>( + mut env: JNIEnv<'local>, + + _class: JClass<'local>, + initial_string: JString<'local>, + ) -> jstring { + let initial_string: String = env + .get_string(&initial_string) + .expect("Couldn't get java string!") + .into(); + let string: &str = initial_string.as_str(); + let nickname = generate_short_nickname(string); + match nickname { + Ok(nick) => { + let output = env.new_string(nick).expect("Couldn't create java string!"); + // Finally, extract the raw pointer to return. + output.into_raw() + } + Err(_) => todo!(), + } + } +} + #[cfg(test)] mod tests { use super::*;