Getting started with platform2

What is platform2?

platform2 is an effort to make the build of platform packages faster and simpler; it uses GN and ninja to build everything.

The common build system includes:

  • protobuf and dbus adaptors generation
  • incremental builds
  • pkg-config support
  • dependencies extraction (dependencies are extracted from your GN files and be used to generate pkg-config files)

The platform2 philosophy

Given a package ${package_name}:

  1. The code should live in src/platform2/${package_name}.
  2. The code should be built from a single GN file src/platform2/${package_name}/
  3. Each package should have their own ebuild. The logic to build platform2 packages is contained in platform.eclass.

How to write an ebuild for a platform2 package

Packages in platform2 should have very lean ebuilds. Most of the build configuration should happen behind the scene (platform.eclass) or in the common GN logic. In most cases, your ebuild will only contain dependencies, an install section, and a list of tests to run.

Note: as for all cros_workon packages, you only need to create the -9999 version of the ebuild (${package_name}-9999.ebuild). The CQ will automatically create the stable version of the ebuild (${package_name}-x.y.z-r${revision_number}.ebuild).

Example ebuild:

# Copyright <copyright_year> The ChromiumOS Authors.
# Distributed under the terms of the GNU General Public License v2


CROS_WORKON_SUBTREE="common-mk package .gn"

PLATFORM_SUBDIR="package" # name of the directory in src/platform2

inherit cros-workon platform





src_install() {
  # ${OUT} is the build output directory containing all your binaries.
  dobin "${OUT}/binary"

platform_pkg_test() {
  local tests=(
    package_test # name of tests defined in GN

  local test_bin
  for test_bin in "${tests[@]}"; do
    # platform_test takes care of setting up your test environment.
    platform_test run "${OUT}/${test_bin}"

Adding generators

platform2 already has pre-made generators for dbus-xml2cpp and protoc, but if you need to add a new generator, here's how.

  1. Create a new .gni file under src/platform2/common-mk/ named appropriately to describe your generator.
  2. Copy the contents of an existing one, such as dbus_glib.gni
  3. Modify the rule specified to call your tool with the arguments
  4. Update the inputs/outputs lists to reflect the files your tool will generate. (Ensure your tool is in virtual/target-sdk if necessary.)

You can now include this generator in a target for a project you're porting to platform2, by writing a target in your project that uses the template you just wrote.

Adding a new USE flag conditional

It is possible to have conditional GN rules that depend on the value of a USE flag at the time of build. This is useful, for example, if certain targets must only be built if a certain feature (controlled by a USE flag) is enabled.

See the instructions in GN in ChromeOS.

Rapid development

cros_workon_make will use the ebuild to build a package, but will build incrementally and skip as many steps as possible from the normal emerge process (not checking dependencies, not installing, etc.). However, please use this command with caution because it allows ebuilds to directly modify the source repository. For example, it may delete some files.

For extra speed, you can also use --noreconf to skip the configure stage (creating the ninja file from the GN files):

cros_workon_make --board=${BOARD} --noreconf shill

You can inspect the produced artifacts and test binaries in /build/${BOARD}/var/cache/portage/chromeos-base/shill.

Running the unittests in gdb

Once your unittests are built, you need to enter the sysroot (in /build/${BOARD}) with will set up the environment so that the binaries can be executed correctly (entering the sysroot, using qemu, etc.).

Note: qemu doesn‘t support the ptrace syscall used by gdb, so even though the program will run under the debugger, breakpoints/traps/etc won’t work.

To run a shell within the sysroot:

common-mk/ --board=${BOARD} /bin/bash

From within the shell, you can run your binaries (including unit tests), and run them with gdb. Within the sysroot, the gdb accessible from $PATH is the correct target gdb. Since you have entered the sysroot, you will find the build artifacts under /var/cache/portage/chromeos-base/${package_name}.

Running unit tests

You have 2 options for running unit tests for a package residing in platform2:

# Use cros_workon_make to run unit tests.
cros_workon_make --board=${BOARD} ${PACKAGE_NAME} --test

# Alternatively, use your board-specific emerge - this might give
# additional useful output.

You can select a subset of tests to run by setting P2_TEST_FILTER. cros_workon_make will run only the tests whose name match one of the regex specified in P2_TEST_FILTER. E.g.

# Run the string utilities unit tests for libchromeos.
P2_TEST_FILTER="StringUtils.*" cros_workon_make --board=${BOARD} --test libchromeos

For more information, consult the upstream gtest documentation.

Unit tests typically run code on your host machine which is compiled for the target ChromeOS system. When the target CPU architecture differs from host machine (e.g., running ARM unit tests on an x86 host), we run them under QEMU. When running x86 unit tests on an x86 host, we run them natively. Note that this can cause problems when the ISA is similar but not identical -- see for more info. In practice, this means that unit tests built for AMD systems may fail when your host system uses an Intel CPU.

Further reading