1# Fuchsia Self-Hosted Build
2## Build Fuchsia Native Tools
3* If not already cloned, check out the `gcc_none_toolchains` repository.
4```bash
5git clone https://fuchsia.googlesource.com/third_party/gcc_none_toolchains
6```
7* Build the Fuchsia-native tools.
8```bash
9cd <gcc_none_toolchains-dir>
10./do-build \
11	--host x86_64-fuchsia \
12	--target x86_64-fuchsia \
13	--sysroot <path-to-target-sysroot> \
14	--strip
15```
16By default, this will leave the compiler in the
17`x86_64-fuchsia-<gcc-ver>-native` subdirectory of the current directory.
18
19## Build Fuchsia-Hosted Bare-Metal Tools
20* In the same `gcc_none_toolchains` repository, build the fuchsia-hosted
21bare-metal tools:
22```bash
23./do-build \
24	--host x86_64-fuchsia \
25	--target x86_64-none \
26	--sysroot <path-to-target-sysroot> \
27	--strip
28```
29By default, this will leave the compiler in the
30`x86_64-elf-<gcc-ver>-Fuchsia-x86_64` subdirectory of the current directory.
31
32## Build GNU make
33* Checkout make sources, if needed:
34```bash
35git clone https://fuchsia.googlesource.com/third_party/make
36```
37
38* Follow the instructions provided in README.md of the GNU make sources
39to build a Fuchsia-hosted version of make.
40
41## Build utilities (Shouldn't be necessary)
42The following utilities are required by the build scripts. As long as your
43build manifest included the sbase project, these should have been installed
44with your Fuchsia build:
45```text
46uname
47which
48tr
49find
50mv
51cmp
52sort
53basename
54sed
55```
56
57## Build Zircon
58Follow standard Zircon/Fuchsia source configuration instructions. Note that
59you will want a minimum of `zircon` and `sbase`.
60
61In order for gcc-built executables to run in Fuchsia, we will need the gcc
62runtime libraries, built for Fuchsia. They were built as part of the Fuchsia
63native build, but we need them to be installed into one of the standard runtime
64library locations on the target. One way to do this is to add the libraries to
65the manifest lines in `zircon/kernel/engine.mk`:
66```code
67USER_MANIFEST_LINES += lib/libgcc_s.so.1=<path-to-gcc>/x86_64-fuchsia-6.3.0-native/lib/libgcc_s.so.1
68USER_MANIFEST_LINES += lib/libgcc_s.so=<path-to-gcc>/x86_64-fuchsia-6.3.0-native/lib/libgcc_s.so
69USER_MANIFEST_LINES += lib/libstdc++.so=<path-to-gcc>/x86_64-fuchsia-6.3.0-native/lib/libstdc++.so
70```
71Follow the standard Zircon/Fuchsia build instructions, and run the resulting
72image on the desired target.
73
74## Copy Files Onto Target
75Create a new empty directory in the target environment (for this example, we'll
76use /data). For your own sanity, this should be persistent storage of some
77sort. For this example, we'll use the following /data subdirectories:
78```text
79bin             Directory for miscellaneous tools not provided in Fuchsia or Zircon
80gcc             Native gcc installation
81gcc-bare-metal  Fuchsia-hosted bare-metal tools
82zircon         The Zircon source files
83sysroot         The sysroot of the installed Fuchsia
84```
85* Netcp all native gcc files into `/data/gcc`
86```bash
87cd <gcc-install-dir>/x86_64-fuchsia-<gcc-ver>-native
88for filename in `find . -type f`
89do
90  echo "Copying $filename"
91  netcp "$filename" ":/data/gcc/$filename"
92done
93```
94* Netcp all bare-metal gcc files into `/data/gcc-bare-metal`
95```bash
96cd <gcc-install-dir>/x86_64-elf-gcc-<gcc-ver>-Fuchsia-x86_64
97for filename in `find . -type f`
98do
99  echo "Copying $filename"
100  netcp "$filename" ":/data/gcc-bare-metal/$filename"
101done
102```
103* Netcp make to `/data/bin`
104```bash
105netcp <make-build-dir>/make :/data/bin
106```
107* Copy the zircon source code onto the target, either using netcp or a mounted
108image, or git. Note that if you are using the same source tree as was used to
109build the target image, you'll want to remove any modifications to
110kernel/engine.mk that were made for previous steps (or update them to reflect
111the target location of these files), otherwise the build will fail when trying
112to generate the bootfs image.
113```bash
114cd <path-to-zircon>
115for filename in `find . -name .git -prune \
116                        -o -path "./build-*" -prune \
117                        -o -path "./prebuilt*" \
118                        -o -type f -print`
119do
120  echo "Copying $filename"
121  netcp $filename :/data/zircon/$filename
122done
123```
124* Copy the Zircon sysroot onto the target device in `/data/sysroot`
125```bash
126cd <zircon-sysroot>
127for filename in `find . -type f`
128do
129  echo "Copying $filename"
130  netcp $filename :/data/sysroot/$filename
131done
132```
133* Copy the compiler shared libraries into `/data/sysroot/lib`
134```bash
135cd <path-to-gcc>/x86_64-fuchsia-<gcc-ver>-native
136for filename in libstdc++.so libgcc_s.so libgcc_s.so.1
137do
138  echo "Copying $filename"
139  netcp lib/$filename :/data/sysroot/lib/$filename
140done
141```
142
143## Build Zircon
144On the Fuchsia target, add `/data/bin`, `/data/gcc/bin`, and `data/gcc-bare-metal/bin` to your `PATH`
145```bash
146export PATH="$PATH:/data/bin:/data/gcc/bin:/data/gcc-bare-metal/bin"
147```
148* Finally, start the build in the zircon directory:
149```bash
150cd zircon
151make \
152    HOST_SYSROOT=/data/sysroot \
153    ARCH_x86_64_TOOLCHAIN_PREFIX="x86_64-elf-" \
154    HOST_TOOLCHAIN_PREFIX=""
155```
156