4. Integration commands

Sometimes a software requires more configuration or processing than what is performed at installation time, otherwise it will not run properly.

This is especially true in cases where a daemon or library interoperates with third party extensions and needs to maintain a system wide cache whenever it’s extensions are installed or removed; system wide font caches are an example of this.

In these cases we use integration commands to ensure that a runtime is ready to run after all of it’s components have been staged.

Note

This example is distributed with BuildStream in the doc/examples/integration-commands subdirectory.

4.1. Overview

In this chapter, we’ll be exploring integration commands, which will be our first look at public data.

4.2. Project structure

4.2.1. project.conf and elements/base.bst

The project.conf and base stack stack element are configured in the same way as in the previous chapter: Running commands.

4.2.2. elements/base/alpine.bst

kind: import
description: |

    Alpine Linux base runtime

sources:
- kind: tar
  url: alpine:integration-tests-base.v1.x86_64.tar.xz
  ref: 3eb559250ba82b64a68d86d0636a6b127aa5f6d25d3601a79f79214dc9703639

#
# Run ldconfig in the libdir before running anything
#
public:
  bst:
    integration-commands:
    - ldconfig "%{libdir}"

This is the same base/alpine.bst we’ve seen in previous chapters, except that we’ve added an integration command.

This informs BuildStream that whenever the output of this element is expected to run, this command should be run first. In this case we are simply running ldconfig as a precautionary measure, to ensure that the runtime linker is ready to find any shared libraries we may have added to %{libdir}.

4.2.2.1. Looking at public data

The integration commands used here is the first time we’ve used any builtin public data.

Public data is a free form portion of an element’s configuration and is not necessarily understood by the element on which it is declared, public data is intended to be read by it’s reverse dependency elements.

This allows annotations on some elements to inform elements later in the dependency chain about details of it’s artifact, or to suggest how it should be processed.

4.2.3. elements/libhello.bst and elements/hello.bst

These are basically manual elements very similar to the ones we’ve seen in the previous chapter: Running commands.

These produce a library and a hello program which uses the library, we will consider these irrelevant to the topic and leave examination of their sources as an exercise for the reader.

4.3. Using the project

4.3.1. Build the hello.bst element

To build the project, run bst build in the following way:

user@host:~/integration-commands$ bst build hello.bst

[--:--:--][][] STATUS  Cache usage recomputed: 12K / infinity (0%)
[--:--:--][][] START   Build
[--:--:--][][] START   Loading elements
[00:00:00][][] SUCCESS Loading elements
[--:--:--][][] START   Resolving elements
[00:00:00][][] SUCCESS Resolving elements
[--:--:--][][] START   Resolving cached state
[00:00:00][][] SUCCESS Resolving cached state
[--:--:--][][] START   Checking sources
[00:00:00][][] SUCCESS Checking sources

BuildStream Version 1.4.2+20.g89765b75
  Session Start: Wednesday, 13-05-2020 at 10:41:26
  Project:       integration-commands (/home/user/integration-commands)
  Targets:       hello.bst
  Cache Usage:   12K / infinity (0%)

User Configuration
  Configuration File:      /home/user/.config/buildstream.conf
  Log Files:               /home/user/.cache/buildstream/logs
  Source Mirrors:          /home/user/.cache/buildstream/sources
  Build Area:              /home/user/.cache/buildstream/build
  Artifact Cache:          /home/user/.cache/buildstream/artifacts
  Strict Build Plan:       Yes
  Maximum Fetch Tasks:     10
  Maximum Build Tasks:     4
  Maximum Push Tasks:      4
  Maximum Network Retries: 2

Pipeline
   buildable 8175172a6d0ef8e2aaaec63b6545b5e6571977c68d44b6c8325d2b31f91ee2fa base/alpine.bst 
     waiting 499981895f448544e7bf88260c3567a75b9a963bce16ad3082644b1ca8986b9b base.bst 
     waiting 3cc7855da0df0c55b74a4c39d854c15baca63b629c6e911277a3714bffe29654 libhello.bst 
     waiting 8e37238eaf58697f9d49cfe777023d197225eebb76c0660490349cb87530c667 hello.bst 
===============================================================================
[--:--:--][8175172a][build:base/alpine.bst               ] START   integration-commands/base-alpine/8175172a-build.2763.log
[--:--:--][8175172a][build:base/alpine.bst               ] START   Staging sources
[00:00:08][8175172a][build:base/alpine.bst               ] SUCCESS Staging sources
[--:--:--][8175172a][build:base/alpine.bst               ] START   Caching artifact
[00:00:04][8175172a][build:base/alpine.bst               ] SUCCESS Caching artifact
[00:00:14][8175172a][build:base/alpine.bst               ] SUCCESS integration-commands/base-alpine/8175172a-build.2763.log
[--:--:--][49998189][build:base.bst                      ] START   integration-commands/base/49998189-build.2765.log
[--:--:--][49998189][build:base.bst                      ] START   Caching artifact
[00:00:00][49998189][build:base.bst                      ] SUCCESS Caching artifact
[00:00:00][49998189][build:base.bst                      ] SUCCESS integration-commands/base/49998189-build.2765.log
[--:--:--][3cc7855d][build:libhello.bst                  ] START   integration-commands/libhello/3cc7855d-build.2767.log
[--:--:--][3cc7855d][build:libhello.bst                  ] START   Staging dependencies
[00:00:00][3cc7855d][build:libhello.bst                  ] SUCCESS Staging dependencies
[--:--:--][3cc7855d][build:libhello.bst                  ] START   Integrating sandbox
[--:--:--][3cc7855d][build:libhello.bst                  ] STATUS  Running integration command

    ldconfig "/usr/lib"

[00:00:00][3cc7855d][build:libhello.bst                  ] SUCCESS Integrating sandbox
[--:--:--][3cc7855d][build:libhello.bst                  ] START   Staging sources
[00:00:00][3cc7855d][build:libhello.bst                  ] SUCCESS Staging sources
[--:--:--][3cc7855d][build:libhello.bst                  ] START   Running build-commands
[--:--:--][3cc7855d][build:libhello.bst                  ] STATUS  Running build-commands

    make PREFIX="/usr"

[00:00:00][3cc7855d][build:libhello.bst                  ] SUCCESS Running build-commands
[--:--:--][3cc7855d][build:libhello.bst                  ] START   Running install-commands
[--:--:--][3cc7855d][build:libhello.bst                  ] STATUS  Running install-commands

    make -j1 PREFIX="/usr" DESTDIR="/buildstream-install" install

[00:00:00][3cc7855d][build:libhello.bst                  ] SUCCESS Running install-commands
[--:--:--][3cc7855d][build:libhello.bst                  ] START   Running strip-commands
[--:--:--][3cc7855d][build:libhello.bst                  ] STATUS  Running strip-commands

    cd "/buildstream-install" && find -type f \
      '(' -perm -111 -o -name '*.so*' \
          -o -name '*.cmxs' -o -name '*.node' ')' \
      -exec sh -ec \
      'read -n4 hdr <"$1" # check for elf header
       if [ "$hdr" != "$(printf \\x7fELF)" ]; then
           exit 0
       fi
       debugfile="/buildstream-install/usr/lib/debug/$1"
       mkdir -p "$(dirname "$debugfile")"
       objcopy --only-keep-debug --compress-debug-sections "$1" "$debugfile"
       chmod 644 "$debugfile"
       strip --remove-section=.comment --remove-section=.note --strip-unneeded "$1"
       objcopy --add-gnu-debuglink "$debugfile" "$1"' - {} ';'

[00:00:00][3cc7855d][build:libhello.bst                  ] SUCCESS Running strip-commands
[--:--:--][3cc7855d][build:libhello.bst                  ] START   Caching artifact
[00:00:00][3cc7855d][build:libhello.bst                  ] SUCCESS Caching artifact
[00:00:01][3cc7855d][build:libhello.bst                  ] SUCCESS integration-commands/libhello/3cc7855d-build.2767.log
[--:--:--][8e37238e][build:hello.bst                     ] START   integration-commands/hello/8e37238e-build.2802.log
[--:--:--][8e37238e][build:hello.bst                     ] START   Staging dependencies
[00:00:00][8e37238e][build:hello.bst                     ] SUCCESS Staging dependencies
[--:--:--][8e37238e][build:hello.bst                     ] START   Integrating sandbox
[--:--:--][8e37238e][build:hello.bst                     ] STATUS  Running integration command

    ldconfig "/usr/lib"

[00:00:00][8e37238e][build:hello.bst                     ] SUCCESS Integrating sandbox
[--:--:--][8e37238e][build:hello.bst                     ] START   Staging sources
[00:00:00][8e37238e][build:hello.bst                     ] SUCCESS Staging sources
[--:--:--][8e37238e][build:hello.bst                     ] START   Running build-commands
[--:--:--][8e37238e][build:hello.bst                     ] STATUS  Running build-commands

    make PREFIX="/usr"

[00:00:00][8e37238e][build:hello.bst                     ] SUCCESS Running build-commands
[--:--:--][8e37238e][build:hello.bst                     ] START   Running install-commands
[--:--:--][8e37238e][build:hello.bst                     ] STATUS  Running install-commands

    make -j1 PREFIX="/usr" DESTDIR="/buildstream-install" install

[00:00:00][8e37238e][build:hello.bst                     ] SUCCESS Running install-commands
[--:--:--][8e37238e][build:hello.bst                     ] START   Running strip-commands
[--:--:--][8e37238e][build:hello.bst                     ] STATUS  Running strip-commands

    cd "/buildstream-install" && find -type f \
      '(' -perm -111 -o -name '*.so*' \
          -o -name '*.cmxs' -o -name '*.node' ')' \
      -exec sh -ec \
      'read -n4 hdr <"$1" # check for elf header
       if [ "$hdr" != "$(printf \\x7fELF)" ]; then
           exit 0
       fi
       debugfile="/buildstream-install/usr/lib/debug/$1"
       mkdir -p "$(dirname "$debugfile")"
       objcopy --only-keep-debug --compress-debug-sections "$1" "$debugfile"
       chmod 644 "$debugfile"
       strip --remove-section=.comment --remove-section=.note --strip-unneeded "$1"
       objcopy --add-gnu-debuglink "$debugfile" "$1"' - {} ';'

[00:00:00][8e37238e][build:hello.bst                     ] SUCCESS Running strip-commands
[--:--:--][8e37238e][build:hello.bst                     ] START   Caching artifact
[00:00:00][8e37238e][build:hello.bst                     ] SUCCESS Caching artifact
[00:00:01][8e37238e][build:hello.bst                     ] SUCCESS integration-commands/hello/8e37238e-build.2802.log
[00:00:17][][] SUCCESS Build

Pipeline Summary
  Total:       4
  Session:     4
  Fetch Queue: processed 0, skipped 4, failed 0 
  Build Queue: processed 4, skipped 0, failed 0

Observe in the build process above, the integration command declared on the base/alpine.bst element is run after staging the dependency artifacts into the build sandbox and before running any of the build commands, for both of the libhello.bst and hello.bst elements.

BuildStream assumes that commands which are to be run in the build sandbox need to be run in an integrated sandbox.

Tip

Integration commands can be taxing on your overall build process, because they need to run at the beginning of every build which runtime depends on the element declaring them.

For this reason, it is better to leave out more onerous tasks if they are not needed at software build time, and handle those specific tasks differently later in the pipeline, before deployment.

4.3.2. Run the hello world program

Unlike the previous chapters, this hello world program takes an argument, we can invoke the program using bst shell:

user@host:~/integration-commands$ bst shell hello.bst -- hello pony

[--:--:--][][] START   Loading elements
[00:00:00][][] SUCCESS Loading elements
[--:--:--][][] START   Resolving elements
[00:00:00][][] SUCCESS Resolving elements
[--:--:--][][] START   Resolving cached state
[00:00:00][][] SUCCESS Resolving cached state
[--:--:--][8e37238e][ main:hello.bst                     ] START   Staging dependencies
[00:00:00][8e37238e][ main:hello.bst                     ] SUCCESS Staging dependencies
[--:--:--][8e37238e][ main:hello.bst                     ] START   Integrating sandbox
[--:--:--][8175172a][ main:base/alpine.bst               ] STATUS  Running integration command

    ldconfig "/usr/lib"

[00:00:00][8e37238e][ main:hello.bst                     ] SUCCESS Integrating sandbox
[--:--:--][8e37238e][ main:hello.bst                     ] STATUS  Running command

    hello pony

Hello pony

Here we see again, the integration commands are also used when preparing the shell to launch a command.

4.4. Summary

In this chapter we’ve observed how integration commands work, and we now know about public data, which plugins can read from their dependencies in order to influence their build process.