Skip to content

Commit 5bde3ef

Browse files
Add basic steps for a new target (#805)
Co-authored-by: Yuki Okushi <[email protected]>
1 parent eeca375 commit 5bde3ef

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Documenting Compiler](./building/compiler-documenting.md)
1717
- [Rustdoc](./rustdoc.md)
1818
- [ctags](./building/ctags.md)
19+
- [Adding a new target](./building/new-target.md)
1920
- [The compiler testing framework](./tests/intro.md)
2021
- [Running tests](./tests/running.md)
2122
- [Adding new tests](./tests/adding.md)

src/building/new-target.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Adding a new target
2+
3+
These are a set of steps to add support for a new target. There are
4+
numerous end states and paths to get there, so not all sections may be
5+
relevant to your desired goal.
6+
7+
## Specifying a new LLVM
8+
9+
For very new targets, you may need to use a different fork of LLVM
10+
than what is currently shipped with Rust. In that case, navigate to
11+
the `src/llvm_project` git submodule (you might need to run `x.py
12+
check` at least once so the submodule is updated), check out the
13+
appropriate commit for your fork, then commit that new submodule
14+
reference in the main Rust repository.
15+
16+
An example would be:
17+
18+
```
19+
cd src/llvm_project
20+
git remote add my-target-llvm some-llvm-repository
21+
git checkout my-target-llvm/my-branch
22+
cd ..
23+
git add llvm_target
24+
git commit -m 'Use my custom LLVM'
25+
```
26+
27+
If you have a local LLVM checkout that is already built, you *may* be
28+
able to configure Rust to treat your build as the [system
29+
LLVM][sysllvm] to avoid redundant builds.
30+
31+
[sysllvm]: ./suggested.md#building-with-system-llvm
32+
33+
## Creating a target specification
34+
35+
You should start with a target JSON file. You can see the specification
36+
for an existing target using `--print target-spec-json`:
37+
38+
```
39+
rustc -Z unstable-options --target=wasm32-unknown-unknown --print target-spec-json
40+
```
41+
42+
Save that JSON to a file and modify it as appropriate for your target.
43+
44+
### Adding a target specification
45+
46+
Once you have filled out a JSON specification and been able to compile
47+
somewhat successfully, you can copy the specification into the
48+
compiler itself.
49+
50+
You will need to add a line to the big table inside of the
51+
`supported_targets` macro in the `librustc_target::spec` module. You
52+
will then add a corresponding file for your new target containing a
53+
`target` function.
54+
55+
Look for existing targets to use as examples
56+
57+
## Patching crates
58+
59+
You may need to make changes to crates that the compiler depends on,
60+
such as [`libc`][] or [`cc`][]. If so, you can use Cargo's
61+
[`[patch]`][patch] ability. For example, if you want to use an
62+
unreleased version of `libc`, you can add it to the top-level
63+
`Cargo.toml` file:
64+
65+
```diff
66+
diff --git a/Cargo.toml b/Cargo.toml
67+
index be15e50e2bc..4fb1248ba99 100644
68+
--- a/Cargo.toml
69+
+++ b/Cargo.toml
70+
@@ -66,10 +66,11 @@ cargo = { path = "src/tools/cargo" }
71+
[patch.crates-io]
72+
# Similar to Cargo above we want the RLS to use a vendored version of `rustfmt`
73+
# that we're shipping as well (to ensure that the rustfmt in RLS and the
74+
# `rustfmt` executable are the same exact version).
75+
rustfmt-nightly = { path = "src/tools/rustfmt" }
76+
+libc = { git = "https://github.com/rust-lang/libc", rev = "0bf7ce340699dcbacabdf5f16a242d2219a49ee0" }
77+
78+
# See comments in `src/tools/rustc-workspace-hack/README.md` for what's going on
79+
# here
80+
rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' }
81+
```
82+
83+
After this, run `cargo update -p libc` to update the lockfiles.
84+
85+
[`libc`]: https://crates.io/crates/libc
86+
[`cc`]: https://crates.io/crates/cc
87+
[patch]: https://doc.rust-lang.org/stable/cargo/reference/overriding-dependencies.html#the-patch-section
88+
89+
## Cross-compiling
90+
91+
Once you have a target specification in JSON and in the code, you can
92+
cross-compile `rustc`:
93+
94+
```
95+
DESTDIR=/path/to/install/in \
96+
./x.py install -i --stage 1 --host aarch64-apple-darwin.json --target aarch64-apple-darwin \
97+
src/librustc src/libstd
98+
```
99+
100+
If your target specification is already available in the bootstrap
101+
compiler, you can use it instead of the JSON file for both arguments.

0 commit comments

Comments
 (0)