Further build-many-glibcs.py fixes for utcnow() deprecation
[glibc.git] / scripts / build-many-glibcs.py
blobd08afc407428bf012a9cacdf28a8d5e8d18ae2c4
1 #!/usr/bin/python3
2 # Build many configurations of glibc.
3 # Copyright (C) 2016-2024 Free Software Foundation, Inc.
4 # Copyright The GNU Toolchain Authors.
5 # This file is part of the GNU C Library.
7 # The GNU C Library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # The GNU C Library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with the GNU C Library; if not, see
19 # <https://www.gnu.org/licenses/>.
21 """Build many configurations of glibc.
23 This script takes as arguments a directory name (containing a src
24 subdirectory with sources of the relevant toolchain components) and a
25 description of what to do: 'checkout', to check out sources into that
26 directory, 'bot-cycle', to run a series of checkout and build steps,
27 'bot', to run 'bot-cycle' repeatedly, 'host-libraries', to build
28 libraries required by the toolchain, 'compilers', to build
29 cross-compilers for various configurations, or 'glibcs', to build
30 glibc for various configurations and run the compilation parts of the
31 testsuite. Subsequent arguments name the versions of components to
32 check out (<component>-<version), for 'checkout', or, for actions
33 other than 'checkout' and 'bot-cycle', name configurations for which
34 compilers or glibc are to be built.
36 The 'list-compilers' command prints the name of each available
37 compiler configuration, without building anything. The 'list-glibcs'
38 command prints the name of each glibc compiler configuration, followed
39 by the space, followed by the name of the compiler configuration used
40 for building this glibc variant.
42 """
44 import argparse
45 import datetime
46 import email.mime.text
47 import email.utils
48 import json
49 import os
50 import re
51 import shutil
52 import smtplib
53 import stat
54 import subprocess
55 import sys
56 import time
57 import urllib.request
59 # This is a list of system utilities that are expected to be available
60 # to this script, and, if a non-zero version is included, the minimum
61 # version required to work with this sccript.
62 def get_list_of_required_tools():
63 global REQUIRED_TOOLS
64 REQUIRED_TOOLS = {
65 'awk' : (get_version_awk, (0,0,0)),
66 'bison' : (get_version, (0,0)),
67 'flex' : (get_version, (0,0,0)),
68 'git' : (get_version, (1,8,3)),
69 'make' : (get_version, (4,0)),
70 'makeinfo' : (get_version, (0,0)),
71 'patch' : (get_version, (0,0,0)),
72 'sed' : (get_version, (0,0)),
73 'tar' : (get_version, (0,0,0)),
74 'gzip' : (get_version, (0,0)),
75 'bzip2' : (get_version_bzip2, (0,0,0)),
76 'xz' : (get_version, (0,0,0)),
79 try:
80 subprocess.run
81 except:
82 class _CompletedProcess:
83 def __init__(self, args, returncode, stdout=None, stderr=None):
84 self.args = args
85 self.returncode = returncode
86 self.stdout = stdout
87 self.stderr = stderr
89 def _run(*popenargs, input=None, timeout=None, check=False, **kwargs):
90 assert(timeout is None)
91 with subprocess.Popen(*popenargs, **kwargs) as process:
92 try:
93 stdout, stderr = process.communicate(input)
94 except:
95 process.kill()
96 process.wait()
97 raise
98 returncode = process.poll()
99 if check and returncode:
100 raise subprocess.CalledProcessError(returncode, popenargs)
101 return _CompletedProcess(popenargs, returncode, stdout, stderr)
103 subprocess.run = _run
106 class Context(object):
107 """The global state associated with builds in a given directory."""
109 def __init__(self, topdir, parallelism, keep, replace_sources, strip,
110 full_gcc, action, shallow=False):
111 """Initialize the context."""
112 self.topdir = topdir
113 self.parallelism = parallelism
114 self.keep = keep
115 self.replace_sources = replace_sources
116 self.strip = strip
117 self.full_gcc = full_gcc
118 self.shallow = shallow
119 self.srcdir = os.path.join(topdir, 'src')
120 self.versions_json = os.path.join(self.srcdir, 'versions.json')
121 self.build_state_json = os.path.join(topdir, 'build-state.json')
122 self.bot_config_json = os.path.join(topdir, 'bot-config.json')
123 self.installdir = os.path.join(topdir, 'install')
124 self.host_libraries_installdir = os.path.join(self.installdir,
125 'host-libraries')
126 self.builddir = os.path.join(topdir, 'build')
127 self.logsdir = os.path.join(topdir, 'logs')
128 self.logsdir_old = os.path.join(topdir, 'logs-old')
129 self.makefile = os.path.join(self.builddir, 'Makefile')
130 self.wrapper = os.path.join(self.builddir, 'wrapper')
131 self.save_logs = os.path.join(self.builddir, 'save-logs')
132 self.script_text = self.get_script_text()
133 if action not in ('checkout', 'list-compilers', 'list-glibcs'):
134 self.build_triplet = self.get_build_triplet()
135 self.glibc_version = self.get_glibc_version()
136 self.configs = {}
137 self.glibc_configs = {}
138 self.makefile_pieces = ['.PHONY: all\n']
139 self.add_all_configs()
140 self.load_versions_json()
141 self.load_build_state_json()
142 self.status_log_list = []
143 self.email_warning = False
145 def get_script_text(self):
146 """Return the text of this script."""
147 with open(sys.argv[0], 'r') as f:
148 return f.read()
150 def exec_self(self):
151 """Re-execute this script with the same arguments."""
152 sys.stdout.flush()
153 os.execv(sys.executable, [sys.executable] + sys.argv)
155 def get_build_triplet(self):
156 """Determine the build triplet with config.guess."""
157 config_guess = os.path.join(self.component_srcdir('gcc'),
158 'config.guess')
159 cg_out = subprocess.run([config_guess], stdout=subprocess.PIPE,
160 check=True, universal_newlines=True).stdout
161 return cg_out.rstrip()
163 def get_glibc_version(self):
164 """Determine the glibc version number (major.minor)."""
165 version_h = os.path.join(self.component_srcdir('glibc'), 'version.h')
166 with open(version_h, 'r') as f:
167 lines = f.readlines()
168 starttext = '#define VERSION "'
169 for l in lines:
170 if l.startswith(starttext):
171 l = l[len(starttext):]
172 l = l.rstrip('"\n')
173 m = re.fullmatch(r'([0-9]+)\.([0-9]+)[.0-9]*', l)
174 return '%s.%s' % m.group(1, 2)
175 print('error: could not determine glibc version')
176 exit(1)
178 def add_all_configs(self):
179 """Add all known glibc build configurations."""
180 self.add_config(arch='aarch64',
181 os_name='linux-gnu',
182 extra_glibcs=[{'variant': 'disable-multi-arch',
183 'cfg': ['--disable-multi-arch']}])
184 self.add_config(arch='aarch64_be',
185 os_name='linux-gnu')
186 self.add_config(arch='arc',
187 os_name='linux-gnu',
188 gcc_cfg=['--disable-multilib', '--with-cpu=hs38'])
189 self.add_config(arch='arc',
190 os_name='linux-gnuhf',
191 gcc_cfg=['--disable-multilib', '--with-cpu=hs38_linux'])
192 self.add_config(arch='arceb',
193 os_name='linux-gnu',
194 gcc_cfg=['--disable-multilib', '--with-cpu=hs38'])
195 self.add_config(arch='alpha',
196 os_name='linux-gnu')
197 self.add_config(arch='arm',
198 os_name='linux-gnueabi',
199 extra_glibcs=[{'variant': 'v4t',
200 'ccopts': '-march=armv4t'}])
201 self.add_config(arch='armeb',
202 os_name='linux-gnueabi')
203 self.add_config(arch='armeb',
204 os_name='linux-gnueabi',
205 variant='be8',
206 gcc_cfg=['--with-arch=armv7-a'])
207 self.add_config(arch='arm',
208 os_name='linux-gnueabihf',
209 gcc_cfg=['--with-float=hard', '--with-cpu=arm926ej-s'],
210 extra_glibcs=[{'variant': 'v7a',
211 'ccopts': '-march=armv7-a -mfpu=vfpv3'},
212 {'variant': 'thumb',
213 'ccopts':
214 '-mthumb -march=armv7-a -mfpu=vfpv3'},
215 {'variant': 'v7a-disable-multi-arch',
216 'ccopts': '-march=armv7-a -mfpu=vfpv3',
217 'cfg': ['--disable-multi-arch']}])
218 self.add_config(arch='armeb',
219 os_name='linux-gnueabihf',
220 gcc_cfg=['--with-float=hard', '--with-cpu=arm926ej-s'])
221 self.add_config(arch='armeb',
222 os_name='linux-gnueabihf',
223 variant='be8',
224 gcc_cfg=['--with-float=hard', '--with-arch=armv7-a',
225 '--with-fpu=vfpv3'])
226 self.add_config(arch='csky',
227 os_name='linux-gnuabiv2',
228 variant='soft',
229 gcc_cfg=['--disable-multilib'])
230 self.add_config(arch='csky',
231 os_name='linux-gnuabiv2',
232 gcc_cfg=['--with-float=hard', '--disable-multilib'])
233 self.add_config(arch='hppa',
234 os_name='linux-gnu')
235 self.add_config(arch='i686',
236 os_name='gnu')
237 self.add_config(arch='loongarch64',
238 os_name='linux-gnu',
239 variant='lp64d',
240 gcc_cfg=['--with-abi=lp64d','--disable-multilib'])
241 self.add_config(arch='loongarch64',
242 os_name='linux-gnu',
243 variant='lp64s',
244 gcc_cfg=['--with-abi=lp64s','--disable-multilib'])
245 self.add_config(arch='m68k',
246 os_name='linux-gnu',
247 gcc_cfg=['--disable-multilib'])
248 self.add_config(arch='m68k',
249 os_name='linux-gnu',
250 variant='coldfire',
251 gcc_cfg=['--with-arch=cf', '--disable-multilib'])
252 self.add_config(arch='m68k',
253 os_name='linux-gnu',
254 variant='coldfire-soft',
255 gcc_cfg=['--with-arch=cf', '--with-cpu=54455',
256 '--disable-multilib'])
257 self.add_config(arch='microblaze',
258 os_name='linux-gnu',
259 gcc_cfg=['--disable-multilib'])
260 self.add_config(arch='microblazeel',
261 os_name='linux-gnu',
262 gcc_cfg=['--disable-multilib'])
263 self.add_config(arch='mips64',
264 os_name='linux-gnu',
265 gcc_cfg=['--with-mips-plt'],
266 glibcs=[{'variant': 'n32'},
267 {'arch': 'mips',
268 'ccopts': '-mabi=32'},
269 {'variant': 'n64',
270 'ccopts': '-mabi=64'}])
271 self.add_config(arch='mips64',
272 os_name='linux-gnu',
273 variant='soft',
274 gcc_cfg=['--with-mips-plt', '--with-float=soft'],
275 glibcs=[{'variant': 'n32-soft'},
276 {'variant': 'soft',
277 'arch': 'mips',
278 'ccopts': '-mabi=32'},
279 {'variant': 'n64-soft',
280 'ccopts': '-mabi=64'}])
281 self.add_config(arch='mips64',
282 os_name='linux-gnu',
283 variant='nan2008',
284 gcc_cfg=['--with-mips-plt', '--with-nan=2008',
285 '--with-arch-64=mips64r2',
286 '--with-arch-32=mips32r2'],
287 glibcs=[{'variant': 'n32-nan2008'},
288 {'variant': 'nan2008',
289 'arch': 'mips',
290 'ccopts': '-mabi=32'},
291 {'variant': 'n64-nan2008',
292 'ccopts': '-mabi=64'}])
293 self.add_config(arch='mips64',
294 os_name='linux-gnu',
295 variant='nan2008-soft',
296 gcc_cfg=['--with-mips-plt', '--with-nan=2008',
297 '--with-arch-64=mips64r2',
298 '--with-arch-32=mips32r2',
299 '--with-float=soft'],
300 glibcs=[{'variant': 'n32-nan2008-soft'},
301 {'variant': 'nan2008-soft',
302 'arch': 'mips',
303 'ccopts': '-mabi=32'},
304 {'variant': 'n64-nan2008-soft',
305 'ccopts': '-mabi=64'}])
306 self.add_config(arch='mips64el',
307 os_name='linux-gnu',
308 gcc_cfg=['--with-mips-plt'],
309 glibcs=[{'variant': 'n32'},
310 {'arch': 'mipsel',
311 'ccopts': '-mabi=32'},
312 {'variant': 'n64',
313 'ccopts': '-mabi=64'}])
314 self.add_config(arch='mips64el',
315 os_name='linux-gnu',
316 variant='soft',
317 gcc_cfg=['--with-mips-plt', '--with-float=soft'],
318 glibcs=[{'variant': 'n32-soft'},
319 {'variant': 'soft',
320 'arch': 'mipsel',
321 'ccopts': '-mabi=32'},
322 {'variant': 'n64-soft',
323 'ccopts': '-mabi=64'}])
324 self.add_config(arch='mips64el',
325 os_name='linux-gnu',
326 variant='nan2008',
327 gcc_cfg=['--with-mips-plt', '--with-nan=2008',
328 '--with-arch-64=mips64r2',
329 '--with-arch-32=mips32r2'],
330 glibcs=[{'variant': 'n32-nan2008'},
331 {'variant': 'nan2008',
332 'arch': 'mipsel',
333 'ccopts': '-mabi=32'},
334 {'variant': 'n64-nan2008',
335 'ccopts': '-mabi=64'}])
336 self.add_config(arch='mips64el',
337 os_name='linux-gnu',
338 variant='nan2008-soft',
339 gcc_cfg=['--with-mips-plt', '--with-nan=2008',
340 '--with-arch-64=mips64r2',
341 '--with-arch-32=mips32r2',
342 '--with-float=soft'],
343 glibcs=[{'variant': 'n32-nan2008-soft'},
344 {'variant': 'nan2008-soft',
345 'arch': 'mipsel',
346 'ccopts': '-mabi=32'},
347 {'variant': 'n64-nan2008-soft',
348 'ccopts': '-mabi=64'}])
349 self.add_config(arch='mipsisa64r6el',
350 os_name='linux-gnu',
351 gcc_cfg=['--with-mips-plt', '--with-nan=2008',
352 '--with-arch-64=mips64r6',
353 '--with-arch-32=mips32r6',
354 '--with-float=hard'],
355 glibcs=[{'variant': 'n32'},
356 {'arch': 'mipsisa32r6el',
357 'ccopts': '-mabi=32'},
358 {'variant': 'n64',
359 'ccopts': '-mabi=64'}])
360 self.add_config(arch='nios2',
361 os_name='linux-gnu')
362 self.add_config(arch='or1k',
363 os_name='linux-gnu',
364 variant='soft',
365 gcc_cfg=['--with-multilib-list=mcmov'])
366 self.add_config(arch='powerpc',
367 os_name='linux-gnu',
368 gcc_cfg=['--disable-multilib', '--enable-secureplt'],
369 extra_glibcs=[{'variant': 'power4',
370 'ccopts': '-mcpu=power4',
371 'cfg': ['--with-cpu=power4']}])
372 self.add_config(arch='powerpc',
373 os_name='linux-gnu',
374 variant='soft',
375 gcc_cfg=['--disable-multilib', '--with-float=soft',
376 '--enable-secureplt'])
377 self.add_config(arch='powerpc64',
378 os_name='linux-gnu',
379 gcc_cfg=['--disable-multilib', '--enable-secureplt'])
380 self.add_config(arch='powerpc64le',
381 os_name='linux-gnu',
382 gcc_cfg=['--disable-multilib', '--enable-secureplt'],
383 extra_glibcs=[{'variant': 'disable-multi-arch',
384 'cfg': ['--disable-multi-arch']}])
385 self.add_config(arch='riscv32',
386 os_name='linux-gnu',
387 variant='rv32imac-ilp32',
388 gcc_cfg=['--with-arch=rv32imac', '--with-abi=ilp32',
389 '--disable-multilib'])
390 self.add_config(arch='riscv32',
391 os_name='linux-gnu',
392 variant='rv32imafdc-ilp32',
393 gcc_cfg=['--with-arch=rv32imafdc', '--with-abi=ilp32',
394 '--disable-multilib'])
395 self.add_config(arch='riscv32',
396 os_name='linux-gnu',
397 variant='rv32imafdc-ilp32d',
398 gcc_cfg=['--with-arch=rv32imafdc', '--with-abi=ilp32d',
399 '--disable-multilib'])
400 self.add_config(arch='riscv64',
401 os_name='linux-gnu',
402 variant='rv64imac-lp64',
403 gcc_cfg=['--with-arch=rv64imac', '--with-abi=lp64',
404 '--disable-multilib'])
405 self.add_config(arch='riscv64',
406 os_name='linux-gnu',
407 variant='rv64imafdc-lp64',
408 gcc_cfg=['--with-arch=rv64imafdc', '--with-abi=lp64',
409 '--disable-multilib'])
410 self.add_config(arch='riscv64',
411 os_name='linux-gnu',
412 variant='rv64imafdc-lp64d',
413 gcc_cfg=['--with-arch=rv64imafdc', '--with-abi=lp64d',
414 '--disable-multilib'])
415 self.add_config(arch='s390x',
416 os_name='linux-gnu',
417 glibcs=[{},
418 {'arch': 's390', 'ccopts': '-m31'}],
419 extra_glibcs=[{'variant': 'O3',
420 'cflags': '-O3'}])
421 self.add_config(arch='sh3',
422 os_name='linux-gnu')
423 self.add_config(arch='sh3eb',
424 os_name='linux-gnu')
425 self.add_config(arch='sh4',
426 os_name='linux-gnu')
427 self.add_config(arch='sh4eb',
428 os_name='linux-gnu')
429 self.add_config(arch='sh4',
430 os_name='linux-gnu',
431 variant='soft',
432 gcc_cfg=['--without-fp'])
433 self.add_config(arch='sh4eb',
434 os_name='linux-gnu',
435 variant='soft',
436 gcc_cfg=['--without-fp'])
437 self.add_config(arch='sparc64',
438 os_name='linux-gnu',
439 glibcs=[{},
440 {'arch': 'sparcv9',
441 'ccopts': '-m32 -mlong-double-128 -mcpu=v9'}],
442 extra_glibcs=[{'variant': 'leon3',
443 'arch' : 'sparcv8',
444 'ccopts' : '-m32 -mlong-double-128 -mcpu=leon3'},
445 {'variant': 'disable-multi-arch',
446 'cfg': ['--disable-multi-arch']},
447 {'variant': 'disable-multi-arch',
448 'arch': 'sparcv9',
449 'ccopts': '-m32 -mlong-double-128 -mcpu=v9',
450 'cfg': ['--disable-multi-arch']}])
451 self.add_config(arch='x86_64',
452 os_name='linux-gnu',
453 gcc_cfg=['--with-multilib-list=m64,m32,mx32'],
454 glibcs=[{},
455 {'variant': 'x32', 'ccopts': '-mx32'},
456 {'arch': 'i686', 'ccopts': '-m32 -march=i686'}],
457 extra_glibcs=[{'variant': 'disable-multi-arch',
458 'cfg': ['--disable-multi-arch']},
459 {'variant': 'minimal',
460 'cfg': ['--disable-multi-arch',
461 '--disable-profile',
462 '--disable-timezone-tools',
463 '--disable-mathvec',
464 '--disable-build-nscd',
465 '--disable-nscd']},
466 {'variant': 'no-pie',
467 'cfg': ['--disable-default-pie']},
468 {'variant': 'x32-no-pie',
469 'ccopts': '-mx32',
470 'cfg': ['--disable-default-pie']},
471 {'variant': 'no-pie',
472 'arch': 'i686',
473 'ccopts': '-m32 -march=i686',
474 'cfg': ['--disable-default-pie']},
475 {'variant': 'disable-multi-arch',
476 'arch': 'i686',
477 'ccopts': '-m32 -march=i686',
478 'cfg': ['--disable-multi-arch']},
479 {'arch': 'i486',
480 'ccopts': '-m32 -march=i486'},
481 {'arch': 'i586',
482 'ccopts': '-m32 -march=i586'},
483 {'variant': 'enable-fortify-source',
484 'cfg': ['--enable-fortify-source']}])
485 self.add_config(arch='x86_64',
486 os_name='gnu',
487 gcc_cfg=['--disable-multilib'])
489 def add_config(self, **args):
490 """Add an individual build configuration."""
491 cfg = Config(self, **args)
492 if cfg.name in self.configs:
493 print('error: duplicate config %s' % cfg.name)
494 exit(1)
495 self.configs[cfg.name] = cfg
496 for c in cfg.all_glibcs:
497 if c.name in self.glibc_configs:
498 print('error: duplicate glibc config %s' % c.name)
499 exit(1)
500 self.glibc_configs[c.name] = c
502 def component_srcdir(self, component):
503 """Return the source directory for a given component, e.g. gcc."""
504 return os.path.join(self.srcdir, component)
506 def component_builddir(self, action, config, component, subconfig=None):
507 """Return the directory to use for a build."""
508 if config is None:
509 # Host libraries.
510 assert subconfig is None
511 return os.path.join(self.builddir, action, component)
512 if subconfig is None:
513 return os.path.join(self.builddir, action, config, component)
514 else:
515 # glibc build as part of compiler build.
516 return os.path.join(self.builddir, action, config, component,
517 subconfig)
519 def compiler_installdir(self, config):
520 """Return the directory in which to install a compiler."""
521 return os.path.join(self.installdir, 'compilers', config)
523 def compiler_bindir(self, config):
524 """Return the directory in which to find compiler binaries."""
525 return os.path.join(self.compiler_installdir(config), 'bin')
527 def compiler_sysroot(self, config):
528 """Return the sysroot directory for a compiler."""
529 return os.path.join(self.compiler_installdir(config), 'sysroot')
531 def glibc_installdir(self, config):
532 """Return the directory in which to install glibc."""
533 return os.path.join(self.installdir, 'glibcs', config)
535 def run_builds(self, action, configs):
536 """Run the requested builds."""
537 if action == 'checkout':
538 self.checkout(configs)
539 return
540 if action == 'bot-cycle':
541 if configs:
542 print('error: configurations specified for bot-cycle')
543 exit(1)
544 self.bot_cycle()
545 return
546 if action == 'bot':
547 if configs:
548 print('error: configurations specified for bot')
549 exit(1)
550 self.bot()
551 return
552 if action in ('host-libraries', 'list-compilers',
553 'list-glibcs') and configs:
554 print('error: configurations specified for ' + action)
555 exit(1)
556 if action == 'list-compilers':
557 for name in sorted(self.configs.keys()):
558 print(name)
559 return
560 if action == 'list-glibcs':
561 for config in sorted(self.glibc_configs.values(),
562 key=lambda c: c.name):
563 print(config.name, config.compiler.name)
564 return
565 self.clear_last_build_state(action)
566 build_time = datetime.datetime.now(datetime.timezone.utc)
567 if action == 'host-libraries':
568 build_components = ('gmp', 'mpfr', 'mpc')
569 old_components = ()
570 old_versions = {}
571 self.build_host_libraries()
572 elif action == 'compilers':
573 build_components = ('binutils', 'gcc', 'glibc', 'linux', 'mig',
574 'gnumach', 'hurd')
575 old_components = ('gmp', 'mpfr', 'mpc')
576 old_versions = self.build_state['host-libraries']['build-versions']
577 self.build_compilers(configs)
578 else:
579 build_components = ('glibc',)
580 old_components = ('gmp', 'mpfr', 'mpc', 'binutils', 'gcc', 'linux',
581 'mig', 'gnumach', 'hurd')
582 old_versions = self.build_state['compilers']['build-versions']
583 if action == 'update-syscalls':
584 self.update_syscalls(configs)
585 else:
586 self.build_glibcs(configs)
587 self.write_files()
588 self.do_build()
589 if configs:
590 # Partial build, do not update stored state.
591 return
592 build_versions = {}
593 for k in build_components:
594 if k in self.versions:
595 build_versions[k] = {'version': self.versions[k]['version'],
596 'revision': self.versions[k]['revision']}
597 for k in old_components:
598 if k in old_versions:
599 build_versions[k] = {'version': old_versions[k]['version'],
600 'revision': old_versions[k]['revision']}
601 self.update_build_state(action, build_time, build_versions)
603 @staticmethod
604 def remove_dirs(*args):
605 """Remove directories and their contents if they exist."""
606 for dir in args:
607 shutil.rmtree(dir, ignore_errors=True)
609 @staticmethod
610 def remove_recreate_dirs(*args):
611 """Remove directories if they exist, and create them as empty."""
612 Context.remove_dirs(*args)
613 for dir in args:
614 os.makedirs(dir, exist_ok=True)
616 def add_makefile_cmdlist(self, target, cmdlist, logsdir):
617 """Add makefile text for a list of commands."""
618 commands = cmdlist.makefile_commands(self.wrapper, logsdir)
619 self.makefile_pieces.append('all: %s\n.PHONY: %s\n%s:\n%s\n' %
620 (target, target, target, commands))
621 self.status_log_list.extend(cmdlist.status_logs(logsdir))
623 def write_files(self):
624 """Write out the Makefile and wrapper script."""
625 mftext = ''.join(self.makefile_pieces)
626 with open(self.makefile, 'w') as f:
627 f.write(mftext)
628 wrapper_text = (
629 '#!/bin/sh\n'
630 'prev_base=$1\n'
631 'this_base=$2\n'
632 'desc=$3\n'
633 'dir=$4\n'
634 'path=$5\n'
635 'shift 5\n'
636 'prev_status=$prev_base-status.txt\n'
637 'this_status=$this_base-status.txt\n'
638 'this_log=$this_base-log.txt\n'
639 'date > "$this_log"\n'
640 'echo >> "$this_log"\n'
641 'echo "Description: $desc" >> "$this_log"\n'
642 'printf "%s" "Command:" >> "$this_log"\n'
643 'for word in "$@"; do\n'
644 ' if expr "$word" : "[]+,./0-9@A-Z_a-z-]\\\\{1,\\\\}\\$" > /dev/null; then\n'
645 ' printf " %s" "$word"\n'
646 ' else\n'
647 ' printf " \'"\n'
648 ' printf "%s" "$word" | sed -e "s/\'/\'\\\\\\\\\'\'/"\n'
649 ' printf "\'"\n'
650 ' fi\n'
651 'done >> "$this_log"\n'
652 'echo >> "$this_log"\n'
653 'echo "Directory: $dir" >> "$this_log"\n'
654 'echo "Path addition: $path" >> "$this_log"\n'
655 'echo >> "$this_log"\n'
656 'record_status ()\n'
657 '{\n'
658 ' echo >> "$this_log"\n'
659 ' echo "$1: $desc" > "$this_status"\n'
660 ' echo "$1: $desc" >> "$this_log"\n'
661 ' echo >> "$this_log"\n'
662 ' date >> "$this_log"\n'
663 ' echo "$1: $desc"\n'
664 ' exit 0\n'
665 '}\n'
666 'check_error ()\n'
667 '{\n'
668 ' if [ "$1" != "0" ]; then\n'
669 ' record_status FAIL\n'
670 ' fi\n'
671 '}\n'
672 'if [ "$prev_base" ] && ! grep -q "^PASS" "$prev_status"; then\n'
673 ' record_status UNRESOLVED\n'
674 'fi\n'
675 'if [ "$dir" ]; then\n'
676 ' cd "$dir"\n'
677 ' check_error "$?"\n'
678 'fi\n'
679 'if [ "$path" ]; then\n'
680 ' PATH=$path:$PATH\n'
681 'fi\n'
682 '"$@" < /dev/null >> "$this_log" 2>&1\n'
683 'check_error "$?"\n'
684 'record_status PASS\n')
685 with open(self.wrapper, 'w') as f:
686 f.write(wrapper_text)
687 # Mode 0o755.
688 mode_exec = (stat.S_IRWXU|stat.S_IRGRP|stat.S_IXGRP|
689 stat.S_IROTH|stat.S_IXOTH)
690 os.chmod(self.wrapper, mode_exec)
691 save_logs_text = (
692 '#!/bin/sh\n'
693 'if ! [ -f tests.sum ]; then\n'
694 ' echo "No test summary available."\n'
695 ' exit 0\n'
696 'fi\n'
697 'save_file ()\n'
698 '{\n'
699 ' echo "Contents of $1:"\n'
700 ' echo\n'
701 ' cat "$1"\n'
702 ' echo\n'
703 ' echo "End of contents of $1."\n'
704 ' echo\n'
705 '}\n'
706 'save_file tests.sum\n'
707 'non_pass_tests=$(grep -v "^PASS: " tests.sum | sed -e "s/^PASS: //")\n'
708 'for t in $non_pass_tests; do\n'
709 ' if [ -f "$t.out" ]; then\n'
710 ' save_file "$t.out"\n'
711 ' fi\n'
712 'done\n')
713 with open(self.save_logs, 'w') as f:
714 f.write(save_logs_text)
715 os.chmod(self.save_logs, mode_exec)
717 def do_build(self):
718 """Do the actual build."""
719 cmd = ['make', '-O', '-j%d' % self.parallelism]
720 subprocess.run(cmd, cwd=self.builddir, check=True)
722 def build_host_libraries(self):
723 """Build the host libraries."""
724 installdir = self.host_libraries_installdir
725 builddir = os.path.join(self.builddir, 'host-libraries')
726 logsdir = os.path.join(self.logsdir, 'host-libraries')
727 self.remove_recreate_dirs(installdir, builddir, logsdir)
728 cmdlist = CommandList('host-libraries', self.keep)
729 self.build_host_library(cmdlist, 'gmp')
730 self.build_host_library(cmdlist, 'mpfr',
731 ['--with-gmp=%s' % installdir])
732 self.build_host_library(cmdlist, 'mpc',
733 ['--with-gmp=%s' % installdir,
734 '--with-mpfr=%s' % installdir])
735 cmdlist.add_command('done', ['touch', os.path.join(installdir, 'ok')])
736 self.add_makefile_cmdlist('host-libraries', cmdlist, logsdir)
738 def build_host_library(self, cmdlist, lib, extra_opts=None):
739 """Build one host library."""
740 srcdir = self.component_srcdir(lib)
741 builddir = self.component_builddir('host-libraries', None, lib)
742 installdir = self.host_libraries_installdir
743 cmdlist.push_subdesc(lib)
744 cmdlist.create_use_dir(builddir)
745 cfg_cmd = [os.path.join(srcdir, 'configure'),
746 '--prefix=%s' % installdir,
747 '--disable-shared']
748 if extra_opts:
749 cfg_cmd.extend (extra_opts)
750 cmdlist.add_command('configure', cfg_cmd)
751 cmdlist.add_command('build', ['make'])
752 cmdlist.add_command('check', ['make', 'check'])
753 cmdlist.add_command('install', ['make', 'install'])
754 cmdlist.cleanup_dir()
755 cmdlist.pop_subdesc()
757 def build_compilers(self, configs):
758 """Build the compilers."""
759 if not configs:
760 self.remove_dirs(os.path.join(self.builddir, 'compilers'))
761 self.remove_dirs(os.path.join(self.installdir, 'compilers'))
762 self.remove_dirs(os.path.join(self.logsdir, 'compilers'))
763 configs = sorted(self.configs.keys())
764 for c in configs:
765 self.configs[c].build()
767 def build_glibcs(self, configs):
768 """Build the glibcs."""
769 if not configs:
770 self.remove_dirs(os.path.join(self.builddir, 'glibcs'))
771 self.remove_dirs(os.path.join(self.installdir, 'glibcs'))
772 self.remove_dirs(os.path.join(self.logsdir, 'glibcs'))
773 configs = sorted(self.glibc_configs.keys())
774 for c in configs:
775 self.glibc_configs[c].build()
777 def update_syscalls(self, configs):
778 """Update the glibc syscall lists."""
779 if not configs:
780 self.remove_dirs(os.path.join(self.builddir, 'update-syscalls'))
781 self.remove_dirs(os.path.join(self.logsdir, 'update-syscalls'))
782 configs = sorted(self.glibc_configs.keys())
783 for c in configs:
784 self.glibc_configs[c].update_syscalls()
786 def load_versions_json(self):
787 """Load information about source directory versions."""
788 if not os.access(self.versions_json, os.F_OK):
789 self.versions = {}
790 return
791 with open(self.versions_json, 'r') as f:
792 self.versions = json.load(f)
794 def store_json(self, data, filename):
795 """Store information in a JSON file."""
796 filename_tmp = filename + '.tmp'
797 with open(filename_tmp, 'w') as f:
798 json.dump(data, f, indent=2, sort_keys=True)
799 os.rename(filename_tmp, filename)
801 def store_versions_json(self):
802 """Store information about source directory versions."""
803 self.store_json(self.versions, self.versions_json)
805 def set_component_version(self, component, version, explicit, revision):
806 """Set the version information for a component."""
807 self.versions[component] = {'version': version,
808 'explicit': explicit,
809 'revision': revision}
810 self.store_versions_json()
812 def checkout(self, versions):
813 """Check out the desired component versions."""
814 default_versions = {'binutils': 'vcs-2.41',
815 'gcc': 'vcs-13',
816 'glibc': 'vcs-mainline',
817 'gmp': '6.3.0',
818 'linux': '6.7',
819 'mpc': '1.3.1',
820 'mpfr': '4.2.1',
821 'mig': 'vcs-mainline',
822 'gnumach': 'vcs-mainline',
823 'hurd': 'vcs-mainline'}
824 use_versions = {}
825 explicit_versions = {}
826 for v in versions:
827 found_v = False
828 for k in default_versions.keys():
829 kx = k + '-'
830 if v.startswith(kx):
831 vx = v[len(kx):]
832 if k in use_versions:
833 print('error: multiple versions for %s' % k)
834 exit(1)
835 use_versions[k] = vx
836 explicit_versions[k] = True
837 found_v = True
838 break
839 if not found_v:
840 print('error: unknown component in %s' % v)
841 exit(1)
842 for k in default_versions.keys():
843 if k not in use_versions:
844 if k in self.versions and self.versions[k]['explicit']:
845 use_versions[k] = self.versions[k]['version']
846 explicit_versions[k] = True
847 else:
848 use_versions[k] = default_versions[k]
849 explicit_versions[k] = False
850 os.makedirs(self.srcdir, exist_ok=True)
851 for k in sorted(default_versions.keys()):
852 update = os.access(self.component_srcdir(k), os.F_OK)
853 v = use_versions[k]
854 if (update and
855 k in self.versions and
856 v != self.versions[k]['version']):
857 if not self.replace_sources:
858 print('error: version of %s has changed from %s to %s, '
859 'use --replace-sources to check out again' %
860 (k, self.versions[k]['version'], v))
861 exit(1)
862 shutil.rmtree(self.component_srcdir(k))
863 update = False
864 if v.startswith('vcs-'):
865 revision = self.checkout_vcs(k, v[4:], update)
866 else:
867 self.checkout_tar(k, v, update)
868 revision = v
869 self.set_component_version(k, v, explicit_versions[k], revision)
870 if self.get_script_text() != self.script_text:
871 # Rerun the checkout process in case the updated script
872 # uses different default versions or new components.
873 self.exec_self()
875 def checkout_vcs(self, component, version, update):
876 """Check out the given version of the given component from version
877 control. Return a revision identifier."""
878 if component == 'binutils':
879 git_url = 'https://sourceware.org/git/binutils-gdb.git'
880 if version == 'mainline':
881 git_branch = 'master'
882 else:
883 trans = str.maketrans({'.': '_'})
884 git_branch = 'binutils-%s-branch' % version.translate(trans)
885 return self.git_checkout(component, git_url, git_branch, update)
886 elif component == 'gcc':
887 if version == 'mainline':
888 branch = 'master'
889 else:
890 branch = 'releases/gcc-%s' % version
891 return self.gcc_checkout(branch, update)
892 elif component == 'glibc':
893 git_url = 'https://sourceware.org/git/glibc.git'
894 if version == 'mainline':
895 git_branch = 'master'
896 else:
897 git_branch = 'release/%s/master' % version
898 r = self.git_checkout(component, git_url, git_branch, update)
899 self.fix_glibc_timestamps()
900 return r
901 elif component == 'gnumach':
902 git_url = 'git://git.savannah.gnu.org/hurd/gnumach.git'
903 git_branch = 'master'
904 r = self.git_checkout(component, git_url, git_branch, update)
905 subprocess.run(['autoreconf', '-i'],
906 cwd=self.component_srcdir(component), check=True)
907 return r
908 elif component == 'mig':
909 git_url = 'git://git.savannah.gnu.org/hurd/mig.git'
910 git_branch = 'master'
911 r = self.git_checkout(component, git_url, git_branch, update)
912 subprocess.run(['autoreconf', '-i'],
913 cwd=self.component_srcdir(component), check=True)
914 return r
915 elif component == 'hurd':
916 git_url = 'git://git.savannah.gnu.org/hurd/hurd.git'
917 git_branch = 'master'
918 r = self.git_checkout(component, git_url, git_branch, update)
919 subprocess.run(['autoconf'],
920 cwd=self.component_srcdir(component), check=True)
921 return r
922 else:
923 print('error: component %s coming from VCS' % component)
924 exit(1)
926 def git_checkout(self, component, git_url, git_branch, update):
927 """Check out a component from git. Return a commit identifier."""
928 if update:
929 subprocess.run(['git', 'remote', 'prune', 'origin'],
930 cwd=self.component_srcdir(component), check=True)
931 if self.replace_sources:
932 subprocess.run(['git', 'clean', '-dxfq'],
933 cwd=self.component_srcdir(component), check=True)
934 subprocess.run(['git', 'pull', '-q'],
935 cwd=self.component_srcdir(component), check=True)
936 else:
937 if self.shallow:
938 depth_arg = ('--depth', '1')
939 else:
940 depth_arg = ()
941 subprocess.run(['git', 'clone', '-q', '-b', git_branch,
942 *depth_arg, git_url,
943 self.component_srcdir(component)], check=True)
944 r = subprocess.run(['git', 'rev-parse', 'HEAD'],
945 cwd=self.component_srcdir(component),
946 stdout=subprocess.PIPE,
947 check=True, universal_newlines=True).stdout
948 return r.rstrip()
950 def fix_glibc_timestamps(self):
951 """Fix timestamps in a glibc checkout."""
952 # Ensure that builds do not try to regenerate generated files
953 # in the source tree.
954 srcdir = self.component_srcdir('glibc')
955 # These files have Makefile dependencies to regenerate them in
956 # the source tree that may be active during a normal build.
957 # Some other files have such dependencies but do not need to
958 # be touched because nothing in a build depends on the files
959 # in question.
960 for f in ('sysdeps/mach/hurd/bits/errno.h',):
961 to_touch = os.path.join(srcdir, f)
962 subprocess.run(['touch', '-c', to_touch], check=True)
963 for dirpath, dirnames, filenames in os.walk(srcdir):
964 for f in filenames:
965 if (f == 'configure' or
966 f == 'preconfigure' or
967 f.endswith('-kw.h')):
968 to_touch = os.path.join(dirpath, f)
969 subprocess.run(['touch', to_touch], check=True)
971 def gcc_checkout(self, branch, update):
972 """Check out GCC from git. Return the commit identifier."""
973 if os.access(os.path.join(self.component_srcdir('gcc'), '.svn'),
974 os.F_OK):
975 if not self.replace_sources:
976 print('error: GCC has moved from SVN to git, use '
977 '--replace-sources to check out again')
978 exit(1)
979 shutil.rmtree(self.component_srcdir('gcc'))
980 update = False
981 if not update:
982 self.git_checkout('gcc', 'https://gcc.gnu.org/git/gcc.git',
983 branch, update)
984 subprocess.run(['contrib/gcc_update', '--silent'],
985 cwd=self.component_srcdir('gcc'), check=True)
986 r = subprocess.run(['git', 'rev-parse', 'HEAD'],
987 cwd=self.component_srcdir('gcc'),
988 stdout=subprocess.PIPE,
989 check=True, universal_newlines=True).stdout
990 return r.rstrip()
992 def checkout_tar(self, component, version, update):
993 """Check out the given version of the given component from a
994 tarball."""
995 if update:
996 return
997 url_map = {'binutils': 'https://ftp.gnu.org/gnu/binutils/binutils-%(version)s.tar.bz2',
998 'gcc': 'https://ftp.gnu.org/gnu/gcc/gcc-%(version)s/gcc-%(version)s.tar.gz',
999 'gmp': 'https://ftp.gnu.org/gnu/gmp/gmp-%(version)s.tar.xz',
1000 'linux': 'https://www.kernel.org/pub/linux/kernel/v%(major)s.x/linux-%(version)s.tar.xz',
1001 'mpc': 'https://ftp.gnu.org/gnu/mpc/mpc-%(version)s.tar.gz',
1002 'mpfr': 'https://ftp.gnu.org/gnu/mpfr/mpfr-%(version)s.tar.xz',
1003 'mig': 'https://ftp.gnu.org/gnu/mig/mig-%(version)s.tar.bz2',
1004 'gnumach': 'https://ftp.gnu.org/gnu/gnumach/gnumach-%(version)s.tar.bz2',
1005 'hurd': 'https://ftp.gnu.org/gnu/hurd/hurd-%(version)s.tar.bz2'}
1006 if component not in url_map:
1007 print('error: component %s coming from tarball' % component)
1008 exit(1)
1009 version_major = version.split('.')[0]
1010 url = url_map[component] % {'version': version, 'major': version_major}
1011 filename = os.path.join(self.srcdir, url.split('/')[-1])
1012 response = urllib.request.urlopen(url)
1013 data = response.read()
1014 with open(filename, 'wb') as f:
1015 f.write(data)
1016 subprocess.run(['tar', '-C', self.srcdir, '-x', '-f', filename],
1017 check=True)
1018 os.rename(os.path.join(self.srcdir, '%s-%s' % (component, version)),
1019 self.component_srcdir(component))
1020 os.remove(filename)
1022 def load_build_state_json(self):
1023 """Load information about the state of previous builds."""
1024 if os.access(self.build_state_json, os.F_OK):
1025 with open(self.build_state_json, 'r') as f:
1026 self.build_state = json.load(f)
1027 else:
1028 self.build_state = {}
1029 for k in ('host-libraries', 'compilers', 'glibcs', 'update-syscalls'):
1030 if k not in self.build_state:
1031 self.build_state[k] = {}
1032 if 'build-time' not in self.build_state[k]:
1033 self.build_state[k]['build-time'] = ''
1034 if 'build-versions' not in self.build_state[k]:
1035 self.build_state[k]['build-versions'] = {}
1036 if 'build-results' not in self.build_state[k]:
1037 self.build_state[k]['build-results'] = {}
1038 if 'result-changes' not in self.build_state[k]:
1039 self.build_state[k]['result-changes'] = {}
1040 if 'ever-passed' not in self.build_state[k]:
1041 self.build_state[k]['ever-passed'] = []
1043 def store_build_state_json(self):
1044 """Store information about the state of previous builds."""
1045 self.store_json(self.build_state, self.build_state_json)
1047 def clear_last_build_state(self, action):
1048 """Clear information about the state of part of the build."""
1049 # We clear the last build time and versions when starting a
1050 # new build. The results of the last build are kept around,
1051 # as comparison is still meaningful if this build is aborted
1052 # and a new one started.
1053 self.build_state[action]['build-time'] = ''
1054 self.build_state[action]['build-versions'] = {}
1055 self.store_build_state_json()
1057 def update_build_state(self, action, build_time, build_versions):
1058 """Update the build state after a build."""
1059 build_time = build_time.replace(microsecond=0)
1060 self.build_state[action]['build-time'] = build_time.strftime(
1061 '%Y-%m-%d %H:%M:%S')
1062 self.build_state[action]['build-versions'] = build_versions
1063 build_results = {}
1064 for log in self.status_log_list:
1065 with open(log, 'r') as f:
1066 log_text = f.read()
1067 log_text = log_text.rstrip()
1068 m = re.fullmatch('([A-Z]+): (.*)', log_text)
1069 result = m.group(1)
1070 test_name = m.group(2)
1071 assert test_name not in build_results
1072 build_results[test_name] = result
1073 old_build_results = self.build_state[action]['build-results']
1074 self.build_state[action]['build-results'] = build_results
1075 result_changes = {}
1076 all_tests = set(old_build_results.keys()) | set(build_results.keys())
1077 for t in all_tests:
1078 if t in old_build_results:
1079 old_res = old_build_results[t]
1080 else:
1081 old_res = '(New test)'
1082 if t in build_results:
1083 new_res = build_results[t]
1084 else:
1085 new_res = '(Test removed)'
1086 if old_res != new_res:
1087 result_changes[t] = '%s -> %s' % (old_res, new_res)
1088 self.build_state[action]['result-changes'] = result_changes
1089 old_ever_passed = {t for t in self.build_state[action]['ever-passed']
1090 if t in build_results}
1091 new_passes = {t for t in build_results if build_results[t] == 'PASS'}
1092 self.build_state[action]['ever-passed'] = sorted(old_ever_passed |
1093 new_passes)
1094 self.store_build_state_json()
1096 def load_bot_config_json(self):
1097 """Load bot configuration."""
1098 with open(self.bot_config_json, 'r') as f:
1099 self.bot_config = json.load(f)
1101 def part_build_old(self, action, delay):
1102 """Return whether the last build for a given action was at least a
1103 given number of seconds ago, or does not have a time recorded."""
1104 old_time_str = self.build_state[action]['build-time']
1105 if not old_time_str:
1106 return True
1107 old_time = datetime.datetime.strptime(
1108 old_time_str, '%Y-%m-%d %H:%M:%S').replace(
1109 tzinfo=datetime.timezone.utc)
1110 new_time = datetime.datetime.now(datetime.timezone.utc)
1111 delta = new_time - old_time
1112 return delta.total_seconds() >= delay
1114 def bot_cycle(self):
1115 """Run a single round of checkout and builds."""
1116 print('Bot cycle starting %s.'
1117 % str(datetime.datetime.now(datetime.timezone.utc)))
1118 self.load_bot_config_json()
1119 actions = ('host-libraries', 'compilers', 'glibcs')
1120 self.bot_run_self(['--replace-sources'], 'checkout')
1121 self.load_versions_json()
1122 if self.get_script_text() != self.script_text:
1123 print('Script changed, re-execing.')
1124 # On script change, all parts of the build should be rerun.
1125 for a in actions:
1126 self.clear_last_build_state(a)
1127 self.exec_self()
1128 check_components = {'host-libraries': ('gmp', 'mpfr', 'mpc'),
1129 'compilers': ('binutils', 'gcc', 'glibc', 'linux',
1130 'mig', 'gnumach', 'hurd'),
1131 'glibcs': ('glibc',)}
1132 must_build = {}
1133 for a in actions:
1134 build_vers = self.build_state[a]['build-versions']
1135 must_build[a] = False
1136 if not self.build_state[a]['build-time']:
1137 must_build[a] = True
1138 old_vers = {}
1139 new_vers = {}
1140 for c in check_components[a]:
1141 if c in build_vers:
1142 old_vers[c] = build_vers[c]
1143 new_vers[c] = {'version': self.versions[c]['version'],
1144 'revision': self.versions[c]['revision']}
1145 if new_vers == old_vers:
1146 print('Versions for %s unchanged.' % a)
1147 else:
1148 print('Versions changed or rebuild forced for %s.' % a)
1149 if a == 'compilers' and not self.part_build_old(
1150 a, self.bot_config['compilers-rebuild-delay']):
1151 print('Not requiring rebuild of compilers this soon.')
1152 else:
1153 must_build[a] = True
1154 if must_build['host-libraries']:
1155 must_build['compilers'] = True
1156 if must_build['compilers']:
1157 must_build['glibcs'] = True
1158 for a in actions:
1159 if must_build[a]:
1160 print('Must rebuild %s.' % a)
1161 self.clear_last_build_state(a)
1162 else:
1163 print('No need to rebuild %s.' % a)
1164 if os.access(self.logsdir, os.F_OK):
1165 shutil.rmtree(self.logsdir_old, ignore_errors=True)
1166 shutil.copytree(self.logsdir, self.logsdir_old)
1167 for a in actions:
1168 if must_build[a]:
1169 build_time = datetime.datetime.now(datetime.timezone.utc)
1170 print('Rebuilding %s at %s.' % (a, str(build_time)))
1171 self.bot_run_self([], a)
1172 self.load_build_state_json()
1173 self.bot_build_mail(a, build_time)
1174 print('Bot cycle done at %s.'
1175 % str(datetime.datetime.now(datetime.timezone.utc)))
1177 def bot_build_mail(self, action, build_time):
1178 """Send email with the results of a build."""
1179 if not ('email-from' in self.bot_config and
1180 'email-server' in self.bot_config and
1181 'email-subject' in self.bot_config and
1182 'email-to' in self.bot_config):
1183 if not self.email_warning:
1184 print("Email not configured, not sending.")
1185 self.email_warning = True
1186 return
1188 build_time = build_time.replace(microsecond=0)
1189 subject = (self.bot_config['email-subject'] %
1190 {'action': action,
1191 'build-time': build_time.strftime('%Y-%m-%d %H:%M:%S')})
1192 results = self.build_state[action]['build-results']
1193 changes = self.build_state[action]['result-changes']
1194 ever_passed = set(self.build_state[action]['ever-passed'])
1195 versions = self.build_state[action]['build-versions']
1196 new_regressions = {k for k in changes if changes[k] == 'PASS -> FAIL'}
1197 all_regressions = {k for k in ever_passed if results[k] == 'FAIL'}
1198 all_fails = {k for k in results if results[k] == 'FAIL'}
1199 if new_regressions:
1200 new_reg_list = sorted(['FAIL: %s' % k for k in new_regressions])
1201 new_reg_text = ('New regressions:\n\n%s\n\n' %
1202 '\n'.join(new_reg_list))
1203 else:
1204 new_reg_text = ''
1205 if all_regressions:
1206 all_reg_list = sorted(['FAIL: %s' % k for k in all_regressions])
1207 all_reg_text = ('All regressions:\n\n%s\n\n' %
1208 '\n'.join(all_reg_list))
1209 else:
1210 all_reg_text = ''
1211 if all_fails:
1212 all_fail_list = sorted(['FAIL: %s' % k for k in all_fails])
1213 all_fail_text = ('All failures:\n\n%s\n\n' %
1214 '\n'.join(all_fail_list))
1215 else:
1216 all_fail_text = ''
1217 if changes:
1218 changes_list = sorted(changes.keys())
1219 changes_list = ['%s: %s' % (changes[k], k) for k in changes_list]
1220 changes_text = ('All changed results:\n\n%s\n\n' %
1221 '\n'.join(changes_list))
1222 else:
1223 changes_text = ''
1224 results_text = (new_reg_text + all_reg_text + all_fail_text +
1225 changes_text)
1226 if not results_text:
1227 results_text = 'Clean build with unchanged results.\n\n'
1228 versions_list = sorted(versions.keys())
1229 versions_list = ['%s: %s (%s)' % (k, versions[k]['version'],
1230 versions[k]['revision'])
1231 for k in versions_list]
1232 versions_text = ('Component versions for this build:\n\n%s\n' %
1233 '\n'.join(versions_list))
1234 body_text = results_text + versions_text
1235 msg = email.mime.text.MIMEText(body_text)
1236 msg['Subject'] = subject
1237 msg['From'] = self.bot_config['email-from']
1238 msg['To'] = self.bot_config['email-to']
1239 msg['Message-ID'] = email.utils.make_msgid()
1240 msg['Date'] = email.utils.format_datetime(
1241 datetime.datetime.now(datetime.timezone.utc))
1242 with smtplib.SMTP(self.bot_config['email-server']) as s:
1243 s.send_message(msg)
1245 def bot_run_self(self, opts, action, check=True):
1246 """Run a copy of this script with given options."""
1247 cmd = [sys.executable, sys.argv[0], '--keep=none',
1248 '-j%d' % self.parallelism]
1249 if self.full_gcc:
1250 cmd.append('--full-gcc')
1251 cmd.extend(opts)
1252 cmd.extend([self.topdir, action])
1253 sys.stdout.flush()
1254 subprocess.run(cmd, check=check)
1256 def bot(self):
1257 """Run repeated rounds of checkout and builds."""
1258 while True:
1259 self.load_bot_config_json()
1260 if not self.bot_config['run']:
1261 print('Bot exiting by request.')
1262 exit(0)
1263 self.bot_run_self([], 'bot-cycle', check=False)
1264 self.load_bot_config_json()
1265 if not self.bot_config['run']:
1266 print('Bot exiting by request.')
1267 exit(0)
1268 time.sleep(self.bot_config['delay'])
1269 if self.get_script_text() != self.script_text:
1270 print('Script changed, bot re-execing.')
1271 self.exec_self()
1273 class LinuxHeadersPolicyForBuild(object):
1274 """Names and directories for installing Linux headers. Build variant."""
1276 def __init__(self, config):
1277 self.arch = config.arch
1278 self.srcdir = config.ctx.component_srcdir('linux')
1279 self.builddir = config.component_builddir('linux')
1280 self.headers_dir = os.path.join(config.sysroot, 'usr')
1282 class LinuxHeadersPolicyForUpdateSyscalls(object):
1283 """Names and directories for Linux headers. update-syscalls variant."""
1285 def __init__(self, glibc, headers_dir):
1286 self.arch = glibc.compiler.arch
1287 self.srcdir = glibc.compiler.ctx.component_srcdir('linux')
1288 self.builddir = glibc.ctx.component_builddir(
1289 'update-syscalls', glibc.name, 'build-linux')
1290 self.headers_dir = headers_dir
1292 def install_linux_headers(policy, cmdlist):
1293 """Install Linux kernel headers."""
1294 arch_map = {'aarch64': 'arm64',
1295 'alpha': 'alpha',
1296 'arc': 'arc',
1297 'arm': 'arm',
1298 'csky': 'csky',
1299 'hppa': 'parisc',
1300 'i486': 'x86',
1301 'i586': 'x86',
1302 'i686': 'x86',
1303 'i786': 'x86',
1304 'loongarch64': 'loongarch',
1305 'm68k': 'm68k',
1306 'microblaze': 'microblaze',
1307 'mips': 'mips',
1308 'nios2': 'nios2',
1309 'or1k': 'openrisc',
1310 'powerpc': 'powerpc',
1311 's390': 's390',
1312 'riscv32': 'riscv',
1313 'riscv64': 'riscv',
1314 'sh': 'sh',
1315 'sparc': 'sparc',
1316 'x86_64': 'x86'}
1317 linux_arch = None
1318 for k in arch_map:
1319 if policy.arch.startswith(k):
1320 linux_arch = arch_map[k]
1321 break
1322 assert linux_arch is not None
1323 cmdlist.push_subdesc('linux')
1324 cmdlist.create_use_dir(policy.builddir)
1325 cmdlist.add_command('install-headers',
1326 ['make', '-C', policy.srcdir, 'O=%s' % policy.builddir,
1327 'ARCH=%s' % linux_arch,
1328 'INSTALL_HDR_PATH=%s' % policy.headers_dir,
1329 'headers_install'])
1330 cmdlist.cleanup_dir()
1331 cmdlist.pop_subdesc()
1333 class Config(object):
1334 """A configuration for building a compiler and associated libraries."""
1336 def __init__(self, ctx, arch, os_name, variant=None, gcc_cfg=None,
1337 first_gcc_cfg=None, binutils_cfg=None, glibcs=None,
1338 extra_glibcs=None):
1339 """Initialize a Config object."""
1340 self.ctx = ctx
1341 self.arch = arch
1342 self.os = os_name
1343 self.variant = variant
1344 if variant is None:
1345 self.name = '%s-%s' % (arch, os_name)
1346 else:
1347 self.name = '%s-%s-%s' % (arch, os_name, variant)
1348 self.triplet = '%s-glibc-%s' % (arch, os_name)
1349 if gcc_cfg is None:
1350 self.gcc_cfg = []
1351 else:
1352 self.gcc_cfg = gcc_cfg
1353 if first_gcc_cfg is None:
1354 self.first_gcc_cfg = []
1355 else:
1356 self.first_gcc_cfg = first_gcc_cfg
1357 if binutils_cfg is None:
1358 self.binutils_cfg = []
1359 else:
1360 self.binutils_cfg = binutils_cfg
1361 if glibcs is None:
1362 glibcs = [{'variant': variant}]
1363 if extra_glibcs is None:
1364 extra_glibcs = []
1365 glibcs = [Glibc(self, **g) for g in glibcs]
1366 extra_glibcs = [Glibc(self, **g) for g in extra_glibcs]
1367 self.all_glibcs = glibcs + extra_glibcs
1368 self.compiler_glibcs = glibcs
1369 self.installdir = ctx.compiler_installdir(self.name)
1370 self.bindir = ctx.compiler_bindir(self.name)
1371 self.sysroot = ctx.compiler_sysroot(self.name)
1372 self.builddir = os.path.join(ctx.builddir, 'compilers', self.name)
1373 self.logsdir = os.path.join(ctx.logsdir, 'compilers', self.name)
1375 def component_builddir(self, component):
1376 """Return the directory to use for a (non-glibc) build."""
1377 return self.ctx.component_builddir('compilers', self.name, component)
1379 def build(self):
1380 """Generate commands to build this compiler."""
1381 self.ctx.remove_recreate_dirs(self.installdir, self.builddir,
1382 self.logsdir)
1383 cmdlist = CommandList('compilers-%s' % self.name, self.ctx.keep)
1384 cmdlist.add_command('check-host-libraries',
1385 ['test', '-f',
1386 os.path.join(self.ctx.host_libraries_installdir,
1387 'ok')])
1388 cmdlist.use_path(self.bindir)
1389 self.build_cross_tool(cmdlist, 'binutils', 'binutils',
1390 ['--disable-gdb',
1391 '--disable-gdbserver',
1392 '--disable-libdecnumber',
1393 '--disable-readline',
1394 '--disable-sim'] + self.binutils_cfg)
1395 if self.os.startswith('linux'):
1396 install_linux_headers(LinuxHeadersPolicyForBuild(self), cmdlist)
1397 self.build_gcc(cmdlist, True)
1398 if self.os == 'gnu':
1399 self.install_gnumach_headers(cmdlist)
1400 self.build_cross_tool(cmdlist, 'mig', 'mig')
1401 self.install_hurd_headers(cmdlist)
1402 for g in self.compiler_glibcs:
1403 cmdlist.push_subdesc('glibc')
1404 cmdlist.push_subdesc(g.name)
1405 g.build_glibc(cmdlist, GlibcPolicyForCompiler(g))
1406 cmdlist.pop_subdesc()
1407 cmdlist.pop_subdesc()
1408 self.build_gcc(cmdlist, False)
1409 cmdlist.add_command('done', ['touch',
1410 os.path.join(self.installdir, 'ok')])
1411 self.ctx.add_makefile_cmdlist('compilers-%s' % self.name, cmdlist,
1412 self.logsdir)
1414 def build_cross_tool(self, cmdlist, tool_src, tool_build, extra_opts=None):
1415 """Build one cross tool."""
1416 srcdir = self.ctx.component_srcdir(tool_src)
1417 builddir = self.component_builddir(tool_build)
1418 cmdlist.push_subdesc(tool_build)
1419 cmdlist.create_use_dir(builddir)
1420 cfg_cmd = [os.path.join(srcdir, 'configure'),
1421 '--prefix=%s' % self.installdir,
1422 '--build=%s' % self.ctx.build_triplet,
1423 '--host=%s' % self.ctx.build_triplet,
1424 '--target=%s' % self.triplet,
1425 '--with-sysroot=%s' % self.sysroot]
1426 if extra_opts:
1427 cfg_cmd.extend(extra_opts)
1428 cmdlist.add_command('configure', cfg_cmd)
1429 cmdlist.add_command('build', ['make'])
1430 # Parallel "make install" for GCC has race conditions that can
1431 # cause it to fail; see
1432 # <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42980>. Such
1433 # problems are not known for binutils, but doing the
1434 # installation in parallel within a particular toolchain build
1435 # (as opposed to installation of one toolchain from
1436 # build-many-glibcs.py running in parallel to the installation
1437 # of other toolchains being built) is not known to be
1438 # significantly beneficial, so it is simplest just to disable
1439 # parallel install for cross tools here.
1440 cmdlist.add_command('install', ['make', '-j1', 'install'])
1441 cmdlist.cleanup_dir()
1442 cmdlist.pop_subdesc()
1444 def install_gnumach_headers(self, cmdlist):
1445 """Install GNU Mach headers."""
1446 srcdir = self.ctx.component_srcdir('gnumach')
1447 builddir = self.component_builddir('gnumach')
1448 cmdlist.push_subdesc('gnumach')
1449 cmdlist.create_use_dir(builddir)
1450 cmdlist.add_command('configure',
1451 [os.path.join(srcdir, 'configure'),
1452 '--build=%s' % self.ctx.build_triplet,
1453 '--host=%s' % self.triplet,
1454 '--prefix=',
1455 '--disable-user32',
1456 'CC=%s-gcc -nostdlib' % self.triplet])
1457 cmdlist.add_command('install', ['make', 'DESTDIR=%s' % self.sysroot,
1458 'install-data'])
1459 cmdlist.cleanup_dir()
1460 cmdlist.pop_subdesc()
1462 def install_hurd_headers(self, cmdlist):
1463 """Install Hurd headers."""
1464 srcdir = self.ctx.component_srcdir('hurd')
1465 builddir = self.component_builddir('hurd')
1466 cmdlist.push_subdesc('hurd')
1467 cmdlist.create_use_dir(builddir)
1468 cmdlist.add_command('configure',
1469 [os.path.join(srcdir, 'configure'),
1470 '--build=%s' % self.ctx.build_triplet,
1471 '--host=%s' % self.triplet,
1472 '--prefix=',
1473 '--disable-profile', '--without-parted',
1474 'CC=%s-gcc -nostdlib' % self.triplet])
1475 cmdlist.add_command('install', ['make', 'prefix=%s' % self.sysroot,
1476 'no_deps=t', 'install-headers'])
1477 cmdlist.cleanup_dir()
1478 cmdlist.pop_subdesc()
1480 def build_gcc(self, cmdlist, bootstrap):
1481 """Build GCC."""
1482 # libssp is of little relevance with glibc's own stack
1483 # checking support. libcilkrts does not support GNU/Hurd (and
1484 # has been removed in GCC 8, so --disable-libcilkrts can be
1485 # removed once glibc no longer supports building with older
1486 # GCC versions). --enable-initfini-array is enabled by default
1487 # in GCC 12, which can be removed when GCC 12 becomes the
1488 # minimum requirement.
1489 cfg_opts = list(self.gcc_cfg)
1490 cfg_opts += ['--enable-initfini-array']
1491 cfg_opts += ['--disable-libssp', '--disable-libcilkrts']
1492 host_libs = self.ctx.host_libraries_installdir
1493 cfg_opts += ['--with-gmp=%s' % host_libs,
1494 '--with-mpfr=%s' % host_libs,
1495 '--with-mpc=%s' % host_libs]
1496 if bootstrap:
1497 tool_build = 'gcc-first'
1498 # Building a static-only, C-only compiler that is
1499 # sufficient to build glibc. Various libraries and
1500 # features that may require libc headers must be disabled.
1501 # When configuring with a sysroot, --with-newlib is
1502 # required to define inhibit_libc (to stop some parts of
1503 # libgcc including libc headers); --without-headers is not
1504 # sufficient.
1505 cfg_opts += ['--enable-languages=c', '--disable-shared',
1506 '--disable-threads',
1507 '--disable-libatomic',
1508 '--disable-decimal-float',
1509 '--disable-gcov',
1510 '--disable-libffi',
1511 '--disable-libgomp',
1512 '--disable-libitm',
1513 '--disable-libmpx',
1514 '--disable-libquadmath',
1515 '--disable-libsanitizer',
1516 '--without-headers', '--with-newlib',
1517 '--with-glibc-version=%s' % self.ctx.glibc_version
1519 cfg_opts += self.first_gcc_cfg
1520 else:
1521 tool_build = 'gcc'
1522 # libsanitizer commonly breaks because of glibc header
1523 # changes, or on unusual targets. C++ pre-compiled
1524 # headers are not used during the glibc build and are
1525 # expensive to create.
1526 if not self.ctx.full_gcc:
1527 cfg_opts += ['--disable-libsanitizer',
1528 '--disable-libstdcxx-pch']
1529 langs = 'all' if self.ctx.full_gcc else 'c,c++'
1530 cfg_opts += ['--enable-languages=%s' % langs,
1531 '--enable-shared', '--enable-threads']
1532 self.build_cross_tool(cmdlist, 'gcc', tool_build, cfg_opts)
1534 class GlibcPolicyDefault(object):
1535 """Build policy for glibc: common defaults."""
1537 def __init__(self, glibc):
1538 self.srcdir = glibc.ctx.component_srcdir('glibc')
1539 self.use_usr = glibc.os != 'gnu'
1540 self.prefix = '/usr' if self.use_usr else ''
1541 self.configure_args = [
1542 '--prefix=%s' % self.prefix,
1543 '--enable-profile',
1544 '--build=%s' % glibc.ctx.build_triplet,
1545 '--host=%s' % glibc.triplet,
1546 'CC=%s' % glibc.tool_name('gcc'),
1547 'CXX=%s' % glibc.tool_name('g++'),
1549 if glibc.os == 'gnu':
1550 self.configure_args.append('MIG=%s' % glibc.tool_name('mig'))
1551 if glibc.cflags:
1552 self.configure_args.append('CFLAGS=%s' % glibc.cflags)
1553 self.configure_args.append('CXXFLAGS=%s' % glibc.cflags)
1554 self.configure_args += glibc.cfg
1556 def configure(self, cmdlist):
1557 """Invoked to add the configure command to the command list."""
1558 cmdlist.add_command('configure',
1559 [os.path.join(self.srcdir, 'configure'),
1560 *self.configure_args])
1562 def extra_commands(self, cmdlist):
1563 """Invoked to inject additional commands (make check) after build."""
1564 pass
1566 class GlibcPolicyForCompiler(GlibcPolicyDefault):
1567 """Build policy for glibc during the compilers stage."""
1569 def __init__(self, glibc):
1570 super().__init__(glibc)
1571 self.builddir = glibc.ctx.component_builddir(
1572 'compilers', glibc.compiler.name, 'glibc', glibc.name)
1573 self.installdir = glibc.compiler.sysroot
1575 class GlibcPolicyForBuild(GlibcPolicyDefault):
1576 """Build policy for glibc during the glibcs stage."""
1578 def __init__(self, glibc):
1579 super().__init__(glibc)
1580 self.builddir = glibc.ctx.component_builddir(
1581 'glibcs', glibc.name, 'glibc')
1582 self.installdir = glibc.ctx.glibc_installdir(glibc.name)
1583 if glibc.ctx.strip:
1584 self.strip = glibc.tool_name('strip')
1585 else:
1586 self.strip = None
1587 self.save_logs = glibc.ctx.save_logs
1589 def extra_commands(self, cmdlist):
1590 if self.strip:
1591 # Avoid stripping libc.so and libpthread.so, which are
1592 # linker scripts stored in /lib on Hurd.
1593 find_command = 'find %s/lib* -name "*.so*"' % self.installdir
1594 cmdlist.add_command('strip', ['sh', '-c', (
1595 'set -e; for f in $(%s); do '
1596 'if ! head -c16 $f | grep -q "GNU ld script"; then %s $f; fi; '
1597 'done' % (find_command, self.strip))])
1598 cmdlist.add_command('check', ['make', 'check'])
1599 cmdlist.add_command('save-logs', [self.save_logs], always_run=True)
1601 class GlibcPolicyForUpdateSyscalls(GlibcPolicyDefault):
1602 """Build policy for glibc during update-syscalls."""
1604 def __init__(self, glibc):
1605 super().__init__(glibc)
1606 self.builddir = glibc.ctx.component_builddir(
1607 'update-syscalls', glibc.name, 'glibc')
1608 self.linuxdir = glibc.ctx.component_builddir(
1609 'update-syscalls', glibc.name, 'linux')
1610 self.linux_policy = LinuxHeadersPolicyForUpdateSyscalls(
1611 glibc, self.linuxdir)
1612 self.configure_args.insert(
1613 0, '--with-headers=%s' % os.path.join(self.linuxdir, 'include'))
1614 # self.installdir not set because installation is not supported
1616 class Glibc(object):
1617 """A configuration for building glibc."""
1619 def __init__(self, compiler, arch=None, os_name=None, variant=None,
1620 cfg=None, ccopts=None, cflags=None):
1621 """Initialize a Glibc object."""
1622 self.ctx = compiler.ctx
1623 self.compiler = compiler
1624 if arch is None:
1625 self.arch = compiler.arch
1626 else:
1627 self.arch = arch
1628 if os_name is None:
1629 self.os = compiler.os
1630 else:
1631 self.os = os_name
1632 self.variant = variant
1633 if variant is None:
1634 self.name = '%s-%s' % (self.arch, self.os)
1635 else:
1636 self.name = '%s-%s-%s' % (self.arch, self.os, variant)
1637 self.triplet = '%s-glibc-%s' % (self.arch, self.os)
1638 if cfg is None:
1639 self.cfg = []
1640 else:
1641 self.cfg = cfg
1642 # ccopts contain ABI options and are passed to configure as CC / CXX.
1643 self.ccopts = ccopts
1644 # cflags contain non-ABI options like -g or -O and are passed to
1645 # configure as CFLAGS / CXXFLAGS.
1646 self.cflags = cflags
1648 def tool_name(self, tool):
1649 """Return the name of a cross-compilation tool."""
1650 ctool = '%s-%s' % (self.compiler.triplet, tool)
1651 if self.ccopts and (tool == 'gcc' or tool == 'g++'):
1652 ctool = '%s %s' % (ctool, self.ccopts)
1653 return ctool
1655 def build(self):
1656 """Generate commands to build this glibc."""
1657 builddir = self.ctx.component_builddir('glibcs', self.name, 'glibc')
1658 installdir = self.ctx.glibc_installdir(self.name)
1659 logsdir = os.path.join(self.ctx.logsdir, 'glibcs', self.name)
1660 self.ctx.remove_recreate_dirs(installdir, builddir, logsdir)
1661 cmdlist = CommandList('glibcs-%s' % self.name, self.ctx.keep)
1662 cmdlist.add_command('check-compilers',
1663 ['test', '-f',
1664 os.path.join(self.compiler.installdir, 'ok')])
1665 cmdlist.use_path(self.compiler.bindir)
1666 self.build_glibc(cmdlist, GlibcPolicyForBuild(self))
1667 self.ctx.add_makefile_cmdlist('glibcs-%s' % self.name, cmdlist,
1668 logsdir)
1670 def build_glibc(self, cmdlist, policy):
1671 """Generate commands to build this glibc, either as part of a compiler
1672 build or with the bootstrapped compiler (and in the latter case, run
1673 tests as well)."""
1674 cmdlist.create_use_dir(policy.builddir)
1675 policy.configure(cmdlist)
1676 cmdlist.add_command('build', ['make'])
1677 cmdlist.add_command('install', ['make', 'install',
1678 'install_root=%s' % policy.installdir])
1679 # GCC uses paths such as lib/../lib64, so make sure lib
1680 # directories always exist.
1681 mkdir_cmd = ['mkdir', '-p',
1682 os.path.join(policy.installdir, 'lib')]
1683 if policy.use_usr:
1684 mkdir_cmd += [os.path.join(policy.installdir, 'usr', 'lib')]
1685 cmdlist.add_command('mkdir-lib', mkdir_cmd)
1686 policy.extra_commands(cmdlist)
1687 cmdlist.cleanup_dir()
1689 def update_syscalls(self):
1690 if self.os == 'gnu':
1691 # Hurd does not have system call tables that need updating.
1692 return
1694 policy = GlibcPolicyForUpdateSyscalls(self)
1695 logsdir = os.path.join(self.ctx.logsdir, 'update-syscalls', self.name)
1696 self.ctx.remove_recreate_dirs(policy.builddir, logsdir)
1697 cmdlist = CommandList('update-syscalls-%s' % self.name, self.ctx.keep)
1698 cmdlist.add_command('check-compilers',
1699 ['test', '-f',
1700 os.path.join(self.compiler.installdir, 'ok')])
1701 cmdlist.use_path(self.compiler.bindir)
1703 install_linux_headers(policy.linux_policy, cmdlist)
1705 cmdlist.create_use_dir(policy.builddir)
1706 policy.configure(cmdlist)
1707 cmdlist.add_command('build', ['make', 'update-syscall-lists'])
1708 cmdlist.cleanup_dir()
1709 self.ctx.add_makefile_cmdlist('update-syscalls-%s' % self.name,
1710 cmdlist, logsdir)
1712 class Command(object):
1713 """A command run in the build process."""
1715 def __init__(self, desc, num, dir, path, command, always_run=False):
1716 """Initialize a Command object."""
1717 self.dir = dir
1718 self.path = path
1719 self.desc = desc
1720 trans = str.maketrans({' ': '-'})
1721 self.logbase = '%03d-%s' % (num, desc.translate(trans))
1722 self.command = command
1723 self.always_run = always_run
1725 @staticmethod
1726 def shell_make_quote_string(s):
1727 """Given a string not containing a newline, quote it for use by the
1728 shell and make."""
1729 assert '\n' not in s
1730 if re.fullmatch('[]+,./0-9@A-Z_a-z-]+', s):
1731 return s
1732 strans = str.maketrans({"'": "'\\''"})
1733 s = "'%s'" % s.translate(strans)
1734 mtrans = str.maketrans({'$': '$$'})
1735 return s.translate(mtrans)
1737 @staticmethod
1738 def shell_make_quote_list(l, translate_make):
1739 """Given a list of strings not containing newlines, quote them for use
1740 by the shell and make, returning a single string. If translate_make
1741 is true and the first string is 'make', change it to $(MAKE)."""
1742 l = [Command.shell_make_quote_string(s) for s in l]
1743 if translate_make and l[0] == 'make':
1744 l[0] = '$(MAKE)'
1745 return ' '.join(l)
1747 def shell_make_quote(self):
1748 """Return this command quoted for the shell and make."""
1749 return self.shell_make_quote_list(self.command, True)
1752 class CommandList(object):
1753 """A list of commands run in the build process."""
1755 def __init__(self, desc, keep):
1756 """Initialize a CommandList object."""
1757 self.cmdlist = []
1758 self.dir = None
1759 self.path = None
1760 self.desc = [desc]
1761 self.keep = keep
1763 def desc_txt(self, desc):
1764 """Return the description to use for a command."""
1765 return '%s %s' % (' '.join(self.desc), desc)
1767 def use_dir(self, dir):
1768 """Set the default directory for subsequent commands."""
1769 self.dir = dir
1771 def use_path(self, path):
1772 """Set a directory to be prepended to the PATH for subsequent
1773 commands."""
1774 self.path = path
1776 def push_subdesc(self, subdesc):
1777 """Set the default subdescription for subsequent commands (e.g., the
1778 name of a component being built, within the series of commands
1779 building it)."""
1780 self.desc.append(subdesc)
1782 def pop_subdesc(self):
1783 """Pop a subdescription from the list of descriptions."""
1784 self.desc.pop()
1786 def create_use_dir(self, dir):
1787 """Remove and recreate a directory and use it for subsequent
1788 commands."""
1789 self.add_command_dir('rm', None, ['rm', '-rf', dir])
1790 self.add_command_dir('mkdir', None, ['mkdir', '-p', dir])
1791 self.use_dir(dir)
1793 def add_command_dir(self, desc, dir, command, always_run=False):
1794 """Add a command to run in a given directory."""
1795 cmd = Command(self.desc_txt(desc), len(self.cmdlist), dir, self.path,
1796 command, always_run)
1797 self.cmdlist.append(cmd)
1799 def add_command(self, desc, command, always_run=False):
1800 """Add a command to run in the default directory."""
1801 cmd = Command(self.desc_txt(desc), len(self.cmdlist), self.dir,
1802 self.path, command, always_run)
1803 self.cmdlist.append(cmd)
1805 def cleanup_dir(self, desc='cleanup', dir=None):
1806 """Clean up a build directory. If no directory is specified, the
1807 default directory is cleaned up and ceases to be the default
1808 directory."""
1809 if dir is None:
1810 dir = self.dir
1811 self.use_dir(None)
1812 if self.keep != 'all':
1813 self.add_command_dir(desc, None, ['rm', '-rf', dir],
1814 always_run=(self.keep == 'none'))
1816 def makefile_commands(self, wrapper, logsdir):
1817 """Return the sequence of commands in the form of text for a Makefile.
1818 The given wrapper script takes arguments: base of logs for
1819 previous command, or empty; base of logs for this command;
1820 description; directory; PATH addition; the command itself."""
1821 # prev_base is the base of the name for logs of the previous
1822 # command that is not always-run (that is, a build command,
1823 # whose failure should stop subsequent build commands from
1824 # being run, as opposed to a cleanup command, which is run
1825 # even if previous commands failed).
1826 prev_base = ''
1827 cmds = []
1828 for c in self.cmdlist:
1829 ctxt = c.shell_make_quote()
1830 if prev_base and not c.always_run:
1831 prev_log = os.path.join(logsdir, prev_base)
1832 else:
1833 prev_log = ''
1834 this_log = os.path.join(logsdir, c.logbase)
1835 if not c.always_run:
1836 prev_base = c.logbase
1837 if c.dir is None:
1838 dir = ''
1839 else:
1840 dir = c.dir
1841 if c.path is None:
1842 path = ''
1843 else:
1844 path = c.path
1845 prelims = [wrapper, prev_log, this_log, c.desc, dir, path]
1846 prelim_txt = Command.shell_make_quote_list(prelims, False)
1847 cmds.append('\t@%s %s' % (prelim_txt, ctxt))
1848 return '\n'.join(cmds)
1850 def status_logs(self, logsdir):
1851 """Return the list of log files with command status."""
1852 return [os.path.join(logsdir, '%s-status.txt' % c.logbase)
1853 for c in self.cmdlist]
1856 def get_parser():
1857 """Return an argument parser for this module."""
1858 parser = argparse.ArgumentParser(description=__doc__)
1859 parser.add_argument('-j', dest='parallelism',
1860 help='Run this number of jobs in parallel',
1861 type=int, default=os.cpu_count())
1862 parser.add_argument('--keep', dest='keep',
1863 help='Whether to keep all build directories, '
1864 'none or only those from failed builds',
1865 default='none', choices=('none', 'all', 'failed'))
1866 parser.add_argument('--replace-sources', action='store_true',
1867 help='Remove and replace source directories '
1868 'with the wrong version of a component')
1869 parser.add_argument('--strip', action='store_true',
1870 help='Strip installed glibc libraries')
1871 parser.add_argument('--full-gcc', action='store_true',
1872 help='Build GCC with all languages and libsanitizer')
1873 parser.add_argument('--shallow', action='store_true',
1874 help='Do not download Git history during checkout')
1875 parser.add_argument('topdir',
1876 help='Toplevel working directory')
1877 parser.add_argument('action',
1878 help='What to do',
1879 choices=('checkout', 'bot-cycle', 'bot',
1880 'host-libraries', 'compilers', 'glibcs',
1881 'update-syscalls', 'list-compilers',
1882 'list-glibcs'))
1883 parser.add_argument('configs',
1884 help='Versions to check out or configurations to build',
1885 nargs='*')
1886 return parser
1889 def get_version_common(progname,line,word,delchars,arg1):
1890 try:
1891 out = subprocess.run([progname, arg1],
1892 stdout=subprocess.PIPE,
1893 stderr=subprocess.DEVNULL,
1894 stdin=subprocess.DEVNULL,
1895 check=True, universal_newlines=True)
1896 v = out.stdout.splitlines()[line].split()[word]
1897 if delchars:
1898 v = v.replace(delchars,'')
1899 return [int(x) for x in v.split('.')]
1900 except:
1901 return 'missing';
1903 def get_version_common_stderr(progname,line,word,delchars,arg1):
1904 try:
1905 out = subprocess.run([progname, arg1],
1906 stdout=subprocess.DEVNULL,
1907 stderr=subprocess.PIPE,
1908 stdin=subprocess.DEVNULL,
1909 check=True, universal_newlines=True)
1910 v = out.stderr.splitlines()[line].split()[word]
1911 if delchars:
1912 v = v.replace(delchars,'')
1913 return [int(x) for x in v.split('.')]
1914 except:
1915 return 'missing';
1917 def get_version(progname):
1918 return get_version_common (progname, 0, -1, None, '--version');
1920 def get_version_awk(progname):
1921 return get_version_common (progname, 0, 2, ',', '--version');
1923 def get_version_bzip2(progname):
1924 return get_version_common_stderr (progname, 0, 6, ',', '-h');
1926 def check_version(ver, req):
1927 for v, r in zip(ver, req):
1928 if v > r:
1929 return True
1930 if v < r:
1931 return False
1932 return True
1934 def version_str(ver):
1935 return '.'.join([str (x) for x in ver])
1937 def check_for_required_tools():
1938 get_list_of_required_tools()
1939 count_old_tools = 0
1940 count_missing_tools = 0
1942 for k, v in REQUIRED_TOOLS.items():
1943 version = v[0](k)
1944 if version == 'missing':
1945 ok = 'missing'
1946 else:
1947 ok = 'ok' if check_version (version, v[1]) else 'old'
1948 if ok == 'old':
1949 if count_old_tools == 0:
1950 print("One or more required tools are too old:")
1951 count_old_tools = count_old_tools + 1
1952 print('{:9}: {:3} (obtained=\"{}\" required=\"{}\")'.format(k, ok,
1953 version_str(version), version_str(v[1])))
1954 if ok == 'missing':
1955 if count_missing_tools == 0:
1956 print("One or more required tools are missing:")
1957 count_missing_tools = count_missing_tools + 1
1958 print('{:9}: {:3} (required=\"{}\")'.format(k, ok,
1959 version_str(v[1])))
1961 if count_old_tools > 0 or count_missing_tools > 0:
1962 exit (1);
1964 def main(argv):
1965 """The main entry point."""
1966 check_for_required_tools();
1967 parser = get_parser()
1968 opts = parser.parse_args(argv)
1969 topdir = os.path.abspath(opts.topdir)
1970 ctx = Context(topdir, opts.parallelism, opts.keep, opts.replace_sources,
1971 opts.strip, opts.full_gcc, opts.action,
1972 shallow=opts.shallow)
1973 ctx.run_builds(opts.action, opts.configs)
1976 if __name__ == '__main__':
1977 main(sys.argv[1:])