From dde471f2bd17ebf9326d6b2203f768d54bd9e2bb Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Thu, 11 Apr 2024 14:31:58 -0400 Subject: [PATCH 1/4] Fix tls feature flag and add uniffi-build feature flag --- .github/workflows/aarch64-linux-android.yml | 175 ------------ .github/workflows/apple-devices.yml | 93 ------- .github/workflows/ci.yml | 290 ++++++++++++++++++++ .github/workflows/linux.yml | 46 ---- .github/workflows/macos.yml | 105 ------- Cargo.toml | 22 +- src/ffi/socket.rs | 2 + src/rust/socket.rs | 1 + src/rust/socket/listener.rs | 8 +- uniffi-bindgen.rs | 2 +- 10 files changed, 315 insertions(+), 429 deletions(-) delete mode 100644 .github/workflows/aarch64-linux-android.yml delete mode 100644 .github/workflows/apple-devices.yml create mode 100644 .github/workflows/ci.yml delete mode 100644 .github/workflows/linux.yml delete mode 100644 .github/workflows/macos.yml diff --git a/.github/workflows/aarch64-linux-android.yml b/.github/workflows/aarch64-linux-android.yml deleted file mode 100644 index 9f6f12c..0000000 --- a/.github/workflows/aarch64-linux-android.yml +++ /dev/null @@ -1,175 +0,0 @@ -# based on https://github.com/xaynetwork/cargo_tai -name: aarch64-linux-android - -on: pull_request - -env: - ANDROID_COMMANDLINE_TOOLS_VERSION: 9477386 - ANDROID_API: 33 - ANDROID_NDK_VERSION: 25.2.9519653 - ANDROID_SYSTEM_IMAGE_PACKAGE: system-images;android-33;google_apis_playstore;arm64-v8a - OPENSSL_VERSION: 1.1.1s - TOOLCHAIN: nightly - CARGO_TERM_COLOR: always - CARGO_NDK_REV: "8478e58" - CARGO_DINGHY_REV: "78df50e" - -jobs: - integration_tests: - # rust cross-compilation - # macos-*-xlarge is needed to get M1 chips - https://github.blog/2023-10-02-introducing-the-new-apple-silicon-powered-m1-macos-larger-runner-for-github-actions/ - runs-on: self-hosted - - steps: - - uses: actions/checkout@v4 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: nightly - target: aarch64-linux-android - default: true - - - name: Cache Cargo - uses: actions/cache@v4 - with: - path: | - ~/.cargo/bin - ~/.cargo/git - ~/.cargo/registry - key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ env.CARGO_NDK_REV }}-${{ env.CARGO_DINGHY_REV }}-${{ hashFiles('**/Cargo.lock') }} - - - name: Install cargo-ndk - run: | - if ! command -v cargo-ndk &> /dev/null; then - cargo install --git https://github.com/bbqsrc/cargo-ndk --rev ${CARGO_NDK_REV} - fi - - - name: Install cargo-dinghy - run: | - if ! command -v cargo-dinghy &> /dev/null; then - cargo install --git https://github.com/sonos/dinghy.git --force --rev ${CARGO_DINGHY_REV} cargo-dinghy - fi - - name: Set up Elixir - run: brew install elixir - - name: Cache deps - id: cache-deps - uses: actions/cache@v4 - env: - cache-name: cache-elixir-deps - with: - path: tests/support/test_server/deps - key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} - restore-keys: | - ${{ runner.os }}-mix-${{ env.cache-name }}- - - name: Cache compiled build - id: cache-build - uses: actions/cache@v4 - env: - cache-name: cache-compiled-build - with: - path: tests/support/test_server/_build - key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} - restore-keys: | - ${{ runner.os }}-mix-${{ env.cache-name }}- - ${{ runner.os }}-mix- - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - java-version: 17 - distribution: 'temurin' - - name: Cache target - id: cache-target - uses: actions/cache@v4 - env: - cache-name: cache-target - with: - path: target - key: ${{ github.workflow }}-${{ github.job }}-${{ env.cache-name }}-${{ env.TOOLCHAIN }}-cargo-${{ env.CARGO_NDK_REV }}-${{ env.CARGO_DINGHY_REV }}-${{ hashFiles('**/Cargo.lock') }} - - name: Build phoenix_channels_client - uses: actions-rs/cargo@v1 - with: - command: build - args: --all-targets - - name: Install hex - - run: mix local.hex --force - - name: Install rebar3 - run: mix local.rebar --force - - name: Install test server dependencies - working-directory: ./tests/support/test_server - run: mix deps.get - - name: Build test server - working-directory: ./tests/support/test_server - run: mix compile - - name: Run test server in background - working-directory: ./tests/support/test_server - run: mix phx.server & - - - name: Cache android - id: cache-android - uses: actions/cache@v4 - env: - cache-name: cache-android - with: - path: ~/android - key: ${{ runner.os }}-android-${{ env.cache-name }}-${{ env.ANDROID_COMMANDLINE_TOOLS_VERSION }} - restore-keys: | - ${{ runner.os }}-android-${{ env.cache-name }}-${{ env.ANDROID_COMMANDLINE_TOOLS_VERSION }} - ${{ runner.os }}-mix- - - name: Install Android Command Line Tools dependencies - run: brew install wget - - name: Download Android Command Line Tools - run: | - if [ ! -d "$HOME/android/cmdline-tools/${ANDROID_COMMANDLINE_TOOLS_VERSION}" ]; then - cd $HOME - # clear old version if it exists - rm -rf android - mkdir -p android/cmdline-tools - cd android/cmdline-tools - PACKAGE=commandlinetools-mac-${ANDROID_COMMANDLINE_TOOLS_VERSION}_latest - wget https://dl.google.com/android/repository/${PACKAGE}.zip - unzip ${PACKAGE}.zip - rm ${PACKAGE}.zip - mv cmdline-tools ${ANDROID_COMMANDLINE_TOOLS_VERSION} - ln -sf ${ANDROID_COMMANDLINE_TOOLS_VERSION} latest - fi - - name: Add Android home and subdirectories to environment - run: | - export ANDROID_HOME="$HOME/android" - echo "ANDROID_HOME=$ANDROID_HOME" >> $GITHUB_ENV - echo "${ANDROID_HOME}/cmdline-tools/latest/bin" >> $GITHUB_PATH - echo "${ANDROID_HOME}/emulator" >> $GITHUB_PATH - echo "${ANDROID_HOME}/platform-tools" >> $GITHUB_PATH - - name: sdkmanager setup - run: | - yes | sdkmanager --licenses - sdkmanager --install "platform-tools" \ - "platforms;android-${ANDROID_API}" \ - "build-tools;${ANDROID_API}.0.2" \ - "emulator" \ - "ndk;${ANDROID_NDK_VERSION}" \ - "${ANDROID_SYSTEM_IMAGE_PACKAGE}" - - - - name: Create Android Virtual Device - run: | - echo no | \ - avdmanager create avd --force \ - --name AVD \ - --abi google_apis_playstore/arm64-v8a \ - --package "${ANDROID_SYSTEM_IMAGE_PACKAGE}" - - - name: Start Android emulator - run: | - echo "Starting ADB server" - ${ANDROID_HOME}/platform-tools/adb start-server - echo "Starting Emulator" - ${ANDROID_HOME}/emulator/emulator -avd AVD -netdelay none -netspeed full -no-boot-anim -no-window -noaudio -accel auto -verbose & - ${ANDROID_HOME}/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82' - - - name: Test phoenix_channel_clients - run: | - cargo-dinghy all-platforms - cargo-dinghy all-devices - export RUSTFLAGS="--cfg tokio_unstable" - RANLIB=${ANDROID_HOME}/ndk/${ANDROID_NDK_VERSION}/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-ranlib cargo-dinghy -p auto-android-aarch64-api${ANDROID_API} test diff --git a/.github/workflows/apple-devices.yml b/.github/workflows/apple-devices.yml deleted file mode 100644 index 8013af3..0000000 --- a/.github/workflows/apple-devices.yml +++ /dev/null @@ -1,93 +0,0 @@ -# based on https://gist.github.com/learnopengles/60c945b92fbe60a0fecaa0143c35c4a for testing in simulator -name: Apple Simulator devices - -on: pull_request - -env: - TOOLCHAIN: nightly - CARGO_TERM_COLOR: always - CARGO_DINGHY_REV: "a8ec5b0" - -jobs: - integration_tests: - runs-on: macos-latest - strategy: - matrix: - rust-target: ["x86_64-apple-ios" ] - apple-sim: [ "iOS" ] - include: - - rust-target: "x86_64-apple-tvos" - apple-sim: "tvOS" - - rust-target: "x86_64-apple-watchos-sim" - apple-sim: "watchOS" - - steps: - - uses: actions/checkout@v4 - - - uses: dtolnay/rust-toolchain@master - with: - toolchain: ${{ env.TOOLCHAIN }} - components: rust-src - - - name: Set up Elixir - run: brew install elixir - - name: Cache Cargo - uses: actions/cache@v4 - with: - path: | - ~/.cargo/bin - ~/.cargo/git - ~/.cargo/registry - key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ env.CARGO_DINGHY_REV }}-${{ hashFiles('**/Cargo.lock') }} - - name: Cache deps - id: cache-deps - uses: actions/cache@v4 - env: - cache-name: cache-elixir-deps - with: - path: tests/support/test_server/deps - key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} - restore-keys: | - ${{ runner.os }}-mix-${{ env.cache-name }}- - - name: Cache compiled build - id: cache-build - uses: actions/cache@v4 - env: - cache-name: cache-compiled-build - with: - path: tests/support/test_server/_build - key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} - restore-keys: | - ${{ runner.os }}-mix-${{ env.cache-name }}- - ${{ runner.os }}-mix- - - name: Install cargo-dinghy - run: | - if ! command -v cargo-dinghy &> /dev/null; then - cargo install --git https://github.com/liveview-native/dinghy.git --force --rev ${CARGO_DINGHY_REV} cargo-dinghy - fi - - name: Install hex - run: mix local.hex --force - - name: Install rebar3 - run: mix local.rebar --force - - name: Install test server dependencies - working-directory: ./tests/support/test_server - run: mix deps.get - - name: Build test server - working-directory: ./tests/support/test_server - run: mix compile - - name: Run test server in background - working-directory: ./tests/support/test_server - run: mix phx.server & - - - name: Start Simulator - uses: futureware-tech/simulator-action@v3 - with: - os: ${{ matrix.apple-sim }} - - - name: Test phoenix_channel_clients - env: - DINGHY_LOG: debug - run: | - cargo dinghy all-platforms - cargo dinghy all-devices - cargo -Zbuild-std test --target ${{ matrix.rust-target }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..0746417 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,290 @@ +name: CI + +on: pull_request + +env: + TOOLCHAIN: nightly + CARGO_TERM_COLOR: always + ANDROID_API: 33 + ANDROID_NDK_VERSION: 25.2.9519653 + ANDROID_SYSTEM_IMAGE_PACKAGE: system-images;android-33;google_apis_playstore;arm64-v8a + OPENSSL_VERSION: 1.1.1s + +jobs: + integration_tests: + runs-on: macos-14 + strategy: + matrix: + rust-target: ["aarch64-apple-ios-sim"] + apple-target-os: [ "iOS" ] + include: + - rust-target: "aarch64-apple-tvos-sim" + apple-target-os: "tvOS" + - rust-target: "aarch64-apple-watchos-sim" + apple-target-os: "watchOS" + - rust-target: "aarch64-apple-darwin" + apple-target-os: "macOS" + + steps: + - uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ env.TOOLCHAIN }} + components: rust-src + + - name: Set up Elixir + run: brew install elixir + - name: Cache Cargo + uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin + ~/.cargo/git + ~/.cargo/registry + key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - name: Cache deps + id: cache-deps + uses: actions/cache@v4 + env: + cache-name: cache-elixir-deps + with: + path: tests/support/test_server/deps + key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-mix-${{ env.cache-name }}- + - name: Cache compiled build + id: cache-build + uses: actions/cache@v4 + env: + cache-name: cache-compiled-build + with: + path: tests/support/test_server/_build + key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-mix-${{ env.cache-name }}- + ${{ runner.os }}-mix- + + - name: Install hex, rebar3 + run: | + mix local.hex --force + mix local.rebar --force + - name: Install test server dependencies, compile server, run server in background + working-directory: ./tests/support/test_server + run: | + mix deps.get + mix compile + mix phx.server & + + - uses: taiki-e/install-action@v2 + if: ${{ matrix.apple-target-os != 'macOS' }} + with: + tool: cargo-dinghy@0.7.1 + - name: Start Simulator + if: ${{ matrix.apple-target-os != 'macOS' }} + uses: futureware-tech/simulator-action@v3 + with: + os: ${{ matrix.apple-target-os }} + + - name: Test phoenix_channel_clients + env: + DINGHY_LOG: debug + if: ${{ matrix.apple-target-os != 'macOS' }} + run: | + cargo dinghy all-platforms + cargo dinghy all-devices + cargo -Zbuild-std test --target ${{ matrix.rust-target }} + + - name: Build phoenix_channel_clients with tls + run: + cargo build -Zbuild-sd --target ${{ matrix.rust-target }} --features native-tls + + - name: Get JNA jar + if: ${{ matrix.apple-target-os == 'macOS' }} + run: wget 'https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.14.0/jna-5.14.0.jar' + + - name: Set up Elixir, kotlin and ktlint + if: ${{ matrix.apple-target-os == 'macOS' }} + run: brew install kotlin ktlint + + - name: Test phoenix_channel_clients + if: ${{ matrix.apple-target-os == 'macOS' }} + run: cargo test + env: + # This is required for the kotlin uniffi tests + CLASSPATH: "/opt/homebrew/opt/kotlin/libexec/lib/kotlinx-coroutines-core-jvm.jar:./jna-5.14.0.jar" + + uniffi-check: + runs-on: macos-14 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ env.TOOLCHAIN }} + - name: Cache Cargo + uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin + ~/.cargo/git + ~/.cargo/registry + key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ hashFiles('**/Cargo.lock') }} + + - name: Build static library + run: cargo build --lib --release # For some unclear reason, the release build has better errors. + - name: Generate swift bindings + run: | + cargo run --bin uniffi-bindgen -- generate --library ./target/release/libphoenix_channels_client.dylib --out-dir ./binding-swift/ --language swift + - name: Generate kotlin bindings + run: | + cargo run --bin uniffi-bindgen -- generate --library ./target/release/libphoenix_channels_client.dylib --out-dir ./binding-kotlin/ --language kotlin + + docs: + # rust cross-compilation + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ env.TOOLCHAIN }} + - name: Cache Cargo + uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin + ~/.cargo/git + ~/.cargo/registry + key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - name: Cache target + id: cache-target + uses: actions/cache@v4 + env: + cache-name: cache-target + with: + path: target + key: ${{ github.workflow }}-${{ github.job }}-${{ env.cache-name }}-${{ env.TOOLCHAIN }}-cargo--${{ hashFiles('**/Cargo.lock') }} + + - name: Build phoenix_channels_client + run: cargo build --all-targets + - name: Generate docs for phoenix_channel_clients + run: cargo doc + + android_simulator_integration_tests: + # rust cross-compilation + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Install Rust Nightly + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ env.TOOLCHAIN }} + target: x86_64-linux-android + + - name: Cache Cargo + uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin + ~/.cargo/git + ~/.cargo/registry + key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ hashFiles('**/Cargo.lock') }} + + - uses: taiki-e/install-action@v2 + with: + tool: cargo-dinghy@0.7.1 + + - uses: erlef/setup-beam@v1 + with: + elixir-version: 1.15 + otp-version: 25 + + - name: Cache deps + id: cache-deps + uses: actions/cache@v4 + env: + cache-name: cache-elixir-deps + with: + path: tests/support/test_server/deps + key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-mix-${{ env.cache-name }}- + + - name: Cache compiled build + id: cache-build + uses: actions/cache@v4 + env: + cache-name: cache-compiled-build + with: + path: tests/support/test_server/_build + key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-mix-${{ env.cache-name }}- + ${{ runner.os }}-mix- + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: 17 + distribution: 'temurin' + + - name: Install hex, rebar3 + run: | + mix local.hex --force + mix local.rebar --force + - name: Install test server dependencies, compile server, run server in background + working-directory: ./tests/support/test_server + run: | + mix deps.get + mix compile + mix phx.server & + + - name: Enable KVM + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + + - name: Gradle cache + uses: gradle/actions/setup-gradle@v3 + + - name: AVD cache + uses: actions/cache@v4 + id: avd-cache + with: + path: | + ~/.android/avd/* + ~/.android/adb* + key: avd-${{ env.ANDROID_API }} + + - name: Create AVD and generate snapshot for caching + if: steps.avd-cache.outputs.cache-hit != 'true' + uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: ${{ env.ANDROID_API }} + ndk: ${{ env.ANDROID_NDK_VERSION }} + force-avd-creation: false + target: google_apis + arch: x86_64 + emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none + disable-animations: false + script: echo "Generated AVD snapshot for caching." + + - name: Run tests in android emulator + uses: reactivecircus/android-emulator-runner@v2 + env: + RANLIB: "${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ranlib" + with: + api-level: ${{ env.ANDROID_API }} + ndk: ${{ env.ANDROID_NDK_VERSION }} + target: google_apis + arch: x86_64 + force-avd-creation: false + emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none + disable-animations: true + script: | + cargo dinghy all-platforms + cargo dinghy all-devices + export RUSTFLAGS="--cfg tokio_unstable" + cargo dinghy -p auto-android-x86_64-api${ANDROID_API} test diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml deleted file mode 100644 index a43cbf6..0000000 --- a/.github/workflows/linux.yml +++ /dev/null @@ -1,46 +0,0 @@ -name: linux - -on: pull_request - -env: - RUSTDOCFLAGS: "-D warnings" - TOOLCHAIN: nightly - CARGO_TERM_COLOR: always - -jobs: - docs: - # rust cross-compilation - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ env.TOOLCHAIN }} - default: true - - name: Cache Cargo - uses: actions/cache@v4 - with: - path: | - ~/.cargo/bin - ~/.cargo/git - ~/.cargo/registry - key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ env.CARGO_NDK_REV }}-${{ env.CARGO_TAI_REV }}-${{ hashFiles('**/Cargo.lock') }} - - name: Cache target - id: cache-target - uses: actions/cache@v4 - env: - cache-name: cache-target - with: - path: target - key: ${{ github.workflow }}-${{ github.job }}-${{ env.cache-name }}-${{ env.TOOLCHAIN }}-cargo-${{ env.CARGO_NDK_REV }}-${{ env.CARGO_TAI_REV }}-${{ hashFiles('**/Cargo.lock') }} - - name: Build phoenix_channels_client - uses: actions-rs/cargo@v1 - with: - command: build - args: --all-targets - - name: Generate docs for phoenix_channel_clients - uses: actions-rs/cargo@v1 - with: - command: doc diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml deleted file mode 100644 index 7b7f3c5..0000000 --- a/.github/workflows/macos.yml +++ /dev/null @@ -1,105 +0,0 @@ -name: macos - -on: pull_request - -env: - CARGO_TERM_COLOR: always - TOOLCHAIN: nightly - -jobs: - integration_tests: - runs-on: self-hosted - - steps: - - uses: actions/checkout@v4 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ env.TOOLCHAIN }} - default: true - - name: Set up Elixir, kotlin and ktlint - run: brew install elixir kotlin ktlint - - name: Cache Cargo - uses: actions/cache@v4 - with: - path: | - ~/.cargo/bin - ~/.cargo/git - ~/.cargo/registry - key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ hashFiles('**/Cargo.lock') }} - - name: Cache deps - id: cache-deps - uses: actions/cache@v4 - env: - cache-name: cache-elixir-deps - with: - path: tests/support/test_server/deps - key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} - restore-keys: | - ${{ runner.os }}-mix-${{ env.cache-name }}- - - name: Cache compiled build - id: cache-build - uses: actions/cache@v4 - env: - cache-name: cache-compiled-build - with: - path: tests/support/test_server/_build - key: ${{ runner.os }}-mix-${{ env.cache-name }}-2-${{ hashFiles('**/mix.lock') }} - restore-keys: | - ${{ runner.os }}-mix-${{ env.cache-name }}- - ${{ runner.os }}-mix- - - name: Build phoenix_channels_client - uses: actions-rs/cargo@v1 - with: - command: build - args: --all-targets - - name: Install hex - run: mix local.hex --force - - name: Install rebar3 - run: mix local.rebar --force - - name: Install test server dependencies - working-directory: ./tests/support/test_server - run: mix deps.get - - name: Build test server - working-directory: ./tests/support/test_server - run: mix compile - - name: Run test server in background - working-directory: ./tests/support/test_server - run: mix phx.server & - - - - name: Get JNA jar - run: wget 'https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.14.0/jna-5.14.0.jar' - - - name: Test phoenix_channel_clients - run: cargo test - env: - # This is required for the kotlin uniffi tests - CLASSPATH: "/opt/homebrew/opt/kotlin/libexec/lib/kotlinx-coroutines-core-jvm.jar:./jna-5.14.0.jar" - - uniffi-check: - runs-on: self-hosted - steps: - - uses: actions/checkout@v4 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ env.TOOLCHAIN }} - default: true - - name: Cache Cargo - uses: actions/cache@v4 - with: - path: | - ~/.cargo/bin - ~/.cargo/git - ~/.cargo/registry - key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ hashFiles('**/Cargo.lock') }} - - - name: Build static library - run: cargo build --lib --release # For some unclear reason, the release build has better errors. - - name: Generate swift bindings - run: | - cargo run --bin uniffi-bindgen -- generate --library ./target/release/libphoenix_channels_client.dylib --out-dir ./binding-swift/ --language swift - - name: Generate kotlin bindings - run: | - cargo run --bin uniffi-bindgen -- generate --library ./target/release/libphoenix_channels_client.dylib --out-dir ./binding-kotlin/ --language kotlin diff --git a/Cargo.toml b/Cargo.toml index c83e701..035faaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,11 +27,20 @@ crate-type = [ name = "uniffi-bindgen" path = "uniffi-bindgen.rs" +[profile.release] +opt-level = 'z' # Optimize for size +lto = true # Enable link-time optimization +codegen-units = 1 # Reduce number of codegen units to increase optimizations +panic = 'abort' # Abort on panic +strip = true # Strip symbols from binary* + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["native-tls"] +default = [] nightly = [] -native-tls = ["tokio-tungstenite/native-tls-vendored"] +native-tls = ["tokio-tungstenite/native-tls-vendored", "dep:native-tls"] +uniffi-build = ["uniffi/build", "uniffi/cli"] + [dependencies] arc-swap = "1.7.0" @@ -48,20 +57,17 @@ strum_macros = "0.26.1" thiserror = "1.0" tokio = { version = "1.37", features = ["sync", "macros", "time", "tracing"] } tokio-tungstenite = "0.21.0" -uniffi = { version = "0.27", features = ["cli", "tokio"] } +uniffi = { version = "0.27", features = ["tokio"] } url = "2.5" uuid = { version = "1.8.0", features = ["v4"] } -native-tls = { version = "0.2", features = ["vendored"] } +native-tls = { version = "0.2", features = ["vendored"], optional = true} [dev-dependencies] chrono = "0.4.31" env_logger = "0.10" -uniffi = { version = "0.27", features = ["bindgen-tests", "tokio", "cli"]} +uniffi = { version = "0.27", features = ["bindgen-tests", "tokio"]} tokio = { version = "1.37", features = ["full", "tracing", "test-util"] } -[build-dependencies] -uniffi = { version = "0.27", features = ["build"] } - [patch.crates-io] # Work for https://github.com/sfackler/rust-native-tls/pull/285 hasn't been published to crates.io yet. native-tls = { git = "https://github.com/sfackler/rust-native-tls", rev = "0b69ce6a3c4bfe973ede44f6862fc13f3f09c773" } diff --git a/src/ffi/socket.rs b/src/ffi/socket.rs index cfd7110..b9d53fa 100644 --- a/src/ffi/socket.rs +++ b/src/ffi/socket.rs @@ -526,6 +526,7 @@ pub enum ConnectError { /// When the [Socket] will automatically [Socket::connect] next. until: SystemTime, }, + #[cfg(feature = "native-tls")] /// These are TLS errors. #[error("tls error: {tls_error}")] Tls { @@ -552,6 +553,7 @@ impl From for ConnectError { rust::socket::ConnectError::WaitingToReconnect(until) => Self::WaitingToReconnect { until: instant_to_system_time(until), }, + #[cfg(feature = "native-tls")] rust::socket::ConnectError::Tls(tls_error) => Self::Tls { tls_error } diff --git a/src/rust/socket.rs b/src/rust/socket.rs index 1e95938..6a386ff 100644 --- a/src/rust/socket.rs +++ b/src/rust/socket.rs @@ -132,6 +132,7 @@ pub enum ConnectError { #[error("websocket error: {0}")] WebSocket(#[from] Arc), + #[cfg(feature = "native-tls")] #[error("tls error: {0}")] Tls(#[from] native_tls::Error), /// [Socket] shutting down because [Socket::shutdown] was called. diff --git a/src/rust/socket/listener.rs b/src/rust/socket/listener.rs index d5924ee..d755136 100644 --- a/src/rust/socket/listener.rs +++ b/src/rust/socket/listener.rs @@ -702,6 +702,7 @@ impl Listener { request = request.header("Cookie", cookie); } } + #[cfg(feature = "native-tls")] let connector = match url.scheme() { "ws" => { Some(tokio_tungstenite::Connector::Plain) @@ -715,10 +716,15 @@ impl Listener { None } }; + #[cfg(feature = "native-tls")] + let connector = tokio_tungstenite::connect_async_tls_with_config(request.body(()).expect("Failed to build http request"), None, false, connector); + #[cfg(not(feature = "native-tls"))] + let connector = tokio_tungstenite::connect_async_with_config(request.body(()).expect("Failed to build http request"), None, false); + match time::timeout_at( created_at + reconnect.connect_timeout, - tokio_tungstenite::connect_async_tls_with_config(request.body(()).expect("Failed to build http request"), None, false, connector) + connector ) .await { diff --git a/uniffi-bindgen.rs b/uniffi-bindgen.rs index df6d104..f3e7bf0 100644 --- a/uniffi-bindgen.rs +++ b/uniffi-bindgen.rs @@ -1,4 +1,4 @@ fn main() { + #[cfg(feature = "uniffi-build")] uniffi::uniffi_bindgen_main() - } From f515ec27a012391ea0d2e34de67eebfeebdd22d9 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 29 Apr 2024 13:06:36 -0400 Subject: [PATCH 2/4] Fix ci --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0746417..6f471fe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -97,7 +97,7 @@ jobs: - name: Build phoenix_channel_clients with tls run: - cargo build -Zbuild-sd --target ${{ matrix.rust-target }} --features native-tls + cargo build -Zbuild-std --target ${{ matrix.rust-target }} --features native-tls - name: Get JNA jar if: ${{ matrix.apple-target-os == 'macOS' }} From 8010e283edd6b17fcc16f6159103854e5bf1efce Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 29 Apr 2024 13:33:26 -0400 Subject: [PATCH 3/4] Fix ci --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6f471fe..a3c0336 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ env: OPENSSL_VERSION: 1.1.1s jobs: - integration_tests: + apple_integration_tests: runs-on: macos-14 strategy: matrix: @@ -139,7 +139,7 @@ jobs: run: | cargo run --bin uniffi-bindgen -- generate --library ./target/release/libphoenix_channels_client.dylib --out-dir ./binding-kotlin/ --language kotlin - docs: + integration_tests: # rust cross-compilation runs-on: ubuntu-latest From 24aaafd3504707f93f408aa5886007b8ff04fb81 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 29 Apr 2024 13:38:05 -0400 Subject: [PATCH 4/4] Fix ci --- .github/workflows/ci.yml | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a3c0336..c52bb4a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -148,23 +148,6 @@ jobs: - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ env.TOOLCHAIN }} - - name: Cache Cargo - uses: actions/cache@v4 - with: - path: | - ~/.cargo/bin - ~/.cargo/git - ~/.cargo/registry - key: ${{ github.workflow }}-${{ github.job }}-toolchain-${{ env.TOOLCHAIN }}-cargo-${{ hashFiles('**/Cargo.lock') }} - - name: Cache target - id: cache-target - uses: actions/cache@v4 - env: - cache-name: cache-target - with: - path: target - key: ${{ github.workflow }}-${{ github.job }}-${{ env.cache-name }}-${{ env.TOOLCHAIN }}-cargo--${{ hashFiles('**/Cargo.lock') }} - - name: Build phoenix_channels_client run: cargo build --all-targets - name: Generate docs for phoenix_channel_clients