| 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 |
.. _developing_strict_mode: |
| 17 |
|
| 18 |
Strict mode |
| 19 |
=========== |
| 20 |
In this section, we will cover the usage of :ref:`strict vs non-strict <user_config_strict_mode>` |
| 21 |
build plans in conjunction with :ref:`workspaces <developing_workspaces>`, and how this |
| 22 |
can help to improve your edit/compile/test cycles. |
| 23 |
|
| 24 |
.. note:: |
| 25 |
|
| 26 |
This example is distributed with BuildStream |
| 27 |
in the `doc/examples/strict-mode |
| 28 |
<https://github.com/apache/buildstream/tree/master/doc/examples/strict-mode>`_ |
| 29 |
subdirectory. |
| 30 |
|
| 31 |
|
| 32 |
Overview |
| 33 |
-------- |
| 34 |
When working with BuildStream to create integrations, it is typical that you have a |
| 35 |
lot of components to build, and you frequently need to modify a component |
| 36 |
at various levels of the stack. When developing one or more applications, you might |
| 37 |
want to open a workspace and fix a bug in an application, or you might need to |
| 38 |
open a workspace on a low level shared library to fix the behavior of one or |
| 39 |
more misbehaving applications. |
| 40 |
|
| 41 |
By default, BuildStream will always choose to be deterministic in order to |
| 42 |
produce the most correct build results as possible. As such, modifying a low |
| 43 |
level library will result in rebuilding all of it's reverse dependencies, but |
| 44 |
this can be very time consuming and inconvenient for your edit/compile/test |
| 45 |
cycles. |
| 46 |
|
| 47 |
This is when enabling :ref:`non-strict build plans <user_config_strict_mode>` |
| 48 |
can be helpful. |
| 49 |
|
| 50 |
To illustrate the facets of how this works, this example will present a project |
| 51 |
consisting of an application which is linked both statically and dynamically |
| 52 |
linked to a common library. |
| 53 |
|
| 54 |
|
| 55 |
Project structure |
| 56 |
----------------- |
| 57 |
This project is mostly based on the :ref:`integration commands <tutorial_integration_commands>` |
| 58 |
example, as such we will ignore large parts of this project and only focus |
| 59 |
on the elements which are of specific interest. |
| 60 |
|
| 61 |
To illustrate the relationship of these two applications and the library, |
| 62 |
let's briefly take a look at the underlying Makefiles which are used in this |
| 63 |
project, starting with the library and followed by both Makefiles used to |
| 64 |
build the application. |
| 65 |
|
| 66 |
|
| 67 |
``files/libhello/Makefile`` |
| 68 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 69 |
|
| 70 |
.. literalinclude:: ../../examples/strict-mode/files/libhello/Makefile |
| 71 |
:language: Makefile |
| 72 |
|
| 73 |
|
| 74 |
``files/hello/Makefile.dynamic`` |
| 75 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 76 |
|
| 77 |
.. literalinclude:: ../../examples/strict-mode/files/hello/Makefile.dynamic |
| 78 |
:language: Makefile |
| 79 |
|
| 80 |
|
| 81 |
``files/hello/Makefile.static`` |
| 82 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 83 |
|
| 84 |
.. literalinclude:: ../../examples/strict-mode/files/hello/Makefile.static |
| 85 |
:language: Makefile |
| 86 |
|
| 87 |
As we can see, we have a library that is distributed both as the dynamic |
| 88 |
library ``libhello.so`` and also as the static archive ``libhello.a``. |
| 89 |
|
| 90 |
Now let's take a look at the two separate elements which build the |
| 91 |
application, first the dynamically linked version and then the static one. |
| 92 |
|
| 93 |
|
| 94 |
``elements/hello-dynamic.bst`` |
| 95 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 96 |
|
| 97 |
.. literalinclude:: ../../examples/strict-mode/elements/hello-dynamic.bst |
| 98 |
:language: yaml |
| 99 |
|
| 100 |
Nothing very special to observe about this hello program, just a |
| 101 |
:mod:`manual <elements.manual>` element quite similar to the one we've |
| 102 |
already seen in the :ref:`running commands <tutorial_running_commands>` |
| 103 |
example. |
| 104 |
|
| 105 |
|
| 106 |
``elements/hello-static.bst`` |
| 107 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 108 |
|
| 109 |
.. literalinclude:: ../../examples/strict-mode/elements/hello-static.bst |
| 110 |
:language: yaml |
| 111 |
|
| 112 |
Almost the same as the dynamic element, except here we have declared the |
| 113 |
dependency to the ``libhello.bst`` element differently: this time we have enabled |
| 114 |
the ``strict`` option in the :ref:`dependency declaration <format_dependencies>`. |
| 115 |
|
| 116 |
The side effect of setting this option is that ``hello-static.bst`` will be |
| 117 |
rebuilt any time that ``libhello.bst`` has changed, even when |
| 118 |
:ref:`non-strict build plans <user_config_strict_mode>` have been enabled. |
| 119 |
|
| 120 |
.. tip:: |
| 121 |
|
| 122 |
Some element plugins are designed to consume the content of their |
| 123 |
dependencies entirely, and output an artifact without any transient |
| 124 |
runtime dependencies, an example of this is the :mod:`compose <elements.compose>` |
| 125 |
element. |
| 126 |
|
| 127 |
In cases such as :mod:`compose <elements.compose>`, it is not necessary to |
| 128 |
explicitly annotate their dependencies as ``strict``. |
| 129 |
|
| 130 |
It is only helpful to set the ``strict`` attribute on a |
| 131 |
:ref:`dependency declaration <format_dependencies>` in the case that the |
| 132 |
specific dependency relationship causes data to be consumed verbatim, |
| 133 |
as is the case with static linking. |
| 134 |
|
| 135 |
|
| 136 |
Using the project |
| 137 |
----------------- |
| 138 |
For the sake of brevity, let's assume that you've already built all of the |
| 139 |
elements of this project, and that you want to make some changes to the |
| 140 |
``libhello.bst`` element, and test how it might effect the hello program. |
| 141 |
|
| 142 |
|
| 143 |
Everything is already built |
| 144 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 145 |
|
| 146 |
.. raw:: html |
| 147 |
:file: ../sessions/strict-mode-show-initial.html |
| 148 |
|
| 149 |
|
| 150 |
Open a workspace and modify libhello.c |
| 151 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 152 |
Now let's open up a workspace on the hello library |
| 153 |
|
| 154 |
.. raw:: html |
| 155 |
:file: ../sessions/strict-mode-workspace-open.html |
| 156 |
|
| 157 |
And go ahead and make a modification like this: |
| 158 |
|
| 159 |
.. literalinclude:: ../../examples/strict-mode/update.patch |
| 160 |
:language: diff |
| 161 |
|
| 162 |
|
| 163 |
Observing ``hello-dynamic.bst`` |
| 164 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 165 |
Let's take a look at the :ref:`bst show <invoking_show>` output for |
| 166 |
the dynamically linked ``hello-dynamic.bst`` element. |
| 167 |
|
| 168 |
.. raw:: html |
| 169 |
:file: ../sessions/strict-mode-show-dynamic-strict.html |
| 170 |
|
| 171 |
As one might expect, the ``libhello.bst`` element is ready to be built |
| 172 |
after having been modified, and the ``hello-dynamic.bst`` element is |
| 173 |
waiting for ``libhello.bst`` to be built before it can build. |
| 174 |
|
| 175 |
Now let's take a look at the same elements if we pass the ``--no-strict`` |
| 176 |
option to ``bst``: |
| 177 |
|
| 178 |
.. raw:: html |
| 179 |
:file: ../sessions/strict-mode-show-dynamic-no-strict.html |
| 180 |
|
| 181 |
Note that this time, the ``libhello.bst`` still needs to be built, |
| 182 |
but the ``hello-dymamic.bst`` element is showing up as ``cached``. |
| 183 |
|
| 184 |
.. tip:: |
| 185 |
|
| 186 |
The :ref:`bst show <invoking_show>` output will show some cache |
| 187 |
keys dimmed out in the case that they are not entirely deterministic. |
| 188 |
|
| 189 |
Here we can see that ``hello-dynamic.bst`` is dimmed out because |
| 190 |
it will not be rebuilt against the changed ``libhello.bst`` element, |
| 191 |
and it also has a different cache key because of this. |
| 192 |
|
| 193 |
|
| 194 |
Observing ``hello-static.bst`` |
| 195 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 196 |
Now let's observe the ``hello-static.bst`` element with strict mode |
| 197 |
disabled: |
| 198 |
|
| 199 |
.. raw:: html |
| 200 |
:file: ../sessions/strict-mode-show-static-no-strict.html |
| 201 |
|
| 202 |
Note that in this case the ``hello-strict.bst`` is going to be |
| 203 |
rebuilt even in strict mode. This is because we annotated the |
| 204 |
declaration of the ``libhello.bst`` dependency with the ``strict`` |
| 205 |
attribute. |
| 206 |
|
| 207 |
We did this because ``hello-strict.bst`` consumes the input of |
| 208 |
``libhello.bst`` verbatim, by way of statically linking to it, instead |
| 209 |
of merely being affected by the content of ``libhello.bst`` at runtime, |
| 210 |
as would be the case of static linking. |
| 211 |
|
| 212 |
|
| 213 |
Building and running ``hello-dynamic.bst`` |
| 214 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 215 |
Now let's build ``hello-dynamic.bst`` with strict mode disabled. |
| 216 |
|
| 217 |
.. raw:: html |
| 218 |
:file: ../sessions/strict-mode-build-dynamic-no-strict.html |
| 219 |
|
| 220 |
Note that the :ref:`bst build <invoking_build>` command completed without |
| 221 |
having to build ``hello-dynamic.bst`` at all. |
| 222 |
|
| 223 |
And now we can also run ``hello-dynamic.bst`` |
| 224 |
|
| 225 |
.. raw:: html |
| 226 |
:file: ../sessions/strict-mode-run-dynamic-no-strict.html |
| 227 |
|
| 228 |
When running ``hello-dynamic.bst`` with no-strict mode, we are |
| 229 |
actually reusing the old build of ``hello-dynamic.bst`` staged against |
| 230 |
the new build of the modified ``libhello.bst`` element. |
| 231 |
|
| 232 |
|
| 233 |
Building and running ``hello-static.bst`` |
| 234 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 235 |
Finally, if we build ``hello-static.bst`` with strict mode disabled, |
| 236 |
we can see that it will be rebuilt regardless of strict mode being |
| 237 |
enabled. |
| 238 |
|
| 239 |
.. raw:: html |
| 240 |
:file: ../sessions/strict-mode-build-static-no-strict.html |
| 241 |
|
| 242 |
This is of course because we declared its dependency on ``libhello.bst`` |
| 243 |
as a ``strict`` dependency. |
| 244 |
|
| 245 |
And by the same virtue, we can see that when we run the example |
| 246 |
it has properly relinked against the changed static archive, and |
| 247 |
has the updated text in the greeting: |
| 248 |
|
| 249 |
.. raw:: html |
| 250 |
:file: ../sessions/strict-mode-run-static-no-strict.html |
| 251 |
|
| 252 |
|
| 253 |
Summary |
| 254 |
------- |
| 255 |
In this chapter we've explored how to use :ref:`non-strict build plans <user_config_strict_mode>` |
| 256 |
in order to avoid rebuilding reverse dependencies of a lower level |
| 257 |
element you might be working with in a :ref:`workspace <invoking_workspace_open>`, |
| 258 |
consequently improving your edit/compile/test experience. |
| 259 |
|
| 260 |
We've also explained how to ensure your project still works properly |
| 261 |
with non-strict build plans when some elements perform static linking |
| 262 |
(or other operations which consume data from their dependencies |
| 263 |
verbatim), by annotating :ref:`dependency declarations <format_dependencies>` |
| 264 |
as ``strict``. |