工作階段是 session_manager
可在啟動時啟動的一般元件。也就是說,建立工作階段元件的步驟,與建立任何其他元件的步驟相同。本文件說明如何建立在啟動時啟動的範例工作階段,並將「Hello World!」輸出到系統記錄。
建立目錄結構
元件需要特定的目錄結構。fx
工具會提供產生器,為您建立這個結構。這會採用元件名稱以及您要用做引數的語言。舉例來說,這個元件稱為 hello-world-session
,並以 Rust 編寫。
執行下列指令來建立這個範例的目錄結構:
fx create component --path hello-world-session --lang rust
這個指令會使用提供服務的元件範本建立下列目錄結構:
hello-world-session
|- meta
| |- hello-world-session.cml
|
|- src
| |- main.rs
|
|- BUILD.gn
建立元件資訊清單
元件資訊清單檔案 (hello-world-session.cml
) 會將元件宣告為 Fuchsia。在這個範例中,預設資訊清單已足夠,但請花一點時間探索 hello-world-session.cml
中的下列程式碼:
檔案一開始會視需要加入其他 cml 檔案。
include: [ "inspect/client.shard.cml", "syslog/client.shard.cml", ],
此
include
鍵可讓工作階段元件使用fuchsia.logger.LogSink
能力,以便將其列印至系統記錄。接下來是
program
區塊。// Information about the program to run. program: { // Use the built-in ELF runner to run native binaries. runner: "elf", // The binary to run for this component. binary: "bin/hello-world-session", },
program
區塊會指示component_manager
找到工作階段元件的二進位檔。runner
鍵會指示component_manager
使用 ELF 執行器執行元件二進位檔。最後,元件資訊清單會說明元件可為
use
、offer
或expose
提供的其他功能。// Capabilities used by this component. use: [ // List your component's dependencies here, ex: // { protocol: "fuchsia.net.name.Lookup" } ], expose: [ // session_manager uses this protocol to start the session. { protocol: "fuchsia.component.Binder", from: "framework", }, ],
在 Rust 中編寫工作階段
您現在可以為工作階段元件編寫實作。在產生的 src/main.rs
檔案中,有許多程式碼不需用到此範例。
將 src/main.rs
的內容替換為下列程式碼:
use anyhow::Error;
/// Creates a simple session that just prints "Hello World" to the syslog.
#[fuchsia::main(logging = true)]
async fn main() -> Result<(), Error> {
tracing::info!("Hello World!");
Ok(())
}
這個程式碼會初始化系統記錄,然後輸出「Hello World!」。tracing::info!
是一個巨集,可列印到記錄層級的 info
層級。error
和 warn
也有類似的巨集,
編寫 BUILD.gn
最後一個要修改的檔案是 BUILD.gn
。藉此告知編譯器如何建構工作階段元件。
Rust 二進位檔
下一節將說明實際的 Rust 二進位檔。這會向編譯器告知二進位檔的名稱,其中包含單元測試、其具有的依附元件,以及其來源所在位置。在這個範例中,預設的依附元件組合就已足夠:
rustc_binary("bin") {
edition = "2021"
output_name = "hello-world-session"
# Generates a GN target for unit-tests with the label `bin_test`, and
# a binary named `hello_world_session_bin_test`.
with_unit_tests = true
deps = [
"//src/lib/fuchsia",
"//third_party/rust_crates:anyhow",
"//third_party/rust_crates:tracing",
]
sources = [ "src/main.rs" ]
}
fuchsia_component()
和 fuchsia_package()
範本會向 Fuchsia 提供更多該元件的相關資訊,包括呼叫的內容、資訊清單的位置,以及套件和元件具有哪些依附元件:
fuchsia_component("component") {
component_name = "hello-world-session"
manifest = "meta/hello-world-session.cml"
deps = [ ":bin" ]
}
fuchsia_package("hello-world-session") {
deps = [ ":component" ]
}
找出工作階段網址
session_manager
需要知道啟動時要啟動哪個工作階段元件,然後提供工作階段的元件網址來進行設定。
元件網址應採用以下格式:
fuchsia-pkg://fuchsia.com/package_name#meta/component_name.cm
請注意,路徑會指向 .cm
檔案。.cm
檔案是 .cml
檔案的編譯版本,會在執行 fx build
時產生。因此在這個示例中,元件網址如下:
fuchsia-pkg://fuchsia.com/hello-world-session#meta/hello-world-session.cm
建構課程
您必須先使用 fx set
建構工作階段,才能設定版本,使 session_manager
、工作階段元件和工作階段設定包含在基本套件集中。方法是使用 --with-base
旗標。您也必須設定工作階段網址,方法是使用 --args
標記。
fx set core.x64 \
--with-base //src/session/bin/session_manager \
--with-base //path/to/your/session \
--args=product_config.session_url = "fuchsia-pkg://fuchsia.com/<var>package_name</var>#meta/<var>component_name</var>.cm"
如果您使用的是 //src/session/examples
目錄中的範例專案,則 fx set
指令如下:
fx set core.x64 \
--with-base //src/session/bin/session_manager \
--with-base //src/session/examples/hello-world-session \
--args=product_config.session_url = "fuchsia-pkg://fuchsia.com/hello-world-session#meta/hello-world-session.cm"
完成後,建構 session_manager
時,應會在開機時自動啟動您的工作階段。您應該會在系統記錄中看到「Hello」訊息。
$ ffx log --filter hello
[session_manager] INFO: Launching session: fuchsia-pkg://fuchsia.com/hello-world-session#meta/hello-world-session.cm
[hello_world_session] INFO: Hello World!