1 # `aarch64-fuchsia` and `x86_64-fuchsia`
5 [Fuchsia] is a modern open source operating system that's simple, secure,
6 updatable, and performant.
12 - Tyler Mandry ([@tmandry](https://github.com/tmandry))
13 - Dan Johnson ([@computerdruid](https://github.com/computerdruid))
14 - David Koloski ([@djkoloski](https://github.com/djkoloski))
15 - Andrew Pollack ([@andrewpollack](https://github.com/andrewpollack))
16 - Joseph Ryan ([@P1n3appl3](https://github.com/P1n3appl3))
18 As the team evolves over time, the specific members listed here may differ from
19 the members reported by the API. The API should be considered to be
20 authoritative if this occurs. Instead of pinging individual members, use
21 `@rustbot ping fuchsia` to contact the team on GitHub.
25 1. [Requirements](#requirements)
26 1. [Walkthrough structure](#walkthrough-structure)
27 1. [Compiling a Rust binary targeting Fuchsia](#compiling-a-rust-binary-targeting-fuchsia)
28 1. [Targeting Fuchsia with rustup and cargo](#targeting-fuchsia-with-rustup-and-cargo)
29 1. [Targeting Fuchsia with a compiler built from source](#targeting-fuchsia-with-a-compiler-built-from-source)
30 1. [Creating a Fuchsia package](#creating-a-fuchsia-package)
31 1. [Creating a Fuchsia component](#creating-a-fuchsia-component)
32 1. [Building a Fuchsia package](#building-a-fuchsia-package)
33 1. [Publishing a Fuchsia package](#publishing-a-fuchsia-package)
34 1. [Creating a Fuchsia package repository](#creating-a-fuchsia-package-repository)
35 1. [Publishing Fuchsia package to repository](#publishing-fuchsia-package-to-repository)
36 1. [Running a Fuchsia component on an emulator](#running-a-fuchsia-component-on-an-emulator)
37 1. [Starting the Fuchsia emulator](#starting-the-fuchsia-emulator)
38 1. [Watching emulator logs](#watching-emulator-logs)
39 1. [Serving a Fuchsia package](#serving-a-fuchsia-package)
40 1. [Running a Fuchsia component](#running-a-fuchsia-component)
41 1. [`.gitignore` extensions](#gitignore-extensions)
42 1. [Testing](#testing)
43 1. [Running unit tests](#running-unit-tests)
44 1. [Running the compiler test suite](#running-the-compiler-test-suite)
48 This target is cross-compiled from a host environment. You will need a recent
49 copy of the [Fuchsia SDK], which provides the tools, libraries, and binaries
50 required to build and link programs for Fuchsia.
52 Development may also be done from the [source tree].
54 Fuchsia targets support `std` and follow the `sysv64` calling convention on
55 x86_64. Fuchsia binaries use the ELF file format.
57 ## Walkthrough structure
59 This walkthrough will cover:
61 1. Compiling a Rust binary targeting Fuchsia.
62 1. Building a Fuchsia package.
63 1. Publishing and running a Fuchsia package to a Fuchsia emulator.
65 For the purposes of this walkthrough, we will only target `x86_64-fuchsia`.
67 ## Compiling a Rust binary targeting Fuchsia
69 Today, there are two main ways to build a Rust binary targeting Fuchsia
70 using the Fuchsia SDK:
71 1. Allow [rustup] to handle the installation of Fuchsia targets for you.
72 1. Build a toolchain locally that can target Fuchsia.
74 ### Targeting Fuchsia with rustup and cargo
76 The easiest way to build a Rust binary targeting Fuchsia is by allowing [rustup]
77 to handle the installation of Fuchsia targets for you. This can be done by issuing
78 the following commands:
81 rustup target add x86_64-fuchsia
82 rustup target add aarch_64-fuchsia
85 After installing our Fuchsia targets, we can now compile a Rust binary that targets
88 To create our Rust project, we can issue a standard `cargo` command as follows:
90 **From base working directory**
92 cargo new hello_fuchsia
95 The rest of this walkthrough will take place from `hello_fuchsia`, so we can
96 change into that directory now:
102 *Note: From this point onwards, all commands will be issued from the `hello_fuchsia/`
103 directory, and all `hello_fuchsia/` prefixes will be removed from references for sake of brevity.*
105 We can edit our `src/main.rs` to include a test as follows:
110 println!("Hello Fuchsia!");
115 assert_eq!(2 + 2, 4);
119 In addition to the standard workspace created, we will want to create a
120 `.cargo/config.toml` file to link necessary libraries
123 **`.cargo/config.toml`**
125 [target.x86_64-fuchsia]
128 "-Lnative", "<SDK_PATH>/arch/x64/sysroot/lib",
129 "-Lnative", "<SDK_PATH>/arch/x64/lib"
133 *Note: Make sure to fill out `<SDK_PATH>` with the path to the downloaded [Fuchsia SDK].*
135 In total, our new project will look like:
137 **Current directory structure**
147 Finally, we can build our rust binary as:
150 cargo build --target x86_64-fuchsia
153 Now we have a Rust binary at `target/x86_64-fuchsia/debug/hello_fuchsia`,
154 targeting our desired Fuchsia target.
156 **Current directory structure**
170 ### Targeting Fuchsia with a compiler built from source
172 An alternative to the first workflow is to target Fuchsia by using
173 `rustc` built from source.
175 Before building Rust for Fuchsia, you'll need a clang toolchain that supports
176 Fuchsia as well. A recent version (14+) of clang should be sufficient to compile
179 x86-64 and AArch64 Fuchsia targets can be enabled using the following
182 In `config.toml`, add:
186 target = ["<host_platform>", "aarch64-fuchsia", "x86_64-fuchsia"]
189 Additionally, the following environment variables must be configured (for
190 example, using a script like `config-env.sh`):
193 # Configure this environment variable to be the path to the downloaded SDK
194 export SDK_PATH="<SDK path goes here>"
196 export CFLAGS_aarch64_fuchsia="--target=aarch64-fuchsia --sysroot=${SDK_PATH}/arch/arm64/sysroot -I${SDK_PATH}/pkg/fdio/include"
197 export CXXFLAGS_aarch64_fuchsia="--target=aarch64-fuchsia --sysroot=${SDK_PATH}/arch/arm64/sysroot -I${SDK_PATH}/pkg/fdio/include"
198 export LDFLAGS_aarch64_fuchsia="--target=aarch64-fuchsia --sysroot=${SDK_PATH}/arch/arm64/sysroot -L${SDK_PATH}/arch/arm64/lib"
199 export CARGO_TARGET_AARCH64_FUCHSIA_RUSTFLAGS="-C link-arg=--sysroot=${SDK_PATH}/arch/arm64/sysroot -Lnative=${SDK_PATH}/arch/arm64/sysroot/lib -Lnative=${SDK_PATH}/arch/arm64/lib"
200 export CFLAGS_x86_64_fuchsia="--target=x86_64-fuchsia --sysroot=${SDK_PATH}/arch/x64/sysroot -I${SDK_PATH}/pkg/fdio/include"
201 export CXXFLAGS_x86_64_fuchsia="--target=x86_64-fuchsia --sysroot=${SDK_PATH}/arch/x64/sysroot -I${SDK_PATH}/pkg/fdio/include"
202 export LDFLAGS_x86_64_fuchsia="--target=x86_64-fuchsia --sysroot=${SDK_PATH}/arch/x64/sysroot -L${SDK_PATH}/arch/x64/lib"
203 export CARGO_TARGET_X86_64_FUCHSIA_RUSTFLAGS="-C link-arg=--sysroot=${SDK_PATH}/arch/x64/sysroot -Lnative=${SDK_PATH}/arch/x64/sysroot/lib -Lnative=${SDK_PATH}/arch/x64/lib"
206 These can be run together in a shell environment by executing
207 `(source config-env.sh && ./x.py install)`.
209 Once `rustc` is installed, we can create a new working directory to work from,
210 `hello_fuchsia` along with `hello_fuchsia/src`:
218 *Note: From this point onwards, all commands will be issued from the `hello_fuchsia/`
219 directory, and all `hello_fuchsia/` prefixes will be removed from references for sake of brevity.*
221 There, we can create a new file named `src/hello_fuchsia.rs`:
223 **`src/hello_fuchsia.rs`**
226 println!("Hello Fuchsia!");
231 assert_eq!(2 + 2, 4);
235 **Current directory structure**
242 Using your freshly installed `rustc`, you can compile a binary for Fuchsia using
243 the following options:
245 * `--target x86_64-fuchsia`/`--target aarch64-fuchsia`: Targets the Fuchsia
246 platform of your choice
247 * `-Lnative ${SDK_PATH}/arch/${ARCH}/lib`: Link against Fuchsia libraries from
249 * `-Lnative ${SDK_PATH}/arch/${ARCH}/sysroot/lib`: Link against Fuchsia kernel
250 libraries from the SDK
252 Putting it all together:
255 # Configure these for the Fuchsia target of your choice
256 TARGET_ARCH="<x86_64-fuchsia|aarch64-fuchsia>"
260 --target ${TARGET_ARCH} \
261 -Lnative=${SDK_PATH}/arch/${ARCH}/lib \
262 -Lnative=${SDK_PATH}/arch/${ARCH}/sysroot/lib \
263 --out-dir bin src/hello_fuchsia.rs
266 **Current directory structure**
270 ┃ ┗━ hello_fuchsia.rs
275 ## Creating a Fuchsia package
277 Before moving on, double check your directory structure:
279 **Current directory structure**
282 ┣━ src/ (if using rustc)
283 ┃ ┗━ hello_fuchsia.rs ...
285 ┃ ┗━ hello_fuchsia ...
286 ┣━ src/ (if using cargo)
289 ┗━ x86_64-fuchsia/ ...
294 With our Rust binary built, we can move to creating a Fuchsia package.
295 On Fuchsia, a package is the unit of distribution for software. We'll need to
296 create a new package directory where we will place files like our finished
297 binary and any data it may need.
299 To start, make the `pkg`, and `pkg/meta` directories:
306 **Current directory structure**
313 Now, create the following files inside:
315 **`pkg/meta/package`**
318 "name": "hello_fuchsia",
323 The `package` file describes our package's name and version number. Every
324 package must contain one.
326 **`pkg/hello_fuchsia.manifest`**
328 bin/hello_fuchsia=target/x86_64-fuchsia/debug/hello_fuchsia # If using cargo...
329 bin/hello_fuchsia=bin/hello_fuchsia # If using rustc...
330 lib/ld.so.1=<SDK_PATH>/arch/x64/sysroot/dist/lib/ld.so.1
331 lib/libfdio.so=<SDK_PATH>/arch/x64/dist/libfdio.so
332 meta/package=pkg/meta/package
333 meta/hello_fuchsia.cm=pkg/meta/hello_fuchsia.cm
336 *Note: Relative manifest paths are resolved starting from the working directory
337 of `pm`. Make sure to fill out `<SDK_PATH>` with the path to the downloaded
340 The `.manifest` file will be used to describe the contents of the package by
341 relating their location when installed to their location on the file system. The
342 `bin/hello_fuchsia=` entry will be different depending on how your Rust binary
343 was built, so choose accordingly.
345 **Current directory structure**
351 ┗━ hello_fuchsia.manifest
354 ### Creating a Fuchsia component
356 On Fuchsia, components require a component manifest written in Fuchsia's markup
357 language called CML. The Fuchsia devsite contains an [overview of CML] and a
358 [reference for the file format]. Here's a basic one that can run our single binary:
360 **`pkg/hello_fuchsia.cml`**
363 include: [ "syslog/client.shard.cml" ],
366 binary: "bin/hello_fuchsia",
376 ┣━ hello_fuchsia.manifest
380 Now we can compile that CML into a component manifest:
383 ${SDK_PATH}/tools/${ARCH}/cmc compile \
384 pkg/hello_fuchsia.cml \
385 --includepath ${SDK_PATH}/pkg \
386 -o pkg/meta/hello_fuchsia.cm
389 **Current directory structure**
395 ┃ ┗━ hello_fuchsia.cm
396 ┣━ hello_fuchsia.manifest
400 *Note: `--includepath` tells the compiler where to look for `include`s from our CML.
401 In our case, we're only using `syslog/client.shard.cml`.*
403 ### Building a Fuchsia package
405 Next, we'll build a package manifest as defined by our manifest:
408 ${SDK_PATH}/tools/${ARCH}/pm \
409 -o hello_fuchsia_manifest \
410 -m pkg/hello_fuchsia.manifest \
412 -output-package-manifest hello_fuchsia_package_manifest
415 This will produce `pkg/hello_fuchsia_manifest/` which is a package manifest we can
416 publish directly to a repository.
418 **Current directory structure**
424 ┃ ┗━ hello_fuchsia.cm
425 ┣━ hello_fuchsia_manifest/
427 ┣━ hello_fuchsia.manifest
429 ┗━ hello_fuchsia_package_manifest
432 We are now ready to publish the package.
434 ## Publishing a Fuchsia package
436 With our package and component manifests setup,
437 we can now publish our package. The first step will
438 be to create a Fuchsia package repository to publish
441 ### Creating a Fuchsia package repository
443 We can set up our repository with:
446 ${SDK_PATH}/tools/${ARCH}/pm newrepo \
450 **Current directory structure**
456 ┃ ┗━ hello_fuchsia.cm
457 ┣━ hello_fuchsia_manifest/
461 ┣━ hello_fuchsia.manifest
463 ┗━ hello_fuchsia_package_manifest
466 ## Publishing Fuchsia package to repository
468 We can publish our new package to that repository with:
471 ${SDK_PATH}/tools/${ARCH}/pm publish \
473 -lp -f <(echo "hello_fuchsia_package_manifest")
476 Then we can add the repository to `ffx`'s package server as `hello-fuchsia` using:
479 ${SDK_PATH}/tools/${ARCH}/ffx repository add-from-pm \
484 ## Running a Fuchsia component on an emulator
486 At this point, we are ready to run our Fuchsia
487 component. For reference, our final directory
488 structure will look like:
490 **Final directory structure**
493 ┣━ src/ (if using rustc)
494 ┃ ┗━ hello_fuchsia.rs ...
496 ┃ ┗━ hello_fuchsia ...
497 ┣━ src/ (if using cargo)
500 ┃ ┗━ x86_64-fuchsia/ ...
502 ┃ ┗━ hello_fuchsia ...
506 ┃ ┗━ hello_fuchsia.cm
507 ┣━ hello_fuchsia_manifest/
511 ┣━ hello_fuchsia.manifest
513 ┗━ hello_fuchsia_package_manifest
516 ### Starting the Fuchsia emulator
518 Start a Fuchsia emulator in a new terminal using:
521 ${SDK_PATH}/tools/${ARCH}/ffx product-bundle get workstation_eng.qemu-${ARCH}
522 ${SDK_PATH}/tools/${ARCH}/ffx emu start workstation_eng.qemu-${ARCH} --headless
525 ### Watching emulator logs
527 Once the emulator is running, open a separate terminal to watch the emulator logs:
529 **In separate terminal**
531 ${SDK_PATH}/tools/${ARCH}/ffx log \
535 ### Serving a Fuchsia package
537 Now, start a package repository server to serve our
538 package to the emulator:
541 ${SDK_PATH}/tools/${ARCH}/ffx repository server start
544 Once the repository server is up and running, register it with the target Fuchsia system running in the emulator:
547 ${SDK_PATH}/tools/${ARCH}/ffx target repository register \
548 --repository hello-fuchsia
551 ### Running a Fuchsia component
553 Finally, run the component:
556 ${SDK_PATH}/tools/${ARCH}/ffx component run \
557 fuchsia-pkg://hello-fuchsia/hello_fuchsia_manifest#meta/hello_fuchsia.cm
560 On reruns of the component, the `--recreate` argument may also need to be
564 ${SDK_PATH}/tools/${ARCH}/ffx component run \
566 fuchsia-pkg://hello-fuchsia/hello_fuchsia_manifest#meta/hello_fuchsia.cm
569 ## `.gitignore` extensions
571 Optionally, we can create/extend our `.gitignore` file to ignore files and
572 directories that are not helpful to track:
576 pkg/meta/hello_fuchsia.cm
577 pkg/hello_fuchsia_manifest
578 pkg/hello_fuchsia_package_manifest
583 ### Running unit tests
585 Tests can be run in the same way as a regular binary.
587 * If using `cargo`, you can simply pass `test --no-run`
588 to the `cargo` invocation and then repackage and rerun the Fuchsia package. From our previous example,
589 this would look like `cargo test --target x86_64-fuchsia --no-run`, and moving the executable
590 binary path found from the line `Executable unittests src/main.rs (target/x86_64-fuchsia/debug/deps/hello_fuchsia-<HASH>)`
591 into `pkg/hello_fuchsia.manifest`.
593 * If using the compiled `rustc`, you can simply pass `--test`
594 to the `rustc` invocation and then repackage and rerun the Fuchsia package.
596 The test harness will run the applicable unit tests.
598 Often when testing, you may want to pass additional command line arguments to
599 your binary. Additional arguments can be set in the component manifest:
601 **`pkg/hello_fuchsia.cml`**
604 include: [ "syslog/client.shard.cml" ],
607 binary: "bin/hello_fuchsia",
613 This will pass the argument `it_works` to the binary, filtering the tests to
614 only those tests that match the pattern. There are many more configuration
615 options available in CML including environment variables. More documentation is
616 available on the [Fuchsia devsite].
618 ### Running the compiler test suite
620 Running the Rust test suite on Fuchsia is [not currently supported], but work is
621 underway to enable it.
623 [Fuchsia team]: https://team-api.infra.rust-lang.org/v1/teams/fuchsia.json
624 [Fuchsia]: https://fuchsia.dev/
625 [source tree]: https://fuchsia.dev/fuchsia-src/get-started/learn/build
626 [rustup]: https://rustup.rs/
627 [cargo]: https://doc.rust-lang.org/cargo/
628 [Fuchsia SDK]: https://chrome-infra-packages.appspot.com/p/fuchsia/sdk/core
629 [overview of CML]: https://fuchsia.dev/fuchsia-src/concepts/components/v2/component_manifests
630 [reference for the file format]: https://fuchsia.dev/reference/cml
631 [Fuchsia devsite]: https://fuchsia.dev/reference/cml
632 [not currently supported]: https://fxbug.dev/105393