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)
45 1. [Debugging](#debugging)
47 1. [Attaching `zxdb`](#attaching-zxdb)
48 1. [Using `zxdb`](#using-zxdb)
49 1. [Displaying source code in `zxdb`](#displaying-source-code-in-zxdb)
53 This target is cross-compiled from a host environment. You will need a recent
54 copy of the [Fuchsia SDK], which provides the tools, libraries, and binaries
55 required to build and link programs for Fuchsia.
57 Development may also be done from the [source tree].
59 Fuchsia targets support `std` and follow the `sysv64` calling convention on
60 x86_64. Fuchsia binaries use the ELF file format.
62 ## Walkthrough structure
64 This walkthrough will cover:
66 1. Compiling a Rust binary targeting Fuchsia.
67 1. Building a Fuchsia package.
68 1. Publishing and running a Fuchsia package to a Fuchsia emulator.
70 For the purposes of this walkthrough, we will only target `x86_64-fuchsia`.
72 ## Compiling a Rust binary targeting Fuchsia
74 Today, there are two main ways to build a Rust binary targeting Fuchsia
75 using the Fuchsia SDK:
76 1. Allow [rustup] to handle the installation of Fuchsia targets for you.
77 1. Build a toolchain locally that can target Fuchsia.
79 ### Targeting Fuchsia with rustup and cargo
81 The easiest way to build a Rust binary targeting Fuchsia is by allowing [rustup]
82 to handle the installation of Fuchsia targets for you. This can be done by issuing
83 the following commands:
86 rustup target add x86_64-fuchsia
87 rustup target add aarch64-fuchsia
90 After installing our Fuchsia targets, we can now compile a Rust binary that targets
93 To create our Rust project, we can issue a standard `cargo` command as follows:
95 **From base working directory**
97 cargo new hello_fuchsia
100 The rest of this walkthrough will take place from `hello_fuchsia`, so we can
101 change into that directory now:
107 *Note: From this point onwards, all commands will be issued from the `hello_fuchsia/`
108 directory, and all `hello_fuchsia/` prefixes will be removed from references for sake of brevity.*
110 We can edit our `src/main.rs` to include a test as follows:
115 println!("Hello Fuchsia!");
120 assert_eq!(2 + 2, 4);
124 In addition to the standard workspace created, we will want to create a
125 `.cargo/config.toml` file to link necessary libraries
128 **`.cargo/config.toml`**
130 [target.x86_64-fuchsia]
133 "-Lnative=<SDK_PATH>/arch/x64/lib",
134 "-Lnative=<SDK_PATH>/arch/x64/sysroot/lib"
138 *Note: Make sure to fill out `<SDK_PATH>` with the path to the downloaded [Fuchsia SDK].*
140 These options configure the following:
142 * `-Lnative=${SDK_PATH}/arch/${ARCH}/lib`: Link against Fuchsia libraries from
144 * `-Lnative=${SDK_PATH}/arch/${ARCH}/sysroot/lib`: Link against Fuchsia sysroot
145 libraries from the SDK
147 In total, our new project will look like:
149 **Current directory structure**
159 Finally, we can build our rust binary as:
162 cargo build --target x86_64-fuchsia
165 Now we have a Rust binary at `target/x86_64-fuchsia/debug/hello_fuchsia`,
166 targeting our desired Fuchsia target.
168 **Current directory structure**
182 ### Targeting Fuchsia with a compiler built from source
184 An alternative to the first workflow is to target Fuchsia by using
185 `rustc` built from source.
187 Before building Rust for Fuchsia, you'll need a clang toolchain that supports
188 Fuchsia as well. A recent version (14+) of clang should be sufficient to compile
191 x86-64 and AArch64 Fuchsia targets can be enabled using the following
194 In `config.toml`, add:
198 target = ["<host_platform>", "aarch64-fuchsia", "x86_64-fuchsia"]
201 Additionally, the following environment variables must be configured (for
202 example, using a script like `config-env.sh`):
205 # Configure this environment variable to be the path to the downloaded SDK
206 export SDK_PATH="<SDK path goes here>"
208 export CFLAGS_aarch64_fuchsia="--target=aarch64-fuchsia --sysroot=${SDK_PATH}/arch/arm64/sysroot -I${SDK_PATH}/pkg/fdio/include"
209 export CXXFLAGS_aarch64_fuchsia="--target=aarch64-fuchsia --sysroot=${SDK_PATH}/arch/arm64/sysroot -I${SDK_PATH}/pkg/fdio/include"
210 export LDFLAGS_aarch64_fuchsia="--target=aarch64-fuchsia --sysroot=${SDK_PATH}/arch/arm64/sysroot -L${SDK_PATH}/arch/arm64/lib"
211 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"
212 export CFLAGS_x86_64_fuchsia="--target=x86_64-fuchsia --sysroot=${SDK_PATH}/arch/x64/sysroot -I${SDK_PATH}/pkg/fdio/include"
213 export CXXFLAGS_x86_64_fuchsia="--target=x86_64-fuchsia --sysroot=${SDK_PATH}/arch/x64/sysroot -I${SDK_PATH}/pkg/fdio/include"
214 export LDFLAGS_x86_64_fuchsia="--target=x86_64-fuchsia --sysroot=${SDK_PATH}/arch/x64/sysroot -L${SDK_PATH}/arch/x64/lib"
215 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"
218 These can be run together in a shell environment by executing
219 `(source config-env.sh && ./x.py install)`.
221 Once `rustc` is installed, we can create a new working directory to work from,
222 `hello_fuchsia` along with `hello_fuchsia/src`:
230 *Note: From this point onwards, all commands will be issued from the `hello_fuchsia/`
231 directory, and all `hello_fuchsia/` prefixes will be removed from references for sake of brevity.*
233 There, we can create a new file named `src/hello_fuchsia.rs`:
235 **`src/hello_fuchsia.rs`**
238 println!("Hello Fuchsia!");
243 assert_eq!(2 + 2, 4);
247 **Current directory structure**
254 Using your freshly installed `rustc`, you can compile a binary for Fuchsia using
255 the following options:
257 * `--target x86_64-fuchsia`/`--target aarch64-fuchsia`: Targets the Fuchsia
258 platform of your choice
259 * `-Lnative ${SDK_PATH}/arch/${ARCH}/lib`: Link against Fuchsia libraries from
261 * `-Lnative ${SDK_PATH}/arch/${ARCH}/sysroot/lib`: Link against Fuchsia sysroot
262 libraries from the SDK
264 Putting it all together:
267 # Configure these for the Fuchsia target of your choice
268 TARGET_ARCH="<x86_64-fuchsia|aarch64-fuchsia>"
272 --target ${TARGET_ARCH} \
273 -Lnative=${SDK_PATH}/arch/${ARCH}/lib \
274 -Lnative=${SDK_PATH}/arch/${ARCH}/sysroot/lib \
275 --out-dir bin src/hello_fuchsia.rs
278 **Current directory structure**
282 ┃ ┗━ hello_fuchsia.rs
287 ## Creating a Fuchsia package
289 Before moving on, double check your directory structure:
291 **Current directory structure**
294 ┣━ src/ (if using rustc)
295 ┃ ┗━ hello_fuchsia.rs ...
297 ┃ ┗━ hello_fuchsia ...
298 ┣━ src/ (if using cargo)
301 ┗━ x86_64-fuchsia/ ...
306 With our Rust binary built, we can move to creating a Fuchsia package.
307 On Fuchsia, a package is the unit of distribution for software. We'll need to
308 create a new package directory where we will place files like our finished
309 binary and any data it may need.
311 To start, make the `pkg`, and `pkg/meta` directories:
318 **Current directory structure**
325 Now, create the following files inside:
327 **`pkg/meta/package`**
330 "name": "hello_fuchsia",
335 The `package` file describes our package's name and version number. Every
336 package must contain one.
338 **`pkg/hello_fuchsia.manifest` if using cargo**
340 bin/hello_fuchsia=target/x86_64-fuchsia/debug/hello_fuchsia
341 lib/ld.so.1=<SDK_PATH>/arch/x64/sysroot/dist/lib/ld.so.1
342 lib/libfdio.so=<SDK_PATH>/arch/x64/dist/libfdio.so
343 meta/package=pkg/meta/package
344 meta/hello_fuchsia.cm=pkg/meta/hello_fuchsia.cm
347 **`pkg/hello_fuchsia.manifest` if using rustc**
349 bin/hello_fuchsia=bin/hello_fuchsia
350 lib/ld.so.1=<SDK_PATH>/arch/x64/sysroot/dist/lib/ld.so.1
351 lib/libfdio.so=<SDK_PATH>/arch/x64/dist/libfdio.so
352 meta/package=pkg/meta/package
353 meta/hello_fuchsia.cm=pkg/meta/hello_fuchsia.cm
356 *Note: Relative manifest paths are resolved starting from the working directory
357 of `pm`. Make sure to fill out `<SDK_PATH>` with the path to the downloaded
360 The `.manifest` file will be used to describe the contents of the package by
361 relating their location when installed to their location on the file system. The
362 `bin/hello_fuchsia=` entry will be different depending on how your Rust binary
363 was built, so choose accordingly.
365 **Current directory structure**
371 ┗━ hello_fuchsia.manifest
374 ### Creating a Fuchsia component
376 On Fuchsia, components require a component manifest written in Fuchsia's markup
377 language called CML. The Fuchsia devsite contains an [overview of CML] and a
378 [reference for the file format]. Here's a basic one that can run our single binary:
380 **`pkg/hello_fuchsia.cml`**
383 include: [ "syslog/client.shard.cml" ],
386 binary: "bin/hello_fuchsia",
391 **Current directory structure**
397 ┣━ hello_fuchsia.manifest
401 Now we can compile that CML into a component manifest:
404 ${SDK_PATH}/tools/${ARCH}/cmc compile \
405 pkg/hello_fuchsia.cml \
406 --includepath ${SDK_PATH}/pkg \
407 -o pkg/meta/hello_fuchsia.cm
410 *Note: `--includepath` tells the compiler where to look for `include`s from our CML.
411 In our case, we're only using `syslog/client.shard.cml`.*
413 **Current directory structure**
419 ┃ ┗━ hello_fuchsia.cm
420 ┣━ hello_fuchsia.manifest
424 ### Building a Fuchsia package
426 Next, we'll build a package manifest as defined by our manifest:
429 ${SDK_PATH}/tools/${ARCH}/pm \
430 -api-level $(${SDK_PATH}/tools/${ARCH}/ffx version -v | grep "api-level" | head -1 | awk -F ' ' '{print $2}') \
431 -o pkg/hello_fuchsia_manifest \
432 -m pkg/hello_fuchsia.manifest \
434 -output-package-manifest pkg/hello_fuchsia_package_manifest
437 This will produce `pkg/hello_fuchsia_manifest/` which is a package manifest we can
438 publish directly to a repository.
440 **Current directory structure**
446 ┃ ┗━ hello_fuchsia.cm
447 ┣━ hello_fuchsia_manifest/
449 ┣━ hello_fuchsia.manifest
451 ┗━ hello_fuchsia_package_manifest
454 We are now ready to publish the package.
456 ## Publishing a Fuchsia package
458 With our package and component manifests setup,
459 we can now publish our package. The first step will
460 be to create a Fuchsia package repository to publish
463 ### Creating a Fuchsia package repository
465 We can set up our repository with:
468 ${SDK_PATH}/tools/${ARCH}/pm newrepo \
472 **Current directory structure**
478 ┃ ┗━ hello_fuchsia.cm
479 ┣━ hello_fuchsia_manifest/
483 ┣━ hello_fuchsia.manifest
485 ┗━ hello_fuchsia_package_manifest
488 ## Publishing Fuchsia package to repository
490 We can publish our new package to that repository with:
493 ${SDK_PATH}/tools/${ARCH}/pm publish \
495 -lp -f <(echo "pkg/hello_fuchsia_package_manifest")
498 Then we can add the repository to `ffx`'s package server as `hello-fuchsia` using:
501 ${SDK_PATH}/tools/${ARCH}/ffx repository add-from-pm \
506 ## Running a Fuchsia component on an emulator
508 At this point, we are ready to run our Fuchsia
509 component. For reference, our final directory
510 structure will look like:
512 **Final directory structure**
515 ┣━ src/ (if using rustc)
516 ┃ ┗━ hello_fuchsia.rs ...
518 ┃ ┗━ hello_fuchsia ...
519 ┣━ src/ (if using cargo)
522 ┃ ┗━ x86_64-fuchsia/ ...
524 ┃ ┗━ hello_fuchsia ...
528 ┃ ┗━ hello_fuchsia.cm
529 ┣━ hello_fuchsia_manifest/
533 ┣━ hello_fuchsia.manifest
535 ┗━ hello_fuchsia_package_manifest
538 ### Starting the Fuchsia emulator
540 Start a Fuchsia emulator in a new terminal using:
543 ${SDK_PATH}/tools/${ARCH}/ffx product-bundle get workstation_eng.qemu-${ARCH}
544 ${SDK_PATH}/tools/${ARCH}/ffx emu start workstation_eng.qemu-${ARCH} --headless
547 ### Watching emulator logs
549 Once the emulator is running, open a separate terminal to watch the emulator logs:
551 **In separate terminal**
553 ${SDK_PATH}/tools/${ARCH}/ffx log \
557 ### Serving a Fuchsia package
559 Now, start a package repository server to serve our
560 package to the emulator:
563 ${SDK_PATH}/tools/${ARCH}/ffx repository server start
566 Once the repository server is up and running, register it with the target Fuchsia system running in the emulator:
569 ${SDK_PATH}/tools/${ARCH}/ffx target repository register \
570 --repository hello-fuchsia
573 ### Running a Fuchsia component
575 Finally, run the component:
578 ${SDK_PATH}/tools/${ARCH}/ffx component run \
579 /core/ffx-laboratory:hello_fuchsia \
580 fuchsia-pkg://hello-fuchsia/hello_fuchsia_manifest#meta/hello_fuchsia.cm
583 On reruns of the component, the `--recreate` argument may also need to be
587 ${SDK_PATH}/tools/${ARCH}/ffx component run \
589 /core/ffx-laboratory:hello_fuchsia \
590 fuchsia-pkg://hello-fuchsia/hello_fuchsia_manifest#meta/hello_fuchsia.cm
593 ## `.gitignore` extensions
595 Optionally, we can create/extend our `.gitignore` file to ignore files and
596 directories that are not helpful to track:
600 pkg/meta/hello_fuchsia.cm
601 pkg/hello_fuchsia_manifest
602 pkg/hello_fuchsia_package_manifest
607 ### Running unit tests
609 Tests can be run in the same way as a regular binary.
611 * If using `cargo`, you can simply pass `test --no-run`
612 to the `cargo` invocation and then repackage and rerun the Fuchsia package. From our previous example,
613 this would look like `cargo test --target x86_64-fuchsia --no-run`, and moving the executable
614 binary path found from the line `Executable unittests src/main.rs (target/x86_64-fuchsia/debug/deps/hello_fuchsia-<HASH>)`
615 into `pkg/hello_fuchsia.manifest`.
617 * If using the compiled `rustc`, you can simply pass `--test`
618 to the `rustc` invocation and then repackage and rerun the Fuchsia package.
620 The test harness will run the applicable unit tests.
622 Often when testing, you may want to pass additional command line arguments to
623 your binary. Additional arguments can be set in the component manifest:
625 **`pkg/hello_fuchsia.cml`**
628 include: [ "syslog/client.shard.cml" ],
631 binary: "bin/hello_fuchsia",
637 This will pass the argument `it_works` to the binary, filtering the tests to
638 only those tests that match the pattern. There are many more configuration
639 options available in CML including environment variables. More documentation is
640 available on the [Fuchsia devsite].
642 ### Running the compiler test suite
644 Running the Rust test suite on Fuchsia is [not currently supported], but work is
645 underway to enable it.
651 Debugging components running on a Fuchsia emulator can be done using the
652 console-mode debugger: [zxdb]. We will demonstrate attaching necessary symbol
653 paths to debug our `hello-fuchsia` component.
657 In a separate terminal, issue the following command from our `hello_fuchsia`
658 directory to launch `zxdb`:
660 **In separate terminal**
662 ${SDK_PATH}/tools/${ARCH}/ffx debug connect -- \
663 --symbol-path target/x86_64-fuchsia/debug
666 * `--symbol-path` gets required symbol paths, which are
667 necessary for stepping through your program.
669 The "[displaying source code in `zxdb`](#displaying-source-code-in-zxdb)" section describes how you can
670 display Rust and/or Fuchsia source code in your debugging session.
674 Once launched, you will be presented with the window:
677 Connecting (use "disconnect" to cancel)...
678 Connected successfully.
679 👉 To get started, try "status" or "help".
683 To attach to our program, we can run:
686 [zxdb] attach hello_fuchsia
691 Waiting for process matching "hello_fuchsia".
692 Type "filter" to see the current filters.
695 Next, we can create a breakpoint at main using "b main":
703 Created Breakpoint 1 @ main
706 Finally, we can re-run the "hello_fuchsia" component from our original
710 ${SDK_PATH}/tools/${ARCH}/ffx component run \
712 fuchsia-pkg://hello-fuchsia/hello_fuchsia_manifest#meta/hello_fuchsia.cm
715 Once our component is running, our `zxdb` window will stop execution
716 in our main as desired:
720 Breakpoint 1 now matching 1 addrs for main
721 🛑 on bp 1 hello_fuchsia::main() • main.rs:2
723 ▶ 2 println!("Hello Fuchsia!");
729 `zxdb` has similar commands to other debuggers like [gdb].
730 To list the available commands, run "help" in the
731 `zxdb` window or visit [the zxdb documentation].
741 Type "help <command>" for command-specific help.
743 Other help topics (see "help <topic>")
747 ### Displaying source code in `zxdb`
749 By default, the debugger will not be able to display
750 source code while debugging. For our user code, we displayed
751 source code by pointing our debugger to our debug binary via
752 the `--symbol-path` arg. To display library source code in
753 the debugger, you must provide paths to the source using
754 `--build-dir`. For example, to display the Rust and Fuchsia
758 ${SDK_PATH}/tools/${ARCH}/ffx debug connect -- \
759 --symbol-path target/x86_64-fuchsia/debug \
760 --build-dir ${RUST_SRC_PATH}/rust \
761 --build-dir ${FUCHSIA_SRC_PATH}/fuchsia/out/default
764 * `--build-dir` links against source code paths, which
765 are not strictly necessary for debugging, but is a nice-to-have
766 for displaying source code in `zxdb`.
768 Linking to a Fuchsia checkout can help with debugging Fuchsia libraries,
771 [Fuchsia team]: https://team-api.infra.rust-lang.org/v1/teams/fuchsia.json
772 [Fuchsia]: https://fuchsia.dev/
773 [source tree]: https://fuchsia.dev/fuchsia-src/get-started/learn/build
774 [rustup]: https://rustup.rs/
775 [cargo]: https://doc.rust-lang.org/cargo/
776 [Fuchsia SDK]: https://chrome-infra-packages.appspot.com/p/fuchsia/sdk/core
777 [overview of CML]: https://fuchsia.dev/fuchsia-src/concepts/components/v2/component_manifests
778 [reference for the file format]: https://fuchsia.dev/reference/cml
779 [Fuchsia devsite]: https://fuchsia.dev/reference/cml
780 [not currently supported]: https://fxbug.dev/105393
781 [zxdb]: https://fuchsia.dev/fuchsia-src/development/debugger
782 [gdb]: https://www.sourceware.org/gdb/
783 [the zxdb documentation]: https://fuchsia.dev/fuchsia-src/development/debugger
784 [fdio]: https://cs.opensource.google/fuchsia/fuchsia/+/main:sdk/lib/fdio/