diff --git a/.github/actions/cache_cargo/action.yml b/.github/actions/cache_cargo/action.yml index 27c5d66..656a3e0 100644 --- a/.github/actions/cache_cargo/action.yml +++ b/.github/actions/cache_cargo/action.yml @@ -5,10 +5,12 @@ runs: steps: # Fix timestamps + - uses: cargo-bins/cargo-binstall@main - name: restore timestamps uses: chetan/git-restore-mtime-action@v2 - - name: Cache cargo registry - uses: actions/cache@v2 + - name: Cache cargo registry pc-test + if: runner.prefix == 'pc-test' + uses: actions/cache@v3 with: path: | ~/.cargo/bin/ @@ -16,8 +18,34 @@ runs: ~/.cargo/registry/cache/ ~/.cargo/git/db/ target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo- + key: ${{ runner.os }}-${{ runner.version }}-${{ runner.prefix }}-${{ runner.target }}-${{ runner.api-level }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-${{ runner.version }}-${{ runner.prefix }}-${{ runner.target }}-${{ runner.api-level }}-cargo- + - name: Cache cargo registry pc-run + uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + examples/pc/target/ + key: ${{ runner.os }}-${{ runner.version }}-${{ runner.prefix }}-${{ runner.target }}-${{ runner.api-level }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-${{ runner.version }}-${{ runner.prefix }}-${{ runner.target }}-${{ runner.api-level }}-cargo- + +jobs: + post: + - name: Clear the cargo caches powershell + if: runner.os == 'windows-latest' + shell: powershell + run: | + cargo install cargo-cache --no-default-features --features ci-autoclean --force --debug + cargo-cache + - name: Clear the cargo caches bash + if: runner.os != 'windows-latest' + shell: bash + run: | + cargo install cargo-cache --no-default-features --features ci-autoclean --force --debug + cargo-cache # https://zenn.dev/naokifujita/articles/c890954165c21f # https://zenn.dev/kt3k/articles/d557cc874961ab diff --git a/.github/actions/install_rust_by_version/action.yml b/.github/actions/install_rust_by_version/action.yml new file mode 100644 index 0000000..b6de653 --- /dev/null +++ b/.github/actions/install_rust_by_version/action.yml @@ -0,0 +1,10 @@ +name: install_rust_by_version +description: install rust by version +runs: + using: composite + + steps: + - name: Install Rust (${{ matrix.version }}) + uses: actions-rust-lang/setup-rust-toolchain@v1.5.0 + with: + toolchain: ${{ matrix.version }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..defc324 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,349 @@ +on: [push, pull_request] + +jobs: + build_cache_pc_tier1: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [pc_test_target_tier1] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + steps: + - uses: actions/checkout@v3 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - name: build + run: | + cargo build + cargo build --release + - name: build linux + if: matrix.os == 'ubuntu-latest' + run: | + target=("aarch64-unknown-linux-gnu" "i686-unknown-linux-gnu" "x86_64-unknown-linux-gnu") + for v in "${target[@]}"; do + rustup target add ${v} + cargo build --target ${v} + cargo build --target ${v} --release + done + - name: build macos + if: matrix.os == 'macos-latest' + run: | + target=("x86_64-apple-darwin") + for v in "${target[@]}"; do + rustup target add ${v} + cargo build --target ${v} + cargo build --target ${v} --release + done + - name: build windows + if: matrix.os == 'windows-latest' + run: | + $target = @("i686-pc-windows-gnu", "i686-pc-windows-msvc", "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc") + foreach ($v in $target) { + rustup target add $v + cargo build --target ${v} + cargo build --target ${v} --release + } + + fmt_pc: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [pc_test_target_tier1] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + needs: build_cache_pc_tier1 + steps: + - uses: actions/checkout@v3 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - run: rustup component add rustfmt + - name: fmt + run: cargo fmt --all -- --check + + clippy_pc_tier1: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [pc_test] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + needs: build_cache_pc_tier1 + steps: + - uses: actions/checkout@v3 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - run: rustup component add clippy + - name: clippy + run: | + cargo clippy --all-targets --all-features -- -D warnings + - name: clippy linux + if: matrix.os == 'ubuntu-latest' + run: | + target=("aarch64-unknown-linux-gnu" "i686-unknown-linux-gnu" "x86_64-unknown-linux-gnu") + for v in "${target[@]}"; do + rustup target add ${v} + cargo clippy --target ${v} --all-targets --all-features -- -D warnings + done + - name: clippy macos + if: matrix.os == 'macos-latest' + run: | + target=("x86_64-apple-darwin") + for v in "${target[@]}"; do + rustup target add ${v} + cargo clippy --target ${v} --all-targets --all-features -- -D warnings + done + - name: clippy windows + if: matrix.os == 'windows-latest' + run: | + $target = @("i686-pc-windows-gnu", "i686-pc-windows-msvc", "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc") + foreach ($v in $target) { + rustup target add $v + cargo clippy --target ${v} --all-targets --all-features -- -D warnings + } + + test_pc_tier1: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [pc_test] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + needs: build_cache_pc_tier1 + steps: + - uses: actions/checkout@v3 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - name: test + run: | + cargo test --all -- --nocapture + cargo test --all --release -- --nocapture + - name: test linux aarch64 + if: matrix.os == 'ubuntu-latest' + # && matrix.version != '1.68.0' + uses: uraimo/run-on-arch-action@v2 + with: + arch: aarch64 + distro: ubuntu_latest + # Not required, but speeds up builds by reusing the cache from the previous job + # githubToken: ${{ github.token }} + run: | + apt-get update -q -y + apt-get install -y -q curl gcc + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${{ matrix.version }} --profile minimal + source "$HOME/.cargo/env" + CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse cargo test --all --target aarch64-unknown-linux-gnu -- --nocapture + CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse cargo test --all --target aarch64-unknown-linux-gnu --release -- --nocapture + - name: test linux i686 + if: matrix.os == 'ubuntu-latest' + # until merged https://github.com/uraimo/run-on-arch-action/pull/94 + uses: lalten/run-on-arch-action@amd64-support + with: + arch: i686 + distro: ubuntu_latest + run: | + apt-get update -q -y + apt-get install -q -y curl gcc + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${{ matrix.version }} --profile minimal + source "$HOME/.cargo/env" + CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse cargo test --all --target i686-unknown-linux-gnu -- --nocapture + CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse cargo test --all --target i686-unknown-linux-gnu --release -- --nocapture + - name: test linux + if: matrix.os == 'ubuntu-latest' + # cargo test can only real platform + run: | + target=("x86_64-unknown-linux-gnu") + for v in "${target[@]}"; do + rustup target add ${v} + cargo test --all --target ${v} -- --nocapture + cargo test --all --release --target ${v} -- --nocapture + done + - name: test macos + if: matrix.os == 'macos-latest' + run: | + target=("x86_64-apple-darwin") + for v in "${target[@]}"; do + rustup target add ${v} + cargo test --all --target ${v} -- --nocapture + cargo test --all --release --target ${v} -- --nocapture + done + - name: test windows + if: matrix.os == 'windows-latest' + run: | + $target = @("i686-pc-windows-gnu", "i686-pc-windows-msvc", "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc") + foreach ($v in $target) { + rustup target add $v + cargo test --all --target $v -- --nocapture + cargo test --all --release --target $v -- --nocapture + } + + build_target_tier2: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [pc_test_target_tier2] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + steps: + - uses: actions/checkout@v3 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - name: build linux + if: matrix.os == 'ubuntu-latest' + # note: not all platforms have the standard library pre-compiled: https://doc.rust-lang.org/nightly/rustc/platform-support.html + # "loongarch64-unknown-linux-gnu" + run: | + target=("aarch64-unknown-linux-musl" "arm-unknown-linux-gnueabi" "arm-unknown-linux-gnueabihf" "armv7-unknown-linux-gnueabihf" "powerpc-unknown-linux-gnu" "powerpc64-unknown-linux-gnu" "powerpc64le-unknown-linux-gnu" "riscv64gc-unknown-linux-gnu" "s390x-unknown-linux-gnu" "x86_64-unknown-freebsd" "x86_64-unknown-illumos" "x86_64-unknown-linux-musl" "x86_64-unknown-netbsd") + for v in "${target[@]}"; do + rustup target add ${v} + cargo build --target ${v} + cargo build --target ${v} --release + done + - name: build linux loongarch64 + if: matrix.os == 'ubuntu-latest' && matrix.version != '1.68.0' + run: | + rustup target add loongarch64-unknown-linux-gnu + cargo build --target loongarch64-unknown-linux-gnu + cargo build --target loongarch64-unknown-linux-gnu --release + - name: build macos + if: matrix.os == 'macos-latest' + run: | + target=("aarch64-apple-darwin") + for v in "${target[@]}"; do + rustup target add ${v} + cargo build --target ${v} + cargo build --target ${v} --release + done + - name: build windows + if: matrix.os == 'windows-latest' + run: | + $target = @("aarch64-pc-windows-msvc") + foreach ($v in $target) { + rustup target add $v + cargo build --target ${v} + cargo build --target ${v} --release + } + + build_cache_android: + strategy: + matrix: + os: [ubuntu-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [android_test] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - name: build android + run: | + target=("aarch64-linux-android" "arm-linux-androideabi" "armv7-linux-androideabi" "i686-linux-android" "x86_64-linux-android") + for v in "${target[@]}"; do + rustup target add ${v} + cargo build --target ${v} + cargo build --target ${v} --release + done + - name: build + run: cargo build + + clippy_android: + strategy: + matrix: + os: [ubuntu-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [android_test] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + needs: build_cache_android + steps: + - uses: actions/checkout@v3 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - run: rustup component add clippy + - name: clippy + run: | + cargo clippy --all-targets --all-features -- -D warnings + - name: clippy linux + run: | + target=("aarch64-linux-android" "arm-linux-androideabi" "armv7-linux-androideabi" "i686-linux-android" "x86_64-linux-android") + for v in "${target[@]}"; do + rustup target add ${v} + cargo clippy --target ${v} --all-targets --all-features -- -D warnings + done + + test_android: + strategy: + matrix: + os: [ubuntu-latest] + version: [1.68.0, stable, beta, nightly] + api-level: [24, 30, 33] + prefix: [android_test] + fail-fast: false + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + needs: build_cache_android + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 1 + - uses: ./.github/actions/cache_cargo + - uses: ./.github/actions/install_rust_by_version + - name: install rust_android_ndk + # run: rustup target add aarch64-linux-android arm-linux-androideabi armv7-linux-androideabi i686-linux-android x86_64-linux-android + run: rustup target add x86_64-linux-android + - name: install dinghy + run: | + if [ ${{matrix.version}} == "1.68.0" ]; then + if [ ${{matrix.os}} == "macos-latest" ]; then + cargo binstall -q --no-confirm --force --pkg-url="https://github.com/sonos/dinghy/releases/download/0.6.5/cargo-dinghy-macos-0.6.5.tgz" cargo-dinghy@0.6.5 + else + cargo binstall -q --no-confirm --force --pkg-url="https://github.com/sonos/dinghy/releases/download/0.6.5/cargo-dinghy-linux-0.6.5.tgz" cargo-dinghy@0.6.5 + fi + else + cargo binstall -q --no-confirm --force cargo-dinghy + fi + - name: start android emulator + uses: ReactiveCircus/android-emulator-runner@v2 + with: + api-level: ${{ matrix.api-level }} + target: default + arch: x86_64 + profile: pixel_2 + emulator-boot-timeout: 900 + script: | + cargo dinghy --platform auto-android-x86_64 test + +# https://zenn.dev/naokifujita/articles/c890954165c21f +# https://github.com/actix/actix-web/blob/master/.github/workflows/ci.yml +# https://github.com/rust-lang/cargo/issues/10781 +# https://doc.rust-lang.org/cargo/reference/registries.html diff --git a/.github/workflows/ci_linux.yml b/.github/workflows/ci_linux.yml deleted file mode 100644 index d1373e5..0000000 --- a/.github/workflows/ci_linux.yml +++ /dev/null @@ -1,48 +0,0 @@ -on: [push, pull_request] - -jobs: - build_cache: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - uses: ./.github/actions/cache_cargo - - name: build - run: cargo build - working-directory: . - - fmt: - runs-on: ubuntu-latest - needs: build_cache - steps: - - uses: actions/checkout@v3 - - run: rustup component add rustfmt - - uses: ./.github/actions/cache_cargo - - name: fmt - run: cargo fmt --all -- --check - working-directory: . - - clippy: - runs-on: ubuntu-latest - env: - RUSTC_FORCE_INCREMENTAL: 1 - needs: build_cache - steps: - - uses: actions/checkout@v3 - - run: rustup component add clippy - - uses: ./.github/actions/cache_cargo - - name: clippy - run: cargo clippy --all-targets --all-features -- -D warnings - working-directory: . - - test: - runs-on: ubuntu-latest - needs: build_cache - steps: - - uses: actions/checkout@v3 - - uses: ./.github/actions/cache_cargo - - name: test - run: cargo test --all -- --nocapture - working-directory: . -# https://zenn.dev/naokifujita/articles/c890954165c21f diff --git a/.github/workflows/ci_mac.yml b/.github/workflows/ci_mac.yml deleted file mode 100644 index a0edc0f..0000000 --- a/.github/workflows/ci_mac.yml +++ /dev/null @@ -1,48 +0,0 @@ -on: [push, pull_request] - -jobs: - build_cache: - runs-on: macOS-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - uses: ./.github/actions/cache_cargo - - name: build - run: cargo build - working-directory: . - - fmt: - runs-on: macOS-latest - needs: build_cache - steps: - - uses: actions/checkout@v3 - - run: rustup component add rustfmt - - uses: ./.github/actions/cache_cargo - - name: fmt - run: cargo fmt --all -- --check - working-directory: . - - clippy: - runs-on: macOS-latest - env: - RUSTC_FORCE_INCREMENTAL: 1 - needs: build_cache - steps: - - uses: actions/checkout@v3 - - run: rustup component add clippy - - uses: ./.github/actions/cache_cargo - - name: clippy - run: cargo clippy --all-targets --all-features -- -D warnings - working-directory: . - - test: - runs-on: macOS-latest - needs: build_cache - steps: - - uses: actions/checkout@v3 - - uses: ./.github/actions/cache_cargo - - name: test - run: cargo test --all -- --nocapture - working-directory: . -# https://zenn.dev/naokifujita/articles/c890954165c21f diff --git a/.github/workflows/ci_windows.yml b/.github/workflows/ci_windows.yml deleted file mode 100644 index 9a0feff..0000000 --- a/.github/workflows/ci_windows.yml +++ /dev/null @@ -1,48 +0,0 @@ -on: [push, pull_request] - -jobs: - build_cache: - runs-on: windows-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - uses: ./.github/actions/cache_cargo - - name: build - run: cargo build - working-directory: . - - fmt: - runs-on: windows-latest - needs: build_cache - steps: - - uses: actions/checkout@v3 - - run: rustup component add rustfmt - - uses: ./.github/actions/cache_cargo - - name: fmt - run: cargo fmt --all -- --check - working-directory: . - - clippy: - runs-on: windows-latest - env: - RUSTC_FORCE_INCREMENTAL: 1 - needs: build_cache - steps: - - uses: actions/checkout@v3 - - run: rustup component add clippy - - uses: ./.github/actions/cache_cargo - - name: clippy - run: cargo clippy --all-targets --all-features -- -D warnings - working-directory: . - - test: - runs-on: windows-latest - needs: build_cache - steps: - - uses: actions/checkout@v3 - - uses: ./.github/actions/cache_cargo - - name: test - run: cargo test --all -- --nocapture - working-directory: . -# https://zenn.dev/naokifujita/articles/c890954165c21f diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml new file mode 100644 index 0000000..6f854fa --- /dev/null +++ b/.github/workflows/run.yml @@ -0,0 +1,152 @@ +on: [push, pull_request] + +jobs: + run_pc: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + version: [1.68.0, stable, beta, nightly] + prefix: [pc_run] + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: ./.github/actions/install_rust_by_version + - uses: ./.github/actions/cache_cargo + - name: run windows release + if: matrix.os == 'windows-latest' + run: | + cargo build --release + cargo run --release > ${{ matrix.os }}-${{ matrix.version }}-release-run.log + cargo build + cargo run > ${{ matrix.os }}-${{ matrix.version }}-debug-run.log + working-directory: ./examples/common + - name: run release + if: matrix.os != 'windows-latest' + run: | + cargo build --release + sudo target/release/pc > ${{ matrix.os }}-${{ matrix.version }}-release-run.log + cargo build + sudo target/debug/pc > ${{ matrix.os }}-${{ matrix.version }}-debug-run.log + working-directory: ./examples/common + - name: Upload log release on run + uses: actions/upload-artifact@v3 + with: + name: run-log + path: ./examples/common/${{ matrix.os }}-${{ matrix.version }}-release-run.log + - name: Upload log on run + uses: actions/upload-artifact@v3 + with: + name: run-log + path: ./examples/common/${{ matrix.os }}-${{ matrix.version }}-debug-run.log + + run_android: + strategy: + matrix: + os: [ubuntu-latest] + api-level: [24, 30, 33] + prefix: [android_run] + fail-fast: false + # https://github.com/ReactiveCircus/android-emulator-runner/issues/46#issuecomment-1474555282 + # https://github.com/ReactiveCircus/android-emulator-runner/issues/15 + runs-on: ${{ matrix.os }} + env: + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + RUSTC_FORCE_INCREMENTAL: 1 + steps: + - uses: actions/checkout@v3 + - name: install target + # run: rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android arm-linux-androideabi + run: rustup target add x86_64-linux-android + - uses: cargo-bins/cargo-binstall@main + - name: install rust android + run: cargo binstall -q --no-confirm --force cargo-apk + - name: logcat + run: adb logcat RustStdoutStderr:D '*:S' > android-${{ matrix.api-level }}-run.log & + - name: start android emulator + uses: ReactiveCircus/android-emulator-runner@v2 + with: + api-level: ${{ matrix.api-level }} + target: default + arch: x86_64 + profile: pixel_2 + emulator-boot-timeout: 900 + script: bash ../../workflow/android/run.sh + working-directory: ./examples/android/ + - name: Upload log on cargo-apk + uses: actions/upload-artifact@v3 + with: + name: run-log + path: android-${{ matrix.api-level }}-run.log + + # not initialize ndk_context + # run_android_dinghy: + # strategy: + # matrix: + # os: [ubuntu-latest] + # api-level: [24, 30, 33] + # prefix: [android_run] + # fail-fast: false + # # https://github.com/ReactiveCircus/android-emulator-runner/issues/46#issuecomment-1474555282 + # # https://github.com/ReactiveCircus/android-emulator-runner/issues/15 + # runs-on: ${{ matrix.os }} + # env: + # CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + # RUSTC_FORCE_INCREMENTAL: 1 + # steps: + # - uses: actions/checkout@v3 + # - name: install target + # # run: rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android arm-linux-androideabi + # run: rustup target add x86_64-linux-android + # - name: install rust android + # run: cargo install cargo-dinghy --force + # - name: start android emulator + # uses: ReactiveCircus/android-emulator-runner@v2 + # with: + # api-level: ${{ matrix.api-level }} + # target: default + # arch: x86_64 + # profile: pixel_2 + # emulator-boot-timeout: 900 + # script: | + # cargo dinghy --platform auto-android-x86_64 build + # cargo dinghy --platform auto-android-x86_64 run > android-dinghy-${{ matrix.api-level }}-run.log + # cargo dinghy --platform auto-android-x86_64 build --release + # cargo dinghy --platform auto-android-x86_64 run --release > android-dinghy-${{ matrix.api-level }}-run.log + # working-directory: ./examples/common/ + # - name: Upload log on cargo-apk + # uses: actions/upload-artifact@v3 + # with: + # name: run-log + # path: ./examples/common/android-dinghy-${{ matrix.api-level }}-run.log + + concat_logs: + runs-on: ubuntu-latest + needs: [run_pc, run_android] + steps: + - name: Download artifacts on run + uses: actions/download-artifact@v3 + with: + name: run-log + path: artifacts + - name: Concatenate logs + run: | + directory="artifacts" + echo "" > all-logs.txt + for file in "$directory"/*; do + echo "================ ${file} ================" >> all-logs.txt + cat "$file" >> all-logs.txt + done + - name: Upload concatenated log + uses: actions/upload-artifact@v3 + with: + name: run-log + path: all-logs.txt +# https://zenn.dev/takeyaqa/articles/github-actions-appium-android-emulator +# https://stackoverflow.com/questions/3152681/android-emulator-5554-offline + +# https://github.com/kiwix/kiwix-android/blob/develop/.github/workflows/ci.yml diff --git a/.github/workflows/test_android.yml b/.github/workflows/test_android.yml deleted file mode 100644 index 419cacd..0000000 --- a/.github/workflows/test_android.yml +++ /dev/null @@ -1,69 +0,0 @@ -on: [push, pull_request] - -jobs: - test: - name: test - strategy: - matrix: - api-level: [24, 30, 33] - fail-fast: false - runs-on: macOS-11 - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 1 - - name: Set up JDK 11 - uses: actions/setup-java@v3 - with: - distribution: temurin - java-version: 11 - - name: install target - run: rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android - - name: install rust android - run: cargo install cargo-apk - - name: logcat - run: adb logcat RustStdoutStderr:D '*:S' > cargo-apk-${{ matrix.api-level }}.log & - - name: create instrumentation coverage - uses: ReactiveCircus/android-emulator-runner@v2 - env: - GRADLE_OPTS: "-Dorg.gradle.internal.http.connectionTimeout=60000 -Dorg.gradle.internal.http.socketTimeout=60000 -Dorg.gradle.internal.network.retry.max.attempts=6 -Dorg.gradle.internal.network.retry.initial.backOff=2000" - if: ${{ matrix.api-level != 33 }} - with: - api-level: ${{ matrix.api-level }} - target: default - arch: x86_64 - profile: pixel_2 - ram-size: "4096M" - disk-size: "14G" - sdcard-path-or-size: "1000M" - disable-animations: false - script: bash ../../workflow/android/run.sh - working-directory: ./examples/android/ - continue-on-error: true - - name: create instrumentation coverage on google_apis for android 33 - uses: ReactiveCircus/android-emulator-runner@v2 - env: - GRADLE_OPTS: "-Dorg.gradle.internal.http.connectionTimeout=60000 -Dorg.gradle.internal.http.socketTimeout=60000 -Dorg.gradle.internal.network.retry.max.attempts=6 -Dorg.gradle.internal.network.retry.initial.backOff=2000" - if: ${{ matrix.api-level == 33 }} - with: - api-level: ${{ matrix.api-level }} - target: google_apis - arch: x86_64 - profile: pixel_2 - heap-size: "512M" - ram-size: "4096M" - disk-size: "14G" - sdcard-path-or-size: "4096M" - disable-animations: false - script: bash ../../workflow/android/run.sh - working-directory: ./examples/android/ - continue-on-error: true - - name: Upload log on cargo-apk - uses: actions/upload-artifact@v1 - with: - name: cargo-apk-${{ matrix.api-level }}.log - path: cargo-apk-${{ matrix.api-level }}.log -# https://zenn.dev/takeyaqa/articles/github-actions-appium-android-emulator -# https://stackoverflow.com/questions/3152681/android-emulator-5554-offline - -# https://github.com/kiwix/kiwix-android/blob/develop/.github/workflows/ci.yml diff --git a/.github/workflows/test_iphone.yml b/.github/workflows/test_iphone.yml new file mode 100644 index 0000000..66c4b9c --- /dev/null +++ b/.github/workflows/test_iphone.yml @@ -0,0 +1,67 @@ +on: [push, pull_request] + +jobs: + dinghy: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: Install stable rust toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + target: x86_64-apple-ios + override: true + # - name: install target + # run: rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim + - uses: cargo-bins/cargo-binstall@main + - name: install cargo-dinghy + run: cargo binstall -q --no-confirm --force cargo-dinghy + - name: start simulator + uses: futureware-tech/simulator-action@v3 + with: + model: "iPhone 8" + - name: Dinghy test + run: cargo dinghy --platform auto-ios-x86_64 test + working-directory: ./examples/iphone/ + + bundle: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: Install stable rust toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + target: x86_64-apple-ios + override: true + - run: rustup target add x86_64-apple-ios + - uses: cargo-bins/cargo-binstall@main + - name: install rust iphone + run: cargo binstall -q --no-confirm --force cargo-bundle + - name: move target dir + run: | + cp -r ./examples/iphone /tmp/ + cp -r ../. /tmp/ + ls -al /tmp/ + cd /tmp/iphone/ + sed -i.bak 's|path = "../../"|path = "../nickname"|' Cargo.toml + - name: build + run: cargo bundle --format ios --target x86_64-apple-ios --release + working-directory: /tmp/iphone/ + - name: start simulator + uses: futureware-tech/simulator-action@v3 + # https://github.com/futureware-tech/simulator-action/wiki/Devices-macos-latest + with: + model: "iPhone 11" + shutdown_after_job: false + - name: pre drive + run: | + ls -al target/x86_64-apple-ios/release/bundle/ios/nickname-ios-test-app.app + cat target/x86_64-apple-ios/release/bundle/ios/nickname-ios-test-app.app/Info.plist + xcrun simctl install booted target/x86_64-apple-ios/release/bundle/ios/nickname-ios-test-app.app & + working-directory: /tmp/iphone/ + - name: drive + run: bash /tmp/nickname/workflow/iphone/run.sh +# https://github.com/simlay/uikit-sys/blob/master/.github/workflows/rust.yml diff --git a/.vscode/settings.json b/.vscode/settings.json index edb15a0..b7f5240 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,19 +1,23 @@ { "rust-analyzer.linkedProjects": [ - ".\\Cargo.toml", ".\\Cargo.toml", ".\\examples\\android\\Cargo.toml", - ".\\examples\\pc\\Cargo.toml", + ".\\examples\\iphone\\Cargo.toml", + ".\\examples\\common\\Cargo.toml" ], - "rust-analyzer.cargo.target": "aarch64-linux-android", + // "rust-analyzer.cargo.target": "aarch64-linux-android", + "rust-analyzer.cargo.target": "x86_64-apple-ios", // "rust-analyzer.cargo.target": "x86_64-unknown-linux-gnu", // "rust-analyzer.cargo.target": "wasm32-wasi", "cSpell.words": [ + "binstall", "COMPUTERNAME", "gethostname", "libc", + "objc", "sysinfoapi", "thiserror", + "wasi", "winapi", "winbase" ], diff --git a/Cargo.toml b/Cargo.toml index 8def31b..6edef92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ cfg-if = "1" [target.'cfg(target_os = "android")'.dependencies] jni = "0.21" -ndk-context = "0.1.1" +ndk-context = "0.1" [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = [ @@ -35,9 +35,8 @@ winapi = { version = "0.3", features = [ "iptypes", ] } -[target.'cfg(target_os = "ios")'.dependencies] -cocoa = "0.25" -objc = "0.2" +[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] +objc = { version = "0.4.1", package = "objc2" } -[target.'cfg(any(unix, target_os = "redox", target_os = "wasi"))'.dependencies] +[target.'cfg(any(unix, target_os = "redox", target_os = "macos", target_os = "wasi"))'.dependencies] libc = "0.2" diff --git a/README.md b/README.md index 5af7b02..ce5fdf7 100644 --- a/README.md +++ b/README.md @@ -17,20 +17,26 @@ https://github.com/WebAssembly/wasi-libc/issues/196 - get - set -## rust target +# rust target https://doc.rust-lang.org/nightly/rustc/platform-support.html -## build +# build look README.md on example -## next -### android +# next +## android get nickname from bluetooth -## extension function -### android +# extension function +## android - finish call finishAndRemoveTask() - get_device_api_level get api_level call VERSION.SDK_INT + +# github actions test on mobile +## android +- api-level 24: `error: "API level 25 or higher is required"` +- api-level 30: `Android SDK built for x86_64` +- api-level 33: `sdk_gphone_x86_64` diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..5573771 --- /dev/null +++ b/build.rs @@ -0,0 +1,12 @@ +fn main() { + #[cfg(target_os = "ios")] + { + println!("cargo:rustc-link-lib=framework=Foundation"); + + println!("cargo:rustc-link-lib=framework=UIKit"); + + println!("cargo:rustc-link-lib=framework=CoreGraphics"); + println!("cargo:rustc-link-lib=framework=QuartzCore"); + println!("cargo:rustc-link-lib=framework=Security"); + } +} diff --git a/examples/android/Cargo.lock b/examples/android/Cargo.lock index 35b276f..d78db72 100644 --- a/examples/android/Cargo.lock +++ b/examples/android/Cargo.lock @@ -18,7 +18,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "052ad56e336bcc615a214bffbeca6c181ee9550acec193f0327e0b103b033a4d" dependencies = [ "android-properties", - "bitflags 2.4.1", + "bitflags", "cc", "cesu8", "jni", @@ -56,24 +56,12 @@ dependencies = [ "once_cell", ] -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" -[[package]] -name = "block" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" - [[package]] name = "bytes" version = "1.5.0" @@ -101,36 +89,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cocoa" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6140449f97a6e97f9511815c5632d84c8aacf8ac271ad77c559218161a1373c" -dependencies = [ - "bitflags 1.3.2", - "block", - "cocoa-foundation", - "core-foundation", - "core-graphics", - "foreign-types", - "libc", - "objc", -] - -[[package]] -name = "cocoa-foundation" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c6234cbb2e4c785b456c0644748b1ac416dd045799740356f8363dfe00c93f7" -dependencies = [ - "bitflags 1.3.2", - "block", - "core-foundation", - "core-graphics-types", - "libc", - "objc", -] - [[package]] name = "combine" version = "4.6.6" @@ -141,46 +99,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "core-foundation" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" - -[[package]] -name = "core-graphics" -version = "0.23.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "970a29baf4110c26fedbc7f82107d42c23f7e88e404c4577ed73fe99ff85a212" -dependencies = [ - "bitflags 1.3.2", - "core-foundation", - "core-graphics-types", - "foreign-types", - "libc", -] - -[[package]] -name = "core-graphics-types" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bb142d41022986c1d8ff29103a1411c8a3dfad3552f87a4f8dc50d61d4f4e33" -dependencies = [ - "bitflags 1.3.2", - "core-foundation", - "libc", -] - [[package]] name = "env_logger" version = "0.10.1" @@ -211,33 +129,6 @@ dependencies = [ "nick-name", ] -[[package]] -name = "foreign-types" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" -dependencies = [ - "foreign-types-macros", - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-macros" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "foreign-types-shared" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" - [[package]] name = "hashbrown" version = "0.14.2" @@ -288,15 +179,6 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -[[package]] -name = "malloc_buf" -version = "0.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" -dependencies = [ - "libc", -] - [[package]] name = "memchr" version = "2.6.4" @@ -309,7 +191,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2076a31b7010b17a38c01907c45b945e8f11495ee4dd588309718901b1f7a5b7" dependencies = [ - "bitflags 2.4.1", + "bitflags", "jni-sys", "log", "ndk-sys", @@ -338,11 +220,10 @@ name = "nick-name" version = "0.1.0" dependencies = [ "cfg-if", - "cocoa", "jni", "libc", "ndk-context", - "objc", + "objc2", "thiserror", "winapi", ] @@ -369,14 +250,27 @@ dependencies = [ ] [[package]] -name = "objc" -version = "0.2.7" +name = "objc-sys" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +checksum = "99e1d07c6eab1ce8b6382b8e3c7246fe117ff3f8b34be065f5ebace6749fe845" + +[[package]] +name = "objc2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "559c5a40fdd30eb5e344fbceacf7595a81e242529fb4e21cf5f43fb4f11ff98d" dependencies = [ - "malloc_buf", + "objc-sys", + "objc2-encode", ] +[[package]] +name = "objc2-encode" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d079845b37af429bfe5dfa76e6d087d788031045b25cfc6fd898486fd9847666" + [[package]] name = "once_cell" version = "1.18.0" diff --git a/examples/android/README.md b/examples/android/README.md index 166fc83..20e5e15 100644 --- a/examples/android/README.md +++ b/examples/android/README.md @@ -17,4 +17,5 @@ cargo apk run ``` cargo install cargo-apk +cargo install dinghy ``` diff --git a/examples/android/src/lib.rs b/examples/android/src/lib.rs index a87e4e2..8ceaaad 100644 --- a/examples/android/src/lib.rs +++ b/examples/android/src/lib.rs @@ -22,5 +22,5 @@ fn android_main(_app: AndroidApp) { nick_name::finish().unwrap(); - eprintln!("__finish__"); + println!("__finish__"); } diff --git a/examples/pc/Cargo.lock b/examples/common/Cargo.lock similarity index 71% rename from examples/pc/Cargo.lock rename to examples/common/Cargo.lock index ba33520..cd9bbd5 100644 --- a/examples/pc/Cargo.lock +++ b/examples/common/Cargo.lock @@ -2,18 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "block" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" - [[package]] name = "bytes" version = "1.5.0" @@ -32,36 +20,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cocoa" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6140449f97a6e97f9511815c5632d84c8aacf8ac271ad77c559218161a1373c" -dependencies = [ - "bitflags", - "block", - "cocoa-foundation", - "core-foundation", - "core-graphics", - "foreign-types", - "libc", - "objc", -] - -[[package]] -name = "cocoa-foundation" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c6234cbb2e4c785b456c0644748b1ac416dd045799740356f8363dfe00c93f7" -dependencies = [ - "bitflags", - "block", - "core-foundation", - "core-graphics-types", - "libc", - "objc", -] - [[package]] name = "combine" version = "4.6.6" @@ -72,73 +30,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "core-foundation" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" - -[[package]] -name = "core-graphics" -version = "0.23.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "970a29baf4110c26fedbc7f82107d42c23f7e88e404c4577ed73fe99ff85a212" -dependencies = [ - "bitflags", - "core-foundation", - "core-graphics-types", - "foreign-types", - "libc", -] - -[[package]] -name = "core-graphics-types" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bb142d41022986c1d8ff29103a1411c8a3dfad3552f87a4f8dc50d61d4f4e33" -dependencies = [ - "bitflags", - "core-foundation", - "libc", -] - -[[package]] -name = "foreign-types" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" -dependencies = [ - "foreign-types-macros", - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-macros" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "foreign-types-shared" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" - [[package]] name = "jni" version = "0.21.1" @@ -173,15 +64,6 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -[[package]] -name = "malloc_buf" -version = "0.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" -dependencies = [ - "libc", -] - [[package]] name = "memchr" version = "2.6.4" @@ -199,22 +81,41 @@ name = "nick-name" version = "0.1.0" dependencies = [ "cfg-if", - "cocoa", "jni", "libc", "ndk-context", - "objc", + "objc2", "thiserror", "winapi", ] [[package]] -name = "objc" -version = "0.2.7" +name = "objc-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99e1d07c6eab1ce8b6382b8e3c7246fe117ff3f8b34be065f5ebace6749fe845" + +[[package]] +name = "objc2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "559c5a40fdd30eb5e344fbceacf7595a81e242529fb4e21cf5f43fb4f11ff98d" +dependencies = [ + "objc-sys", + "objc2-encode", +] + +[[package]] +name = "objc2-encode" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +checksum = "d079845b37af429bfe5dfa76e6d087d788031045b25cfc6fd898486fd9847666" + +[[package]] +name = "pc" +version = "0.1.0" dependencies = [ - "malloc_buf", + "nick-name", ] [[package]] @@ -322,13 +223,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows" -version = "0.1.0" -dependencies = [ - "nick-name", -] - [[package]] name = "windows-sys" version = "0.45.0" diff --git a/examples/pc/Cargo.toml b/examples/common/Cargo.toml similarity index 92% rename from examples/pc/Cargo.toml rename to examples/common/Cargo.toml index c0e45d6..b617855 100644 --- a/examples/pc/Cargo.toml +++ b/examples/common/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "windows" +name = "pc" version = "0.1.0" edition = "2021" diff --git a/examples/common/README.md b/examples/common/README.md new file mode 100644 index 0000000..b135b45 --- /dev/null +++ b/examples/common/README.md @@ -0,0 +1,25 @@ +# run +## windows +```bash +cargo run +``` +## macos +```bash +cargo run +``` +## linux +```bash +cargo run +``` +## android +```bash +cargo dinghy -p auto-android-aarch64 run +``` +This is failed +## iphone +```bash +cargo dinghy --platform auto-ios-aarch64 run +``` + +if failed: + change target auto-android-i686, auto-android-x86_64 diff --git a/examples/common/src/main.rs b/examples/common/src/main.rs new file mode 100644 index 0000000..ece2c69 --- /dev/null +++ b/examples/common/src/main.rs @@ -0,0 +1,15 @@ +fn main() { + let nickname = nick_name::NickName::new().unwrap(); + let device_name = nickname.get().unwrap(); + println!("{device_name}"); + + #[cfg(not(any(target_os = "ios", target_os = "android")))] + { + let set = nickname.set("oligami-pc"); + println!("{:?}", set); + (); + + let device_name = nickname.get().unwrap(); + println!("{device_name}"); + } +} diff --git a/examples/iphone/Cargo.lock b/examples/iphone/Cargo.lock new file mode 100644 index 0000000..b7d2f0d --- /dev/null +++ b/examples/iphone/Cargo.lock @@ -0,0 +1,535 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitmask-enum" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9990737a6d5740ff51cdbbc0f0503015cb30c390f6623968281eb214a520cfc0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "block" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" + +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + +[[package]] +name = "cacao" +version = "0.4.0-beta2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6de2bcb2324367ffb6ea53f977fab8234fa59e9a57e88f0f7be1ad7cb425c7b9" +dependencies = [ + "bitmask-enum", + "block", + "core-foundation", + "core-graphics", + "dispatch", + "lazy_static", + "libc", + "objc", + "objc_id", + "os_info", + "url", +] + +[[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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "combine" +version = "4.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "core-foundation" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + +[[package]] +name = "core-graphics" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "970a29baf4110c26fedbc7f82107d42c23f7e88e404c4577ed73fe99ff85a212" +dependencies = [ + "bitflags", + "core-foundation", + "core-graphics-types", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bb142d41022986c1d8ff29103a1411c8a3dfad3552f87a4f8dc50d61d4f4e33" +dependencies = [ + "bitflags", + "core-foundation", + "libc", +] + +[[package]] +name = "dispatch" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b" + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + +[[package]] +name = "form_urlencoded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "idna" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[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", +] + +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "malloc_buf" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" +dependencies = [ + "libc", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "ndk-context" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b" + +[[package]] +name = "nick-name" +version = "0.1.0" +dependencies = [ + "cfg-if", + "jni", + "libc", + "ndk-context", + "objc2", + "thiserror", + "winapi", +] + +[[package]] +name = "nickname-ios-test-app" +version = "0.1.0" +dependencies = [ + "cacao", + "nick-name", +] + +[[package]] +name = "objc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +dependencies = [ + "malloc_buf", +] + +[[package]] +name = "objc-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99e1d07c6eab1ce8b6382b8e3c7246fe117ff3f8b34be065f5ebace6749fe845" + +[[package]] +name = "objc2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "559c5a40fdd30eb5e344fbceacf7595a81e242529fb4e21cf5f43fb4f11ff98d" +dependencies = [ + "objc-sys", + "objc2-encode", +] + +[[package]] +name = "objc2-encode" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d079845b37af429bfe5dfa76e6d087d788031045b25cfc6fd898486fd9847666" + +[[package]] +name = "objc_id" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" +dependencies = [ + "objc", +] + +[[package]] +name = "os_info" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "006e42d5b888366f1880eda20371fedde764ed2213dc8496f49622fa0c99cd5e" +dependencies = [ + "log", + "serde", + "winapi", +] + +[[package]] +name = "percent-encoding" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.192" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.192" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +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", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +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", +] + +[[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_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[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_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[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_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/examples/iphone/Cargo.toml b/examples/iphone/Cargo.toml new file mode 100644 index 0000000..514378e --- /dev/null +++ b/examples/iphone/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "nickname-ios-test-app" +version = "0.1.0" +edition = "2021" +description = "A simple iPhone app" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cacao = { version = "0.4.0-beta2", features = [ + "uikit", + "autolayout", +], default-features = false } + +# this library +nick-name = { path = "../../" } + +[package.metadata.bundle] +identifier = "com.oligami.nickname-ios-test" diff --git a/examples/iphone/README.md b/examples/iphone/README.md new file mode 100644 index 0000000..9d13a38 --- /dev/null +++ b/examples/iphone/README.md @@ -0,0 +1,6 @@ +cargo install cargo-bundle +cargo bundle --target x86_64-apple-ios +xcrun simctl install booted target/x86_64-apple-ios/debug/examples/bundle/ios/ios-beta.app +xcrun simctl launch --console booted com.cacao.ios-test + +https://github.com/ryanmcgrath/cacao/tree/trunk/examples/ios-beta diff --git a/examples/iphone/src/main.rs b/examples/iphone/src/main.rs new file mode 100644 index 0000000..b988398 --- /dev/null +++ b/examples/iphone/src/main.rs @@ -0,0 +1,87 @@ +use std::sync::RwLock; + +use cacao::uikit::{ + App, AppDelegate, Scene, SceneConfig, SceneConnectionOptions, SceneSession, Window, + WindowSceneDelegate, +}; + +use cacao::layout::LayoutConstraint; +use cacao::view::{View, ViewController, ViewDelegate}; + +#[derive(Default)] +struct TestApp; + +impl AppDelegate for TestApp { + fn config_for_scene_session( + &self, + session: SceneSession, + _options: SceneConnectionOptions, + ) -> SceneConfig { + SceneConfig::new("Default Configuration", session.role()) + } +} + +pub struct RootView { + pub green: View, + pub blue: View, +} + +impl Default for RootView { + fn default() -> Self { + RootView { + green: View::new(), + blue: View::new(), + } + } +} + +impl ViewDelegate for RootView { + const NAME: &'static str = "RootView"; + + fn did_load(&mut self, _view: View) { + LayoutConstraint::activate(&[]); + std::thread::spawn(|| { + launch_handle(); + }); + } +} + +#[derive(Default)] +pub struct WindowScene { + pub window: RwLock>, + pub root_view_controller: RwLock>>, +} + +impl WindowSceneDelegate for WindowScene { + fn will_connect(&self, scene: Scene, _session: SceneSession, _options: SceneConnectionOptions) { + let bounds = scene.get_bounds(); + let mut window = Window::new(bounds); + window.set_window_scene(scene); + + let root_view_controller = ViewController::new(RootView::default()); + window.set_root_view_controller(&root_view_controller); + window.show(); + + { + let mut w = self.window.write().unwrap(); + *w = Some(window); + + let mut vc = self.root_view_controller.write().unwrap(); + *vc = Some(root_view_controller); + } + } +} + +fn launch_handle() { + println!("start"); + + if let Ok(device_name) = nick_name::NickName::new() { + println!("{:?}", device_name.get()); + }; + + println!("__finish__"); +} + +fn main() { + App::new(TestApp::default(), || Box::new(WindowScene::default())).run(); +} diff --git a/examples/pc/src/main.rs b/examples/pc/src/main.rs deleted file mode 100644 index cea8c18..0000000 --- a/examples/pc/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - let nickname = nick_name::NickName::new().unwrap(); - let device_name = nickname.get().unwrap(); - println!("{device_name}"); - - nickname.set("oligami-pc").unwrap(); -} diff --git a/rm_workflow.sh b/rm_workflow.sh new file mode 100644 index 0000000..d65f100 --- /dev/null +++ b/rm_workflow.sh @@ -0,0 +1,11 @@ +owner="nziq53" +repo="nickname" +file_name="test_iphone.yml" + +gh api "repos/${owner}/${repo}/actions/workflows/${file_name}/runs?per_page=100" \ +| jq -r '.workflow_runs[].id' \ +| xargs -P4 -I{} gh api repos/{owner}/{repo}/actions/runs/{} -X DELETE + +# https://qiita.com/tippy/items/79ca3f7b7bcac1d92136 + +# bash rm_workflow.sh diff --git a/rust/android/jni/mod.rs b/rust/android/jni/mod.rs index 465df10..f3db1f0 100644 --- a/rust/android/jni/mod.rs +++ b/rust/android/jni/mod.rs @@ -36,12 +36,12 @@ pub fn global_jvm() -> &'static JavaVM { } } }; - return unsafe { + unsafe { match JAVA_VM { Some(ref v) => v, None => panic!("JVM has not been initialized. Please initialize it with init()."), } - }; + } } // .expect("JVM has not been initialized. Please initialize it with init().") @@ -62,24 +62,21 @@ pub fn global_ctx() -> &'static Context { } } }; - return unsafe { + unsafe { match CONTEXT { Some(ref v) => v, None => panic!("JVM has not been initialized. Please initialize it with init()."), } - }; + } } impl Drop for NickName { fn drop(&mut self) { unsafe { - match JNI_ENV { - Some(ref _v) => { - JNI_ENV = None; - JAVA_VM = None; - CONTEXT = None; - } - None => {} + if let Some(ref _v) = JNI_ENV { + JNI_ENV = None; + JAVA_VM = None; + CONTEXT = None; } } } diff --git a/rust/android/jni/nick_name.rs b/rust/android/jni/nick_name.rs index 6b3dbdb..0871d18 100644 --- a/rust/android/jni/nick_name.rs +++ b/rust/android/jni/nick_name.rs @@ -202,8 +202,7 @@ impl NickName { let device_name = jni_env .get_static_field(&setting_global, "DEVICE_NAME", "Ljava/lang/String;")? - .l()? - .into(); + .l()?; let device_name = jni_env .call_static_method( diff --git a/rust/android/mod.rs b/rust/android/mod.rs index 7cd671e..983544b 100644 --- a/rust/android/mod.rs +++ b/rust/android/mod.rs @@ -1,8 +1,8 @@ mod jni; -pub use jni::attach_current_thread; -pub use jni::init; -pub use jni::nick_name::NickName; +pub use self::jni::attach_current_thread; +pub use self::jni::init; +pub use self::jni::nick_name::NickName; use self::jni::global_ctx; use self::jni::global_jvm; diff --git a/rust/iphone/mod.rs b/rust/iphone/mod.rs index e69de29..3d4bd2a 100644 --- a/rust/iphone/mod.rs +++ b/rust/iphone/mod.rs @@ -0,0 +1,2 @@ +mod objc; +pub use objc::nick_name::NickName; diff --git a/rust/iphone/objc/mod.rs b/rust/iphone/objc/mod.rs new file mode 100644 index 0000000..9dd4718 --- /dev/null +++ b/rust/iphone/objc/mod.rs @@ -0,0 +1,9 @@ +use objc::runtime; + +pub mod nick_name; + +use objc::runtime::NSObject; +use objc::Encode; + +#[allow(non_camel_case_types)] +pub type id = *const NSObject; diff --git a/rust/iphone/objc/nick_name.rs b/rust/iphone/objc/nick_name.rs new file mode 100644 index 0000000..6253709 --- /dev/null +++ b/rust/iphone/objc/nick_name.rs @@ -0,0 +1,47 @@ +// https://docs.rs/cacao/latest/src/cacao/filesystem/manager.rs.html#23 + +use std::error::Error; +use std::ffi::CStr; +use std::fmt::{Debug, Formatter}; +use std::sync::{Arc, RwLock}; + +use super::id; +use objc::rc::Id; +use objc::runtime::{AnyObject, NSObject, Object}; +use objc::{class, msg_send, msg_send_id, ClassType}; + +#[repr(transparent)] +#[derive(Clone)] +pub struct NickName(pub Arc>); + +impl Debug for NickName { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + f.debug_struct("NickName").finish() + } +} + +// https://developer.apple.com/documentation/uikit/uidevice +// https://simlay.net/uikit-sys/master/src/uikit_sys/Users/simlay/projects/uikit-sys/target/x86_64-apple-ios/debug/build/uikit-sys-344536fd54f83e27/out/uikit.rs.html#98727 +impl NickName { + pub fn new() -> crate::Result { + Ok(Self(Arc::new(RwLock::new(unsafe { + // Create an instance of the UIDevice class + msg_send![class!(UIDevice), alloc] + })))) + } + + pub fn get(&self) -> crate::Result { + let device = self.0.read().unwrap(); + + // Get the name property + let current: id = unsafe { msg_send![&**device, name] }; + + // Convert the Objective-C string to a Rust string + let name_cstr: &CStr = unsafe { CStr::from_ptr(msg_send![current, UTF8String]) }; + + // Convert &CStr to Rust string + let name_str = name_cstr.to_str().unwrap(); + + Ok(name_str.to_string()) + } +} diff --git a/rust/lib.rs b/rust/lib.rs index 5b93b55..3657c24 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -4,6 +4,12 @@ cfg_if! { if #[cfg(target_os = "android")] { mod android; pub use android::*; + } else if #[cfg(target_os = "ios")] { + mod iphone; + pub use iphone::*; + } else if #[cfg(target_os = "macos")] { + mod macos; + pub use macos::*; } else if #[cfg(any(unix, target_os = "redox"))] { mod linux; pub use linux::*; diff --git a/rust/linux/mod.rs b/rust/linux/mod.rs index 366d1c4..408b4d7 100644 --- a/rust/linux/mod.rs +++ b/rust/linux/mod.rs @@ -1,2 +1,2 @@ mod nick_name; -pub use nick_name::NickName; +pub use self::nick_name::NickName; diff --git a/rust/linux/nick_name.rs b/rust/linux/nick_name.rs index 21543eb..b5a5fd5 100644 --- a/rust/linux/nick_name.rs +++ b/rust/linux/nick_name.rs @@ -52,7 +52,9 @@ impl NickName { target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", + target_os = "solarish", + target_os = "illumos", )))] #[allow(non_camel_case_types)] type nickname_len_t = libc::size_t; @@ -61,7 +63,9 @@ impl NickName { target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", + target_os = "solarish", + target_os = "illumos", ))] #[allow(non_camel_case_types)] type nickname_len_t = libc::c_int; @@ -70,7 +74,9 @@ impl NickName { target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", + target_os = "solarish", + target_os = "illumos", )))] #[allow(clippy::absurd_extreme_comparisons)] if nickname.len() > nickname_len_t::MAX { @@ -81,7 +87,9 @@ impl NickName { target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", + target_os = "solarish", + target_os = "illumos", ))] if nickname.len() > nickname_len_t::MAX as usize { return Err(std::io::Error::new(std::io::ErrorKind::Other, "nickname too long").into()); diff --git a/rust/macos/mod.rs b/rust/macos/mod.rs new file mode 100644 index 0000000..408b4d7 --- /dev/null +++ b/rust/macos/mod.rs @@ -0,0 +1,2 @@ +mod nick_name; +pub use self::nick_name::NickName; diff --git a/rust/macos/nick_name.rs b/rust/macos/nick_name.rs new file mode 100644 index 0000000..4627ff3 --- /dev/null +++ b/rust/macos/nick_name.rs @@ -0,0 +1,77 @@ +// https://github.com/svartalf/hostname/blob/master/src/nix.rs + +use std::ffi::CStr; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::os::unix::ffi::OsStrExt; +const _POSIX_HOST_NAME_MAX: libc::c_long = 255; + +pub struct NickName {} + +impl Debug for NickName { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + f.debug_struct("NickName").finish() + } +} + +impl NickName { + pub fn new() -> crate::Result { + Ok(Self {}) + } + + pub fn get(&self) -> crate::Result { + let hostname = self.get_hostname()?; + Ok(hostname) + } + + pub fn get_hostname(&self) -> crate::Result { + // ホスト名を格納するバッファのサイズを指定 + // https://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html + let limit = unsafe { libc::sysconf(libc::_SC_HOST_NAME_MAX) }; + let size = libc::c_long::max(limit, _POSIX_HOST_NAME_MAX) as usize; + let mut hostname_buffer: Vec = vec![0; size + 1]; + + // libcのgethostname関数を呼び出し、ホスト名を取得 + let result = + unsafe { libc::gethostname(hostname_buffer.as_mut_ptr() as *mut libc::c_char, size) }; + + if result != 0 { + return Err(std::io::Error::last_os_error().into()); + } + + // ヌル終端されたC文字列をRustの文字列に変換 + let hostname_cstr = + unsafe { CStr::from_ptr(hostname_buffer.as_ptr() as *const libc::c_char) }; + match hostname_cstr.to_str() { + Ok(v) => Ok(v.into()), + Err(e) => Err(crate::Error::Other(Box::new(e))), + } + } + + pub fn set>(&self, nickname: S) -> crate::Result<()> { + let nickname: String = nickname.into(); + self.set_hostname(nickname)?; + Ok(()) + } + + // https://github.com/svartalf/hostname/blob/master/src/nix.rs + pub fn set_hostname>(&self, nickname: S) -> crate::Result<()> { + let nickname: String = nickname.into(); + let nickname = std::ffi::OsStr::new(&nickname); + + if nickname.len() > libc::c_int::MAX as usize { + return Err(std::io::Error::new(std::io::ErrorKind::Other, "nickname too long").into()); + } + + let size = nickname.len() as libc::c_int; + + let result = + unsafe { libc::sethostname(nickname.as_bytes().as_ptr() as *const libc::c_char, size) }; + + if result != 0 { + Err(std::io::Error::last_os_error().into()) + } else { + Ok(()) + } + } +} diff --git a/rust/windows/mod.rs b/rust/windows/mod.rs index 366d1c4..408b4d7 100644 --- a/rust/windows/mod.rs +++ b/rust/windows/mod.rs @@ -1,2 +1,2 @@ mod nick_name; -pub use nick_name::NickName; +pub use self::nick_name::NickName; diff --git a/rust/windows/nick_name.rs b/rust/windows/nick_name.rs index bdbf156..85066ab 100644 --- a/rust/windows/nick_name.rs +++ b/rust/windows/nick_name.rs @@ -209,8 +209,6 @@ impl NickName { let mut buffer = OsStr::new(&nickname).encode_wide().collect::>(); buffer.push(0); - println!("buffer: {:?}", buffer); - // SetComputerNameExWを呼び出してコンピュータ名を設定 if SetComputerNameExW(ComputerNamePhysicalDnsHostname, buffer.as_ptr()) != 0 { Ok(()) diff --git a/workflow/android/run.sh b/workflow/android/run.sh index 0b751ec..3dc15fd 100644 --- a/workflow/android/run.sh +++ b/workflow/android/run.sh @@ -10,8 +10,8 @@ adb logcat RustStdoutStderr:D '*:S' | while IFS= read -r line; do echo "$line" if [[ "$line" == *"__finish__"* ]]; then echo "String '__finish__' found. Killing the process." - kill $PID adb kill-server + kill $PID exit 0 fi done diff --git a/workflow/iphone/run.sh b/workflow/iphone/run.sh new file mode 100644 index 0000000..ca27746 --- /dev/null +++ b/workflow/iphone/run.sh @@ -0,0 +1,8 @@ +xcrun simctl launch --console booted com.oligami.nickname-ios-test | while IFS= read -r line; do + echo "$line" + if [[ "$line" == *"__finish__"* ]]; then + echo "String '__finish__' found. Killing the process." + xcrun simctl shutdown booted + exit 0 + fi +done