blob: 3103bc9b735d7879562de9e354763df1a50f6f55 [file] [log] [blame] [edit]
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Adding a new target - Rust Compiler Development Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="A guide to developing the Rust compiler (rustc)">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="../pagetoc.css">
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../";
const default_light_theme = "light";
const default_dark_theme = "navy";
window.path_to_searchindex_js = "../searchindex.js";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd></kbd> or <kbd></kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
sidebar_toggle.checked = false;
}
if (sidebar === 'visible') {
sidebar_toggle.checked = true;
} else {
html.classList.remove('sidebar-visible');
}
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Rust Compiler Development Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide/edit/main/src/building/new-target.md" title="Suggest an edit" aria-label="Suggest an edit" rel="edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<div class="search-wrapper">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
<div class="spinner-wrapper">
<i class="fa fa-spinner fa-spin"></i>
</div>
</div>
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="adding-a-new-target"><a class="header" href="#adding-a-new-target">Adding a new target</a></h1>
<p>These are a set of steps to add support for a new target. There are
numerous end states and paths to get there, so not all sections may be
relevant to your desired goal.</p>
<p>See also the associated documentation in the <a href="https://doc.rust-lang.org/rustc/target-tier-policy.html#adding-a-new-target">target tier policy</a>.</p>
<h2 id="specifying-a-new-llvm"><a class="header" href="#specifying-a-new-llvm">Specifying a new LLVM</a></h2>
<p>For very new targets, you may need to use a different fork of LLVM
than what is currently shipped with Rust. In that case, navigate to
the <code>src/llvm-project</code> git submodule (you might need to run <code>./x check</code> at least once so the submodule is updated), check out the
appropriate commit for your fork, then commit that new submodule
reference in the main Rust repository.</p>
<p>An example would be:</p>
<pre><code>cd src/llvm-project
git remote add my-target-llvm some-llvm-repository
git checkout my-target-llvm/my-branch
cd ..
git add llvm-project
git commit -m 'Use my custom LLVM'
</code></pre>
<h3 id="using-pre-built-llvm"><a class="header" href="#using-pre-built-llvm">Using pre-built LLVM</a></h3>
<p>If you have a local LLVM checkout that is already built, you may be
able to configure Rust to treat your build as the system LLVM to avoid
redundant builds.</p>
<p>You can tell Rust to use a pre-built version of LLVM using the <code>target</code> section
of <code>bootstrap.toml</code>:</p>
<pre><code class="language-toml">[target.x86_64-unknown-linux-gnu]
llvm-config = "/path/to/llvm/llvm-7.0.1/bin/llvm-config"
</code></pre>
<p>If you are attempting to use a system LLVM, we have observed the following paths
before, though they may be different from your system:</p>
<ul>
<li><code>/usr/bin/llvm-config-8</code></li>
<li><code>/usr/lib/llvm-8/bin/llvm-config</code></li>
</ul>
<p>Note that you need to have the LLVM <code>FileCheck</code> tool installed, which is used
for codegen tests. This tool is normally built with LLVM, but if you use your
own preinstalled LLVM, you will need to provide <code>FileCheck</code> in some other way.
On Debian-based systems, you can install the <code>llvm-N-tools</code> package (where <code>N</code>
is the LLVM version number, e.g. <code>llvm-8-tools</code>). Alternately, you can specify
the path to <code>FileCheck</code> with the <code>llvm-filecheck</code> config item in <code>bootstrap.toml</code>
or you can disable codegen test with the <code>codegen-tests</code> item in <code>bootstrap.toml</code>.</p>
<h2 id="creating-a-target-specification"><a class="header" href="#creating-a-target-specification">Creating a target specification</a></h2>
<p>You should start with a target JSON file. You can see the specification
for an existing target using <code>--print target-spec-json</code>:</p>
<pre><code>rustc -Z unstable-options --target=wasm32-unknown-unknown --print target-spec-json
</code></pre>
<p>Save that JSON to a file and modify it as appropriate for your target.</p>
<h3 id="adding-a-target-specification"><a class="header" href="#adding-a-target-specification">Adding a target specification</a></h3>
<p>Once you have filled out a JSON specification and been able to compile
somewhat successfully, you can copy the specification into the
compiler itself.</p>
<p>You will need to add a line to the big table inside of the
<code>supported_targets</code> macro in the <code>rustc_target::spec</code> module. You
will then add a corresponding file for your new target containing a
<code>target</code> function.</p>
<p>Look for existing targets to use as examples.</p>
<p>After adding your target to the <code>rustc_target</code> crate you may want to add
<code>core</code>, <code>std</code>, ... with support for your new target. In that case you will
probably need access to some <code>target_*</code> cfg. Unfortunately when building with
stage0 (a precompiled compiler), you'll get an error that the target cfg is
unexpected because stage0 doesn't know about the new target specification and
we pass <code>--check-cfg</code> in order to tell it to check.</p>
<p>To fix the errors you will need to manually add the unexpected value to the
different <code>Cargo.toml</code> in <code>library/{std,alloc,core}/Cargo.toml</code>. Here is an
example for adding <code>NEW_TARGET_ARCH</code> as <code>target_arch</code>:</p>
<p><em><code>library/std/Cargo.toml</code></em>:</p>
<pre><code class="language-diff"> [lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(bootstrap)',
- 'cfg(target_arch, values("xtensa"))',
+ # #[cfg(bootstrap)] NEW_TARGET_ARCH
+ 'cfg(target_arch, values("xtensa", "NEW_TARGET_ARCH"))',
</code></pre>
<p>To use this target in bootstrap, we need to explicitly add the target triple to the <code>STAGE0_MISSING_TARGETS</code>
list in <code>src/bootstrap/src/core/sanity.rs</code>. This is necessary because the default compiler bootstrap uses does
not recognize the new target we just added. Therefore, it should be added to <code>STAGE0_MISSING_TARGETS</code> so that the
bootstrap is aware that this target is not yet supported by the stage0 compiler.</p>
<pre><code class="language-diff">const STAGE0_MISSING_TARGETS: &amp;[&amp;str] = &amp;[
+ "NEW_TARGET_TRIPLE"
];
</code></pre>
<h2 id="patching-crates"><a class="header" href="#patching-crates">Patching crates</a></h2>
<p>You may need to make changes to crates that the compiler depends on,
such as <a href="https://crates.io/crates/libc"><code>libc</code></a> or <a href="https://crates.io/crates/cc"><code>cc</code></a>. If so, you can use Cargo's
<a href="https://doc.rust-lang.org/stable/cargo/reference/overriding-dependencies.html#the-patch-section"><code>[patch]</code></a> ability. For example, if you want to use an
unreleased version of <code>libc</code>, you can add it to the top-level
<code>Cargo.toml</code> file:</p>
<pre><code class="language-diff">diff --git a/Cargo.toml b/Cargo.toml
index 1e83f05e0ca..4d0172071c1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -113,6 +113,8 @@ cargo-util = { path = "src/tools/cargo/crates/cargo-util" }
[patch.crates-io]
+libc = { git = "https://github.com/rust-lang/libc", rev = "0bf7ce340699dcbacabdf5f16a242d2219a49ee0" }
# See comments in `src/tools/rustc-workspace-hack/README.md` for what's going on
# here
rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' }
</code></pre>
<p>After this, run <code>cargo update -p libc</code> to update the lockfiles.</p>
<p>Beware that if you patch to a local <code>path</code> dependency, this will enable
warnings for that dependency. Some dependencies are not warning-free, and due
to the <code>deny-warnings</code> setting in <code>bootstrap.toml</code>, the build may suddenly start
to fail.
To work around warnings, you may want to:</p>
<ul>
<li>Modify the dependency to remove the warnings</li>
<li>Or for local development purposes, suppress the warnings by setting deny-warnings = false in bootstrap.toml.</li>
</ul>
<pre><code class="language-toml"># bootstrap.toml
[rust]
deny-warnings = false
</code></pre>
<h2 id="cross-compiling"><a class="header" href="#cross-compiling">Cross-compiling</a></h2>
<p>Once you have a target specification in JSON and in the code, you can
cross-compile <code>rustc</code>:</p>
<pre><code>DESTDIR=/path/to/install/in \
./x install -i --stage 1 --host aarch64-apple-darwin.json --target aarch64-apple-darwin \
compiler/rustc library/std
</code></pre>
<p>If your target specification is already available in the bootstrap
compiler, you can use it instead of the JSON file for both arguments.</p>
<h2 id="promoting-a-target-from-tier-2-target-to-tier-2-host"><a class="header" href="#promoting-a-target-from-tier-2-target-to-tier-2-host">Promoting a target from tier 2 (target) to tier 2 (host)</a></h2>
<p>There are two levels of tier 2 targets:</p>
<ul>
<li>Targets that are only cross-compiled (<code>rustup target add</code>)</li>
<li>Targets that <a href="https://doc.rust-lang.org/nightly/rustc/target-tier-policy.html#tier-2-with-host-tools">have a native toolchain</a> (<code>rustup toolchain install</code>)</li>
</ul>
<p>For an example of promoting a target from cross-compiled to native,
see <a href="https://github.com/rust-lang/rust/pull/75914">#75914</a>.</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../rustdoc.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../building/optimized-build.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../rustdoc.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../building/optimized-build.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
<script src="../mermaid.min.js"></script>
<script src="../mermaid-init.js"></script>
<script src="../pagetoc.js"></script>
</div>
</body>
</html>