Skip to content

Commit ad26dab

Browse files
zhaixiaojuanheiher
authored andcommitted
Initial support for loongarch64-unknown-linux-gnu
1 parent 5ce70ed commit ad26dab

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::spec::{Target, TargetOptions};
2+
3+
pub fn target() -> Target {
4+
Target {
5+
llvm_target: "loongarch64-unknown-linux-gnu".into(),
6+
pointer_width: 64,
7+
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n64-S128".into(),
8+
arch: "loongarch64".into(),
9+
options: TargetOptions {
10+
cpu: "generic".into(),
11+
features: "+f,+d".into(),
12+
llvm_abiname: "lp64d".into(),
13+
max_atomic_width: Some(64),
14+
..super::linux_gnu_base::opts()
15+
},
16+
}
17+
}

compiler/rustc_target/src/spec/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ supported_targets! {
10211021
("x86_64-unknown-linux-gnux32", x86_64_unknown_linux_gnux32),
10221022
("i686-unknown-linux-gnu", i686_unknown_linux_gnu),
10231023
("i586-unknown-linux-gnu", i586_unknown_linux_gnu),
1024+
("loongarch64-unknown-linux-gnu", loongarch64_unknown_linux_gnu),
10241025
("m68k-unknown-linux-gnu", m68k_unknown_linux_gnu),
10251026
("mips-unknown-linux-gnu", mips_unknown_linux_gnu),
10261027
("mips64-unknown-linux-gnuabi64", mips64_unknown_linux_gnuabi64),

src/doc/rustc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [\*-android and \*-androideabi](platform-support/android.md)
2929
- [\*-unknown-fuchsia](platform-support/fuchsia.md)
3030
- [\*-kmc-solid_\*](platform-support/kmc-solid.md)
31+
- [loongarch\*-unknown-linux-\*](platform-support/loongarch-linux.md)
3132
- [m68k-unknown-linux-gnu](platform-support/m68k-unknown-linux-gnu.md)
3233
- [mips64-openwrt-linux-musl](platform-support/mips64-openwrt-linux-musl.md)
3334
- [mipsel-sony-psx](platform-support/mipsel-sony-psx.md)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ target | std | host | notes
263263
`i686-uwp-windows-gnu` | ? | |
264264
`i686-uwp-windows-msvc` | ? | |
265265
`i686-wrs-vxworks` | ? | |
266+
[`loongarch64-unknown-linux-gnu`](platform-support/loongarch-linux.md) | ? | | LoongArch64 Linux (LP64D ABI)
266267
[`m68k-unknown-linux-gnu`](platform-support/m68k-unknown-linux-gnu.md) | ? | | Motorola 680x0 Linux
267268
`mips-unknown-linux-uclibc` | ✓ | | MIPS Linux with uClibc
268269
[`mips64-openwrt-linux-musl`](platform-support/mips64-openwrt-linux-musl.md) | ? | | MIPS64 for OpenWrt Linux MUSL
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# loongarch\*-unknown-linux-\*
2+
3+
**Tier: 3**
4+
5+
[LoongArch] is a new RISC ISA developed by Loongson Technology Corporation Limited.
6+
7+
[LoongArch]: https://loongson.github.io/LoongArch-Documentation/README-EN.html
8+
9+
The target name follow this format: `<machine>-<vendor>-<os><fabi_suffix>, where `<machine>` specifies the CPU family/model, `<vendor>` specifies the vendor and `<os>` the operating system name.
10+
While the integer base ABI is implied by the machine field, the floating point base ABI type is encoded into the os field of the specifier using the string suffix `<fabi-suffix>`.
11+
12+
| `<fabi-suffix>` | `Description` |
13+
|------------------------|--------------------------------------------------------------------|
14+
| f64 | The base ABI use 64-bits FPRs for parameter passing.(lp64d)|
15+
| f32 | The base ABI uses 32-bit FPRs for parameter passing. (lp64f)|
16+
| sf | The base ABI uses no FPR for parameter passing. (lp64s) |
17+
18+
|`ABI type(Base ABI/ABI extension)`| `C library` | `kernel` | `target tuple` |
19+
|----------------------------------|-------------|----------|----------------------------------|
20+
| lp64d/base | glibc | linux | loongarch64-unknown-linux-gnu |
21+
| lp64f/base | glibc | linux | loongarch64-unknown-linux-gnuf32 |
22+
| lp64s/base | glibc | linux | loongarch64-unknown-linux-gnusf |
23+
| lp64d/base | musl libc | linux | loongarch64-unknown-linux-musl|
24+
| lp64f/base | musl libc | linux | loongarch64-unknown-linux-muslf32|
25+
| lp64s/base | musl libc | linux | loongarch64-unknown-linux-muslsf |
26+
27+
## Target maintainers
28+
29+
- [ZHAI xiaojuan](https://github.com/zhaixiaojuan) `zhaixiaojuan@loongson.cn`
30+
- [WANG rui](https://github.com/heiher) `wangrui@loongson.cn`
31+
- [ZHAI xiang](https://github.com/xiangzhai) `zhaixiang@loongson.cn`
32+
- [WANG Xuerui](https://github.com/xen0n) `git@xen0n.name`
33+
34+
## Requirements
35+
36+
This target is cross-compiled.
37+
A GNU toolchain for LoongArch target is required. It can be downloaded from https://github.com/loongson/build-tools/releases, or built from the source code of GCC (12.1.0 or later) and Binutils (2.40 or later).
38+
39+
## Building the target
40+
41+
The target can be built by enabling it for a `rustc` build.
42+
43+
```toml
44+
[build]
45+
target = ["loongarch64-unknown-linux-gnu"]
46+
```
47+
48+
Make sure `loongarch64-unknown-linux-gnu-gcc` can be searched from the directories specified in`$PATH`. Alternatively, you can use GNU LoongArch Toolchain by adding the following to `config.toml`:
49+
50+
```toml
51+
[target.loongarch64-unknown-linux-gnu]
52+
# ADJUST THIS PATH TO POINT AT YOUR TOOLCHAIN
53+
cc = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc"
54+
cxx = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-g++"
55+
ar = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-ar"
56+
ranlib = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-ranlib"
57+
linker = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc"
58+
```
59+
60+
## Cross-compilation
61+
62+
This target can be cross-compiled on a `x86_64-unknown-linux-gnu` host. Cross-compilation on other hosts may work but is not tested.
63+
64+
## Testing
65+
To test a cross-compiled binary on your build system, install the qemu binary that supports the LoongArch architecture and execute the following commands.
66+
```text
67+
CC_loongarch64_unknown_linux_gnu=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc \
68+
CXX_loongarch64_unknown_linux_gnu=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-g++ \
69+
AR_loongarch64_unknown_linux_gnu=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc-ar \
70+
CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNUN_LINKER=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc \
71+
# SET TARGET SYSTEM LIBRARY PATH
72+
CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNUN_RUNNER="qemu-loongarch64 -L /TOOLCHAIN_PATH/TARGET_LIBRAY_PATH" \
73+
cargo run --target loongarch64-unknown-linux-gnu --release
74+
```
75+
Tested on x86 architecture, other architectures not tested.
76+
77+
## Building Rust programs
78+
79+
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 `std` by using `build-std` or similar.
80+
81+
If `rustc` has support for that target and the library artifacts are available, then Rust static libraries can be built for that target:
82+
83+
```shell
84+
$ rustc --target loongarch64-unknown-linux-gnu your-code.rs --crate-type staticlib
85+
$ ls libyour_code.a
86+
```
87+
88+
On Rust Nightly it's possible to build without the target artifacts available:
89+
90+
```text
91+
cargo build -Z build-std --target loongarch64-unknown-linux-gnu
92+
```

tests/ui/check-cfg/compact-values.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition value
44
LL | #[cfg(target(os = "linux", arch = "X"))]
55
| ^^^^^^^^^^
66
|
7-
= note: expected values for `target_arch` are: aarch64, arm, avr, bpf, hexagon, m68k, mips, mips64, msp430, nvptx64, powerpc, powerpc64, riscv32, riscv64, s390x, sparc, sparc64, wasm32, wasm64, x86, x86_64
7+
= note: expected values for `target_arch` are: aarch64, arm, avr, bpf, hexagon, loongarch64, m68k, mips, mips64, msp430, nvptx64, powerpc, powerpc64, riscv32, riscv64, s390x, sparc, sparc64, wasm32, wasm64, x86, x86_64
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

0 commit comments

Comments
 (0)