Skip to content

Commit 11d96d9

Browse files
Chen GuoyinMaoHan002
Chen Guoyin
authored andcommitted
Add risc-v based Android compile target
Signed-off-by: Chen Guoyin <[email protected]> Signed-off-by: Mao Han <[email protected]>
1 parent 96c2df8 commit 11d96d9

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

compiler/rustc_target/src/spec/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ supported_targets! {
873873
("armv7-linux-androideabi", armv7_linux_androideabi),
874874
("thumbv7neon-linux-androideabi", thumbv7neon_linux_androideabi),
875875
("aarch64-linux-android", aarch64_linux_android),
876+
("riscv64gc-linux-android", riscv64gc_linux_android),
876877

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::spec::{CodeModel, SanitizerSet, Target, TargetOptions};
2+
3+
pub fn target() -> Target {
4+
Target {
5+
llvm_target: "riscv64-linux-android".into(),
6+
pointer_width: 64,
7+
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n64-S128".into(),
8+
arch: "riscv64".into(),
9+
options: TargetOptions {
10+
code_model: Some(CodeModel::Medium),
11+
cpu: "generic-rv64".into(),
12+
features: "+m,+a,+f,+d,+c".into(),
13+
llvm_abiname: "lp64d".into(),
14+
supported_sanitizers: SanitizerSet::ADDRESS,
15+
max_atomic_width: Some(64),
16+
..super::android_base::opts()
17+
},
18+
}
19+
}

library/std/src/os/android/raw.rs

+68
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,74 @@ mod arch {
155155
}
156156
}
157157

158+
#[cfg(target_arch = "riscv64")]
159+
mod arch {
160+
use crate::os::raw::{c_uchar, c_ulong};
161+
use crate::os::unix::raw::{gid_t, uid_t};
162+
163+
#[stable(feature = "raw_ext", since = "1.1.0")]
164+
pub type dev_t = u64;
165+
#[stable(feature = "raw_ext", since = "1.1.0")]
166+
pub type mode_t = u32;
167+
168+
#[stable(feature = "raw_ext", since = "1.1.0")]
169+
pub type blkcnt_t = u64;
170+
#[stable(feature = "raw_ext", since = "1.1.0")]
171+
pub type blksize_t = u64;
172+
#[stable(feature = "raw_ext", since = "1.1.0")]
173+
pub type ino_t = u64;
174+
#[stable(feature = "raw_ext", since = "1.1.0")]
175+
pub type nlink_t = u64;
176+
#[stable(feature = "raw_ext", since = "1.1.0")]
177+
pub type off_t = u64;
178+
#[stable(feature = "raw_ext", since = "1.1.0")]
179+
pub type time_t = i64;
180+
181+
#[repr(C)]
182+
#[derive(Clone)]
183+
#[stable(feature = "raw_ext", since = "1.1.0")]
184+
pub struct stat {
185+
#[stable(feature = "raw_ext", since = "1.1.0")]
186+
pub st_dev: dev_t,
187+
#[stable(feature = "raw_ext", since = "1.1.0")]
188+
pub __pad0: [c_uchar; 4],
189+
#[stable(feature = "raw_ext", since = "1.1.0")]
190+
pub __st_ino: ino_t,
191+
#[stable(feature = "raw_ext", since = "1.1.0")]
192+
pub st_mode: mode_t,
193+
#[stable(feature = "raw_ext", since = "1.1.0")]
194+
pub st_nlink: nlink_t,
195+
#[stable(feature = "raw_ext", since = "1.1.0")]
196+
pub st_uid: uid_t,
197+
#[stable(feature = "raw_ext", since = "1.1.0")]
198+
pub st_gid: gid_t,
199+
#[stable(feature = "raw_ext", since = "1.1.0")]
200+
pub st_rdev: dev_t,
201+
#[stable(feature = "raw_ext", since = "1.1.0")]
202+
pub __pad3: [c_uchar; 4],
203+
#[stable(feature = "raw_ext", since = "1.1.0")]
204+
pub st_size: off_t,
205+
#[stable(feature = "raw_ext", since = "1.1.0")]
206+
pub st_blksize: blksize_t,
207+
#[stable(feature = "raw_ext", since = "1.1.0")]
208+
pub st_blocks: blkcnt_t,
209+
#[stable(feature = "raw_ext", since = "1.1.0")]
210+
pub st_atime: time_t,
211+
#[stable(feature = "raw_ext", since = "1.1.0")]
212+
pub st_atime_nsec: c_ulong,
213+
#[stable(feature = "raw_ext", since = "1.1.0")]
214+
pub st_mtime: time_t,
215+
#[stable(feature = "raw_ext", since = "1.1.0")]
216+
pub st_mtime_nsec: c_ulong,
217+
#[stable(feature = "raw_ext", since = "1.1.0")]
218+
pub st_ctime: time_t,
219+
#[stable(feature = "raw_ext", since = "1.1.0")]
220+
pub st_ctime_nsec: c_ulong,
221+
#[stable(feature = "raw_ext", since = "1.1.0")]
222+
pub st_ino: ino_t,
223+
}
224+
}
225+
158226
#[cfg(target_arch = "x86_64")]
159227
mod arch {
160228
use crate::os::raw::{c_long, c_uint, c_ulong};

src/doc/rustc/src/platform-support.md

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ target | std | notes
157157
`riscv32i-unknown-none-elf` | * | Bare RISC-V (RV32I ISA)
158158
`riscv32imac-unknown-none-elf` | * | Bare RISC-V (RV32IMAC ISA)
159159
`riscv32imc-unknown-none-elf` | * | Bare RISC-V (RV32IMC ISA)
160+
`riscv64gc-linux-android` | ✓ | 64-bit RISC-V Android
160161
`riscv64gc-unknown-none-elf` | * | Bare RISC-V (RV64IMAFDC ISA)
161162
`riscv64imac-unknown-none-elf` | * | Bare RISC-V (RV64IMAC ISA)
162163
`sparc64-unknown-linux-gnu` | ✓ | SPARC Linux (kernel 4.4, glibc 2.23)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# riscv64gc-linux-android
2+
3+
**Tier: 3**
4+
5+
Android target similar to "x86_64-linux-android" and "aarch64-linux-android".
6+
7+
## Target maintainers
8+
9+
- [@Mao Han](https://github.com/MaoHan001)
10+
11+
## Requirements
12+
13+
The requirements for android rust toolchian can be downloaded with:
14+
repo init -u https://android.googlesource.com/platform/manifest -b rust-toolchain
15+
16+
## Building the target
17+
18+
The target can be built by enabling it for a `rustc` build.
19+
20+
```toml
21+
[build]
22+
target = ["riscv64gc-linux-android"]
23+
```
24+
25+
Make sure your C compiler is included in `$PATH`, then add it to the `config.toml`:
26+
27+
```toml
28+
[target.riscv64gc-linux-android]
29+
cc="clang-riscv64-linux-android"
30+
ar="llvm-ar"
31+
```
32+
33+
## Building Rust programs
34+
35+
Rust does not yet ship pre-compiled artifacts for this target. To compile for
36+
this target, you will either need to build Rust with the target enabled (see
37+
"Building the target" above), or build your own copy of `core` by using
38+
`build-std` or similar.
39+
40+
## Testing
41+
Check adb can list the attached device
42+
$ adb devices
43+
List of devices attached
44+
emulator-5580 device
45+
46+
Test assembly with stage 3 compiler
47+
$ ./x.py test src/test/assembly --stage 3 --target riscv64gc-linux-android
48+
49+
## Cross-compilation toolchains and C code
50+
51+
Compatible C code can be built with Clang's `riscv64-linux-android` targets as long as LLVM based C toolchains are used.
52+
- [clang-riscv64](https://github.com/riscv-android-src/platform-prebuilts-clang-host-linux-x86/tree/riscv64-android-12.0.0_dev)

0 commit comments

Comments
 (0)