| 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_directives: |
| 17 |
|
| 18 |
Optionality and directives |
| 19 |
========================== |
| 20 |
In this chapter we're going to go over some of the more flexible constructs |
| 21 |
which BuildStream offers for :ref:`optionality <project_options>`, and |
| 22 |
show how we can use :ref:`directives <format_directives>` in the BuildStream |
| 23 |
YAML format. |
| 24 |
|
| 25 |
.. note:: |
| 26 |
|
| 27 |
This example is distributed with BuildStream |
| 28 |
in the `doc/examples/directives |
| 29 |
<https://github.com/apache/buildstream/tree/master/doc/examples/directives>`_ |
| 30 |
subdirectory. |
| 31 |
|
| 32 |
|
| 33 |
Overview |
| 34 |
-------- |
| 35 |
This chapter's example will build another ``hello.c`` program which much |
| 36 |
resembles the program in the :ref:`running commands <tutorial_running_commands>` example, |
| 37 |
but here we're going to make the greeting string *configurable* using the C |
| 38 |
preprocessor. |
| 39 |
|
| 40 |
We'll be compiling the following C file: |
| 41 |
|
| 42 |
|
| 43 |
``files/src/hello.c`` |
| 44 |
~~~~~~~~~~~~~~~~~~~~~ |
| 45 |
.. literalinclude:: ../../examples/directives/files/src/hello.c |
| 46 |
:language: c |
| 47 |
|
| 48 |
And we're going to build it using ``make``, using the following Makefile: |
| 49 |
|
| 50 |
|
| 51 |
``files/src/Makefile`` |
| 52 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 53 |
|
| 54 |
.. literalinclude:: ../../examples/directives/files/src/Makefile |
| 55 |
:language: Makefile |
| 56 |
|
| 57 |
Notice the addition of ``-DGREETING_MESSAGE="\"${GREETING}\""`` in the above |
| 58 |
Makefile, this will allow us to configure the greeting message from the |
| 59 |
``hello.bst`` element declaration. |
| 60 |
|
| 61 |
We will need to add support to our project for *optionality*, and we'll |
| 62 |
have to make *conditional statements* to resolve what kind of greeting |
| 63 |
we want from the hello world program. |
| 64 |
|
| 65 |
|
| 66 |
Project structure |
| 67 |
----------------- |
| 68 |
Since this project has much the same structure as the |
| 69 |
:ref:`running commands <tutorial_running_commands>` chapter did, we won't go over all of |
| 70 |
these elements in detail. Instead let's focus on the addition of the new |
| 71 |
:ref:`project options <project_options>` in ``project.conf``, the added |
| 72 |
file in the ``include/`` project subdirectory, and how these come together |
| 73 |
in the the ``hello.bst`` element. |
| 74 |
|
| 75 |
|
| 76 |
``project.conf`` |
| 77 |
~~~~~~~~~~~~~~~~ |
| 78 |
|
| 79 |
.. literalinclude:: ../../examples/directives/project.conf |
| 80 |
:language: yaml |
| 81 |
|
| 82 |
Here, our ``project.conf`` declares a project option called ``flavor``, and this |
| 83 |
will inform what kind of greeting message we want to use when building the project. |
| 84 |
|
| 85 |
|
| 86 |
``elements/hello.bst`` |
| 87 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 88 |
|
| 89 |
.. literalinclude:: ../../examples/directives/elements/hello.bst |
| 90 |
:language: yaml |
| 91 |
|
| 92 |
Notice the ``(@)`` symbol we've added in the ``variables:`` section, this |
| 93 |
symbol is used to invoke the :ref:`include directive <format_directives_include>`, |
| 94 |
which can be useful for code sharing between elements or simply to improve readability. |
| 95 |
|
| 96 |
In this case, we are compositing the content of ``include/greeting.bst`` into the |
| 97 |
:ref:`variables <format_variables>` section of the element declaration, directives |
| 98 |
can however be used virtually anywhere in the BuildStream YAML format. |
| 99 |
|
| 100 |
|
| 101 |
``include/greeting.bst`` |
| 102 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 103 |
|
| 104 |
.. literalinclude:: ../../examples/directives/include/greeting.bst |
| 105 |
:language: yaml |
| 106 |
|
| 107 |
Here we can see the dictionary which will be composited into the ``variables:`` |
| 108 |
section of the ``hello.bst`` element described above. |
| 109 |
|
| 110 |
Note the usage of the ``(?)`` symbol at the toplevel of the YAML dictionary, |
| 111 |
this is how we perform :ref:`conditional statements <format_directives_conditional>` |
| 112 |
in the BuildStream YAML format. |
| 113 |
|
| 114 |
This include file uses the ``flavor`` project option we declared in ``project.conf`` to |
| 115 |
decide what value will end up being assigned to the ``%{greeting}`` variable, which |
| 116 |
will ultimately be used in the ``hello.bst`` element. |
| 117 |
|
| 118 |
|
| 119 |
Using the project |
| 120 |
----------------- |
| 121 |
Now that we have a project which uses options and conditional statements, |
| 122 |
lets build the project with a few different options and observe the outputs. |
| 123 |
|
| 124 |
|
| 125 |
Building hello.bst element with options |
| 126 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 127 |
Since the :ref:`flavor option <project_options>` we've declared above |
| 128 |
has a default, we can build it the first time using :ref:`bst build <invoking_build>` |
| 129 |
without any special command line options: |
| 130 |
|
| 131 |
.. raw:: html |
| 132 |
:file: ../sessions/directives-build-normal.html |
| 133 |
|
| 134 |
If we want to build the ``somber`` flavor, we just need to specify the |
| 135 |
additional ``--option`` command line option to :ref:`bst <invoking_bst>` |
| 136 |
in order to inform BuildStream of the options we want. |
| 137 |
|
| 138 |
.. raw:: html |
| 139 |
:file: ../sessions/directives-build-somber.html |
| 140 |
|
| 141 |
Note that the ``--option`` option can be specified many times on the |
| 142 |
``bst`` command line, so as to support projects which have multiple |
| 143 |
options. |
| 144 |
|
| 145 |
Finally lets get the ``excited`` flavor built as well: |
| 146 |
|
| 147 |
.. raw:: html |
| 148 |
:file: ../sessions/directives-build-excited.html |
| 149 |
|
| 150 |
If you observe the cache keys above, you will notice that while |
| 151 |
we have only three elements in the pipeline, counting ``base/alpine.bst``, |
| 152 |
``base.bst`` and ``hello.bst``, we have actually built *five artifacts*, |
| 153 |
because the ``hello.bst`` is built differently each time, it has a |
| 154 |
different cache key and is stored separately in the artifact cache. |
| 155 |
|
| 156 |
|
| 157 |
Run the hello world program with options |
| 158 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 159 |
Since the ``--option`` command line option to :ref:`bst <invoking_bst>` |
| 160 |
is a main option, it can be used in any command. |
| 161 |
|
| 162 |
Let's run the ``hello`` program using :ref:`bst shell <invoking_shell>` |
| 163 |
three times in a row, each time using a different option so we can |
| 164 |
observe the results. |
| 165 |
|
| 166 |
|
| 167 |
.. raw:: html |
| 168 |
:file: ../sessions/directives-shell-normal.html |
| 169 |
|
| 170 |
|
| 171 |
.. raw:: html |
| 172 |
:file: ../sessions/directives-shell-somber.html |
| 173 |
|
| 174 |
|
| 175 |
.. raw:: html |
| 176 |
:file: ../sessions/directives-shell-excited.html |
| 177 |
|
| 178 |
|
| 179 |
Summary |
| 180 |
------- |
| 181 |
In this chapter we've demonstrated how to declare :ref:`project options <project_options>`, |
| 182 |
how to use :ref:`conditional directives <format_directives_conditional>`, and also |
| 183 |
how to use :ref:`include directives <format_directives_include>`. |
| 184 |
|
| 185 |
To get more familliar with these concepts, you may want to explore the remaining |
| 186 |
:ref:`directives <format_directives>` in the BuildStream YAML format, and also take |
| 187 |
a look at the various :ref:`types of project options <project_options>` that |
| 188 |
are also supported. |