Debian Codegraph @

mesonbuild/scripts/reprotest.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2024 The Meson development team
3
4 from __future__ import annotations
5
6 import sys, os, subprocess, shutil
7 import pathlib
8 import typing as T
9
10 if T.TYPE_CHECKING:
11 import argparse
12
13 from ..mesonlib import get_meson_command
14
15 # Note: when adding arguments, please also add them to the completion
16 # scripts in $MESONSRC/data/shell-completions/
17 def add_arguments(parser: 'argparse.ArgumentParser') -> None:
18 parser.add_argument('--intermediaries',
19 default=False,
20 action='store_true',
21 help='Check intermediate files.')
22 parser.add_argument('mesonargs', nargs='*',
23 help='Arguments to pass to "meson setup".')
24
25 IGNORE_PATTERNS = ('.ninja_log',
26 '.ninja_deps',
27 'meson-private',
28 'meson-logs',
29 'meson-info',
30 )
31
32 INTERMEDIATE_EXTENSIONS = ('.gch',
33 '.pch',
34 '.o',
35 '.obj',
36 '.class',
37 )
38
39 class ReproTester:
40 def __init__(self, options: T.Any):
41 self.args = options.mesonargs
42 self.meson = get_meson_command()[:]
43 self.builddir = pathlib.Path('buildrepro')
44 self.storagedir = pathlib.Path('buildrepro.1st')
45 self.issues: T.List[str] = []
46 self.check_intermediaries = options.intermediaries
47
48 def run(self) -> int:
49 if not os.path.isfile('meson.build'):
50 sys.exit('This command needs to be run at your project source root.')
51 self.disable_ccache()
52 self.cleanup()
53 self.build()
54 self.check_output()
55 self.print_results()
56 if not self.issues:
57 self.cleanup()
58 return len(self.issues)
59
60 def disable_ccache(self) -> None:
61 os.environ['CCACHE_DISABLE'] = '1'
62
63 def cleanup(self) -> None:
64 if self.builddir.exists():
65 shutil.rmtree(self.builddir)
66 if self.storagedir.exists():
67 shutil.rmtree(self.storagedir)
68
69 def build(self) -> None:
70 setup_command: T.Sequence[str] = self.meson + ['setup', str(self.builddir)] + self.args
71 build_command: T.Sequence[str] = self.meson + ['compile', '-C', str(self.builddir)]
72 subprocess.check_call(setup_command)
73 subprocess.check_call(build_command)
74 self.builddir.rename(self.storagedir)
75 subprocess.check_call(setup_command)
76 subprocess.check_call(build_command)
77
78 def ignore_file(self, fstr: str) -> bool:
79 for p in IGNORE_PATTERNS:
80 if p in fstr:
81 return True
82 if not self.check_intermediaries:
83 if fstr.endswith(INTERMEDIATE_EXTENSIONS):
84 return True
85 return False
86
87 def check_contents(self, fromdir: str, todir: str, check_contents: bool) -> None:
88 import filecmp
89 frompath = fromdir + '/'
90 topath = todir + '/'
91 for fromfile in pathlib.Path(fromdir).glob('**/*'):
92 if not fromfile.is_file():
93 continue
94 fstr = fromfile.as_posix()
95 if self.ignore_file(fstr):
96 continue
97 assert fstr.startswith(frompath)
98 tofile = pathlib.Path(fstr.replace(frompath, topath, 1))
99 if not tofile.exists():
100 self.issues.append(f'Missing file: {tofile}')
101 elif check_contents:
102 if not filecmp.cmp(fromfile, tofile, shallow=False):
103 self.issues.append(f'File contents differ: {fromfile}')
104
105 def print_results(self) -> None:
106 if self.issues:
107 print('Build differences detected')
108 for i in self.issues:
109 print(i)
110 else:
111 print('No differences detected.')
112
113 def check_output(self) -> None:
114 self.check_contents('buildrepro', 'buildrepro.1st', True)
115 self.check_contents('buildrepro.1st', 'buildrepro', False)
116
117 def run(options: argparse.Namespace) -> int:
118 rt = ReproTester(options)
119 try:
120 return rt.run()
121 except Exception as e:
122 print(e)
123 return 1