| 1 |
.. |
| 2 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
you may not use this file except in compliance with the License. |
| 4 |
You may obtain a copy of the License at |
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software |
| 9 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
See the License for the specific language governing permissions and |
| 12 |
limitations under the License. |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
.. _tutorial_running_commands: |
| 17 |
|
| 18 |
Running commands |
| 19 |
================ |
| 20 |
In :ref:`the first chapter <tutorial_first_project>` we only imported |
| 21 |
a file to create an artifact, this time lets run some commands inside |
| 22 |
the :ref:`isolated build sandbox <sandboxing>`. |
| 23 |
|
| 24 |
.. note:: |
| 25 |
|
| 26 |
This example is distributed with BuildStream |
| 27 |
in the `doc/examples/running-commands |
| 28 |
<https://github.com/apache/buildstream/tree/master/doc/examples/running-commands>`_ |
| 29 |
subdirectory. |
| 30 |
|
| 31 |
|
| 32 |
Overview |
| 33 |
-------- |
| 34 |
In this chapter, we'll be running commands inside the sandboxed |
| 35 |
execution environment and producing build output. |
| 36 |
|
| 37 |
We'll be compiling the following simple C file: |
| 38 |
|
| 39 |
|
| 40 |
``files/src/hello.c`` |
| 41 |
~~~~~~~~~~~~~~~~~~~~~ |
| 42 |
.. literalinclude:: ../../examples/running-commands/files/src/hello.c |
| 43 |
:language: c |
| 44 |
|
| 45 |
|
| 46 |
And we're going to build it using ``make``, using the following Makefile: |
| 47 |
|
| 48 |
|
| 49 |
``files/src/Makefile`` |
| 50 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 51 |
|
| 52 |
.. literalinclude:: ../../examples/running-commands/files/src/Makefile |
| 53 |
:language: Makefile |
| 54 |
|
| 55 |
|
| 56 |
We'll be using the most fundamental :ref:`build element <plugins_elements>`, |
| 57 |
the :mod:`manual <elements.manual>` build element. |
| 58 |
|
| 59 |
The :mod:`manual <elements.manual>` element is the backbone on which all the other |
| 60 |
build elements are built, so understanding how it works at this level is helpful. |
| 61 |
|
| 62 |
|
| 63 |
Project structure |
| 64 |
----------------- |
| 65 |
In this project we have a ``project.conf``, a directory with some source |
| 66 |
code, and 3 element declarations. |
| 67 |
|
| 68 |
Let's first take a peek at what we need to build using :ref:`bst show <invoking_show>`: |
| 69 |
|
| 70 |
.. raw:: html |
| 71 |
:file: ../sessions/running-commands-show-before.html |
| 72 |
|
| 73 |
This time we have loaded a pipeline with 3 elements, let's go over what they do |
| 74 |
in detail. |
| 75 |
|
| 76 |
|
| 77 |
.. _tutorial_running_commands_project_conf: |
| 78 |
|
| 79 |
``project.conf`` |
| 80 |
~~~~~~~~~~~~~~~~ |
| 81 |
|
| 82 |
.. literalinclude:: ../../examples/running-commands/project.conf |
| 83 |
:language: yaml |
| 84 |
|
| 85 |
Our ``project.conf`` is very much like the last one, except that we |
| 86 |
have defined a :ref:`source alias <project_source_aliases>` for ``alpine``. |
| 87 |
|
| 88 |
.. tip:: |
| 89 |
|
| 90 |
Using :ref:`source aliases <project_source_aliases>` for groups of sources |
| 91 |
which are generally hosted together is encouraged. This allows one to globally |
| 92 |
change the access scheme or URL for a group of repositories which belong together. |
| 93 |
|
| 94 |
|
| 95 |
``elements/base/alpine.bst`` |
| 96 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 97 |
|
| 98 |
.. literalinclude:: ../../examples/running-commands/elements/base/alpine.bst |
| 99 |
:language: yaml |
| 100 |
|
| 101 |
This :mod:`import <elements.import>` element uses a :mod:`tar <sources.tar>` |
| 102 |
source to download our Alpine Linux tarball to create our base runtime. |
| 103 |
|
| 104 |
This tarball is a sysroot which provides the C runtime libraries |
| 105 |
and some programs - this is what will be providing the programs we're |
| 106 |
going to run in this example. |
| 107 |
|
| 108 |
|
| 109 |
``elements/base.bst`` |
| 110 |
~~~~~~~~~~~~~~~~~~~~~ |
| 111 |
|
| 112 |
.. literalinclude:: ../../examples/running-commands/elements/base.bst |
| 113 |
:language: yaml |
| 114 |
|
| 115 |
This is just a symbolic :mod:`stack <elements.stack>` element which declares that |
| 116 |
anything which depends on it, will implicitly depend on ``base/alpine.bst``. |
| 117 |
|
| 118 |
It is typical to use stack elements in places where the implementing logical |
| 119 |
software stack could change, but you rather not have your higher level components |
| 120 |
carry knowledge about those changing components. |
| 121 |
|
| 122 |
Any element which :ref:`runtime depends <format_dependencies_types>` on |
| 123 |
the ``base.bst`` will now be able to execute programs provided by the imported |
| 124 |
``base/alpine.bst`` runtime. |
| 125 |
|
| 126 |
|
| 127 |
``elements/hello.bst`` |
| 128 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 129 |
|
| 130 |
.. literalinclude:: ../../examples/running-commands/elements/hello.bst |
| 131 |
:language: yaml |
| 132 |
|
| 133 |
Finally we have the element which executes commands. Looking at the |
| 134 |
:mod:`manual <elements.manual>` element's documentation, we can see that |
| 135 |
the element configuration exposes four command lists: |
| 136 |
|
| 137 |
* ``configure-commands`` |
| 138 |
|
| 139 |
Commands which are run in preparation of a build. This is where you |
| 140 |
would normally call any configure stage build tools to configure |
| 141 |
the build how you like and generate some files needed for the build. |
| 142 |
|
| 143 |
* ``build-commands`` |
| 144 |
|
| 145 |
Commands to run the build, usually a build system will |
| 146 |
invoke the compiler for you here. |
| 147 |
|
| 148 |
* ``install-commands`` |
| 149 |
|
| 150 |
Commands to install the build results. |
| 151 |
|
| 152 |
Commands to install the build results into the target system, |
| 153 |
these should install files somewhere under ``%{install-root}``. |
| 154 |
|
| 155 |
* ``strip-commands`` |
| 156 |
|
| 157 |
Commands to doctor the build results after the install. |
| 158 |
|
| 159 |
Typically this involves stripping binaries of debugging |
| 160 |
symbols or stripping timestamps from build results to ensure |
| 161 |
reproducibility. |
| 162 |
|
| 163 |
.. tip:: |
| 164 |
|
| 165 |
All other :ref:`build elements <core_buildelement_builtins>` |
| 166 |
implement exactly the same command lists too, except that they provide |
| 167 |
default commands specific to invoke the build systems they support. |
| 168 |
|
| 169 |
The :mod:`manual <elements.manual>` element however is the most basic |
| 170 |
and does not provide any default commands, so we have instructed it |
| 171 |
to use ``make`` to build and install our program. |
| 172 |
|
| 173 |
|
| 174 |
Using the project |
| 175 |
----------------- |
| 176 |
|
| 177 |
|
| 178 |
Build the hello.bst element |
| 179 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 180 |
To build the project, run :ref:`bst build <invoking_build>` in the |
| 181 |
following way: |
| 182 |
|
| 183 |
.. raw:: html |
| 184 |
:file: ../sessions/running-commands-build.html |
| 185 |
|
| 186 |
Now we've built our hello world program, using ``make`` |
| 187 |
and the C compiler provided by the Alpine Linux image. |
| 188 |
|
| 189 |
In the :ref:`first chapter <tutorial_first_project>` we observed that the inputs |
| 190 |
and output of an element are *directory trees*. In this example, the directory tree |
| 191 |
generated by ``base/alpine.bst`` is consumed by ``hello.bst`` due to the |
| 192 |
:ref:`implicit runtime dependency <format_dependencies_types>` introduced by ``base.bst``. |
| 193 |
|
| 194 |
.. tip:: |
| 195 |
|
| 196 |
All of the :ref:`dependencies <format_dependencies>` which are required to run for |
| 197 |
the sake of a build, are staged at the root of the build sandbox. These comprise the |
| 198 |
runtime environment in which the depending element will run commands. |
| 199 |
|
| 200 |
The result is that the ``make`` program and C compiler provided by ``base/alpine.bst`` |
| 201 |
were already in ``$PATH`` and ready to run when the commands were needed by ``hello.bst``. |
| 202 |
|
| 203 |
Now observe that all of the elements in the loaded pipeline are ``cached``, |
| 204 |
the element is *built*: |
| 205 |
|
| 206 |
.. raw:: html |
| 207 |
:file: ../sessions/running-commands-show-after.html |
| 208 |
|
| 209 |
|
| 210 |
Run the hello world program |
| 211 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 212 |
Now that we've built everything, we can indulge ourselves in running |
| 213 |
the hello world program using :ref:`bst shell <invoking_shell>`: |
| 214 |
|
| 215 |
.. raw:: html |
| 216 |
:file: ../sessions/running-commands-shell.html |
| 217 |
|
| 218 |
Here, :ref:`bst shell <invoking_build>` created a runtime environment for running |
| 219 |
the ``hello.bst`` element. This was done by staging all of the dependencies of |
| 220 |
``hello.bst`` including the ``hello.bst`` output itself into a directory. Once a directory |
| 221 |
with all of the dependencies was staged and ready, we ran the ``hello`` command from |
| 222 |
within the build sandbox environment. |
| 223 |
|
| 224 |
.. tip:: |
| 225 |
|
| 226 |
When specifying a command for :ref:`bst shell <invoking_shell>` to run, |
| 227 |
we always specify ``--`` first. This is a commonly understood shell syntax |
| 228 |
to indicate that the remaining arguments are to be treated literally. |
| 229 |
|
| 230 |
Specifying ``--`` is optional and disambiguates BuildStream's arguments |
| 231 |
and options from those of the program being run by |
| 232 |
:ref:`bst shell <invoking_shell>`. |
| 233 |
|
| 234 |
|
| 235 |
Summary |
| 236 |
------- |
| 237 |
In this chapter we've explored how to use the :mod:`manual <elements.manual>` element, |
| 238 |
which forms the basis of all build elements. |
| 239 |
|
| 240 |
We've also observed how the directory tree from the output *artifact* of one element |
| 241 |
is later *staged* at the root of the sandbox, as input for use by any build elements |
| 242 |
which :ref:`depend <format_dependencies>` on that element. |
| 243 |
|
| 244 |
.. tip:: |
| 245 |
|
| 246 |
The way that elements consume their dependency input can vary across the |
| 247 |
different *kinds* of elements. This chapter describes how it works for |
| 248 |
:mod:`build elements <buildstream.buildelement>` implementations, which |
| 249 |
are the most commonly used element type. |