运行示例组件

本指南将向您介绍如何构建 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 映像中添加示例软件包

如需在 build 配置中添加示例软件包,请在设置产品和板级环境时使用 --with 标志:

fx set product.board --with //examples/hello_world

对于具有最低 build 配置的 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

运行示例组件

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 映像以包含相应的软件包。