/
Autotools.py
603 lines (564 loc) · 24.7 KB
/
Autotools.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/usr/bin/env python3
#
# Copyright (c) 2015-2019 Intel, Inc. All rights reserved.
# $COPYRIGHT$
#
# Additional copyrights may follow
#
# $HEADER$
#
import os
import re
import string
import sys
import shlex
import shutil
from BuildMTTTool import *
## @addtogroup Tools
# @{
# @addtogroup Build
# @section Autotools
# Run typical autotools commands to configure and build a software package
# @param middleware Middleware stage that these tests are to be built against
# @param parent Section that precedes this one in the dependency tree
# @param autogen_cmd Command to be executed to setup the configure script, usually called autogen.sh or autogen.pl
# @param configure_options Options to be passed to configure. Note that the prefix will be automatically set and need not be provided here
# @param make_options Options to be passed to the make command
# @param build_in_place Build tests in current location (no prefix or install)
# @param merge_stdout_stderr Merge stdout and stderr into one output stream
# @param stdout_save_lines Number of lines of stdout to save
# @param stderr_save_lines Number of lines of stderr to save
# @param modules_unload Modules to unload
# @param modules Modules to load
# @param modules_swap Modules to swap
# @param dependencies List of dependencies specified as the build stage name
# @param make_envars Environmental variables to set prior to executing make
# @param subdir Subdirectory that is to be built
# @}
class Autotools(BuildMTTTool):
def __init__(self):
BuildMTTTool.__init__(self)
self.activated = False
self.options = {}
self.options['middleware'] = (None, "Middleware stage that these tests are to be built against")
self.options['parent'] = (None, "Section that precedes this one in the dependency tree")
self.options['autogen_cmd'] = (None, "Command to be executed to setup the configure script, usually called autogen.sh or autogen.pl")
self.options['configure_options'] = (None, "Options to be passed to configure. Note that the prefix will be automatically set and need not be provided here")
self.options['make_options'] = (None, "Options to be passed to the make command")
self.options['build_in_place'] = (False, "Build tests in current location (no prefix or install)")
self.options['merge_stdout_stderr'] = (False, "Merge stdout and stderr into one output stream")
self.options['stdout_save_lines'] = (-1, "Number of lines of stdout to save")
self.options['stderr_save_lines'] = (-1, "Number of lines of stderr to save")
self.options['modules_unload'] = (None, "Modules to unload")
self.options['modules'] = (None, "Modules to load")
self.options['modules_swap'] = (None, "Modules to swap")
self.options['dependencies'] = (None, "List of dependencies specified as the build stage name - e.g., MiddlwareBuild_package to be added to configure using --with-package=location")
self.options['make_envars'] = (None, "Environmental variables to set prior to executing make")
self.options['subdir'] = (None, "Subdirectory that is to be built")
self.exclude = set(string.punctuation)
return
def activate(self):
if not self.activated:
# use the automatic procedure from IPlugin
IPlugin.activate(self)
self.activated = True
return
def deactivate(self):
if self.activated:
IPlugin.deactivate(self)
self.activated = False
return
def print_name(self):
return "Autotools"
def print_options(self, testDef, prefix):
lines = testDef.printOptions(self.options)
for line in lines:
print(prefix + line)
return
def execute(self, log, keyvals, testDef):
testDef.logger.verbose_print("Autotools Execute")
# parse any provided options - these will override the defaults
cmds = {}
testDef.parseOptions(log, self.options, keyvals, cmds)
# get the location of the software we are to build
try:
if cmds['parent'] is not None:
# we have to retrieve the log entry from
# the parent section so we can get the
# location of the package. The logger
# can provide it for us
parentlog = testDef.logger.getLog(cmds['parent'])
if parentlog is None:
log['status'] = 1
log['stderr'] = "Parent",cmds['parent'],"log not found"
return
# record the parent in our log
log['parent'] = cmds['parent']
else:
log['status'] = 1
log['stderr'] = "Parent log not recorded"
return
except KeyError:
log['status'] = 1
log['stderr'] = "Parent not specified"
return
try:
location = parentlog['location']
except KeyError:
log['status'] = 1
log['stderr'] = "Location of package to build was not specified in parent stage"
return
# see if we need to adjust the location to build what is in
# a specific subdirectory of this location
if cmds['subdir'] is not None:
location = os.path.join(location, cmds['subdir'])
inPlace = False
# check if we need to point to middleware
# do this before we load environment modules so we can append to the list if needed
midpath = False
try:
savebinpath = os.environ['PATH']
except KeyError:
savebinpath = None
try:
savelibpath = os.environ['LIBRARY_PATH']
except KeyError:
savelibpath = None
try:
savecpath = os.environ['CPATH']
except KeyError:
savecpath = None
try:
saveldlibpath = os.environ['LD_LIBRARY_PATH']
except KeyError:
saveldlibpath = None
try:
if cmds['middleware'] is not None:
# pass it down
log['middleware'] = cmds['middleware']
# there may be more than one middleware noted here
# so break it apart just in case
# might be comma-delimited or space delimited
mware = re.split("\s|,", cmds['middleware'])
for m in mware:
# get the log entry of its location
midlog = testDef.logger.getLog(m)
if midlog is None:
continue
# get the location of the middleware
try:
if midlog['location'] is not None:
# prepend that location to our paths
try:
oldbinpath = os.environ['PATH']
pieces = oldbinpath.split(':')
except KeyError:
pieces = []
bindir = os.path.join(midlog['location'], "bin")
pieces.insert(0, bindir)
newpath = ":".join(pieces)
os.environ['PATH'] = newpath
# prepend the loadable lib path
try:
oldldlibpath = os.environ['LD_LIBRARY_PATH']
pieces = oldldlibpath.split(':')
except KeyError:
pieces = []
bindir = os.path.join(midlog['location'], "lib")
pieces.insert(0, bindir)
newpath = ":".join(pieces)
os.environ['LD_LIBRARY_PATH'] = newpath
# prepend the include path
try:
oldcpath = os.environ['CPATH']
pieces = oldcpath.split(':')
except KeyError:
pieces = []
bindir = os.path.join(midlog['location'], "include")
pieces.insert(0, bindir)
newpath = ":".join(pieces)
os.environ['CPATH'] = newpath
# prepend the lib path
try:
oldlibpath = os.environ['LIBRARY_PATH']
pieces = oldlibpath.split(':')
except KeyError:
pieces = []
bindir = os.path.join(midlog['location'], "lib")
pieces.insert(0, bindir)
newpath = ":".join(pieces)
os.environ['LIBRARY_PATH'] = newpath
# mark that this was done
midpath = True
except KeyError:
pass
# check for modules required by the middleware
status,stdout,stderr = testDef.modcmd.checkForModules(log['section'], midlog, cmds, testDef)
if 0 != status:
log['status'] = status
log['stdout'] = stdout
log['stderr'] = stderr
return
except KeyError:
pass
# Apply any requested environment module settings
status,stdout,stderr = testDef.modcmd.applyModules(log['section'], cmds, testDef)
if 0 != status:
log['status'] = status
log['stdout'] = stdout
log['stderr'] = stderr
return
# sense and record the compiler being used
plugin = None
availUtil = list(testDef.loader.utilities.keys())
for util in availUtil:
for pluginInfo in testDef.utilities.getPluginsOfCategory(util):
if "Compilers" == pluginInfo.plugin_object.print_name():
plugin = pluginInfo.plugin_object
break
if plugin is None:
log['compiler'] = {'status' : 1, 'family' : "unknown", 'version' : "unknown"}
else:
compilerLog = {}
plugin.execute(compilerLog, testDef)
log['compiler'] = compilerLog
testDef.logger.verbose_print(log['compiler'])
# Find MPI information for IUDatabase plugin if
# mpi_info is not already set
fullLog = testDef.logger.getLog(None)
mpi_info_found = False
for lg in fullLog:
if 'mpi_info' in lg:
mpi_info_found = True
if mpi_info_found is False:
plugin = None
availUtil = list(testDef.loader.utilities.keys())
for util in availUtil:
for pluginInfo in testDef.utilities.getPluginsOfCategory(util):
if "MPIVersion" == pluginInfo.plugin_object.print_name():
plugin = pluginInfo.plugin_object
break
if plugin is None:
log['mpi_info'] = {'name' : 'unknown', 'version' : 'unknown'}
else:
mpi_info = {}
plugin.execute(mpi_info, testDef)
log['mpi_info'] = mpi_info
else:
testDef.logger.verbose_print("mpi_info already in log so skipping MPIVersion")
try:
if cmds['build_in_place']:
prefix = None
log['location'] = location
pfx = location
inPlace = True
else:
# create the prefix path where this build result will be placed
pfx = os.path.join(testDef.options['scratchdir'], log['section'].replace(':','_'))
# convert it to an absolute path
pfx = os.path.abspath(pfx)
# record this location for any follow-on steps
log['location'] = pfx
prefix = "--prefix={0}".format(pfx)
except KeyError:
# create the prefix path where this build result will be placed
pfx = os.path.join(testDef.options['scratchdir'], log['section'].replace(':','_'))
# convert it to an absolute path
pfx = os.path.abspath(pfx)
# record this location for any follow-on steps
log['location'] = pfx
prefix = "--prefix={0}".format(pfx)
# check to see if we are to leave things "as-is"
try:
if cmds['asis']:
# see if the build already exists - if
# it does, then we are done
if os.path.exists(os.path.join(pfx, 'build_complete')):
testDef.logger.verbose_print("As-Is location " + pfx + " exists and has 'build_complete file")
# nothing further to do
log['status'] = 0
return
except KeyError:
pass
# check to see if this is a dryrun
if testDef.options['dryrun']:
# just log success and return
log['status'] = 0
return
# save the current directory so we can return to it
cwd = os.getcwd()
# now move to the package location
try:
os.chdir(location)
except:
log['status'] = 1
return
# see if they want us to execute autogen
try:
if cmds['autogen_cmd'] is not None:
agargs = []
args = cmds['autogen_cmd'].split()
for arg in args:
agargs.append(arg.strip())
results = testDef.execmd.execute(cmds, agargs, testDef)
if 0 != results['status']:
log['status'] = results['status']
log['stdout'] = results['stdout']
log['stderr'] = results['stderr']
log['result'] = testDef.MTT_TEST_FAILED
# Revert any requested environment module settings
status,stdout,stderr = testDef.modcmd.revertModules(log['section'], testDef)
if 0 != status:
log['status'] = status
log['stdout'] = stdout
log['stderr'] = stderr
# return to original location
os.chdir(cwd)
return
# return to original location
os.chdir(cwd)
return
else:
# this is a multistep operation, and so we need to
# retain the output from each step in the log
log['autogen'] = (stdout, stderr)
except KeyError:
# autogen phase is not required
pass
# Check if there is a configure script present and executable
if os.path.isfile("./configure") and os.access("./configure", os.X_OK):
# start building configure options
cfgopts = []
# see if any dependencies were given
try:
deps = cmds['dependencies']
# might be comma-delimited or space delimited
dps = re.split("\s|,", deps)
# loop over the entries
for d in dps:
# get the location where the output of this stage was stored by
# first recovering the log for it
try:
lg = testDef.logger.getLog(d)
try:
loc = lg['location']
except:
# we cannot do what the user requested
log['status'] = 1
log['stderr'] = "Location of dependency " + d + " could not be found"
log['result'] = testDef.MTT_TEST_FAILED
return
except:
# we don't have a record of this dependency
log['status'] = 1
log['stderr'] = "Log for dependency " + d + " could not be found"
log['result'] = testDef.MTT_TEST_FAILED
return
# split the dependency string to get the package name
try:
pkg = d.split(":")
except:
log['status'] = 1
log['stderr'] = "Dependency " + d + " is missing package name"
log['result'] = testDef.MTT_TEST_FAILED
return
# add the location and dependency option
opt = "--with-{0}".format(pkg[1]) + "={0}".format(loc)
cfgopts.append(opt)
except:
pass
# we always have to run configure, but we first
# need to build a target prefix directory option based
# on the scratch directory and section name
cfgargs = ["./configure"]
if prefix is not None:
cfgargs.append(prefix)
# if they gave us any configure args, add them
try:
if cmds['configure_options'] is not None:
args = shlex.split(cmds['configure_options'])
for arg in args:
cfgargs.append(arg.strip())
except KeyError:
pass
# add in any dependency options
for d in cfgopts:
cfgargs.append(d)
# execute the configure cmd
results = testDef.execmd.execute(cmds, cfgargs, testDef)
if 0 != results['status']:
log['status'] = results['status']
log['stdout'] = results['stdout']
log['stderr'] = results['stderr']
log['result'] = testDef.MTT_TEST_FAILED
# Revert any requested environment module settings
status,stdout,stderr = testDef.modcmd.revertModules(log['section'], testDef)
if 0 != status:
log['status'] = status
log['stdout'] = stdout
log['stderr'] = stderr
# return to original location
os.chdir(cwd)
return
# return to original location
os.chdir(cwd)
return
else:
# this is a multistep operation, and so we need to
# retain the output from each step in the log
log['configure'] = (stdout, stderr)
# next we do the build stage, using the custom build cmd
# if one is provided, or else defaulting to the testDef
# default
bldargs = ["make"]
try:
if cmds['make_options'] is not None:
args = cmds['make_options'].split()
for arg in args:
bldargs.append(arg.strip())
except KeyError:
# if they didn't provide it, then use the value in testDef
args = testDef.options.default_make_options.split()
for arg in args:
bldargs.append(arg.strip())
# see if we are to set any envars
loadedenv = []
try:
if cmds['make_envars'] is not None:
args = cmds['make_envars'].split()
for arg in args:
en = arg.split("=")
os.environ[en[0]] = en[1]
loadedenv.append(en[0])
except:
pass
# step thru the process, starting with "clean"
bldargs.append("clean")
results = testDef.execmd.execute(cmds, bldargs, testDef)
if 0 != results['status']:
log['status'] = results['status']
log['stdout'] = results['stdout']
log['stderr'] = results['stderr']
log['result'] = testDef.MTT_TEST_FAILED
# Revert any requested environment module settings
status,stdout,stderr = testDef.modcmd.revertModules(log['section'], testDef)
if 0 != status:
log['status'] = status
log['stdout'] = stdout
log['stderr'] = stderr
# return to original location
os.chdir(cwd)
# unload any envars we added
for en in loadedenv:
del os.environ[en]
return
# return to original location
os.chdir(cwd)
# unload any envars we added
for en in loadedenv:
del os.environ[en]
return
else:
# this is a multistep operation, and so we need to
# retain the output from each step in the log
log['make_clean'] = (stdout, stderr)
# now execute "make all"
bldargs = bldargs[0:-1]
bldargs.append("all")
results = testDef.execmd.execute(cmds, bldargs, testDef)
if 0 != results['status']:
log['status'] = results['status']
log['stdout'] = results['stdout']
log['stderr'] = results['stderr']
log['result'] = testDef.MTT_TEST_FAILED
# Revert any requested environment module settings
status,stdout,stderr = testDef.modcmd.revertModules(log['section'], testDef)
if 0 != status:
log['status'] = status
log['stdout'] = stdout
log['stderr'] = stderr
# return to original location
os.chdir(cwd)
# unload any envars we added
for en in loadedenv:
del os.environ[en]
return
# return to original location
os.chdir(cwd)
# unload any envars we added
for en in loadedenv:
del os.environ[en]
return
else:
# this is a multistep operation, and so we need to
# retain the output from each step in the log
log['make_all'] = (stdout, stderr)
# and finally, execute "make install" if we have a prefix
if prefix is not None:
bldargs = bldargs[0:-1]
bldargs.append("install")
results = testDef.execmd.execute(cmds, bldargs, testDef)
# this is the end of the operation, so the status is our
# overall status
log['status'] = results['status']
log['stdout'] = results['stdout']
log['stderr'] = results['stderr']
if 0 != results['status']:
log['result'] = testDef.MTT_TEST_FAILED
else:
log['result'] = testDef.MTT_TEST_PASSED
# Revert any requested environment module settings
status,stdout,stderr = testDef.modcmd.revertModules(log['section'], testDef)
if 0 != status:
log['status'] = status
log['stdout'] = stdout
log['stderr'] = stderr
# unload any envars we added
for en in loadedenv:
del os.environ[en]
return
# if we added middleware to the paths, remove it
if midpath:
if savebinpath is None:
try:
del os.environ['PATH']
except:
pass
else:
os.environ['PATH'] = savebinpath
if saveldlibpath is None:
try:
del os.environ['LD_LIBRARY_PATH']
except:
pass
else:
os.environ['LD_LIBRARY_PATH'] = saveldlibpath
if savecpath is None:
try:
del os.environ['CPATH']
except:
pass
else:
os.environ['CPATH'] = savecpath
if savelibpath is None:
try:
del os.environ['LIBRARY_PATH']
except:
pass
else:
os.environ['LIBRARY_PATH'] = savelibpath
# Add confirmation that build is complete
try:
confirmation = os.path.join(pfx, 'build_complete')
fo = open(confirmation, 'w')
fo.write("Build was successful")
print("BUILD SUCCESSFUL FILE CREATED AT: " + confirmation)
fo.close()
except:
pass
# return home
os.chdir(cwd)
# unload any envars we added
for en in loadedenv:
del os.environ[en]
return