Skip to content

Commit

Permalink
Enable Android build (imsnif#384)
Browse files Browse the repository at this point in the history
* Support build for target_os android

The target_os `android` is quite similar to `linux` but must be
mentioned in the guards explicitly. Tested for target
`aarch64-linux-android`.

* Add target aarch64-linux-android to ci

Use `cross` for building target `aarch64-linux-android`. In the `ci`
workflow the matrix branch of `os` `ubuntu-latest` is used because the
matrix organization is host os centric instead of target.

Releases are build with `cross` for target `aarch64-linux-android`.

Install `cross` if needed for a run. At the time of writing the latest
release of `cross` can't build binaries for target
`aarch64-linux-android` and it's common sense that the latest git tag or
`master` shall be used [1].

[1] cross-rs/cross#1222

* Add android related readme section
  • Loading branch information
flxo authored Apr 2, 2024
1 parent 6ef2d56 commit 9354683
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,25 @@ jobs:
toolchain: ${{ matrix.rust }}
components: rustfmt, clippy

- name: Install Cross on Ubuntu
if: matrix.os == 'ubuntu-latest'
# The latest realese of `cross` is not able to build/link for `aarch64-linux-android`
# See: https://github.com/cross-rs/cross/issues/1222
# This is fixed on `main` but not yet released. To avoid a breakage somewhen in the future
# pin the cross revision used to the latest HEAD at 04/2024.
# Remove the git source and revision once cross 0.3 is released.
run: cargo install --git https://github.com/cross-rs/cross.git --rev 085092c cross

- name: Check formatting
run: cargo fmt --all -- --check

- name: Build
id: build
run: cargo build --verbose

- name: Build target aarch64-linux-android
if: matrix.os == 'ubuntu-latest'
run: cross build --target aarch64-linux-android --verbose

# This is useful for debugging problems when the expected build artifacts
# (like shell completions and man pages) aren't generated.
- name: Show build.rs stderr
Expand Down Expand Up @@ -103,6 +115,13 @@ jobs:
name: ${{ matrix.os }}-${{ matrix.rust }}-failed_snapshots
path: '**/*.snap.new'

- name: Upload android binary
if: ${{ matrix.os == 'ubuntu-latest' && ( success() || steps.build.outcome == 'success' ) }}
uses: actions/upload-artifact@v3
with:
name: aarch64-linux-android-${{ matrix.rust }}
path: target/aarch64-linux-android/debug/bandwhich

- name: Upload unix binary
if: ${{ matrix.os != 'windows-latest' && ( success() || steps.build.outcome == 'success' ) }}
uses: actions/upload-artifact@v3
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ jobs:
strategy:
matrix:
build:
- aarch64-linux-android
- linux-x64-gnu
- linux-x64-musl
- macos-x64
- windows-x64-msvc
include:
- cargo: cargo # default; overwrite with `cross` if necessary
- build: aarch64-linux-android
os: ubuntu-latest
target: aarch64-linux-android
cargo: cross
- build: linux-x64-gnu
os: ubuntu-latest
target: x86_64-unknown-linux-gnu
Expand Down Expand Up @@ -84,13 +90,17 @@ jobs:
if: matrix.target == 'x86_64-unknown-linux-musl'
run: sudo apt-get install -y --no-install-recommends musl-tools

- name: Install cross
if: matrix.cargo == 'cross'
run: cargo install --git https://github.com/cross-rs/cross.git --rev 085092c cross

- name: Build release binary
shell: bash
env:
RUSTFLAGS: "-C strip=symbols"
run: |
mkdir -p "$BANDWHICH_GEN_DIR"
cargo build --verbose --release --target ${{ matrix.target }}
${{ matrix.cargo }} build --verbose --release --target ${{ matrix.target }}
- name: Collect build artifacts
shell: bash
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [Unreleased]

## Fixed

* Remove redundant imports #377 - @cyqsimon
* CI: use GitHub API to exempt dependabot from changelog requirement #378 - @cyqsimon
* Remove unnecessary logging synchronisation #381 - @cyqsimon
* Apply suggestions from new clippy lint clippy::assigning_clones #382 - @cyqsimon
* Fix IPv6 socket detect logic #383 - @cyqsimon
* Support build for `target_os`` `android` #384 - @flxo

## Added

* CI: include generated assets in release archive #359 - @cyqsimon
* Add PID column to the process table #379 - @notjedi
* CI: add builds for target `aarch64-linux-android` #384 - @flxo

## Changed

* CI: strip release binaries for all targets #358 - @cyqsimon
* Bump MSRV to 1.74 (required by clap 4.5; see #373)
* CI: Configure dependabot grouping #395 - @cyqsimon
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ trust-dns-resolver = "0.23.2"
unicode-width = "0.1.11"
strum = { version = "0.26.2", features = ["derive"] }

[target.'cfg(target_os = "linux")'.dependencies]
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
procfs = "0.16.0"

[target.'cfg(any(target_os = "macos", target_os = "freebsd"))'.dependencies]
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ For more details, see [The Future of Bandwhich #275](https://github.com/imsnif/b

### Download a prebuilt binary

If you're on linux, you can download the generic binary from the releases.
If you're on `android` or `linux`, you can download the generic binary from the [releases](https://github.com/imsnif/bandwhich/releases)

### Arch Linux

Expand Down Expand Up @@ -123,6 +123,19 @@ The minimum supported Rust version is **1.70.0**.
cargo install bandwhich
```

#### Building from source for Android (`aarch64-linux-android`)

Building for target `aarch64-linux-android` is supported via [cross](https://github.com/cross-rs/cross). Until [#1222](https://github.com/cross-rs/cross/issues/1222) is solved, use the latest HEAD:

```sh
cargo install --git https://github.com/cross-rs/cross.git cross
cross build --release --target aarch64-linux-android
```

Kindly be aware that this process generates a pure binary file and not an APK suitable for installation on any arbitrary Android device.

The Android support for bandwhich beyond its build is *not* endorsed by this project and may be unstable or non-functional.

### OpenWRT

To install `bandwhich` on OpenWRT, you'll need to compile a binary that would fit its processor architecture. This might mean you would have to cross compile if, for example, you're working on an `x86_64` and the OpenWRT is installed on an `arm7`.
Expand Down
2 changes: 1 addition & 1 deletion src/os/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "android", target_os = "linux"))]
mod linux;

#[cfg(any(target_os = "macos", target_os = "freebsd"))]
Expand Down
4 changes: 2 additions & 2 deletions src/os/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::runtime::Runtime;

use crate::{network::dns, os::errors::GetInterfaceError, OsInputOutput};

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "android", target_os = "linux"))]
use crate::os::linux::get_open_sockets;
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
use crate::os::lsof::get_open_sockets;
Expand Down Expand Up @@ -220,7 +220,7 @@ fn eperm_message() -> &'static str {
}

#[inline]
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn eperm_message() -> &'static str {
r#"
Insufficient permissions to listen on network interface(s). You can work around
Expand Down

0 comments on commit 9354683

Please sign in to comment.