本指南說明如何建構 Fuchsia,以便納入 Fuchsia 來源 //examples 目錄中的範例套件,並在 Fuchsia 目標上執行元件。
必要條件
您必須先完成下列事項,才能執行範例元件:
探索範例
這個範例元件會將 Hello, world! 列印到系統記錄檔。這個範例有三個主要元素:
可執行程式
Fuchsia 元件可以執行以任何語言編寫的程式,只要該語言有支援的執行階段即可。C++ 和 Rust 程式最常用的執行階段是 ELF 執行器。
C++
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
荒漠油廠
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",
},
}
荒漠油廠
// 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() 的建構目標。
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",
]
}
荒漠油廠
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 映像檔中加入範例套件
如要在建構設定中加入範例套件,請在設定產品和主機板環境時使用 --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如需更多選項,請參閱「設定建構作業」。
設定建構設定後,請使用下列指令建構 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.cm荒漠油廠
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 映像檔,加入適當的套件。