Skip to content

Commit 2925a47

Browse files
authored
Unrolled build for rust-lang#127053
Rollup merge of rust-lang#127053 - xen0n:update-loong-docs, r=Nilstrieb,heiher Update the LoongArch target documentation The docs for the LoongArch targets are a bit dated since their introduction, and the prose has some room for improvement as well. Streamline a bit, referring to the neighboring targets' docs, and provide up-to-date information as much as I can come up with. cc fellow target maintainer `@heiher` for review of target-specific bits
2 parents ef3d6fd + 64be3a3 commit 2925a47

File tree

2 files changed

+125
-70
lines changed

2 files changed

+125
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
# loongarch\*-unknown-linux-\*
1+
# `loongarch*-unknown-linux-*`
22

3-
**Tier: 2**
3+
**Tier: 2 (with Host Tools)**
44

5-
[LoongArch] is a new RISC ISA developed by Loongson Technology Corporation Limited.
5+
[LoongArch][la-docs] Linux targets.
6+
LoongArch is a RISC ISA developed by Loongson Technology Corporation Limited.
67

7-
[LoongArch]: https://loongson.github.io/LoongArch-Documentation/README-EN.html
8+
| Target | Description |
9+
|--------|-------------|
10+
| `loongarch64-unknown-linux-gnu` | LoongArch64 Linux, LP64D ABI (kernel 5.19, glibc 2.36) |
11+
| `loongarch64-unknown-linux-musl` | LoongArch64 Linux, LP64D ABI (kernel 5.19, musl 1.2.5) |
812

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>`.
13+
These support both native and cross builds, and have full support for `std`.
1114

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) |
15+
Reference material:
1716

18-
<br>
17+
* [LoongArch ISA manuals][la-docs]
18+
* [Application Binary Interface for the LoongArch&trade; Architecture][la-abi-specs]
1919

20-
|`ABI type(Base ABI/ABI extension)`| `C library` | `kernel` | `target tuple` |
21-
|----------------------------------|-------------|----------|----------------------------------|
22-
| lp64d/base | glibc | linux | loongarch64-unknown-linux-gnu |
23-
| lp64f/base | glibc | linux | loongarch64-unknown-linux-gnuf32 |
24-
| lp64s/base | glibc | linux | loongarch64-unknown-linux-gnusf |
25-
| lp64d/base | musl libc | linux | loongarch64-unknown-linux-musl|
26-
| lp64f/base | musl libc | linux | loongarch64-unknown-linux-muslf32|
27-
| lp64s/base | musl libc | linux | loongarch64-unknown-linux-muslsf |
20+
[la-abi-specs]: https://github.com/loongson/la-abi-specs
21+
[la-docs]: https://loongson.github.io/LoongArch-Documentation/README-EN.html
2822

2923
## Target maintainers
3024

@@ -35,60 +29,109 @@ While the integer base ABI is implied by the machine field, the floating po
3529

3630
## Requirements
3731

38-
This target is cross-compiled.
39-
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).
32+
### OS Version
4033

41-
## Building the target
34+
The minimum supported Linux version is 5.19.
4235

43-
The target can be built by enabling it for a `rustc` build.
36+
Some Linux distributions, mostly commercial ones, may provide forked Linux
37+
kernels that has a version number less than 5.19 for their LoongArch ports.
38+
Such kernels may still get patched to be compatible with the upstream Linux
39+
5.19 UAPI, therefore supporting the targets described in this document, but
40+
this is not always the case. The `rustup` installer contains a check for this,
41+
and will abort if incompatibility is detected.
42+
43+
### Host toolchain
44+
45+
The targets require a reasonably up-to-date LoongArch toolchain on the host.
46+
Currently the following components are used by the Rust CI to build the target,
47+
and the versions can be seen as the minimum requirement:
48+
49+
* GNU Binutils 2.40
50+
* GCC 13.x
51+
* glibc 2.36
52+
* linux-headers 5.19
53+
54+
Of these, glibc and linux-headers are at their respective earliest versions with
55+
mainline LoongArch support, so it is impossible to use older versions of these.
56+
Older versions of Binutils and GCC will not work either, due to lack of support
57+
for newer LoongArch ELF relocation types, among other features.
58+
59+
Recent LLVM/Clang toolchains may be able to build the targets, but are not
60+
currently being actively tested.
61+
62+
## Building
63+
64+
These targets are distributed through `rustup`, and otherwise require no
65+
special configuration.
66+
67+
If you need to build your own Rust for some reason though, the targets can be
68+
simply enabled in `config.toml`. For example:
4469

4570
```toml
4671
[build]
4772
target = ["loongarch64-unknown-linux-gnu"]
4873
```
4974

50-
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`:
75+
Make sure the LoongArch toolchain binaries are reachable from `$PATH`.
76+
Alternatively, you can explicitly configure the paths in `config.toml`:
5177

5278
```toml
5379
[target.loongarch64-unknown-linux-gnu]
54-
# ADJUST THIS PATH TO POINT AT YOUR TOOLCHAIN
80+
# Adjust the paths to point at your toolchain
81+
# Suppose the toolchain is placed at /TOOLCHAIN_PATH, and the cross prefix is
82+
# "loongarch64-unknown-linux-gnu-":
5583
cc = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc"
5684
cxx = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-g++"
5785
ar = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-ar"
5886
ranlib = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-ranlib"
5987
linker = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc"
6088
```
6189

62-
## Cross-compilation
90+
### Cross-compilation
6391

64-
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.
92+
This target can be cross-compiled on a `x86_64-unknown-linux-gnu` host.
93+
Other hosts are also likely to work, but not actively tested.
94+
95+
You can test the cross build directly on the host, thanks to QEMU linux-user emulation.
96+
An example is given below:
97+
98+
```sh
99+
# Suppose the cross toolchain is placed at $TOOLCHAIN_PATH, with a cross prefix
100+
# of "loongarch64-unknown-linux-gnu-".
101+
export CC_loongarch64_unknown_linux_gnu="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-gcc
102+
export CXX_loongarch64_unknown_linux_gnu="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-g++
103+
export AR_loongarch64_unknown_linux_gnu="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-gcc-ar
104+
export CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNU_LINKER="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-gcc
105+
106+
# Point qemu-loongarch64 to the LoongArch sysroot.
107+
# Suppose the sysroot is located at "sysroot" below the toolchain root:
108+
export CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNU_RUNNER="qemu-loongarch64 -L $TOOLCHAIN_PATH/sysroot"
109+
# Or alternatively, if binfmt_misc is set up for running LoongArch binaries
110+
# transparently:
111+
export QEMU_LD_PREFIX="$TOOLCHAIN_PATH"/sysroot
65112

66-
## Testing
67-
To test a cross-compiled binary on your build system, install the qemu binary that supports the LoongArch architecture and execute the following commands.
68-
```text
69-
CC_loongarch64_unknown_linux_gnu=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc \
70-
CXX_loongarch64_unknown_linux_gnu=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-g++ \
71-
AR_loongarch64_unknown_linux_gnu=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc-ar \
72-
CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNUN_LINKER=/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc \
73-
# SET TARGET SYSTEM LIBRARY PATH
74-
CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNUN_RUNNER="qemu-loongarch64 -L /TOOLCHAIN_PATH/TARGET_LIBRARY_PATH" \
75113
cargo run --target loongarch64-unknown-linux-gnu --release
76114
```
77-
Tested on x86 architecture, other architectures not tested.
78115

79-
## Building Rust programs
116+
## Testing
117+
118+
There are no special requirements for testing and running the targets.
119+
For testing cross builds on the host, please refer to the "Cross-compilation"
120+
section above.
80121

81-
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.
122+
## Building Rust programs
82123

83-
If `rustc` has support for that target and the library artifacts are available, then Rust static libraries can be built for that target:
124+
As the targets are available through `rustup`, it is very easy to build Rust
125+
programs for these targets: same as with other architectures.
126+
Note that you will need a LoongArch C/C++ toolchain for linking, or if you want
127+
to compile C code along with Rust (such as for Rust crates with C dependencies).
84128

85-
```shell
86-
$ rustc --target loongarch64-unknown-linux-gnu your-code.rs --crate-type staticlib
87-
$ ls libyour_code.a
129+
```sh
130+
rustup target add loongarch64-unknown-linux-gnu
131+
cargo build --target loongarch64-unknown-linux-gnu
88132
```
89133

90-
On Rust Nightly it's possible to build without the target artifacts available:
134+
Availability of pre-built artifacts through `rustup` are as follows:
91135

92-
```text
93-
cargo build -Z build-std --target loongarch64-unknown-linux-gnu
94-
```
136+
* `loongarch64-unknown-linux-gnu`: since Rust 1.71;
137+
* `loongarch64-unknown-linux-musl`: since Rust 1.81.

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

+34-22
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
Freestanding/bare-metal LoongArch64 binaries in ELF format: firmware, kernels, etc.
66

7-
| Target | Descriptions |
8-
|------------------------------------|-------------------------------------------------------|
9-
| loongarch64-unknown-none | LoongArch 64-bit, LP64D ABI (freestanding, hardfloat) |
10-
| loongarch64-unknown-none-softfloat | LoongArch 64-bit, LP64S ABI (freestanding, softfloat) |
7+
| Target | Description |
8+
|--------|-------------|
9+
| `loongarch64-unknown-none` | LoongArch 64-bit, LP64D ABI (freestanding, hard-float) |
10+
| `loongarch64-unknown-none-softfloat` | LoongArch 64-bit, LP64S ABI (freestanding, soft-float) |
1111

1212
## Target maintainers
1313

@@ -19,61 +19,73 @@ Freestanding/bare-metal LoongArch64 binaries in ELF format: firmware, kernels, e
1919
This target is cross-compiled. There is no support for `std`. There is no
2020
default allocator, but it's possible to use `alloc` by supplying an allocator.
2121

22+
The `*-softfloat` target does not assume existence of FPU or any other LoongArch
23+
ISA extension, and does not make use of any non-GPR register.
2224
This allows the generated code to run in environments, such as kernels, which
2325
may need to avoid the use of such registers or which may have special considerations
2426
about the use of such registers (e.g. saving and restoring them to avoid breaking
2527
userspace code using the same registers). You can change code generation to use
2628
additional CPU features via the `-C target-feature=` codegen options to rustc, or
2729
via the `#[target_feature]` mechanism within Rust code.
2830

29-
By default, code generated with this target should run on any `loongarch`
30-
hardware; enabling additional target features may raise this baseline.
31+
By default, code generated with the soft-float target should run on any
32+
LoongArch64 hardware, with the hard-float target additionally requiring an FPU;
33+
enabling additional target features may raise this baseline.
3134

32-
Code generated with this target will use the `small` code model by default.
35+
Code generated with the targets will use the `small` code model by default.
3336
You can change this using the `-C code-model=` option to rustc.
3437

35-
On `loongarch64-unknown-none*`, `extern "C"` uses the [standard calling
36-
convention](https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html).
38+
On `loongarch64-unknown-none*`, `extern "C"` uses the [architecture's standard calling convention][lapcs].
3739

38-
This target generates binaries in the ELF format. Any alternate formats or
40+
[lapcs]: https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc
41+
42+
The targets generate binaries in the ELF format. Any alternate formats or
3943
special considerations for binary layout will require linker options or linker
4044
scripts.
4145

4246
## Building the target
4347

44-
You can build Rust with support for the target by adding it to the `target`
48+
You can build Rust with support for the targets by adding them to the `target`
4549
list in `config.toml`:
4650

4751
```toml
4852
[build]
4953
build-stage = 1
50-
target = ["loongarch64-unknown-none"]
54+
target = [
55+
"loongarch64-unknown-none",
56+
"loongarch64-unknown-none-softfloat",
57+
]
5158
```
5259

60+
## Testing
61+
62+
As the targets support a variety of different environments and do not support
63+
`std`, they do not support running the Rust test suite.
64+
5365
## Building Rust programs
5466

55-
```text
67+
Starting with Rust 1.74, precompiled artifacts are provided via `rustup`:
68+
69+
```sh
70+
# install cross-compile toolchain
71+
rustup target add loongarch64-unknown-none
5672
# target flag may be used with any cargo or rustc command
5773
cargo build --target loongarch64-unknown-none
5874
```
5975

60-
## Testing
61-
62-
As `loongarch64-unknown-none*` supports a variety of different environments and does
63-
not support `std`, this target does not support running the Rust test suite.
64-
6576
## Cross-compilation toolchains and C code
6677

67-
If you want to compile C code along with Rust (such as for Rust crates with C
68-
dependencies), you will need an appropriate `loongarch` toolchain.
78+
For cross builds, you will need an appropriate LoongArch C/C++ toolchain for
79+
linking, or if you want to compile C code along with Rust (such as for Rust
80+
crates with C dependencies).
6981

7082
Rust *may* be able to use an `loongarch64-unknown-linux-gnu-` toolchain with
7183
appropriate standalone flags to build for this toolchain (depending on the assumptions
7284
of that toolchain, see below), or you may wish to use a separate
7385
`loongarch64-unknown-none` toolchain.
7486

75-
On some `loongarch` hosts that use ELF binaries, you *may* be able to use the host
87+
On some LoongArch hosts that use ELF binaries, you *may* be able to use the host
7688
C toolchain, if it does not introduce assumptions about the host environment
7789
that don't match the expectations of a standalone environment. Otherwise, you
7890
may need a separate toolchain for standalone/freestanding development, just as
79-
when cross-compiling from a non-`loongarch` platform.
91+
when cross-compiling from a non-LoongArch platform.

0 commit comments

Comments
 (0)