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

Add risc-v based Android compile target #99005

Closed
Closed
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
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ supported_targets! {
("armv7-linux-androideabi", armv7_linux_androideabi),
("thumbv7neon-linux-androideabi", thumbv7neon_linux_androideabi),
("aarch64-linux-android", aarch64_linux_android),
("riscv64gc-linux-android", riscv64gc_linux_android),

("x86_64-unknown-none-linuxkernel", x86_64_unknown_none_linuxkernel),

Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_target/src/spec/riscv64gc_linux_android.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::spec::{CodeModel, SanitizerSet, Target, TargetOptions};

pub fn target() -> Target {
Target {
llvm_target: "riscv64-linux-android".into(),
pointer_width: 64,
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n64-S128".into(),
arch: "riscv64".into(),
options: TargetOptions {
code_model: Some(CodeModel::Medium),
cpu: "generic-rv64".into(),
features: "+m,+a,+f,+d,+c".into(),
llvm_abiname: "lp64d".into(),
supported_sanitizers: SanitizerSet::ADDRESS,
max_atomic_width: Some(64),
..super::android_base::opts()
},
}
}
68 changes: 68 additions & 0 deletions library/std/src/os/android/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,74 @@ mod arch {
}
}

#[cfg(target_arch = "riscv64")]
mod arch {
use crate::os::raw::{c_uchar, c_ulong};
use crate::os::unix::raw::{gid_t, uid_t};

#[stable(feature = "raw_ext", since = "1.1.0")]
pub type dev_t = u64;
#[stable(feature = "raw_ext", since = "1.1.0")]
pub type mode_t = u32;

#[stable(feature = "raw_ext", since = "1.1.0")]
pub type blkcnt_t = u64;
#[stable(feature = "raw_ext", since = "1.1.0")]
pub type blksize_t = u64;
#[stable(feature = "raw_ext", since = "1.1.0")]
pub type ino_t = u64;
#[stable(feature = "raw_ext", since = "1.1.0")]
pub type nlink_t = u64;
#[stable(feature = "raw_ext", since = "1.1.0")]
pub type off_t = u64;
#[stable(feature = "raw_ext", since = "1.1.0")]
pub type time_t = i64;

#[repr(C)]
#[derive(Clone)]
#[stable(feature = "raw_ext", since = "1.1.0")]
pub struct stat {
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_dev: dev_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub __pad0: [c_uchar; 4],
#[stable(feature = "raw_ext", since = "1.1.0")]
pub __st_ino: ino_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_mode: mode_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_nlink: nlink_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_uid: uid_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_gid: gid_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_rdev: dev_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub __pad3: [c_uchar; 4],
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_size: off_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_blksize: blksize_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_blocks: blkcnt_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_atime: time_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_atime_nsec: c_ulong,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_mtime: time_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_mtime_nsec: c_ulong,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_ctime: time_t,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_ctime_nsec: c_ulong,
#[stable(feature = "raw_ext", since = "1.1.0")]
pub st_ino: ino_t,
}
}

#[cfg(target_arch = "x86_64")]
mod arch {
use crate::os::raw::{c_long, c_uint, c_ulong};
Expand Down
1 change: 1 addition & 0 deletions src/doc/rustc/src/platform-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ target | std | notes
`riscv32i-unknown-none-elf` | * | Bare RISC-V (RV32I ISA)
`riscv32imac-unknown-none-elf` | * | Bare RISC-V (RV32IMAC ISA)
`riscv32imc-unknown-none-elf` | * | Bare RISC-V (RV32IMC ISA)
`riscv64gc-linux-android` | ✓ | 64-bit RISC-V Android
`riscv64gc-unknown-none-elf` | * | Bare RISC-V (RV64IMAFDC ISA)
`riscv64imac-unknown-none-elf` | * | Bare RISC-V (RV64IMAC ISA)
`sparc64-unknown-linux-gnu` | ✓ | SPARC Linux (kernel 4.4, glibc 2.23)
Expand Down
52 changes: 52 additions & 0 deletions src/doc/rustc/src/platform-support/riscv64gc_linux_android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# riscv64gc-linux-android

**Tier: 3**

Android target similar to "x86_64-linux-android" and "aarch64-linux-android".

## Target maintainers

- [@Mao Han](https://github.com/MaoHan001)

## Requirements

The requirements for android rust toolchian can be downloaded with:
repo init -u https://android.googlesource.com/platform/manifest -b rust-toolchain

## Building the target

The target can be built by enabling it for a `rustc` build.

```toml
[build]
target = ["riscv64gc-linux-android"]
```

Make sure your C compiler is included in `$PATH`, then add it to the `config.toml`:

```toml
[target.riscv64gc-linux-android]
cc="clang-riscv64-linux-android"
ar="llvm-ar"
```

## Building Rust programs

Rust does not yet ship pre-compiled artifacts for this target. To compile for
this target, you will either need to build Rust with the target enabled (see
"Building the target" above), or build your own copy of `core` by using
`build-std` or similar.

## Testing
Check adb can list the attached device
$ adb devices
List of devices attached
emulator-5580 device

Test assembly with stage 3 compiler
$ ./x.py test src/test/assembly --stage 3 --target riscv64gc-linux-android

## Cross-compilation toolchains and C code

Compatible C code can be built with Clang's `riscv64-linux-android` targets as long as LLVM based C toolchains are used.
- [clang-riscv64](https://github.com/riscv-android-src/platform-prebuilts-clang-host-linux-x86/tree/riscv64-android-12.0.0_dev)