Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nix to compile on android #631

Merged
merged 3 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ matrix:
- env: TARGET=armv7-linux-androideabi DISABLE_TESTS=1
rust: 1.13.0
- env: TARGET=i686-linux-android DISABLE_TESTS=1
rust: 1.13.0
rust: 1.18.0
- env: TARGET=x86_64-linux-android DISABLE_TESTS=1
rust: 1.13.0
rust: 1.18.0

# iOS
- env: TARGET=aarch64-apple-ios DISABLE_TESTS=1
Expand Down Expand Up @@ -104,18 +104,6 @@ matrix:
rust: nightly

allow_failures:
# Android (in the process of fixing these, so they're allowed to fail for now)
- env: TARGET=aarch64-linux-android DISABLE_TESTS=1
rust: 1.13.0
- env: TARGET=arm-linux-androideabi DISABLE_TESTS=1
rust: 1.13.0
- env: TARGET=armv7-linux-androideabi DISABLE_TESTS=1
rust: 1.13.0
- env: TARGET=i686-linux-android DISABLE_TESTS=1
rust: 1.13.0
- env: TARGET=x86_64-linux-android DISABLE_TESTS=1
rust: 1.13.0

# iOS is still being worked on, so for now don't block on compilation failures
- env: TARGET=aarch64-apple-ios DISABLE_TESTS=1
rust: 1.13.0
Expand Down
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Introduced wrapper types for gid_t, pid_t, and uid_t as Gid, Pid, and Uid
respectively. Various functions have been changed to use these new types as
arguments. ([#629](https://github.com/nix-rust/nix/pull/629))
- Promoted all Android targets to Tier 2 support

### Removed
- Removed io::Error from nix::Error and conversion from nix::Error to Errno
Expand All @@ -45,8 +46,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
Now compiles on Linux/MIPS ([#538](https://github.com/nix-rust/nix/pull/538)),
`Linux/PPC` ([#553](https://github.com/nix-rust/nix/pull/553)),
`MacOS/x86_64,i686` ([#553](https://github.com/nix-rust/nix/pull/553)),
`NetBSD/x64_64` ([#538](https://github.com/nix-rust/nix/pull/538)), and
`FreeBSD/x86_64,i686` ([#536](https://github.com/nix-rust/nix/pull/536)).
`NetBSD/x64_64` ([#538](https://github.com/nix-rust/nix/pull/538)),
`FreeBSD/x86_64,i686` ([#536](https://github.com/nix-rust/nix/pull/536)), and
`Android` ([#631](https://github.com/nix-rust/nix/pull/631)).
- `bind` and `errno_location` now work correctly on `Android`
([#631](https://github.com/nix-rust/nix/pull/631))

## [0.8.1] 2017-04-16

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ limitations. Support for platforms is split into two tiers:
*do not* block the inclusion of new code. Testing may be run, but
failures in tests don't block the inclusion of new code.

The following targets are all supported by nix on Rust 1.13.0 or newer:
The following targets are all supported by nix on Rust 1.13.0 or newer (unless
otherwise noted):

Tier 1:
* i686-unknown-linux-gnu
Expand All @@ -68,18 +69,18 @@ Tier 1:
Tier 2:
* i686-unknown-freebsd
* x86_64-unknown-netbsd
* aarch64-linux-android
* arm-linux-androideabi
* armv7-linux-androideabi
* i686-linux-android (requires Rust >= 1.18)
* x86_64-linux-android (requires Rust >= 1.18)

Tier 3:
* aarch64-apple-ios
* aarch64-linux-android
* arm-linux-androideabi
* armv7-apple-ios
* armv7-linux-androideabi
* armv7s-apple-ios
* i386-apple-ios
* i686-linux-android
* x86_64-apple-ios
* x86_64-linux-android

## Usage

Expand Down
8 changes: 7 additions & 1 deletion src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ unsafe fn errno_location() -> *mut c_int {
__errno()
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(target_os = "linux")]
unsafe fn errno_location() -> *mut c_int {
extern { fn __errno_location() -> *mut c_int; }
__errno_location()
}

#[cfg(target_os = "android")]
unsafe fn errno_location() -> *mut c_int {
extern { fn __errno() -> *mut c_int; }
__errno()
}

/// Sets the platform-specific errno to no-error
unsafe fn clear() -> () {
*errno_location() = 0;
Expand Down
52 changes: 52 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
/// }
/// }
/// ```
///
/// Example with casting, due to a mistake in libc. In this example, the
/// various flags have different types, so we cast the broken ones to the right
/// type.
///
/// ```
/// libc_bitflags!{
/// pub flags SaFlags: libc::c_ulong {
/// SA_NOCLDSTOP as libc::c_ulong,
/// SA_NOCLDWAIT,
/// SA_NODEFER as libc::c_ulong,
/// SA_ONSTACK,
/// SA_RESETHAND as libc::c_ulong,
/// SA_RESTART as libc::c_ulong,
/// SA_SIGINFO,
/// }
/// }
/// ```
macro_rules! libc_bitflags {
// (non-pub) Exit rule.
(@call_bitflags
Expand Down Expand Up @@ -130,6 +148,22 @@ macro_rules! libc_bitflags {
}
};

// Munch last ident and cast it to the given type.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident as $ty:ty
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag as $ty;
];
}
};

// Munch an ident; covers terminating comma case.
(@accumulate_flags
$prefix:tt,
Expand All @@ -147,6 +181,24 @@ macro_rules! libc_bitflags {
}
};

// Munch an ident and cast it to the given type; covers terminating comma
// case.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident as $ty:ty, $($tail:tt)*
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag as $ty;
];
$($tail)*
}
};

// (non-pub) Entry rule.
(
$(#[$attr:meta])*
Expand Down
30 changes: 30 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub const SIGIOT : Signal = SIGABRT;
pub const SIGPOLL : Signal = SIGIO;
pub const SIGUNUSED : Signal = SIGSYS;

#[cfg(not(target_os = "android"))]
libc_bitflags!{
pub flags SaFlags: libc::c_int {
SA_NOCLDSTOP,
Expand All @@ -210,6 +211,35 @@ libc_bitflags!{
}
}

// On 64-bit android, sa_flags is c_uint while on 32-bit android, it is
// c_ulong.
// FIXME: https://github.com/rust-lang/libc/pull/511
#[cfg(all(target_os = "android", target_pointer_width = "32"))]
libc_bitflags!{
pub flags SaFlags: libc::c_ulong {
SA_NOCLDSTOP as libc::c_ulong,
SA_NOCLDWAIT as libc::c_ulong,
SA_NODEFER as libc::c_ulong,
SA_ONSTACK as libc::c_ulong,
SA_RESETHAND as libc::c_ulong,
SA_RESTART as libc::c_ulong,
SA_SIGINFO as libc::c_ulong,
}
}

#[cfg(all(target_os = "android", target_pointer_width = "64"))]
libc_bitflags!{
pub flags SaFlags: libc::c_uint {
SA_NOCLDSTOP as libc::c_uint,
SA_NOCLDWAIT as libc::c_uint,
SA_NODEFER as libc::c_uint,
SA_ONSTACK as libc::c_uint,
SA_RESETHAND as libc::c_uint,
SA_RESTART as libc::c_uint,
SA_SIGINFO as libc::c_uint,
}
}

#[repr(i32)]
#[derive(Clone, Copy, PartialEq)]
pub enum SigmaskHow {
Expand Down
3 changes: 0 additions & 3 deletions src/sys/socket/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ mod os {
pub const SO_LINGER: c_int = libc::SO_LINGER;
pub const SO_MARK: c_int = libc::SO_MARK;
pub const SO_OOBINLINE: c_int = libc::SO_OOBINLINE;
#[cfg(not(target_arch="arm"))]
pub const SO_PASSCRED: c_int = libc::SO_PASSCRED;
pub const SO_PEEK_OFF: c_int = libc::SO_PEEK_OFF;
#[cfg(not(target_arch="arm"))]
pub const SO_PEERCRED: c_int = libc::SO_PEERCRED;
pub const SO_PRIORITY: c_int = libc::SO_PRIORITY;
pub const SO_PROTOCOL: c_int = libc::SO_PROTOCOL;
Expand All @@ -57,7 +55,6 @@ mod os {
pub const SO_REUSEPORT: c_int = libc::SO_REUSEPORT;
pub const SO_RXQ_OVFL: c_int = libc::SO_RXQ_OVFL;
pub const SO_SNDBUF: c_int = libc::SO_SNDBUF;
#[cfg(not(target_arch="arm"))]
pub const SO_SNDBUFFORCE: c_int = libc::SO_SNDBUFFORCE;
pub const SO_TIMESTAMP: c_int = libc::SO_TIMESTAMP;
pub const SO_TYPE: c_int = libc::SO_TYPE;
Expand Down
16 changes: 16 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> {
/// Bind a name to a socket
///
/// [Further reading](http://man7.org/linux/man-pages/man2/bind.2.html)
#[cfg(not(all(target_os="android", target_pointer_width="64")))]
pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
let res = unsafe {
let (ptr, len) = addr.as_ffi_pair();
Expand All @@ -403,6 +404,21 @@ pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
Errno::result(res).map(drop)
}

/// Bind a name to a socket
///
/// [Further reading](http://man7.org/linux/man-pages/man2/bind.2.html)
// Android has some weirdness. Its 64-bit bind takes a c_int instead of a
// socklen_t
#[cfg(all(target_os="android", target_pointer_width="64"))]
pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
let res = unsafe {
let (ptr, len) = addr.as_ffi_pair();
ffi::bind(fd, ptr, len as c_int)
};

Errno::result(res).map(drop)
}

/// Accept a connection on a socket
///
/// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html)
Expand Down
1 change: 0 additions & 1 deletion src/sys/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ mod ffi {
pub use self::non_android::*;

// On Android before 5.0, Bionic directly inline these to ioctl() calls.
#[inline]
#[cfg(all(target_os = "android", not(target_arch = "mips")))]
mod android {
use libc;
Expand Down