source: introduction/important/building-notes.xml@ 74dcbc8

11.3 12.0 12.1 kea ken/TL2024 ken/inkscape-core-mods 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 74dcbc8 was 640313a, checked in by Xi Ruoyao <xry111@…>, 16 months ago

building-notes: remove an extra ")"

  • Property mode set to 100644
File size: 44.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.</para>
456 </listitem>
457 <listitem>
458 <para><emphasis>Recommended</emphasis> means that BLFS strongly
459 suggests this package is installed first for a clean and trouble-free
460 build, that won't have issues either during the build process, or at
461 run-time. The instructions in the book assume these packages are
462 installed. Some changes or workarounds may be required if these
463 packages are not installed.</para>
464 </listitem>
465 <listitem>
466 <para><emphasis>Optional</emphasis> means that this package might be
467 installed for added functionality. Often BLFS will describe the
468 dependency to explain the added functionality that will result.</para>
469 </listitem>
470 </itemizedlist>
471
472 </sect2>
473
474 <sect2 id="package_updates">
475 <title>Using the Most Current Package Sources</title>
476
477 <para>On occasion you may run into a situation in the book when a package
478 will not build or work properly. Though the Editors attempt to ensure
479 that every package in the book builds and works properly, sometimes a
480 package has been overlooked or was not tested with this particular version
481 of BLFS.</para>
482
483 <para>If you discover that a package will not build or work properly, you
484 should see if there is a more current version of the package. Typically
485 this means you go to the maintainer's web site and download the most current
486 tarball and attempt to build the package. If you cannot determine the
487 maintainer's web site by looking at the download URLs, use Google and query
488 the package's name. For example, in the Google search bar type:
489 'package_name download' (omit the quotes) or something similar. Sometimes
490 typing: 'package_name home page' will result in you finding the
491 maintainer's web site.</para>
492
493 </sect2>
494
495 <sect2 id="stripping">
496 <title>Stripping One More Time</title>
497
498 <para>
499 In LFS, stripping of debugging symbols and unneeded symbol table
500 entries was discussed a couple of times. When building BLFS packages,
501 there are generally no special instructions that discuss stripping
502 again. Stripping can be done while installing a package, or
503 afterwards.
504 </para>
505
506 <bridgehead renderas="sect3" id="stripping-install">Stripping while Installing a Package</bridgehead>
507
508 <para>
509 There are several ways to strip executables installed by a
510 package. They depend on the build system used (see below <link
511 linkend="buildsystems">the section about build systems</link>),
512 so only some
513 generalities can be listed here:
514 </para>
515
516 <note>
517 <para>
518 The following methods using the feature of a building system
519 (autotools, meson, or cmake) will not strip static libraries if any
520 is installed. Fortunately there are not too many static libraries
521 in BLFS, and a static library can always be stripped safely by
522 running <command>strip --strip-unneeded</command> on it manually.
523 </para>
524 </note>
525
526 <itemizedlist>
527 <listitem>
528 <para>
529 The packages using autotools usually have an
530 <parameter>install-strip</parameter> target in their generated
531 <filename>Makefile</filename> files. So installing stripped
532 executables is just a matter of using
533 <command>make install-strip</command> instead of
534 <command>make install</command>.
535 </para>
536 </listitem>
537 <listitem>
538 <para>
539 The packages using the meson build system can accept
540 <parameter>-Dstrip=true</parameter> when running
541 <command>meson</command>. If you've forgot to add this option
542 running the <command>meson</command>, you can also run
543 <command>meson install --strip</command> instead of
544 <command>ninja install</command>.
545 </para>
546 </listitem>
547 <listitem>
548 <para>
549 <command>cmake</command> generates
550 <parameter>install/strip</parameter> targets for both the
551 <parameter>Unix Makefiles</parameter> and
552 <parameter>Ninja</parameter> generators (the default is
553 <parameter>Unix Makefiles</parameter> on linux). So just run
554 <command>make install/strip</command> or
555 <command>ninja install/strip</command> instead of the
556 <command>install</command> counterparts.
557 </para>
558 </listitem>
559 <listitem>
560 <para>
561 Removing (or not generating) debug symbols can also be
562 achieved by removing the
563 <parameter>-g&lt;something&gt;</parameter> options
564 in C/C++ calls. How to do that is very specific for each
565 package. And, it does not remove unneeded symbol table entries.
566 So it will not be explained in detail here. See also below
567 the paragraphs about optimization.
568 </para>
569 </listitem>
570 </itemizedlist>
571
572 <bridgehead renderas="sect3" id="stripping-installed">Stripping Installed Executables</bridgehead>
573
574 <para>
575 The <command>strip</command> utility changes files in place, which may
576 break anything using it if it is loaded in memory. Note that if a file is
577 in use but just removed from the disk (i.e. not overwritten nor
578 modified), this is not a problem since the kernel can use
579 <quote>deleted</quote> files. Look at <filename>/proc/*/maps</filename>
580 and it is likely that you'll see some <emphasis>(deleted)</emphasis>
581 entries. The <command>mv</command> just removes the destination file from
582 the directory but does not touch its content, so that it satisfies the
583 condition for the kernel to use the old (deleted) file. The script below
584 is just an example.
585 It should be run as the &root; user:
586 </para>
587
588<screen><userinput>cat &gt; /usr/sbin/strip-all.sh &lt;&lt; "EOF"
589<literal>#!/usr/bin/bash
590
591if [ $EUID -ne 0 ]; then
592 echo "Need to be root"
593 exit 1
594fi
595
596{ find /usr/lib -type f -name '*.so*' ! -name '*dbg'
597 find /usr/lib -type f -name '*.a'
598 find /usr/{bin,sbin,libexec} -type f
599} | while read file; do
600 if ! readelf -h $file >/dev/null 2>&amp;1; then continue; fi
601 if file $file | grep --quiet --invert-match 'not stripped'; then continue; fi
602
603 cp --preserve $file ${file}.tmp
604 strip --strip-unneeded ${file}.tmp
605 mv ${file}.tmp $file
606 done</literal>
607EOF
608chmod 744 /usr/sbin/strip-all.sh</userinput></screen>
609
610 <para>
611 If you install programs in other directories such as <filename
612 class="directory">/opt</filename> or <filename
613 class="directory">/usr/local</filename>, you may want to strip the files
614 there too. Just add other directories to scan in the compound list of
615 <command>find</command> commands between the braces.
616 </para>
617
618 <para>
619 For more information on stripping, see <ulink
620 url="https://www.technovelty.org/linux/stripping-shared-libraries.html"/>.
621 </para>
622
623 </sect2>
624
625<!--
626 <sect2 id="libtool">
627 <title>Libtool files</title>
628
629 <para>
630 One of the side effects of packages that use Autotools, including
631 libtool, is that they create many files with an .la extension. These
632 files are not needed in an LFS environment. If there are conflicts with
633 pkgconfig entries, they can actually prevent successful builds. You
634 may want to consider removing these files periodically:
635 </para>
636
637<screen><userinput>find /lib /usr/lib -not -path "*Image*" -a -name \*.la -delete</userinput></screen>
638
639 <para>
640 The above command removes all .la files with the exception of those that
641 have <quote>Image</quote> or <quote>openldap</quote> as a part of the
642 path. These .la files are used by the ImageMagick and openldap programs,
643 respectively. There may be other exceptions by packages not in BLFS.
644 </para>
645
646 </sect2>
647-->
648 <sect2 id="buildsystems">
649 <title>Working with different build systems</title>
650
651 <para>
652 There are now three different build systems in common use for
653 converting C or C++ source code into compiled programs or
654 libraries and their details (particularly, finding out about available
655 options and their default values) differ. It may be easiest to understand
656 the issues caused by some choices (typically slow execution or
657 unexpected use of, or omission of, optimizatons) by starting with
658 the CFLAGS and CXXFLAGS environment variables. There are also some
659 programs which use rust.
660 </para>
661
662 <para>
663 Most LFS and BLFS builders are probably aware of the basics of CFLAGS
664 and CXXFLAGS for altering how a program is compiled. Typically, some
665 form of optimization is used by upstream developers (-O2 or -O3),
666 sometimes with the creation of debug symbols (-g), as defaults.
667 </para>
668
669 <para>
670 If there are contradictory flags (e.g. multiple different -O values),
671 the <emphasis>last</emphasis> value will be used. Sometimes this means
672 that flags specified in environment variables will be picked up before
673 values hardcoded in the Makefile, and therefore ignored. For example,
674 where a user specifies '-O2' and that is followed by '-O3' the build will
675 use '-O3'.
676 </para>
677
678 <para>
679 There are various other things which can be passed in CFLAGS or
680 CXXFLAGS, such as forcing compilation for a specific microarchitecture
681 (e.g. -march=amdfam10, -march=native) or specifying a specific standard
682 for C or C++ (-std=c++17 for example). But one thing which has now come
683 to light is that programmers might include debug assertions in their
684 code, expecting them to be disabled in releases by using -DNDEBUG.
685 Specifically, if <xref linkend="mesa"/> is built with these assertions
686 enabled, some activities such as loading levels of games can take
687 extremely long times, even on high-class video cards.
688 </para>
689
690 <bridgehead renderas="sect3" id="autotools-info">Autotools with Make</bridgehead>
691
692 <para>
693 This combination is often described as 'CMMI' (configure, make, make
694 install) and is used here to also cover the few packages which have a
695 configure script that is not generated by autotools.
696 </para>
697
698 <para>
699 Sometimes running <command>./configure --help</command> will produce
700 useful options about switches which might be used. At other times,
701 after looking at the output from configure you may need to look
702 at the details of the script to find out what it was actually searching
703 for.
704 </para>
705
706 <para>
707 Many configure scripts will pick up any CFLAGS or CXXFLAGS from the
708 environment, but CMMI packages vary about how these will be mixed with
709 any flags which would otherwise be used (<emphasis>variously</emphasis>:
710 ignored, used to replace the programmer's suggestion, used before the
711 programmer's suggestion, or used after the programmer's suggestion).
712 </para>
713
714 <para>
715 In most CMMI packages, running 'make' will list each command and run
716 it, interspersed with any warnings. But some packages try to be 'silent'
717 and only show which file they are compiling or linking instead of showing
718 the command line. If you need to inspect the command, either because of
719 an error, or just to see what options and flags are being used, adding
720 'V=1' to the make invocation may help.
721 </para>
722
723 <bridgehead renderas="sect3" id="cmake-info">CMake</bridgehead>
724
725 <para>
726 CMake works in a very different way, and it has two backends which can
727 be used on BLFS: 'make' and 'ninja'. The default backend is make, but
728 ninja can be faster on large packages with multiple processors. To
729 use ninja, specify '-G Ninja' in the cmake command. However, there are
730 some packages which create fatal errors in their ninja files but build
731 successfully using the default of Unix Makefiles.
732 </para>
733
734 <para>
735 The hardest part of using CMake is knowing what options you might wish
736 to specify. The only way to get a list of what the package knows about
737 is to run <command>cmake -LAH</command> and look at the output for that
738 default configuration.
739 </para>
740
741 <para>
742 Perhaps the most-important thing about CMake is that it has a variety
743 of CMAKE_BUILD_TYPE values, and these affect the flags. The default
744 is that this is not set and no flags are generated. Any CFLAGS or
745 CXXFLAGS in the environment will be used. If the programmer has coded
746 any debug assertions, those will be enabled unless -DNDEBUG is used.
747 The following CMAKE_BUILD_TYPE values will generate the flags shown,
748 and these will come <emphasis>after</emphasis> any flags in the
749 environment and therefore take precedence.
750 </para>
751
752 <informaltable align="center">
753 <tgroup cols="2">
754 <colspec colnum="1" align="center"/>
755 <colspec colnum="2" align="center"/>
756 <thead>
757 <row><entry>Value</entry><entry>Flags</entry></row>
758 </thead>
759 <tbody>
760 <row>
761 <entry>Debug</entry><entry><option>-g</option></entry>
762 </row>
763 <row>
764 <entry>Release</entry><entry><option>-O3 -DNDEBUG</option></entry>
765 </row>
766 <row>
767 <entry>RelWithDebInfo</entry><entry><option>-O2 -g -DNDEBUG</option></entry>
768 </row>
769 <row>
770 <entry>MinSizeRel</entry><entry><option>-Os -DNDEBUG</option></entry>
771 </row>
772 </tbody>
773 </tgroup>
774 </informaltable>
775
776 <para>
777 CMake tries to produce quiet builds. To see the details of the commands
778 which are being run, use <command>make VERBOSE=1</command> or
779 <command>ninja -v</command>.
780 </para>
781
782 <para>
783 By default, CMake treats file installation differently from the other
784 build systems: if a file already exists and is not newer than a file
785 that would overwrite it, then the file is not installed. This may be
786 a problem if a user wants to record which file belongs to a package,
787 either using <envar>LD_PRELOAD</envar>, or by listing files newer
788 than a timestamp. The default can be changed by setting the variable
789 <envar>CMAKE_INSTALL_ALWAYS</envar> to 1 in the
790 <emphasis>environment</emphasis>, for example by
791 <command>export</command>'ing it.
792 </para>
793
794 <bridgehead renderas="sect3" id="meson-info">Meson</bridgehead>
795
796 <para>
797 Meson has some similarities to CMake, but many differences. To get
798 details of the defines that you may wish to change you can look at
799 <filename>meson_options.txt</filename> which is usually in the
800 top-level directory.
801 </para>
802
803 <para>
804 If you have already configured the package by running
805 <command>meson</command> and now wish to change one or more settings,
806 you can either remove the build directory, recreate it, and use the
807 altered options, or within the build directory run <command>meson
808 configure</command>, e.g. to set an option:
809 </para>
810
811<screen><userinput>meson configure -D&lt;some_option&gt;=true</userinput></screen>
812
813 <para>
814 If you do that, the file <filename>meson-private/cmd_line.txt</filename>
815 will show the <emphasis>last</emphasis> commands which were used.
816 </para>
817
818 <para>
819 Meson provides the following buildtype values, and the flags they enable
820 come <emphasis>after</emphasis> any flags supplied in the environment and
821 therefore take precedence.
822 </para>
823
824 <itemizedlist>
825 <listitem>
826 <para>plain : no added flags. This is for distributors to supply their
827 own CLFAGS, CXXFLAGS and LDFLAGS. There is no obvious reason to use
828 this in BLFS.</para>
829 </listitem>
830 <listitem>
831 <para>debug : '-g' - this is the default if nothing is specified
832 in either <filename>meson.build</filename> or the command line.
833 However it results large and slow binaries, so we should override
834 it in BLFS.</para>
835 </listitem>
836 <listitem>
837 <para>debugoptimized : '-O2 -g' : this is the default specified in
838 <filename>meson.build</filename> of some packages.</para>
839 </listitem>
840 <listitem>
841 <para>release : '-O3 -DNDEBUG' (but occasionally a package will force
842 -O2 here)</para>
843 </listitem>
844 </itemizedlist>
845
846 <para>
847 Although the 'release' buildtype is described as enabling -DNDEBUG, and all
848 CMake Release builds pass that, it has so far only been observed (in
849 verbose builds) for <xref linkend="mesa"/>. That suggests that it might
850 only be used when there are debug assertions present.
851 </para>
852
853 <para>
854 The -DNDEBUG flag can also be provided by passing
855 <command>-Db_ndebug=true</command>.
856 </para>
857
858 <para>
859 To see the details of the commands which are being run in a package using
860 meson, use 'ninja -v'.
861 </para>
862
863 <bridgehead renderas="sect3" id="rust-info">Rustc and Cargo</bridgehead>
864
865 <para>
866 Most released rustc programs are provided as crates (source tarballs)
867 which will query a server to check current versions of dependencies
868 and then download them as necessary. These packages are built using
869 <command>cargo --release</command>. In theory, you can manipulate the
870 RUSTFLAGS to change the optimize-level (default is 3, like -O3, e.g.
871 <literal>-Copt-level=3</literal>) or to force it to build for the
872 machine it is being compiled on, using
873 <literal>-Ctarget-cpu=native</literal> but in practice this seems to
874 make no significant difference.
875 </para>
876
877 <para>
878 If you find an interesting rustc program which is only provided as
879 unpackaged source, you should at least specify
880 <literal>RUSTFLAGS=-Copt-level=2</literal> otherwise it will do an
881 unoptimized compile with debug info and run <emphasis>much</emphasis>
882 slower.
883 </para>
884
885 <para>
886 The rust developers seem to assume that everyone will compile on a
887 machine dedicated to producing builds, so by default all CPUs are used.
888 This can often be worked around, either by exporting
889 CARGO_BUILD_JOBS=&lt;N&gt; or passing --jobs &lt;N&gt; to cargo. For
890 compiling rustc itself, specifying --jobs &lt;N&gt; on invocations of
891 x.py (together with the <envar>CARGO_BUILD_JOBS</envar> environment
892 variable, which looks like a "belt and braces" approach but seems to be
893 necessary) mostly works. The exception is running the tests when building
894 rustc, some of them will nevertheless use all online CPUs, at least as of
895 rustc-1.42.0.
896 </para>
897
898 </sect2>
899
900 <sect2 id="optimizations">
901 <title>Optimizing the build</title>
902
903 <para>
904 Many people will prefer to optimize compiles as they see fit, by providing
905 CFLAGS or CXXFLAGS. For an introduction to the options available with gcc
906 and g++ see <ulink
907 url="https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html"/> and <ulink
908 url="https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html"/>
909 and <command>info gcc</command>.
910
911 </para>
912
913 <para>
914 Some packages default to '-O2 -g', others to '-O3 -g', and if CFLAGS or
915 CXXFLAGS are supplied they might be added to the package's defaults,
916 replace the package's defaults, or even be ignored. There are details
917 on some desktop packages which were mostly current in April 2019 at
918 <ulink url="https://www.linuxfromscratch.org/~ken/tuning/"/> - in
919 particular, README.txt, tuning-1-packages-and-notes.txt, and
920 tuning-notes-2B.txt. The particular thing to remember is that if you
921 want to try some of the more interesting flags you may need to force
922 verbose builds to confirm what is being used.
923 </para>
924
925 <para>
926 Clearly, if you are optimizing your own program you can spend time to
927 profile it and perhaps recode some of it if it is too slow. But for
928 building a whole system that approach is impractical. In general,
929 -O3 usually produces faster programs than -O2. Specifying
930 -march=native is also beneficial, but means that you cannot move the
931 binaries to an incompatible machine - this can also apply to newer
932 machines, not just to older machines. For example programs compiled for
933 'amdfam10' run on old Phenoms, Kaveris, and Ryzens : but programs
934 compiled for a Kaveri will not run on a Ryzen because certain op-codes
935 are not present. Similarly, if you build for a Haswell not everything
936 will run on a SandyBridge.
937 </para>
938
939 <para>
940 There are also various other options which some people claim are
941 beneficial. At worst, you get to recompile and test, and then
942 discover that in your usage the options do not provide a benefit.
943 </para>
944
945 <para>
946 If building Perl or Python modules, or Qt packages which use qmake,
947 in general the CFLAGS and CXXFLAGS used are those which were used by
948 those 'parent' packages.
949 </para>
950
951 </sect2>
952
953 <sect2 id="hardening">
954 <title>Options for hardening the build</title>
955
956 <para>
957 Even on desktop systems, there are still a lot of exploitable
958 vulnerabilities. For many of these, the attack comes via javascript
959 in a browser. Often, a series of vulnerabilities are used to gain
960 access to data (or sometimes to pwn, i.e. own, the machine and
961 install rootkits). Most commercial distros will apply various
962 hardening measures.
963 </para>
964
965 <para>
966 In the past, there was Hardened LFS where gcc (a much older version)
967 was forced to use hardening (with options to turn some of it off on a
968 per-package basis). The current LFS and BLFS books are carrying
969 forward a part of its spirit by enabling PIE
970 (<option>-fPIE -pie</option>) and SSP
971 (<option>-fstack-protector-strong</option>) as the defaults
972 for GCC and clang. What is being covered here is different - first
973 you have to make sure that the package is indeed using your added
974 flags and not over-riding them.
975 </para>
976
977 <para>
978 For hardening options which are reasonably cheap, there is some
979 discussion in the 'tuning' link above (occasionally, one or more
980 of these options might be inappropriate for a package). These
981 options are <option>-D_FORTIFY_SOURCE=2</option> and
982 (for C++) <option>-D_GLIBCXX_ASSERTIONS</option>. On modern
983 machines these should only have a little impact on how fast things
984 run, and often they will not be noticeable.
985 </para>
986
987 <para>
988 The main distros use much more, such as RELRO (Relocation Read Only)
989 and perhaps <option>-fstack-clash-protection</option>. You may also
990 encounter the so-called <quote>userspace retpoline</quote>
991 (<option>-mindirect-branch=thunk</option> etc.) which
992 is the equivalent of the spectre mitigations applied to the linux
993 kernel in late 2018. The kernel mitigations caused a lot of complaints
994 about lost performance, if you have a production server you might wish
995 to consider testing that, along with the other available options, to
996 see if performance is still sufficient.
997 </para>
998
999 <para>
1000 Whilst gcc has many hardening options, clang/LLVM's strengths lie
1001 elsewhere. Some options which gcc provides are said to be less effective
1002 in clang/LLVM.
1003 </para>
1004
1005 </sect2>
1006
1007</sect1>
Note: See TracBrowser for help on using the repository browser.