執行範例元件

本指南說明如何建構 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");
}

元件資訊清單

包括宣告程式二進位檔、執行階段資訊,以及元件需要執行的所有功能 (例如記錄支援)。

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() 的建構目標。

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 圖片中加入範例套件

如要在建構設定中加入範例套件,請在設定產品和主面板環境時使用 --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

執行範例元件

run

  1. 開啟終端機並執行 fx serve

    fx serve
    
  2. 開啟另一個終端機並執行範例元件:

    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
    
  3. 開啟另一個終端機並查看系統記錄:

    ffx log --filter hello-world
    

    元件會在記錄中輸出下列輸出內容:

    [ffx-laboratory:hello-world] INFO: Hello, World!
    

疑難排解

如果 fx serve 未執行,指令會從裝置或模擬器輸出錯誤訊息。

如果 fx serve 正在執行,但找不到套件,請重複上述步驟,然後重新建構 Fuchsia 映像檔,以納入適當的套件。