source: introduction/important/building-notes.xml@ ad7f3b7

12.0 12.1 kea ken/TL2024 ken/tuningfonts lazarus lxqt plabs/newcss python3.11 qt5new rahul/power-profiles-daemon renodr/vulkan-addition trunk xry111/llvm18 xry111/xf86-video-removal
Last change on this file since ad7f3b7 was ad7f3b7, checked in by Xi Ruoyao <xry111@…>, 13 months ago

building-notes: Restructure my runtime addition to align with changes from Pierre

BTW, mention external optional dependencies.

  • Property mode set to 100644
File size: 46.7 KB
Line 
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
4 <!ENTITY % general-entities SYSTEM "../../general.ent">
5 %general-entities;
6]>
7
8<sect1 id="unpacking">
9 <?dbhtml filename="notes-on-building.html"?>
10
11
12 <title>Notes on Building Software</title>
13
14 <para>Those people who have built an LFS system may be aware
15 of the general principles of downloading and unpacking software. Some
16 of that information is repeated here for those new to building
17 their own software.</para>
18
19 <para>Each set of installation instructions contains a URL from which you
20 can download the package. The patches; however, are stored on the LFS
21 servers and are available via HTTP. These are referenced as needed in the
22 installation instructions.</para>
23
24 <para>While you can keep the source files anywhere you like, we assume that
25 you have unpacked the package and changed into the directory created by the
26 unpacking process (the 'build' directory). We also assume you have
27 uncompressed any required patches and they are in the directory immediately
28 above the 'build' directory.</para>
29
30 <para>We can not emphasize strongly enough that you should start from a
31 <emphasis>clean source tree</emphasis> each time. This means that if
32 you have had an error during configuration or compilation, it's usually
33 best to delete the source tree and
34 re-unpack it <emphasis>before</emphasis> trying again. This obviously
35 doesn't apply if you're an advanced user used to hacking
36 <filename>Makefile</filename>s and C code, but if in doubt, start from a
37 clean tree.</para>
38
39 <sect2>
40 <title>Building Software as an Unprivileged (non-root) User</title>
41
42 <para>The golden rule of Unix System Administration is to use your
43 superpowers only when necessary. Hence, BLFS recommends that you
44 build software as an unprivileged user and only become the
45 <systemitem class='username'>root</systemitem> user when installing the
46 software. This philosophy is followed in all the packages in this book.
47 Unless otherwise specified, all instructions should be executed as an
48 unprivileged user. The book will advise you on instructions that need
49 <systemitem class='username'>root</systemitem> privileges.</para>
50
51 </sect2>
52
53 <sect2>
54 <title>Unpacking the Software</title>
55
56 <para>If a file is in <filename class='extension'>.tar</filename> format
57 and compressed, it is unpacked by running one of the following
58 commands:</para>
59
60<screen><userinput>tar -xvf filename.tar.gz
61tar -xvf filename.tgz
62tar -xvf filename.tar.Z
63tar -xvf filename.tar.bz2</userinput></screen>
64
65 <note>
66 <para>You may omit using the <option>v</option> parameter in the commands
67 shown above and below if you wish to suppress the verbose listing of all
68 the files in the archive as they are extracted. This can help speed up the
69 extraction as well as make any errors produced during the extraction
70 more obvious to you.</para>
71 </note>
72
73 <para>You can also use a slightly different method:</para>
74
75<screen><userinput>bzcat filename.tar.bz2 | tar -xv</userinput></screen>
76
77 <para>Finally, you sometimes need to be able to unpack patches which are
78 generally not in <filename class='extension'>.tar</filename> format. The
79 best way to do this is to copy the patch file to the parent of the 'build'
80 directory and then run one of the following commands depending on whether
81 the file is a <filename class='extension'>.gz</filename> or <filename
82 class='extension'>.bz2</filename> file:</para>
83
84<screen><userinput>gunzip -v patchname.gz
85bunzip2 -v patchname.bz2</userinput></screen>
86
87 </sect2>
88
89 <sect2>
90 <title>Verifying File Integrity</title>
91
92 <para>Generally, to verify that the downloaded file is complete,
93 many package maintainers also distribute md5sums of the files. To verify the
94 md5sum of the downloaded files, download both the file and the
95 corresponding md5sum file to the same directory (preferably from different
96 on-line locations), and (assuming <filename>file.md5sum</filename> is the
97 md5sum file downloaded) run the following command:</para>
98
99<screen><userinput>md5sum -c file.md5sum</userinput></screen>
100
101 <para>If there are any errors, they will be reported. Note that the BLFS
102 book includes md5sums for all the source files also. To use the BLFS
103 supplied md5sums, you can create a <filename>file.md5sum</filename> (place
104 the md5sum data and the exact name of the downloaded file on the same
105 line of a file, separated by white space) and run the command shown above.
106 Alternately, simply run the command shown below and compare the output
107 to the md5sum data shown in the BLFS book.</para>
108
109<screen><userinput>md5sum <replaceable>&lt;name_of_downloaded_file&gt;</replaceable></userinput></screen>
110
111 <para>MD5 is not cryptographically secure, so the md5sums are only
112 provided for detecting unmalicious changes to the file content. For
113 example, an error or truncation introduced during network transfer, or
114 a <quote>stealth</quote> update to the package from the upstream
115 (updating the content of a released tarball instead of making a new
116 release properly).</para>
117
118 <para>There is no <quote>100%</quote> secure way to make
119 sure the genuity of the source files. Assuming the upstream is managing
120 their website correctly (the private key is not leaked and the domain is
121 not hijacked), and the trust anchors have been set up correctly using
122 <xref linkend="make-ca"/> on the BLFS system, we can reasonably trust
123 download URLs to the upstream official website
124 <emphasis role="bold">with https protocol</emphasis>. Note that
125 BLFS book itself is published on a website with https, so you should
126 already have some confidence in https protocol or you wouldn't trust the
127 book content.</para>
128
129 <para>If the package is downloaded from an unofficial location (for
130 example a local mirror), checksums generated by cryptographically secure
131 digest algorithms (for example SHA256) can be used to verify the
132 genuity of the package. Download the checksum file from the upstream
133 <emphasis role="bold">official</emphasis> website (or somewhere
134 <emphasis role="bold">you can trust</emphasis>) and compare the
135 checksum of the package from unofficial location with it. For example,
136 SHA256 checksum can be checked with the command:</para>
137
138 <note>
139 <para>If the checksum and the package are downloaded from the same
140 untrusted location, you won't gain security enhancement by verifying
141 the package with the checksum. The attacker can fake the checksum as
142 well as compromising the package itself.</para>
143 </note>
144
145<screen><userinput>sha256sum -c <replaceable>file</replaceable>.sha256sum</userinput></screen>
146
147 <para>If <xref linkend="gnupg2"/> is installed, you can also verify the
148 genuity of the package with a GPG signature. Import the upstream GPG
149 public key with:</para>
150
151<screen><userinput>gpg --recv-key <replaceable>keyID</replaceable></userinput></screen>
152
153 <para><replaceable>keyID</replaceable> should be replaced with the key ID
154 from somewhere <emphasis role="bold">you can trust</emphasis> (for
155 example, copy it from the upstream official website using https). Now
156 you can verify the signature with:</para>
157
158<screen><userinput>gpg --recv-key <replaceable>file</replaceable>.sig <replaceable>file</replaceable></userinput></screen>
159
160 <para>The advantage of <application>GnuPG</application> signature is,
161 once you imported a public key which can be trusted, you can download
162 both the package and its signature from the same unofficial location and
163 verify them with the public key. So you won't need to connect to the
164 official upstream website to retrieve a checksum for each new release.
165 You only need to update the public key if it's expired or revoked.
166 </para>
167
168 </sect2>
169
170 <sect2>
171 <title>Creating Log Files During Installation</title>
172
173 <para>For larger packages, it is convenient to create log files instead of
174 staring at the screen hoping to catch a particular error or warning. Log
175 files are also useful for debugging and keeping records. The following
176 command allows you to create an installation log. Replace
177 <replaceable>&lt;command&gt;</replaceable> with the command you intend to execute.</para>
178
179<screen><userinput>( <replaceable>&lt;command&gt;</replaceable> 2&gt;&amp;1 | tee compile.log &amp;&amp; exit $PIPESTATUS )</userinput></screen>
180
181 <para><option>2&gt;&amp;1</option> redirects error messages to the same
182 location as standard output. The <command>tee</command> command allows
183 viewing of the output while logging the results to a file. The parentheses
184 around the command run the entire command in a subshell and finally the
185 <command>exit $PIPESTATUS</command> command ensures the result of the
186 <replaceable>&lt;command&gt;</replaceable> is returned as the result and not the
187 result of the <command>tee</command> command.</para>
188
189 </sect2>
190
191 <sect2 id="parallel-builds" xreflabel="Using Multiple Processors">
192 <title>Using Multiple Processors</title>
193
194 <para>For many modern systems with multiple processors (or cores) the
195 compilation time for a package can be reduced by performing a "parallel
196 make" by either setting an environment variable or telling the make program
197 how many processors are available. For instance, a Core2Duo can support two
198 simultaneous processes with: </para>
199
200 <screen><userinput>export MAKEFLAGS='-j2'</userinput></screen>
201
202 <para>or just building with:</para>
203
204 <screen><userinput>make -j2</userinput></screen>
205
206 <para>
207 If you have applied the optional <command>sed</command> when building
208 <application>ninja</application> in LFS, you can use:
209 </para>
210
211 <screen><userinput>export NINJAJOBS=2</userinput></screen>
212
213 <para>
214 when a package uses <command>ninja</command>, or just:
215 </para>
216
217 <screen><userinput>ninja -j2</userinput></screen>
218
219 <para>
220 but for ninja, the default number of jobs is &lt;N&gt;+2, where &lt;N&gt;
221 is the number of processors available, so that using the above commands
222 is rather for limiting the number of jobs (see below for why this could
223 be necessary).
224 </para>
225
226 <para>Generally the number of processes should not exceed the number of
227 cores supported by the CPU. To list the processors on your
228 system, issue: <userinput>grep processor /proc/cpuinfo</userinput>.
229 </para>
230
231 <para>In some cases, using multiple processes may result in a 'race'
232 condition where the success of the build depends on the order of the
233 commands run by the <command>make</command> program. For instance, if an
234 executable needs File A and File B, attempting to link the program before
235 one of the dependent components is available will result in a failure.
236 This condition usually arises because the upstream developer has not
237 properly designated all the prerequisites needed to accomplish a step in the
238 Makefile.</para>
239
240 <para>If this occurs, the best way to proceed is to drop back to a
241 single processor build. Adding '-j1' to a make command will override
242 the similar setting in the <envar>MAKEFLAGS</envar> environment
243 variable.</para>
244
245 <note><para>When running the package tests or the install portion of the
246 package build process, we do not recommend using an option greater than
247 '-j1' unless specified otherwise. The installation procedures or checks
248 have not been validated using parallel procedures and may fail with issues
249 that are difficult to debug.</para></note>
250
251 <important>
252 <para>
253 Another problem may occur with modern CPU's, which have a lot of cores.
254 Each job started consumes memory, and if the sum of the needed
255 memory for each job exceeds the available memory, you may encounter
256 either an OOM (Out of Memory) kernel interrupt or intense swapping
257 that will slow the build beyond reasonable limits.
258 </para>
259
260 <para>
261 Some compilations with <command>g++</command> may consume up to 2.5 GB
262 of memory, so to be safe, you should restrict the number of jobs
263 to (Total Memory in GB)/2.5, at least for big packages such as LLVM,
264 WebKitGtk, QtWebEngine, or libreoffice.
265 </para>
266 </important>
267 </sect2>
268
269 <sect2 id="automating-builds" xreflabel="Automated Building Procedures">
270 <title>Automated Building Procedures</title>
271
272 <para>There are times when automating the building of a package can come in
273 handy. Everyone has their own reasons for wanting to automate building,
274 and everyone goes about it in their own way. Creating
275 <filename>Makefile</filename>s, <application>Bash</application> scripts,
276 <application>Perl</application> scripts or simply a list of commands used
277 to cut and paste are just some of the methods you can use to automate
278 building BLFS packages. Detailing how and providing examples of the many
279 ways you can automate the building of packages is beyond the scope of this
280 section. This section will expose you to using file redirection and the
281 <command>yes</command> command to help provide ideas on how to automate
282 your builds.</para>
283
284 <bridgehead renderas="sect3">File Redirection to Automate Input</bridgehead>
285
286 <para>You will find times throughout your BLFS journey when you will come
287 across a package that has a command prompting you for information. This
288 information might be configuration details, a directory path, or a response
289 to a license agreement. This can present a challenge to automate the
290 building of that package. Occasionally, you will be prompted for different
291 information in a series of questions. One method to automate this type of
292 scenario requires putting the desired responses in a file and using
293 redirection so that the program uses the data in the file as the answers to
294 the questions.</para>
295
296 <para>Building the <application>CUPS</application> package is a good
297 example of how redirecting a file as input to prompts can help you automate
298 the build. If you run the test suite, you are asked to respond to a series
299 of questions regarding the type of test to run and if you have any
300 auxiliary programs the test can use. You can create a file with your
301 responses, one response per line, and use a command similar to the
302 one shown below to automate running the test suite:</para>
303
304<screen><userinput>make check &lt; ../cups-1.1.23-testsuite_parms</userinput></screen>
305
306 <para>This effectively makes the test suite use the responses in the file
307 as the input to the questions. Occasionally you may end up doing a bit of
308 trial and error determining the exact format of your input file for some
309 things, but once figured out and documented you can use this to automate
310 building the package.</para>
311
312 <bridgehead renderas="sect3">Using <command>yes</command> to Automate
313 Input</bridgehead>
314
315 <para>Sometimes you will only need to provide one response, or provide the
316 same response to many prompts. For these instances, the
317 <command>yes</command> command works really well. The
318 <command>yes</command> command can be used to provide a response (the same
319 one) to one or more instances of questions. It can be used to simulate
320 pressing just the <keycap>Enter</keycap> key, entering the
321 <keycap>Y</keycap> key or entering a string of text. Perhaps the easiest
322 way to show its use is in an example.</para>
323
324 <para>First, create a short <application>Bash</application> script by
325 entering the following commands:</para>
326
327<screen><userinput>cat &gt; blfs-yes-test1 &lt;&lt; "EOF"
328<literal>#!/bin/bash
329
330echo -n -e "\n\nPlease type something (or nothing) and press Enter ---> "
331
332read A_STRING
333
334if test "$A_STRING" = ""; then A_STRING="Just the Enter key was pressed"
335else A_STRING="You entered '$A_STRING'"
336fi
337
338echo -e "\n\n$A_STRING\n\n"</literal>
339EOF
340chmod 755 blfs-yes-test1</userinput></screen>
341
342 <para>Now run the script by issuing <command>./blfs-yes-test1</command> from
343 the command line. It will wait for a response, which can be anything (or
344 nothing) followed by the <keycap>Enter</keycap> key. After entering
345 something, the result will be echoed to the screen. Now use the
346 <command>yes</command> command to automate the entering of a
347 response:</para>
348
349<screen><userinput>yes | ./blfs-yes-test1</userinput></screen>
350
351 <para>Notice that piping <command>yes</command> by itself to the script
352 results in <keycap>y</keycap> being passed to the script. Now try it with a
353 string of text:</para>
354
355<screen><userinput>yes 'This is some text' | ./blfs-yes-test1</userinput></screen>
356
357 <para>The exact string was used as the response to the script. Finally,
358 try it using an empty (null) string:</para>
359
360<screen><userinput>yes '' | ./blfs-yes-test1</userinput></screen>
361
362 <para>Notice this results in passing just the press of the
363 <keycap>Enter</keycap> key to the script. This is useful for times when the
364 default answer to the prompt is sufficient. This syntax is used in the
365 <xref linkend="net-tools-automate-example"/> instructions to accept all the
366 defaults to the many prompts during the configuration step. You may now
367 remove the test script, if desired.</para>
368
369 <bridgehead renderas="sect3">File Redirection to Automate Output</bridgehead>
370
371 <para>In order to automate the building of some packages, especially those
372 that require you to read a license agreement one page at a time, requires
373 using a method that avoids having to press a key to display each page.
374 Redirecting the output to a file can be used in these instances to assist
375 with the automation. The previous section on this page touched on creating
376 log files of the build output. The redirection method shown there used the
377 <command>tee</command> command to redirect output to a file while also
378 displaying the output to the screen. Here, the output will only be sent to
379 a file.</para>
380
381 <para>Again, the easiest way to demonstrate the technique is to show an
382 example. First, issue the command:</para>
383
384<screen><userinput>ls -l /usr/bin | more</userinput></screen>
385
386 <para>Of course, you'll be required to view the output one page at a time
387 because the <command>more</command> filter was used. Now try the same
388 command, but this time redirect the output to a file. The special file
389 <filename>/dev/null</filename> can be used instead of the filename shown,
390 but you will have no log file to examine:</para>
391
392<screen><userinput>ls -l /usr/bin | more &gt; redirect_test.log 2&gt;&amp;1</userinput></screen>
393
394 <para>Notice that this time the command immediately returned to the shell
395 prompt without having to page through the output. You may now remove the
396 log file.</para>
397
398 <para>The last example will use the <command>yes</command> command in
399 combination with output redirection to bypass having to page through the
400 output and then provide a <keycap>y</keycap> to a prompt. This technique
401 could be used in instances when otherwise you would have to page through
402 the output of a file (such as a license agreement) and then answer the
403 question of <quote>do you accept the above?</quote>. For this example,
404 another short <application>Bash</application> script is required:</para>
405
406<screen><userinput>cat &gt; blfs-yes-test2 &lt;&lt; "EOF"
407<literal>#!/bin/bash
408
409ls -l /usr/bin | more
410
411echo -n -e "\n\nDid you enjoy reading this? (y,n) "
412
413read A_STRING
414
415if test "$A_STRING" = "y"; then A_STRING="You entered the 'y' key"
416else A_STRING="You did NOT enter the 'y' key"
417fi
418
419echo -e "\n\n$A_STRING\n\n"</literal>
420EOF
421chmod 755 blfs-yes-test2</userinput></screen>
422
423 <para>This script can be used to simulate a program that requires you to
424 read a license agreement, then respond appropriately to accept the
425 agreement before the program will install anything. First, run the script
426 without any automation techniques by issuing
427 <command>./blfs-yes-test2</command>.</para>
428
429 <para>Now issue the following command which uses two automation techniques,
430 making it suitable for use in an automated build script:</para>
431
432<screen><userinput>yes | ./blfs-yes-test2 &gt; blfs-yes-test2.log 2&gt;&amp;1</userinput></screen>
433
434 <para>If desired, issue <command>tail blfs-yes-test2.log</command> to see
435 the end of the paged output, and confirmation that <keycap>y</keycap> was
436 passed through to the script. Once satisfied that it works as it should,
437 you may remove the script and log file.</para>
438
439 <para>Finally, keep in mind that there are many ways to automate and/or
440 script the build commands. There is not a single <quote>correct</quote> way
441 to do it. Your imagination is the only limit.</para>
442
443 </sect2>
444
445 <sect2>
446 <title>Dependencies</title>
447
448 <para>For each package described, BLFS lists the known dependencies.
449 These are listed under several headings, whose meaning is as follows:</para>
450
451 <itemizedlist>
452 <listitem>
453 <para><emphasis>Required</emphasis> means that the target package
454 cannot be correctly built without the dependency having first been
455 installed, except if the dependency is said to be
456 <quote>runtime</quote>, which means the target package can be built but
457 cannot function without it.</para>
458 <para>
459 Note that a target package can start to <quote>function</quote>
460 in many subtle ways: an installed configuration file can make the
461 init system, cron daemon, or bus daemon to run a program
462 automatically; another package using the target package as an
463 dependency can run a program from the target package in the
464 building system; and the configuration sections in the BLFS book
465 may also run a program from a just installed package. So if
466 you are installing the target package without a
467 <emphasis>Required (runtime)</emphasis> dependency installed,
468 You should install the dependency as soon as possible after the
469 installation of the target package.
470 </para>
471 </listitem>
472 <listitem>
473 <para><emphasis>Recommended</emphasis> means that BLFS strongly
474 suggests this package is installed first (except if said to be
475 <quote>runtime</quote>, see below) for a clean and trouble-free
476 build, that won't have issues either during the build process, or at
477 run-time. The instructions in the book assume these packages are
478 installed. Some changes or workarounds may be required if these
479 packages are not installed. If a recommended dependency is said
480 to be <quote>runtime</quote>, it means that BLFS strongly suggests
481 that this dependency is installed before using the package, for
482 getting full funtionality.</para>
483 </listitem>
484 <listitem>
485 <para><emphasis>Optional</emphasis> means that this package might be
486 installed for added functionality. Often BLFS will describe the
487 dependency to explain the added functionality that will result.
488 An optional dependency may be automatically pick up by the target
489 package if the dependency is installed, but another some optional
490 dependency may also need additional configuration options to enable
491 them when the target package is built. Such additional options are
492 often documented in the BLFS book. If an optional dependency is
493 said to be <quote>runtime</quote>, it means you may install
494 the dependency after installing the target package to support some
495 optional features of the target package if you need these
496 features.</para>
497 <para>An optional dependency may be out of BLFS. If you need such
498 an <emphasis>external</emphasis> optional dependency for some
499 features you need, read <xref linkend='beyond'/> for the general
500 hint about installing an out-of-BLFS package.</para>
501 </listitem>
502 </itemizedlist>
503
504 </sect2>
505
506 <sect2 id="package_updates">
507 <title>Using the Most Current Package Sources</title>
508
509 <para>On occasion you may run into a situation in the book when a package
510 will not build or work properly. Though the Editors attempt to ensure
511 that every package in the book builds and works properly, sometimes a
512 package has been overlooked or was not tested with this particular version
513 of BLFS.</para>
514
515 <para>If you discover that a package will not build or work properly, you
516 should see if there is a more current version of the package. Typically
517 this means you go to the maintainer's web site and download the most current
518 tarball and attempt to build the package. If you cannot determine the
519 maintainer's web site by looking at the download URLs, use Google and query
520 the package's name. For example, in the Google search bar type:
521 'package_name download' (omit the quotes) or something similar. Sometimes
522 typing: 'package_name home page' will result in you finding the
523 maintainer's web site.</para>
524
525 </sect2>
526
527 <sect2 id="stripping">
528 <title>Stripping One More Time</title>
529
530 <para>
531 In LFS, stripping of debugging symbols and unneeded symbol table
532 entries was discussed a couple of times. When building BLFS packages,
533 there are generally no special instructions that discuss stripping
534 again. Stripping can be done while installing a package, or
535 afterwards.
536 </para>
537
538 <bridgehead renderas="sect3" id="stripping-install">Stripping while Installing a Package</bridgehead>
539
540 <para>
541 There are several ways to strip executables installed by a
542 package. They depend on the build system used (see below <link
543 linkend="buildsystems">the section about build systems</link>),
544 so only some
545 generalities can be listed here:
546 </para>
547
548 <note>
549 <para>
550 The following methods using the feature of a building system
551 (autotools, meson, or cmake) will not strip static libraries if any
552 is installed. Fortunately there are not too many static libraries
553 in BLFS, and a static library can always be stripped safely by
554 running <command>strip --strip-unneeded</command> on it manually.
555 </para>
556 </note>
557
558 <itemizedlist>
559 <listitem>
560 <para>
561 The packages using autotools usually have an
562 <parameter>install-strip</parameter> target in their generated
563 <filename>Makefile</filename> files. So installing stripped
564 executables is just a matter of using
565 <command>make install-strip</command> instead of
566 <command>make install</command>.
567 </para>
568 </listitem>
569 <listitem>
570 <para>
571 The packages using the meson build system can accept
572 <parameter>-Dstrip=true</parameter> when running
573 <command>meson</command>. If you've forgot to add this option
574 running the <command>meson</command>, you can also run
575 <command>meson install --strip</command> instead of
576 <command>ninja install</command>.
577 </para>
578 </listitem>
579 <listitem>
580 <para>
581 <command>cmake</command> generates
582 <parameter>install/strip</parameter> targets for both the
583 <parameter>Unix Makefiles</parameter> and
584 <parameter>Ninja</parameter> generators (the default is
585 <parameter>Unix Makefiles</parameter> on linux). So just run
586 <command>make install/strip</command> or
587 <command>ninja install/strip</command> instead of the
588 <command>install</command> counterparts.
589 </para>
590 </listitem>
591 <listitem>
592 <para>
593 Removing (or not generating) debug symbols can also be
594 achieved by removing the
595 <parameter>-g&lt;something&gt;</parameter> options
596 in C/C++ calls. How to do that is very specific for each
597 package. And, it does not remove unneeded symbol table entries.
598 So it will not be explained in detail here. See also below
599 the paragraphs about optimization.
600 </para>
601 </listitem>
602 </itemizedlist>
603
604 <bridgehead renderas="sect3" id="stripping-installed">Stripping Installed Executables</bridgehead>
605
606 <para>
607 The <command>strip</command> utility changes files in place, which may
608 break anything using it if it is loaded in memory. Note that if a file is
609 in use but just removed from the disk (i.e. not overwritten nor
610 modified), this is not a problem since the kernel can use
611 <quote>deleted</quote> files. Look at <filename>/proc/*/maps</filename>
612 and it is likely that you'll see some <emphasis>(deleted)</emphasis>
613 entries. The <command>mv</command> just removes the destination file from
614 the directory but does not touch its content, so that it satisfies the
615 condition for the kernel to use the old (deleted) file. The script below
616 is just an example.
617 It should be run as the &root; user:
618 </para>
619
620<screen><userinput>cat &gt; /usr/sbin/strip-all.sh &lt;&lt; "EOF"
621<literal>#!/usr/bin/bash
622
623if [ $EUID -ne 0 ]; then
624 echo "Need to be root"
625 exit 1
626fi
627
628{ find /usr/lib -type f -name '*.so*' ! -name '*dbg'
629 find /usr/lib -type f -name '*.a'
630 find /usr/{bin,sbin,libexec} -type f
631} | while read file; do
632 if ! readelf -h $file >/dev/null 2>&amp;1; then continue; fi
633 if file $file | grep --quiet --invert-match 'not stripped'; then continue; fi
634
635 cp --preserve $file ${file}.tmp
636 strip --strip-unneeded ${file}.tmp
637 mv ${file}.tmp $file
638 done</literal>
639EOF
640chmod 744 /usr/sbin/strip-all.sh</userinput></screen>
641
642 <para>
643 If you install programs in other directories such as <filename
644 class="directory">/opt</filename> or <filename
645 class="directory">/usr/local</filename>, you may want to strip the files
646 there too. Just add other directories to scan in the compound list of
647 <command>find</command> commands between the braces.
648 </para>
649
650 <para>
651 For more information on stripping, see <ulink
652 url="https://www.technovelty.org/linux/stripping-shared-libraries.html"/>.
653 </para>
654
655 </sect2>
656
657<!--
658 <sect2 id="libtool">
659 <title>Libtool files</title>
660
661 <para>
662 One of the side effects of packages that use Autotools, including
663 libtool, is that they create many files with an .la extension. These
664 files are not needed in an LFS environment. If there are conflicts with
665 pkgconfig entries, they can actually prevent successful builds. You
666 may want to consider removing these files periodically:
667 </para>
668
669<screen><userinput>find /lib /usr/lib -not -path "*Image*" -a -name \*.la -delete</userinput></screen>
670
671 <para>
672 The above command removes all .la files with the exception of those that
673 have <quote>Image</quote> or <quote>openldap</quote> as a part of the
674 path. These .la files are used by the ImageMagick and openldap programs,
675 respectively. There may be other exceptions by packages not in BLFS.
676 </para>
677
678 </sect2>
679-->
680 <sect2 id="buildsystems">
681 <title>Working with different build systems</title>
682
683 <para>
684 There are now three different build systems in common use for
685 converting C or C++ source code into compiled programs or
686 libraries and their details (particularly, finding out about available
687 options and their default values) differ. It may be easiest to understand
688 the issues caused by some choices (typically slow execution or
689 unexpected use of, or omission of, optimizatons) by starting with
690 the CFLAGS and CXXFLAGS environment variables. There are also some
691 programs which use rust.
692 </para>
693
694 <para>
695 Most LFS and BLFS builders are probably aware of the basics of CFLAGS
696 and CXXFLAGS for altering how a program is compiled. Typically, some
697 form of optimization is used by upstream developers (-O2 or -O3),
698 sometimes with the creation of debug symbols (-g), as defaults.
699 </para>
700
701 <para>
702 If there are contradictory flags (e.g. multiple different -O values),
703 the <emphasis>last</emphasis> value will be used. Sometimes this means
704 that flags specified in environment variables will be picked up before
705 values hardcoded in the Makefile, and therefore ignored. For example,
706 where a user specifies '-O2' and that is followed by '-O3' the build will
707 use '-O3'.
708 </para>
709
710 <para>
711 There are various other things which can be passed in CFLAGS or
712 CXXFLAGS, such as forcing compilation for a specific microarchitecture
713 (e.g. -march=amdfam10, -march=native) or specifying a specific standard
714 for C or C++ (-std=c++17 for example). But one thing which has now come
715 to light is that programmers might include debug assertions in their
716 code, expecting them to be disabled in releases by using -DNDEBUG.
717 Specifically, if <xref linkend="mesa"/> is built with these assertions
718 enabled, some activities such as loading levels of games can take
719 extremely long times, even on high-class video cards.
720 </para>
721
722 <bridgehead renderas="sect3" id="autotools-info">Autotools with Make</bridgehead>
723
724 <para>
725 This combination is often described as 'CMMI' (configure, make, make
726 install) and is used here to also cover the few packages which have a
727 configure script that is not generated by autotools.
728 </para>
729
730 <para>
731 Sometimes running <command>./configure --help</command> will produce
732 useful options about switches which might be used. At other times,
733 after looking at the output from configure you may need to look
734 at the details of the script to find out what it was actually searching
735 for.
736 </para>
737
738 <para>
739 Many configure scripts will pick up any CFLAGS or CXXFLAGS from the
740 environment, but CMMI packages vary about how these will be mixed with
741 any flags which would otherwise be used (<emphasis>variously</emphasis>:
742 ignored, used to replace the programmer's suggestion, used before the
743 programmer's suggestion, or used after the programmer's suggestion).
744 </para>
745
746 <para>
747 In most CMMI packages, running 'make' will list each command and run
748 it, interspersed with any warnings. But some packages try to be 'silent'
749 and only show which file they are compiling or linking instead of showing
750 the command line. If you need to inspect the command, either because of
751 an error, or just to see what options and flags are being used, adding
752 'V=1' to the make invocation may help.
753 </para>
754
755 <bridgehead renderas="sect3" id="cmake-info">CMake</bridgehead>
756
757 <para>
758 CMake works in a very different way, and it has two backends which can
759 be used on BLFS: 'make' and 'ninja'. The default backend is make, but
760 ninja can be faster on large packages with multiple processors. To
761 use ninja, specify '-G Ninja' in the cmake command. However, there are
762 some packages which create fatal errors in their ninja files but build
763 successfully using the default of Unix Makefiles.
764 </para>
765
766 <para>
767 The hardest part of using CMake is knowing what options you might wish
768 to specify. The only way to get a list of what the package knows about
769 is to run <command>cmake -LAH</command> and look at the output for that
770 default configuration.
771 </para>
772
773 <para>
774 Perhaps the most-important thing about CMake is that it has a variety
775 of CMAKE_BUILD_TYPE values, and these affect the flags. The default
776 is that this is not set and no flags are generated. Any CFLAGS or
777 CXXFLAGS in the environment will be used. If the programmer has coded
778 any debug assertions, those will be enabled unless -DNDEBUG is used.
779 The following CMAKE_BUILD_TYPE values will generate the flags shown,
780 and these will come <emphasis>after</emphasis> any flags in the
781 environment and therefore take precedence.
782 </para>
783
784 <informaltable align="center">
785 <tgroup cols="2">
786 <colspec colnum="1" align="center"/>
787 <colspec colnum="2" align="center"/>
788 <thead>
789 <row><entry>Value</entry><entry>Flags</entry></row>
790 </thead>
791 <tbody>
792 <row>
793 <entry>Debug</entry><entry><option>-g</option></entry>
794 </row>
795 <row>
796 <entry>Release</entry><entry><option>-O3 -DNDEBUG</option></entry>
797 </row>
798 <row>
799 <entry>RelWithDebInfo</entry><entry><option>-O2 -g -DNDEBUG</option></entry>
800 </row>
801 <row>
802 <entry>MinSizeRel</entry><entry><option>-Os -DNDEBUG</option></entry>
803 </row>
804 </tbody>
805 </tgroup>
806 </informaltable>
807
808 <para>
809 CMake tries to produce quiet builds. To see the details of the commands
810 which are being run, use <command>make VERBOSE=1</command> or
811 <command>ninja -v</command>.
812 </para>
813
814 <para>
815 By default, CMake treats file installation differently from the other
816 build systems: if a file already exists and is not newer than a file
817 that would overwrite it, then the file is not installed. This may be
818 a problem if a user wants to record which file belongs to a package,
819 either using <envar>LD_PRELOAD</envar>, or by listing files newer
820 than a timestamp. The default can be changed by setting the variable
821 <envar>CMAKE_INSTALL_ALWAYS</envar> to 1 in the
822 <emphasis>environment</emphasis>, for example by
823 <command>export</command>'ing it.
824 </para>
825
826 <bridgehead renderas="sect3" id="meson-info">Meson</bridgehead>
827
828 <para>
829 Meson has some similarities to CMake, but many differences. To get
830 details of the defines that you may wish to change you can look at
831 <filename>meson_options.txt</filename> which is usually in the
832 top-level directory.
833 </para>
834
835 <para>
836 If you have already configured the package by running
837 <command>meson</command> and now wish to change one or more settings,
838 you can either remove the build directory, recreate it, and use the
839 altered options, or within the build directory run <command>meson
840 configure</command>, e.g. to set an option:
841 </para>
842
843<screen><userinput>meson configure -D&lt;some_option&gt;=true</userinput></screen>
844
845 <para>
846 If you do that, the file <filename>meson-private/cmd_line.txt</filename>
847 will show the <emphasis>last</emphasis> commands which were used.
848 </para>
849
850 <para>
851 Meson provides the following buildtype values, and the flags they enable
852 come <emphasis>after</emphasis> any flags supplied in the environment and
853 therefore take precedence.
854 </para>
855
856 <itemizedlist>
857 <listitem>
858 <para>plain : no added flags. This is for distributors to supply their
859 own CLFAGS, CXXFLAGS and LDFLAGS. There is no obvious reason to use
860 this in BLFS.</para>
861 </listitem>
862 <listitem>
863 <para>debug : '-g' - this is the default if nothing is specified
864 in either <filename>meson.build</filename> or the command line.
865 However it results large and slow binaries, so we should override
866 it in BLFS.</para>
867 </listitem>
868 <listitem>
869 <para>debugoptimized : '-O2 -g' : this is the default specified in
870 <filename>meson.build</filename> of some packages.</para>
871 </listitem>
872 <listitem>
873 <para>release : '-O3 -DNDEBUG' (but occasionally a package will force
874 -O2 here)</para>
875 </listitem>
876 </itemizedlist>
877
878 <para>
879 Although the 'release' buildtype is described as enabling -DNDEBUG, and all
880 CMake Release builds pass that, it has so far only been observed (in
881 verbose builds) for <xref linkend="mesa"/>. That suggests that it might
882 only be used when there are debug assertions present.
883 </para>
884
885 <para>
886 The -DNDEBUG flag can also be provided by passing
887 <command>-Db_ndebug=true</command>.
888 </para>
889
890 <para>
891 To see the details of the commands which are being run in a package using
892 meson, use 'ninja -v'.
893 </para>
894
895 <bridgehead renderas="sect3" id="rust-info">Rustc and Cargo</bridgehead>
896
897 <para>
898 Most released rustc programs are provided as crates (source tarballs)
899 which will query a server to check current versions of dependencies
900 and then download them as necessary. These packages are built using
901 <command>cargo --release</command>. In theory, you can manipulate the
902 RUSTFLAGS to change the optimize-level (default is 3, like -O3, e.g.
903 <literal>-Copt-level=3</literal>) or to force it to build for the
904 machine it is being compiled on, using
905 <literal>-Ctarget-cpu=native</literal> but in practice this seems to
906 make no significant difference.
907 </para>
908
909 <para>
910 If you find an interesting rustc program which is only provided as
911 unpackaged source, you should at least specify
912 <literal>RUSTFLAGS=-Copt-level=2</literal> otherwise it will do an
913 unoptimized compile with debug info and run <emphasis>much</emphasis>
914 slower.
915 </para>
916
917 <para>
918 The rust developers seem to assume that everyone will compile on a
919 machine dedicated to producing builds, so by default all CPUs are used.
920 This can often be worked around, either by exporting
921 CARGO_BUILD_JOBS=&lt;N&gt; or passing --jobs &lt;N&gt; to cargo. For
922 compiling rustc itself, specifying --jobs &lt;N&gt; on invocations of
923 x.py (together with the <envar>CARGO_BUILD_JOBS</envar> environment
924 variable, which looks like a "belt and braces" approach but seems to be
925 necessary) mostly works. The exception is running the tests when building
926 rustc, some of them will nevertheless use all online CPUs, at least as of
927 rustc-1.42.0.
928 </para>
929
930 </sect2>
931
932 <sect2 id="optimizations">
933 <title>Optimizing the build</title>
934
935 <para>
936 Many people will prefer to optimize compiles as they see fit, by providing
937 CFLAGS or CXXFLAGS. For an introduction to the options available with gcc
938 and g++ see <ulink
939 url="https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html"/> and <ulink
940 url="https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html"/>
941 and <command>info gcc</command>.
942
943 </para>
944
945 <para>
946 Some packages default to '-O2 -g', others to '-O3 -g', and if CFLAGS or
947 CXXFLAGS are supplied they might be added to the package's defaults,
948 replace the package's defaults, or even be ignored. There are details
949 on some desktop packages which were mostly current in April 2019 at
950 <ulink url="https://www.linuxfromscratch.org/~ken/tuning/"/> - in
951 particular, README.txt, tuning-1-packages-and-notes.txt, and
952 tuning-notes-2B.txt. The particular thing to remember is that if you
953 want to try some of the more interesting flags you may need to force
954 verbose builds to confirm what is being used.
955 </para>
956
957 <para>
958 Clearly, if you are optimizing your own program you can spend time to
959 profile it and perhaps recode some of it if it is too slow. But for
960 building a whole system that approach is impractical. In general,
961 -O3 usually produces faster programs than -O2. Specifying
962 -march=native is also beneficial, but means that you cannot move the
963 binaries to an incompatible machine - this can also apply to newer
964 machines, not just to older machines. For example programs compiled for
965 'amdfam10' run on old Phenoms, Kaveris, and Ryzens : but programs
966 compiled for a Kaveri will not run on a Ryzen because certain op-codes
967 are not present. Similarly, if you build for a Haswell not everything
968 will run on a SandyBridge.
969 </para>
970
971 <para>
972 There are also various other options which some people claim are
973 beneficial. At worst, you get to recompile and test, and then
974 discover that in your usage the options do not provide a benefit.
975 </para>
976
977 <para>
978 If building Perl or Python modules, or Qt packages which use qmake,
979 in general the CFLAGS and CXXFLAGS used are those which were used by
980 those 'parent' packages.
981 </para>
982
983 </sect2>
984
985 <sect2 id="hardening">
986 <title>Options for hardening the build</title>
987
988 <para>
989 Even on desktop systems, there are still a lot of exploitable
990 vulnerabilities. For many of these, the attack comes via javascript
991 in a browser. Often, a series of vulnerabilities are used to gain
992 access to data (or sometimes to pwn, i.e. own, the machine and
993 install rootkits). Most commercial distros will apply various
994 hardening measures.
995 </para>
996
997 <para>
998 In the past, there was Hardened LFS where gcc (a much older version)
999 was forced to use hardening (with options to turn some of it off on a
1000 per-package basis). The current LFS and BLFS books are carrying
1001 forward a part of its spirit by enabling PIE
1002 (<option>-fPIE -pie</option>) and SSP
1003 (<option>-fstack-protector-strong</option>) as the defaults
1004 for GCC and clang. What is being covered here is different - first
1005 you have to make sure that the package is indeed using your added
1006 flags and not over-riding them.
1007 </para>
1008
1009 <para>
1010 For hardening options which are reasonably cheap, there is some
1011 discussion in the 'tuning' link above (occasionally, one or more
1012 of these options might be inappropriate for a package). These
1013 options are <option>-D_FORTIFY_SOURCE=2</option> and
1014 (for C++) <option>-D_GLIBCXX_ASSERTIONS</option>. On modern
1015 machines these should only have a little impact on how fast things
1016 run, and often they will not be noticeable.
1017 </para>
1018
1019 <para>
1020 The main distros use much more, such as RELRO (Relocation Read Only)
1021 and perhaps <option>-fstack-clash-protection</option>. You may also
1022 encounter the so-called <quote>userspace retpoline</quote>
1023 (<option>-mindirect-branch=thunk</option> etc.) which
1024 is the equivalent of the spectre mitigations applied to the linux
1025 kernel in late 2018. The kernel mitigations caused a lot of complaints
1026 about lost performance, if you have a production server you might wish
1027 to consider testing that, along with the other available options, to
1028 see if performance is still sufficient.
1029 </para>
1030
1031 <para>
1032 Whilst gcc has many hardening options, clang/LLVM's strengths lie
1033 elsewhere. Some options which gcc provides are said to be less effective
1034 in clang/LLVM.
1035 </para>
1036
1037 </sect2>
1038
1039</sect1>
Note: See TracBrowser for help on using the repository browser.