| 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 |
.. _handling_files_filtering: |
| 17 |
|
| 18 |
Filtering |
| 19 |
========= |
| 20 |
In this chapter we will explore how to *filter* the files in an artifact |
| 21 |
using the :mod:`filter <elements.filter>` element, such that an element might |
| 22 |
depend on a subset of the files provided by a filtered element. |
| 23 |
|
| 24 |
.. note:: |
| 25 |
|
| 26 |
This example is distributed with BuildStream |
| 27 |
in the `doc/examples/filtering |
| 28 |
<https://github.com/apache/buildstream/tree/master/doc/examples/filtering>`_ |
| 29 |
subdirectory. |
| 30 |
|
| 31 |
|
| 32 |
Overview |
| 33 |
-------- |
| 34 |
In some cases, it can be useful to depend on a *subset* of the files of an |
| 35 |
element, without depending on the entire element. |
| 36 |
|
| 37 |
One scenario where filtering can be useful, is when you have an element which |
| 38 |
will build differently depending on what is present in the system where it is |
| 39 |
building. In an edge case where a module fails to offer configure time options to |
| 40 |
disable an unwanted feature or behavior in the build, you might use |
| 41 |
:mod:`filter <elements.filter>` elements to ensure that special header files or |
| 42 |
pkg-config files are *filtered out* from the system at build time, such that |
| 43 |
the unwanted behavior cannot be built. |
| 44 |
|
| 45 |
In many ways, a :mod:`filter <elements.filter>` element is like a |
| 46 |
:mod:`compose <elements.compose>` element, except that it operates on a single |
| 47 |
:ref:`build dependency <format_build_depends>`, without compositing the filtered |
| 48 |
element with its :ref:`runtime dependencies <format_runtime_depends>`. |
| 49 |
|
| 50 |
.. tip:: |
| 51 |
|
| 52 |
The :mod:`filter <elements.filter>` element is special in the sense |
| 53 |
that it acts as a *window* into it's primary |
| 54 |
:ref:`build dependency <format_build_depends>`. |
| 55 |
|
| 56 |
As such, :ref:`opening a workspace <invoking_workspace_open>` on a |
| 57 |
:mod:`filter <elements.filter>` element will result in opening a |
| 58 |
workspace on the element which it filters. Any other workspace |
| 59 |
commands will also be forwarded directly to the filtered element. |
| 60 |
|
| 61 |
|
| 62 |
Project structure |
| 63 |
----------------- |
| 64 |
This example again expands on the example presenting in the chapter about |
| 65 |
:ref:`integration commands <tutorial_integration_commands>`. In this case |
| 66 |
we will modify ``libhello.bst`` such that it produces a new file which, |
| 67 |
if present, will affect the behavior of it's reverse dependency ``hello.bst``. |
| 68 |
|
| 69 |
Let's first take a look at how the sources have changed. |
| 70 |
|
| 71 |
|
| 72 |
``files/hello/Makefile`` |
| 73 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 |
|
| 75 |
.. literalinclude:: ../../examples/filtering/files/hello/Makefile |
| 76 |
:language: Makefile |
| 77 |
|
| 78 |
Now we have our Makefile discovering the system defined default |
| 79 |
person to say hello to. |
| 80 |
|
| 81 |
|
| 82 |
``files/hello/hello.c`` |
| 83 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 84 |
|
| 85 |
.. literalinclude:: ../../examples/filtering/files/hello/hello.c |
| 86 |
:language: c |
| 87 |
|
| 88 |
If this program has been given a ``DEFAULT_PERSON``, then it will |
| 89 |
say hello to that person in the absence of any argument. |
| 90 |
|
| 91 |
|
| 92 |
``project.conf`` |
| 93 |
~~~~~~~~~~~~~~~~ |
| 94 |
|
| 95 |
.. literalinclude:: ../../examples/filtering/project.conf |
| 96 |
:language: yaml |
| 97 |
|
| 98 |
Here, we've added a :ref:`project option <project_options>` to decide |
| 99 |
whether to use the :mod:`filter <elements.filter>` element or not. |
| 100 |
|
| 101 |
This is merely for brevity, so that we can demonstrate the behavior |
| 102 |
of depending on the filtered element without defining two separate versions |
| 103 |
of the ``hello.bst`` element. |
| 104 |
|
| 105 |
|
| 106 |
``elements/libhello.bst`` |
| 107 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 108 |
|
| 109 |
.. literalinclude:: ../../examples/filtering/elements/libhello.bst |
| 110 |
:language: yaml |
| 111 |
|
| 112 |
We've added some :ref:`split rules <public_split_rules>` here to declare |
| 113 |
a new *split domain* named ``defaults``, and we've added the new |
| 114 |
``default-person.txt`` file to this *domain*. |
| 115 |
|
| 116 |
|
| 117 |
``elements/libhello-filtered.bst`` |
| 118 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 119 |
|
| 120 |
.. literalinclude:: ../../examples/filtering/elements/libhello-filtered.bst |
| 121 |
:language: yaml |
| 122 |
|
| 123 |
And we've added a new :mod:`filter <elements.filter>` element to the project |
| 124 |
which uses the ``exclude`` option of the filter configuration. |
| 125 |
|
| 126 |
This is essentially a statement that any files mentioned in the the |
| 127 |
``defaults`` *domain* of the ``libhello.bst`` element should be excluded from |
| 128 |
the resulting artifact. |
| 129 |
|
| 130 |
.. important:: |
| 131 |
|
| 132 |
Notice that you need to explicitly declare any |
| 133 |
:ref:`runtime dependencies <format_runtime_depends>` which are required by the |
| 134 |
resulting artifact of a :mod:`filter <elements.filter>` element, as runtime |
| 135 |
dependencies of the build dependency are not transient. |
| 136 |
|
| 137 |
|
| 138 |
``elements/hello.bst`` |
| 139 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 140 |
|
| 141 |
.. literalinclude:: ../../examples/filtering/elements/hello.bst |
| 142 |
:language: yaml |
| 143 |
|
| 144 |
Here we've merely added a :ref:`conditional statement <format_directives_conditional>` |
| 145 |
which allows us to test the ``hello.bst`` element depending on the filtered |
| 146 |
version of the library, or the unfiltered version. |
| 147 |
|
| 148 |
|
| 149 |
Using the project |
| 150 |
----------------- |
| 151 |
Let's just skip over building the ``hello.bst`` element with the |
| 152 |
``use_filter`` option both ``True`` and ``False``, these elements |
| 153 |
are easily built with :ref:`bst build <invoking_build>` as such: |
| 154 |
|
| 155 |
.. code:: shell |
| 156 |
|
| 157 |
bst --option use_filter True build hello.bst |
| 158 |
bst --option use_filter False build hello.bst |
| 159 |
|
| 160 |
|
| 161 |
Observing the artifacts |
| 162 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 163 |
Let's take a look at the built artifacts. |
| 164 |
|
| 165 |
|
| 166 |
``libhello.bst`` |
| 167 |
'''''''''''''''' |
| 168 |
|
| 169 |
.. raw:: html |
| 170 |
:file: ../sessions/filtering-list-contents-libhello.html |
| 171 |
|
| 172 |
Here we can see the full content of the ``libhello.bst`` artifact. |
| 173 |
|
| 174 |
|
| 175 |
``libhello-filtered.bst`` |
| 176 |
''''''''''''''''''''''''' |
| 177 |
|
| 178 |
.. raw:: html |
| 179 |
:file: ../sessions/filtering-list-contents-libhello-filtered.html |
| 180 |
|
| 181 |
Here we can see that the ``default-person.txt`` file has been filtered |
| 182 |
out of the ``libhello.bst`` artifact when creating the ``libhello-filtered.bst`` |
| 183 |
artifact. |
| 184 |
|
| 185 |
|
| 186 |
Running hello.bst |
| 187 |
~~~~~~~~~~~~~~~~~ |
| 188 |
Now if we run the program built by ``hello.bst`` in either build |
| 189 |
modes, we can observe the expected behavior. |
| 190 |
|
| 191 |
|
| 192 |
Run ``hello.bst`` built directly against ``libhello.bst`` |
| 193 |
''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 194 |
|
| 195 |
.. raw:: html |
| 196 |
:file: ../sessions/filtering-shell-without-filter.html |
| 197 |
|
| 198 |
Here we can see that the hello world program is using the system |
| 199 |
configured default person to say hello to. |
| 200 |
|
| 201 |
|
| 202 |
Run ``hello.bst`` built against ``libhello-filtered.bst`` |
| 203 |
''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 204 |
|
| 205 |
.. raw:: html |
| 206 |
:file: ../sessions/filtering-shell-with-filter.html |
| 207 |
|
| 208 |
And now we're reverting to the behavior we have when no |
| 209 |
system configured default person was installed at build time. |
| 210 |
|
| 211 |
|
| 212 |
Summary |
| 213 |
------- |
| 214 |
In this chapter, we've introduced the :mod:`filter <elements.filter>` |
| 215 |
element which allows one to filter the output of an element and |
| 216 |
effectively create a dependency on a subset of an element's files. |