本指南介绍了如何构建 Fuchsia 以包含 Fuchsia 源代码 //examples
目录中的示例软件包,以及如何在 Fuchsia 目标上运行组件。
前提条件
您必须先完成以下操作,才能运行示例组件:
探索示例
此示例组件会将 Hello, world!
输出到系统日志中。该示例包含三个主要元素:
可执行程序
Fuchsia 组件可以执行使用受支持的运行时编写的任何语言的程序。最常用于 C++ 和 Rust 程序的运行时是 ELF 运行程序。
C++
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
Rust
fn main() {
println!("{}, world!", greeting());
eprintln!("{}, world!", greeting());
}
fn greeting() -> String {
return String::from("Hello");
}
组件清单
介绍了如何将 Fuchsia 程序作为组件运行。 这包括声明程序二进制文件、运行时信息以及组件需要执行的任何功能,例如日志记录支持。
C++
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
// Enable system logging capabilities
include: [ "syslog/client.shard.cml" ],
// Information about the program to run
program: {
// Use the built-in ELF runner for the executable
runner: "elf",
binary: "bin/hello_world_cpp",
// Enable stdout logging
forward_stderr_to: "log",
forward_stdout_to: "log",
},
}
Rust
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
// Enable system logging capabilities
include: [ "syslog/client.shard.cml" ],
// Information about the program to run
program: {
// Use the built-in ELF runner for the executable
runner: "elf",
binary: "bin/hello_world_rust",
// Enable stdout logging
forward_stderr_to: "log",
forward_stdout_to: "log",
},
}
如需详细了解组件清单及其声明语法,请参阅组件清单。
BUILD.gn
Fuchsia 使用 Generate Ninja (GN) 元构建系统为 Ninja 生成输入,后者会执行实际构建。BUILD.gn
文件声明了 fuchsia_component()
和 fuchsia_package()
的 build 目标。
C++
executable("bin") {
output_name = "hello_world_cpp"
sources = [ "hello_world.cc" ]
}
fuchsia_component("hello-world-cpp-component") {
deps = [ ":bin" ]
# Defines the name given to the manifest when included in a fuchsia package.
# In this case: "hello-world-cpp.cm"
component_name = "hello-world-cpp"
manifest = "meta/hello_world_cpp.cml"
}
fuchsia_package("hello-world-cpp") {
deps = [
# component-url: fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
":hello-world-cpp-component",
]
}
Rust
rustc_binary("bin") {
name = "hello_world_rust"
# Generates the "bin_test" build target
with_unit_tests = true
edition = "2021"
deps = []
test_deps = [ "//src/lib/fuchsia" ]
sources = [ "src/main.rs" ]
}
fuchsia_component("hello-world-rust-component") {
deps = [ ":bin" ]
# Defines the name given to the manifest when included in a fuchsia package.
# In this case: "hello-world-rust.cm"
component_name = "hello-world-rust"
manifest = "meta/hello_world_rust.cml"
}
fuchsia_package("hello-world-rust") {
deps = [
# component-url: fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
":hello-world-rust-component",
]
}
如需详细了解 Fuchsia 如何使用 GN 定义组件和软件包,请参阅:构建组件。
在 Fuchsia 映像中添加示例软件包
如需在 build 配置中添加示例软件包,请在设置产品和开发板环境时使用 --with
标志:
fx set product.board --with //examples/hello_world
对于采用最小构建配置的 Fuchsia 模拟器,命令如下:
fx set core.x64 --with //examples/hello_world
在此示例中,core
是一款功能集最少的产品,其中包括常见的网络功能,而 x64
是指 x64 架构。
对于具有最低 build 配置的 Fuchsia 设备,命令如下:
fx set core.x64 --with //examples/hello_world
如需了解更多选项,请参阅配置 build。
设置 build 配置后,使用以下命令构建 Fuchsia:
fx build
现在,您有一个 build,其中包含可按需提取和启动的示例软件包。
探索您的产品配置
您可以使用 list-packages
命令浏览产品配置的内容。
列出所有:
fx list-packages
条目可能很多,因此请添加名称以查找所需条目:
fx list-packages hello-world
hello-world
hello-world-cpp-unittests
hello-world-rust-tests
运行示例组件
打开终端并运行
fx serve
:fx serve
打开另一个终端并运行示例组件:
C++
ffx component run /core/ffx-laboratory:hello-world-cpp fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
Rust
ffx component run /core/ffx-laboratory:hello-world-rust fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
打开另一个终端并查看系统日志:
ffx log --filter hello-world
该组件会将以下输出输出到日志:
[ffx-laboratory:hello-world] INFO: Hello, World!
问题排查
如果 fx serve
未运行,该命令会从设备或模拟器输出一条错误消息。
如果 fx serve
正在运行,但找不到该软件包,请重复这些步骤,并重新构建 Fuchsia 映像以添加适当的软件包。