forked from AdaCore/libadalang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
executable file
·364 lines (308 loc) · 12.5 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#! /usr/bin/env python
import os.path
import subprocess
import sys
# For developer convenience, add the "langkit" directory next to this script to
# the Python path so that.
sys.path.append(
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'langkit')
)
import ada.copyright
from langkit.diagnostics import check_source_language
from langkit.libmanage import ManageScript
from langkit.utils import Colors, printcol
class Manage(ManageScript):
ENABLE_BUILD_WARNINGS_DEFAULT = True
PERF_PARSE = 'parse'
PERF_PARSE_AND_TRAVERSE = 'parse-and-traverse'
PERF_CHOICES = (PERF_PARSE, PERF_PARSE_AND_TRAVERSE)
def add_extra_subcommands(self) -> None:
########
# Test #
########
self.test_parser = self.add_subcommand(
self.do_test, accept_unknown_args=True
)
self.test_parser.add_argument(
'--disable-ocaml', action='store_true',
help='Disable tests involving the OCaml API'
)
self.test_parser.add_argument(
'testsuite-args', nargs='*',
help='Arguments to pass to testsuite.py.'
)
self.add_build_mode_arg(self.test_parser)
#############
# Perf Test #
#############
self.perf_test_parser = self.add_subcommand(self.do_perf_test)
self.perf_test_parser.add_argument(
'--work-dir', default='performance_testsuite',
help='Directory into which the performance testsuite will be'
' executed'
)
self.perf_test_parser.add_argument(
'--nb-runs', type=int, default=4,
help='Number of runs (default: 4)'
)
self.perf_test_parser.add_argument(
'--no-recompile', action='store_true',
help='Do not recompile the library before running the perf'
' testsuite'
)
self.perf_test_parser.add_argument(
'--scenario', '-s',
choices=self.PERF_CHOICES, default=self.PERF_PARSE,
help='Profiling scenario to use. Basically: "what to measure?".'
)
self.perf_test_parser.add_argument(
'--with-trivia', action='store_true',
help='Include trivia in parsing'
)
self.add_generate_args(self.perf_test_parser)
self.add_build_args(self.perf_test_parser)
def create_context(self, args):
# Keep these import statements here so that they are executed only
# after the coverage computation actually started.
from langkit.compile_context import ADA_BODY, CompileCtx, LibraryEntity
from ada.lexer import ada_lexer
from ada.grammar import ada_grammar
from ada.documentation import libadalang_docs
ctx = CompileCtx(
lang_name='Ada',
short_name='LAL',
lexer=ada_lexer,
grammar=ada_grammar,
default_charset='iso-8859-1',
verbosity=args.verbosity,
default_unit_provider=LibraryEntity(
'Libadalang.Internal_Default_Provider', 'Create'
),
symbol_canonicalizer=LibraryEntity('Libadalang.Sources',
'Canonicalize'),
documentations=libadalang_docs,
)
# Internals need to access environment hooks and the symbolizer
ctx.add_with_clause('Implementation',
ADA_BODY, 'Libadalang.Env_Hooks',
use_clause=True)
ctx.add_with_clause('Implementation',
ADA_BODY, 'Libadalang.Sources',
use_clause=False)
# Bind Libadalang's custom iterators to the public API
ctx.add_with_clause('Iterators',
ADA_BODY, 'Libadalang.Iterators.Extensions')
# LAL.Analysis.Is_Keyword is implemented using LAL.Lexer's
ctx.add_with_clause('Analysis', ADA_BODY, 'Libadalang.Lexer')
ctx.post_process_ada = ada.copyright.format_ada
ctx.post_process_cpp = ada.copyright.format_c
ctx.post_process_python = ada.copyright.format_python
return ctx
@property
def main_source_dirs(self):
return super(Manage, self).main_source_dirs | {
os.path.join('testsuite', 'ada'),
os.path.join('testsuite', 'ada', 'gnat_compare')
}
@property
def main_programs(self):
return super(Manage, self).main_programs | {'nameres', 'navigate',
'gnat_compare'}
def do_generate(self, args):
# Always generate the unparsing machinery and report unused
# documentation entries.
args.generate_unparser = True
args.report_unused_doc_entries = True
super(Manage, self).do_generate(args)
do_generate.__doc__ = ManageScript.do_generate.__doc__
def do_install(self, args):
from e3.fs import sync_tree
super(Manage, self).do_install(args)
# Prepare an "examples" directory
examples_dir = self.dirs.install_dir(
"share", "examples", self.lib_name.lower(),
)
if not os.path.isdir(examples_dir):
os.makedirs(examples_dir)
# Install several items from "contrib" to this directory
for item in [
"check_deref_null.py",
"check_same_logic.py",
"check_same_operands.py",
"check_same_test.py",
"check_same_then_else.py",
"check_subp_boxes.py",
"check_test_not_null.py",
"check_useless_assign.py",
"detect_copy_paste_sa.py",
"highlight",
]:
item_from = self.dirs.lang_source_dir("contrib", item)
item_to = os.path.join(examples_dir, item)
sync_tree(item_from, item_to, delete=False)
do_install.__doc__ = ManageScript.do_generate.__doc__
def do_test(self, args, unknown_args):
"""
Run the testsuite.
This is a wrapper around testsuite/testsuite.py tuned for interactive
use: it correctly setups the environment according to the build
directory, it enables colored output and it displays test outputs on
error.
"""
self.set_context(args)
argv = [
sys.executable,
self.dirs.lang_source_dir('testsuite', 'testsuite.py'),
'-Edtmp', '--build-mode={}'.format(args.build_mode),
# Arguments to pass to GNATcoverage, just in case coverage is
# requested.
'--gnatcov-instr-dir={}'.format(
os.path.join(args.build_dir, 'obj', 'libadalang', 'instr')
)
]
if not args.disable_ocaml:
argv.append('--with-ocaml-bindings')
argv.append(os.path.join('build', 'ocaml'))
if not args.library_types.relocatable:
argv.append('--disable-shared')
argv.extend(unknown_args)
argv.extend(getattr(args, 'testsuite-args'))
try:
return self.check_call('Testsuite', argv)
except KeyboardInterrupt:
# At this point, the testsuite already made it explicit we stopped
# after a keyboard interrupt, so we just have to exit.
sys.exit(1)
@staticmethod
def _mkdir(path):
"""
Create a new directory at `path` if it does not exist.
:param path: the path to the new directory.
:type path: str
:raise: OSError | IOError
"""
if os.path.isdir(path):
return
if os.path.exists(path):
raise IOError('{}: already exists'.format(path))
os.makedirs(path)
@staticmethod
def _find_ada_sources(work_dir):
"""
Return the list of .adb and .ads files in `work_dir`.
:param work_dir: the directory in which to search for ada sources.
:type work_dir: str
:rtype: set[str]
"""
ada_files = set()
for root, dirs, files in os.walk(work_dir):
for filename in files:
_, ext = os.path.splitext(filename)
if ext in ('.ads', '.adb'):
ada_files.add(os.path.join(root, filename))
return ada_files
def do_perf_test(self, args):
"""
Run the performance regression testsuite.
"""
from time import time
self.set_context(args)
def file_lines(filename):
with open(filename) as f:
return len(list(f))
check_source_language(
not os.path.isabs(args.build_dir),
"--build-dir should be a relative path for perf testsuite"
)
work_dir = os.path.abspath(args.work_dir)
variant_name = args.build_dir
report_file = os.path.join(work_dir,
'report-{}.txt'.format(variant_name))
args.build_dir = os.path.join(work_dir, args.build_dir)
if not args.no_recompile:
# The perf testsuite only needs the "parse" main program
args.disable_mains = self.main_programs - {'parse'}
# Build libadalang in production mode inside of the perf testsuite
# directory.
self.dirs.set_build_dir(args.build_dir)
args.build_mode = 'prod'
self._mkdir(args.build_dir)
self.do_make(args)
# Checkout the code bases that we will use for the perf testsuite
source_dir = os.path.join(work_dir, "source")
try:
os.mkdir(source_dir)
except OSError:
pass
os.chdir(source_dir)
if not os.path.exists('gnat'):
subprocess.check_call([
'svn', 'co',
'svn+ssh://svn.us.adacore.com/Dev/trunk/gnat',
'-r', '314163',
'--ignore-externals'
])
if not os.path.exists('gps'):
subprocess.check_call(['git', 'clone',
'ssh://review.eu.adacore.com:29418/gps'])
os.chdir('gps')
subprocess.check_call(['git', 'checkout',
'00b73897a867514732d48ae1429faf97fb07ad7c'])
os.chdir('..')
# Make a list of every ada file
# Exclude some files that are contained here but that we do not parse
# correctly.
excluded_patterns = ['@', 'a-numeri', 'rad-project']
ada_files = filter(
lambda f: all(map(lambda p: p not in f, excluded_patterns)),
self._find_ada_sources(source_dir)
)
file_list_name = 'ada_file_list'
with open(file_list_name, 'w') as file_list:
for f in ada_files:
file_list.write(f + '\n')
# Get a count of the total number of ada source lines
lines_count = sum(map(file_lines, ada_files))
with open(report_file, 'w') as f:
def write_report(text, color=None):
if color:
printcol(text, color)
else:
print(text)
print(text, file=f)
write_report('=================================', Colors.HEADER)
write_report('= Performance testsuite results =', Colors.HEADER)
write_report('=================================', Colors.HEADER)
write_report('')
write_report('Name: {}'.format(variant_name))
write_report('Scenario: {}'.format(args.scenario))
write_report('')
elapsed_list = []
parse_args = ['{}/bin/parse'.format(args.build_dir), '-s', '-F',
file_list_name]
if args.scenario == self.PERF_PARSE_AND_TRAVERSE:
parse_args.append('-C')
if args.with_trivia:
parse_args.append('-P')
for _ in range(args.nb_runs):
# Execute parse on the file list and get the elapsed time
t = time()
subprocess.check_call(parse_args)
elapsed = time() - t
elapsed_list.append(elapsed)
# Print a very basic report
write_report(
'Parsed {0} lines of Ada code in {1:.2f} seconds'.format(
lines_count, elapsed
)
)
write_report('')
write_report('= Performance summary =', Colors.OKGREEN)
write_report(
'Mean time to parse {0} lines of code:'
' {1:.2f} seconds'.format(
lines_count, sum(elapsed_list) / float(len(elapsed_list))
)
)
if __name__ == '__main__':
Manage().run()