本指南将向您介绍如何构建 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");
}
组件清单
{1这包括声明程序二进制文件、运行时信息以及组件执行所需的任何功能,例如日志记录支持。
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生成输入,Ninja 会执行实际构建。
BUILD.gn 文件会为 fuchsia_component() 和 fuchsia_package() 声明构建目标。
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 = "2024"
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 架构。
对于具有最低构建配置的 Fuchsia 设备,该命令为:
fx set core.x64 --with //examples/hello_world如需了解更多选项,请参阅配置构建,了解 更多选项。
设置 build 配置后,请使用以下命令构建 Fuchsia:
fx 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.cmRust
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 映像以
包含相应的软件包。