Skip to content

Commit 9354683

Browse files
authored
Enable Android build (#384)
* 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
1 parent 6ef2d56 commit 9354683

File tree

7 files changed

+54
-7
lines changed

7 files changed

+54
-7
lines changed

.github/workflows/ci.yaml

+20-1
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,25 @@ jobs:
4747
toolchain: ${{ matrix.rust }}
4848
components: rustfmt, clippy
4949

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

5362
- name: Build
54-
id: build
5563
run: cargo build --verbose
5664

65+
- name: Build target aarch64-linux-android
66+
if: matrix.os == 'ubuntu-latest'
67+
run: cross build --target aarch64-linux-android --verbose
68+
5769
# This is useful for debugging problems when the expected build artifacts
5870
# (like shell completions and man pages) aren't generated.
5971
- name: Show build.rs stderr
@@ -103,6 +115,13 @@ jobs:
103115
name: ${{ matrix.os }}-${{ matrix.rust }}-failed_snapshots
104116
path: '**/*.snap.new'
105117

118+
- name: Upload android binary
119+
if: ${{ matrix.os == 'ubuntu-latest' && ( success() || steps.build.outcome == 'success' ) }}
120+
uses: actions/upload-artifact@v3
121+
with:
122+
name: aarch64-linux-android-${{ matrix.rust }}
123+
path: target/aarch64-linux-android/debug/bandwhich
124+
106125
- name: Upload unix binary
107126
if: ${{ matrix.os != 'windows-latest' && ( success() || steps.build.outcome == 'success' ) }}
108127
uses: actions/upload-artifact@v3

.github/workflows/release.yaml

+11-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ jobs:
5252
strategy:
5353
matrix:
5454
build:
55+
- aarch64-linux-android
5556
- linux-x64-gnu
5657
- linux-x64-musl
5758
- macos-x64
5859
- windows-x64-msvc
5960
include:
61+
- cargo: cargo # default; overwrite with `cross` if necessary
62+
- build: aarch64-linux-android
63+
os: ubuntu-latest
64+
target: aarch64-linux-android
65+
cargo: cross
6066
- build: linux-x64-gnu
6167
os: ubuntu-latest
6268
target: x86_64-unknown-linux-gnu
@@ -84,13 +90,17 @@ jobs:
8490
if: matrix.target == 'x86_64-unknown-linux-musl'
8591
run: sudo apt-get install -y --no-install-recommends musl-tools
8692

93+
- name: Install cross
94+
if: matrix.cargo == 'cross'
95+
run: cargo install --git https://github.com/cross-rs/cross.git --rev 085092c cross
96+
8797
- name: Build release binary
8898
shell: bash
8999
env:
90100
RUSTFLAGS: "-C strip=symbols"
91101
run: |
92102
mkdir -p "$BANDWHICH_GEN_DIR"
93-
cargo build --verbose --release --target ${{ matrix.target }}
103+
${{ matrix.cargo }} build --verbose --release --target ${{ matrix.target }}
94104
95105
- name: Collect build artifacts
96106
shell: bash

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77
## [Unreleased]
88

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

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

2024
## Changed
25+
2126
* CI: strip release binaries for all targets #358 - @cyqsimon
2227
* Bump MSRV to 1.74 (required by clap 4.5; see #373)
2328
* CI: Configure dependabot grouping #395 - @cyqsimon

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ trust-dns-resolver = "0.23.2"
4343
unicode-width = "0.1.11"
4444
strum = { version = "0.26.2", features = ["derive"] }
4545

46-
[target.'cfg(target_os = "linux")'.dependencies]
46+
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
4747
procfs = "0.16.0"
4848

4949
[target.'cfg(any(target_os = "macos", target_os = "freebsd"))'.dependencies]

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ For more details, see [The Future of Bandwhich #275](https://github.com/imsnif/b
5656

5757
### Download a prebuilt binary
5858

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

6161
### Arch Linux
6262

@@ -123,6 +123,19 @@ The minimum supported Rust version is **1.70.0**.
123123
cargo install bandwhich
124124
```
125125

126+
#### Building from source for Android (`aarch64-linux-android`)
127+
128+
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:
129+
130+
```sh
131+
cargo install --git https://github.com/cross-rs/cross.git cross
132+
cross build --release --target aarch64-linux-android
133+
```
134+
135+
Kindly be aware that this process generates a pure binary file and not an APK suitable for installation on any arbitrary Android device.
136+
137+
The Android support for bandwhich beyond its build is *not* endorsed by this project and may be unstable or non-functional.
138+
126139
### OpenWRT
127140

128141
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`.

src/os/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(target_os = "linux")]
1+
#[cfg(any(target_os = "android", target_os = "linux"))]
22
mod linux;
33

44
#[cfg(any(target_os = "macos", target_os = "freebsd"))]

src/os/shared.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::runtime::Runtime;
1313

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

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

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

0 commit comments

Comments
 (0)