Debian Codegraph @

doc/source/handling-files/composition.rst

raw
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_composition:
17
18 Composition
19 ===========
20 In this chapter we will explore how to create *compositions* of multiple
21 input filesystem trees, using the :mod:`compose <elements.compose>` element.
22
23 .. note::
24
25 This example is distributed with BuildStream
26 in the `doc/examples/composition
27 <https://github.com/apache/buildstream/tree/master/doc/examples/composition>`_
28 subdirectory.
29
30
31 Overview
32 --------
33 Composing a directory tree based on a set of build dependencies is often
34 one of the important steps you might perform in order to create a single artifact
35 which can be checked out and deployed.
36
37 In order to use the :mod:`compose <elements.compose>` element, it is important
38 to first understand the concept of :ref:`split rules <public_split_rules>`, which
39 we will cover in this chapter.
40
41
42 Introducing split rules
43 ~~~~~~~~~~~~~~~~~~~~~~~
44 The :ref:`split rules <public_split_rules>` of an element declaration denote
45 which sets of files in the given element's resulting artifact belong to which
46 *domain name*.
47
48 The *domains* can then be used in various ways, using plugins which understand
49 *split rule domains*.
50
51 BuildStream's :ref:`default project configuration <project_builtin_defaults>`
52 contains a sensible set of default *split rule domains* for the purpose of
53 artifact splitting, they can be overridden in :ref:`your project.conf <project_split_rules>`,
54 and finally on a per element basis in the :ref:`public data <public_builtin>`
55 of your element declarations.
56
57 .. note::
58
59 Projects are free to add additional *split rule domains* on top of the
60 default domains provided by the default project configuration.
61
62 There is nothing wrong with defining split rule domains which *overlap*,
63 possibly capturing some of the same files also captured by another
64 *domain*, however you should be aware of this when later using your
65 split rules with a plugin which processes them, like the
66 :mod:`compose <elements.compose>` element described in this chapter.
67
68
69 Example of split rule declaration
70 '''''''''''''''''''''''''''''''''
71 In an element, you might need to define or extend the ``split-rules``
72 in order to capture files in custom locations in a logical *domain*.
73
74 Here is an example of how you might use the
75 :ref:`list append directive <format_directives_list_append>`
76 to append an additional rule to your ``split-rules`` list in order to
77 capture additional data files which your application or library might
78 want to include in the *runtime domain*:
79
80 .. code:: yaml
81
82 # Add our .dat files to the runtime domain
83 public:
84 bst:
85 split-rules:
86 runtime:
87 (>):
88 - |
89 %{datadir}/foo/*.dat
90
91 Split rules are absolute paths which denote files within an artifact's root
92 directory. The globbing patterns supported in split rules are defined in the
93 :func:`reference documentation here <buildstream.utils.glob>`.
94
95 .. important::
96
97 Note that because of variable expansion, split rules can often be
98 *resolved differently* for elements which have overridden path
99 related variables, like ``%{prefix}``.
100
101 This usually means that you do not need to explicitly extend or override
102 split rules on a specific element unless your element installs files to
103 special case locations.
104
105
106 Project structure
107 -----------------
108 In this example we expand on the chapter about
109 :ref:`integration commands <tutorial_integration_commands>`, so we will
110 only discuss the files which are added or changed from that example.
111
112
113 ``elements/base/alpine.bst``
114 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115
116 .. literalinclude:: ../../examples/composition/elements/base/alpine.bst
117 :language: yaml
118
119 Here we have modified the base runtime, so as to specify that for this
120 element, we want to also include the runtime linker into the *runtime domain*.
121
122
123 ``elements/runtime-only.bst``
124 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125
126 .. literalinclude:: ../../examples/composition/elements/runtime-only.bst
127 :language: yaml
128
129 As we can see, this :mod:`compose <elements.compose>` element has
130 been configured to only include files from the *runtime domain*.
131
132
133 Using the project
134 -----------------
135 Now that we've presented how :ref:`split rules <public_split_rules>`
136 work and shown how to use them in the context of this example, lets
137 use the :mod:`compose <elements.compose>` element we've created and
138 observe the results.
139
140
141 Building the project
142 ~~~~~~~~~~~~~~~~~~~~
143
144 .. raw:: html
145 :file: ../sessions/composition-build.html
146
147 As you can see in the output, this composition has only a few hundred
148 files, but the complete ``alpine.bst`` runtime has several thousand
149 files.
150
151
152 List the content
153 ~~~~~~~~~~~~~~~~
154 At the risk of this being a long list, let's :ref:`list the
155 contents of this artifact <invoking_artifact_list_contents>`
156
157 .. raw:: html
158 :file: ../sessions/composition-list-contents.html
159
160 Some things to observe here:
161
162 * The list does include the ``/usr/bin/hello`` program and
163 also the ``/usr/lib/libhello.so`` shared library.
164
165 These paths are both captured by the default split rules
166 for the *runtime domain*.
167
168 * The list does not include the ``/usr/include/libhello.h``
169 header file which was used to compile ``/usr/bin/hello``.
170
171 The header file is not captured by the *runtime domain*
172 by default. It is however captured by the *devel domain*.
173
174 * The runtime linker ``/lib/ld-musl-x86_64.so.1``, as this was
175 explicitly added to the *runtime domain* for the ``base/alpine.bst``
176 element which provides this file.
177
178 .. tip::
179
180 The reader at this time might want to list the content of
181 other elements built from this project, such as the
182 ``hello.bst`` element by itself, or the ``base/alpine.bst``
183 element.
184
185
186 Run the program
187 ~~~~~~~~~~~~~~~
188 Finally, lets just run the program we built.
189
190 .. raw:: html
191 :file: ../sessions/composition-shell.html
192
193 Here we can see that we at least have the required files to run
194 our hello world program, however we would not have if we were
195 missing the runtime linker which we added in ``base/alpine.bst``.
196
197
198 Summary
199 -------
200 In this chapter we've gotten familiar with :ref:`split rules <public_split_rules>`
201 annotations, and we've learned enough about the :mod:`compose <elements.compose>`
202 element such that we can start creating our own compositions using
203 *split domains*.
204
205 We've also used the :ref:`list append directive <format_directives_list_append>`
206 and we are now observing the contents of artifacts using
207 :ref:`bst artifact list-contents <invoking_artifact_list_contents>`.