1. Workspaces

In this section we will cover the use of BuildStream’s workspaces feature when devloping a BuildStream project.

Note

This example is distributed with BuildStream in the doc/examples/developing subdirectory.

We will start with the project used in the running commands tutorial. Recall the element hello.bst, which builds the bellow C file:

/*
 * hello.c - Simple hello world program
 */
#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hello World\n");
  return 0;
}

Suppose we now want to alter the functionality of the hello command. We can make changes to the source code of Buildstream elements by making use of BuildStream’s workspace command.

1.1. Opening a workspace

First we need to open a workspace, we can do this by running

user@host:~/developing$ bst workspace open --directory workspace_hello hello.bst

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] START   Checking sources
[00:00:00][        ][    main:core activity                 ] SUCCESS Checking sources
[--:--:--][        ][    main:core activity                 ] INFO    Creating workspace for element hello.bst
[--:--:--][        ][    main:hello.bst                     ] START   Staging sources to /home/user/developing/workspace_hello
[--:--:--][        ][    main:hello.bst                     ] START   Staging local files into CAS
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Staging local files into CAS
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Staging sources to /home/user/developing/workspace_hello
[--:--:--][        ][    main:core activity                 ] INFO    Created a workspace for element: hello.bst

This command has created the workspace_hello directory in which you can see the source for the hello.bst element, i.e. hello.c and the corresponding makefile.

You can view existing workspaces using

user@host:~/developing$ bst workspace list

workspaces:
- element: hello.bst
  directory: /home/user/developing/workspace_hello

1.2. Making code changes

Let’s say we want to alter the message printed when the hello command is run. We can open workspace_hello/hello.c and make the following change:

-- hello.c	2018-06-25 14:48:32.077568920 +0100
+++ hello.c	2018-06-25 14:49:23.025553785 +0100
@@ -5,6 +5,6 @@
 
 int main(int argc, char *argv[])
 {
-  printf("Hello World\n");
+  printf("Hello World\nWe can use workspaces!\n");
   return 0;
 }

Now, rebuild the hello.bst element.

user@host:~/developing$ bst build hello.bst

[--:--:--][        ][    main:core activity                 ] START   Build
[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] START   Checking sources
[00:00:00][        ][    main:core activity                 ] SUCCESS Checking sources

BuildStream Version 1.93.1+172.g03ccfcd3b
  Session Start: Friday, 17-04-2020 at 20:51:51
  Project:       developing (/home/user/developing)
  Targets:       hello.bst

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

Pipeline
   buildable 179c6ae937e8e2ece3192ab8dc2a55053134a591c9ccd37507b56d11885fae23 base/alpine.bst 
     waiting 125099c65de3a18455f11c5a4a7379df93d5638460906c7f183cfae1b21a651b base.bst 
     waiting ec74614e1f763b990614c97d00ce3494c11b7a0729fc8a914f8e8e4580599949 hello.bst Workspace: /home/user/developing/workspace_hello
===============================================================================
[--:--:--][179c6ae9][   build:base/alpine.bst               ] START   developing/base-alpine/179c6ae9-build.31085.log
[--:--:--][ec74614e][   fetch:hello.bst                     ] START   developing/hello/ec74614e-fetch.31086.log
[--:--:--][179c6ae9][   build:base/alpine.bst               ] START   Staging sources
[00:00:00][ec74614e][   fetch:hello.bst                     ] SUCCESS developing/hello/ec74614e-fetch.31086.log
[00:00:00][179c6ae9][   build:base/alpine.bst               ] SUCCESS Staging sources
[--:--:--][179c6ae9][   build:base/alpine.bst               ] START   Caching artifact
[00:00:00][179c6ae9][   build:base/alpine.bst               ] SUCCESS Caching artifact
[00:00:00][179c6ae9][   build:base/alpine.bst               ] SUCCESS developing/base-alpine/179c6ae9-build.31085.log
[--:--:--][125099c6][   build:base.bst                      ] START   developing/base/125099c6-build.31096.log
[--:--:--][125099c6][   build:base.bst                      ] START   Caching artifact
[00:00:00][125099c6][   build:base.bst                      ] SUCCESS Caching artifact
[00:00:00][125099c6][   build:base.bst                      ] SUCCESS developing/base/125099c6-build.31096.log
[--:--:--][ec74614e][   build:hello.bst                     ] START   developing/hello/ec74614e-build.31100.log
[--:--:--][ec74614e][   build:hello.bst                     ] START   Staging dependencies
[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS Staging dependencies
[--:--:--][ec74614e][   build:hello.bst                     ] START   Integrating sandbox
[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS Integrating sandbox
[--:--:--][ec74614e][   build:hello.bst                     ] START   Staging sources
[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS Staging sources
[--:--:--][ec74614e][   build:hello.bst                     ] START   Running build-commands
[--:--:--][ec74614e][   build:hello.bst                     ] STATUS  Running command

    make PREFIX="/usr"

[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS Running build-commands
[--:--:--][ec74614e][   build:hello.bst                     ] START   Running install-commands
[--:--:--][ec74614e][   build:hello.bst                     ] STATUS  Running command

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

[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS Running install-commands
[--:--:--][ec74614e][   build:hello.bst                     ] START   Running strip-commands
[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS Running strip-commands
[--:--:--][ec74614e][   build:hello.bst                     ] START   Caching artifact
[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS Caching artifact
[00:00:00][ec74614e][   build:hello.bst                     ] SUCCESS developing/hello/ec74614e-build.31100.log
[00:00:01][        ][    main:core activity                 ] SUCCESS Build

Pipeline Summary
  Total:       3
  Session:     3
  Fetch Queue: processed 1, skipped 2, failed 0 
  Build Queue: processed 3, skipped 0, failed 0

Note that if you run the command from inside the workspace, the element name is optional.

user@host:~/workspace_hello$ bst build

[--:--:--][        ][    main:core activity                 ] START   Build
[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] START   Checking sources
[00:00:00][        ][    main:core activity                 ] SUCCESS Checking sources

BuildStream Version 1.93.1+172.g03ccfcd3b
  Session Start: Friday, 17-04-2020 at 20:51:52
  Project:       developing (/codethink/GNOME/buildstream/doc/examples/developing)
  Targets:       hello.bst

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

Pipeline
      cached 179c6ae937e8e2ece3192ab8dc2a55053134a591c9ccd37507b56d11885fae23 base/alpine.bst 
      cached 125099c65de3a18455f11c5a4a7379df93d5638460906c7f183cfae1b21a651b base.bst 
      cached ec74614e1f763b990614c97d00ce3494c11b7a0729fc8a914f8e8e4580599949 hello.bst Workspace: /home/user/workspace_hello
===============================================================================
[00:00:00][        ][    main:core activity                 ] SUCCESS Build

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

Now running the hello command using bst shell:

user@host:~/developing$ bst shell hello.bst -- hello

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:hello.bst                     ] START   Staging dependencies
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Staging dependencies
[--:--:--][        ][    main:hello.bst                     ] START   Integrating sandbox
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Integrating sandbox
[--:--:--][        ][    main:hello.bst                     ] STATUS  Running command

    hello

Hello World
We can use workspaces!

This gives us the new message we changed in hello.c.

From this point we have several options. If the source is under version control we can commit our changes and push them to the remote repository.

1.3. Incremental builds

Once you have opened up your workspace, the workspace build directory will be reused for subsequent builds, which should improve your edit/compile/test cycle time when working with an open workspace.

In order to optimize incremental builds, BuildStream treats build configure steps separately from the main build steps, and will only call the Element.prepare() method on an element plugin the first time it gets built. This avoids needlessly rebuilding objects due to header files and such being unconditionally recreated by configuration scripts (such as the typical ./configure script which is called for autotools elements for instance).

A caveat of this optimization however is that changes you might make to the configuration scripts will not be taken into account by default on the next incremental build. A forced reconfiguration can also be required in some cases where build scripts automatically detect sources in it’s configuration phase, so newly added sources you add might be ignored.

In order to force the configuration step to be called again on the next build, you can use bst workspace reset --soft, like so:

In these cases, you can perform a hard reset on the workspace using bst workspace reset, like so:

user@host:~/developing$ bst workspace reset --soft hello.bst

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] INFO    Reset workspace state for hello.bst at: /home/user/developing/workspace_hello

This will ensure that the next time you run the build, BuildStream will forcefully rerun the Element.prepare() method and cause the configuration step to occur again.

1.4. Closing your workspace

If we want to close the workspace and come back to our changes later, we can

user@host:~/developing$ bst workspace close hello.bst

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] INFO    Closed workspace for hello.bst

We can then reopen the workspace later using:

user@host:~/developing$ bst workspace open --no-checkout --directory workspace_hello hello.bst

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] INFO    Creating workspace for element hello.bst
[--:--:--][        ][    main:core activity                 ] INFO    Created a workspace for element: hello.bst

The –no-checkout option tells BuildStream not to check the source out but to instead hard-link to the workspace_hello directory.

Alternatively, if we wish to discard the changes we can use

user@host:~/developing$ bst workspace reset hello.bst

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] START   Removing workspace directory /home/user/developing/workspace_hello
[00:00:00][        ][    main:core activity                 ] SUCCESS Removing workspace directory /home/user/developing/workspace_hello
[--:--:--][        ][    main:core activity                 ] INFO    Closed workspace for hello.bst
[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] START   Checking sources
[00:00:00][        ][    main:core activity                 ] SUCCESS Checking sources
[--:--:--][        ][    main:core activity                 ] INFO    Creating workspace for element hello.bst
[--:--:--][        ][    main:hello.bst                     ] START   Staging sources to /home/user/developing/workspace_hello
[--:--:--][        ][    main:hello.bst                     ] START   Staging local files into CAS
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Staging local files into CAS
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Staging sources to /home/user/developing/workspace_hello
[--:--:--][        ][    main:core activity                 ] INFO    Created a workspace for element: hello.bst

This resets the workspace to its original state.

To discard the workspace completely we can do:

user@host:~/developing$ bst workspace close --remove-dir hello.bst

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] START   Removing workspace directory /home/user/developing/workspace_hello
[00:00:00][        ][    main:core activity                 ] SUCCESS Removing workspace directory /home/user/developing/workspace_hello
[--:--:--][        ][    main:core activity                 ] INFO    Closed workspace for hello.bst

This will close the workspace and completely remove the workspace_hello directory.