Skip to content

Commit f668785

Browse files
committed
Add basic steps for a new target
1 parent 6353694 commit f668785

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
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

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

0 commit comments

Comments
 (0)