mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 18:29:37 +02:00
Port pcodetest builder to Python 3.
This commit is contained in:
parent
28e3c2b8db
commit
b3041d045f
3 changed files with 43 additions and 43 deletions
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
@ -13,7 +13,7 @@ from pcodetest import *
|
||||||
# set default properties first, then update values from the command
|
# set default properties first, then update values from the command
|
||||||
# line before they are instantiated.
|
# line before they are instantiated.
|
||||||
|
|
||||||
execfile('defaults.py')
|
exec(compile(open('defaults.py', "rb").read(), 'defaults.py', 'exec'))
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='''Build pcodetests.
|
parser = argparse.ArgumentParser(description='''Build pcodetests.
|
||||||
One and only one of the following options must be given:
|
One and only one of the following options must be given:
|
||||||
|
@ -63,21 +63,24 @@ PCodeTest.defaults.variants = args.variants
|
||||||
PCodeTest.defaults.verbose = args.verbose
|
PCodeTest.defaults.verbose = args.verbose
|
||||||
PCodeTest.defaults.force = args.force
|
PCodeTest.defaults.force = args.force
|
||||||
PCodeTest.defaults.no_publish = args.no_publish
|
PCodeTest.defaults.no_publish = args.no_publish
|
||||||
|
PCodeTest.defaults.toolchain_root = args.toolchain_root
|
||||||
|
PCodeTest.defaults.build_root = args.build_root
|
||||||
|
PCodeTest.defaults.gcc_version = args.gcc_version
|
||||||
|
|
||||||
# load the known pcodetests
|
# load the known pcodetests
|
||||||
|
|
||||||
execfile('pcode_defs.py')
|
exec(compile(open('pcode_defs.py', "rb").read(), 'pcode_defs.py', 'exec'))
|
||||||
|
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
|
|
||||||
if args.pcodetest_list:
|
if args.pcodetest_list:
|
||||||
PCodeTest.print_all()
|
PCodeTest.print_all()
|
||||||
elif args.pcodetest_all:
|
elif args.pcodetest_all:
|
||||||
for n,pct in sorted(PCodeTest.list.iteritems(), key=lambda x: x[0].lower()):
|
for n,pct in sorted(PCodeTest.list.items(), key=lambda x: x[0].lower()):
|
||||||
if pct.config.build_all:
|
if pct.config.build_all:
|
||||||
try: PCodeTestBuild.factory(pct).main()
|
try: PCodeTestBuild.factory(pct).main()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print 'unhandled exception while building %s' % n
|
print('unhandled exception while building %s' % n)
|
||||||
traceback.print_exc(file=sys.stdout)
|
traceback.print_exc(file=sys.stdout)
|
||||||
os.chdir(cwd)
|
os.chdir(cwd)
|
||||||
elif args.pcodetest:
|
elif args.pcodetest:
|
||||||
|
@ -85,7 +88,7 @@ elif args.pcodetest:
|
||||||
PCodeTest = PCodeTest.list[args.pcodetest]
|
PCodeTest = PCodeTest.list[args.pcodetest]
|
||||||
PCodeTestBuild.factory(PCodeTest).main()
|
PCodeTestBuild.factory(PCodeTest).main()
|
||||||
else:
|
else:
|
||||||
print 'the pcode test %s is not in the list' % args.pcodetest
|
print('the pcode test %s is not in the list' % args.pcodetest)
|
||||||
else:
|
else:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ import pwd
|
||||||
import grp
|
import grp
|
||||||
import re
|
import re
|
||||||
|
|
||||||
class BuildUtil(object):
|
class BuildUtil:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.log = False
|
self.log = False
|
||||||
|
@ -30,7 +30,7 @@ class BuildUtil(object):
|
||||||
self.num_warnings = 0
|
self.num_warnings = 0
|
||||||
|
|
||||||
def run(self, cmd, stdout=False, stderr=False, verbose=True):
|
def run(self, cmd, stdout=False, stderr=False, verbose=True):
|
||||||
if isinstance(cmd, basestring):
|
if isinstance(cmd, str):
|
||||||
if stdout and stderr:
|
if stdout and stderr:
|
||||||
cmd += ' 1>%s 2>%s' % (stdout, stderr)
|
cmd += ' 1>%s 2>%s' % (stdout, stderr)
|
||||||
elif stdout and not stderr:
|
elif stdout and not stderr:
|
||||||
|
@ -40,19 +40,19 @@ class BuildUtil(object):
|
||||||
if verbose: self.log_info(cmd)
|
if verbose: self.log_info(cmd)
|
||||||
os.system(cmd)
|
os.system(cmd)
|
||||||
else:
|
else:
|
||||||
str = ' '.join(cmd);
|
string = ' '.join(cmd)
|
||||||
if stdout:
|
if stdout:
|
||||||
f = file(stdout, 'w+')
|
f = open(stdout, 'w+')
|
||||||
str += ' 1>%s 2>&1' % (stdout)
|
string += ' 1>%s 2>&1' % (stdout)
|
||||||
else:
|
else:
|
||||||
f = subprocess.PIPE
|
f = subprocess.PIPE
|
||||||
if verbose: self.log_info(str)
|
if verbose: self.log_info(string)
|
||||||
try:
|
try:
|
||||||
sp = subprocess.Popen(cmd, stdout=f, stderr=subprocess.PIPE)
|
sp = subprocess.Popen(cmd, stdout=f, stderr=subprocess.PIPE)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
self.log_err("Command: " + str)
|
self.log_err("Command: " + string)
|
||||||
self.log_err(e.message)
|
self.log_err(e.strerror)
|
||||||
return 0,e.message#raise
|
return 0, e.strerror
|
||||||
if stdout: f.close()
|
if stdout: f.close()
|
||||||
out, err = sp.communicate()
|
out, err = sp.communicate()
|
||||||
# print 'run returned %d bytes stdout and %d bytes stderr' % (len(out) if out else 0, len(err) if err else 0)
|
# print 'run returned %d bytes stdout and %d bytes stderr' % (len(out) if out else 0, len(err) if err else 0)
|
||||||
|
@ -112,19 +112,18 @@ class BuildUtil(object):
|
||||||
try:
|
try:
|
||||||
if not os.path.isdir(dname):
|
if not os.path.isdir(dname):
|
||||||
self.makedirs(dname)
|
self.makedirs(dname)
|
||||||
else:
|
self.copy(fname, dname, verbose=True)
|
||||||
self.copy(fname, dname, verbose=True)
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
self.log_err('Error occurred exporting %s to %s' % (fname, dname))
|
self.log_err('Error occurred exporting %s to %s' % (fname, dname))
|
||||||
self.log_err("Unexpected error: %s" % str(e))
|
self.log_err("Unexpected error: %s" % str(e))
|
||||||
|
|
||||||
def rmtree(self, dir, verbose=True):
|
def rmtree(self, directory, verbose=True):
|
||||||
if verbose: self.log_info('rm -r %s' % dir)
|
if verbose: self.log_info('rm -r %s' % directory)
|
||||||
shutil.rmtree(dir)
|
shutil.rmtree(directory)
|
||||||
|
|
||||||
def makedirs(self, dir, verbose=True):
|
def makedirs(self, directory, verbose=True):
|
||||||
if verbose: self.log_info('mkdir -p %s' % dir)
|
if verbose: self.log_info('mkdir -p %s' % directory)
|
||||||
try: os.makedirs(dir)
|
try: os.makedirs(directory)
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
# copy a file/directory to a directory
|
# copy a file/directory to a directory
|
||||||
|
@ -138,9 +137,9 @@ class BuildUtil(object):
|
||||||
shutil.rmtree(dname)
|
shutil.rmtree(dname)
|
||||||
shutil.copytree(fname, dname)
|
shutil.copytree(fname, dname)
|
||||||
|
|
||||||
def chdir(self, dir, verbose=True):
|
def chdir(self, directory, verbose=True):
|
||||||
if verbose: self.log_info('cd %s' % dir)
|
if verbose: self.log_info('cd %s' % directory)
|
||||||
os.chdir(dir)
|
os.chdir(directory)
|
||||||
|
|
||||||
def remove(self, fname, verbose=True):
|
def remove(self, fname, verbose=True):
|
||||||
if verbose: self.log_info('rm -f %s' % fname)
|
if verbose: self.log_info('rm -f %s' % fname)
|
||||||
|
@ -189,9 +188,9 @@ class BuildUtil(object):
|
||||||
def log_close(self):
|
def log_close(self):
|
||||||
if self.log:
|
if self.log:
|
||||||
if self.num_errors > 0:
|
if self.num_errors > 0:
|
||||||
print '# ERROR: There were errors, see %s' % self.name
|
print('# ERROR: There were errors, see %s' % self.name)
|
||||||
elif self.num_warnings > 0:
|
elif self.num_warnings > 0:
|
||||||
print '# WARNING: There were warnings, see %s' % self.name
|
print('# WARNING: There were warnings, see %s' % self.name)
|
||||||
self.log.close()
|
self.log.close()
|
||||||
self.log = False
|
self.log = False
|
||||||
self.name = False
|
self.name = False
|
||||||
|
@ -199,7 +198,7 @@ class BuildUtil(object):
|
||||||
self.num_warnings = 0
|
self.num_warnings = 0
|
||||||
|
|
||||||
def log_pr(self, prefix, what):
|
def log_pr(self, prefix, what):
|
||||||
if isinstance(what, basestring):
|
if isinstance(what, str):
|
||||||
log_string = prefix + what
|
log_string = prefix + what
|
||||||
else:
|
else:
|
||||||
log_string = prefix + repr(what)
|
log_string = prefix + repr(what)
|
||||||
|
@ -208,7 +207,7 @@ class BuildUtil(object):
|
||||||
self.log.write(log_string + '\n')
|
self.log.write(log_string + '\n')
|
||||||
self.log.flush()
|
self.log.flush()
|
||||||
else:
|
else:
|
||||||
print log_string
|
print(log_string)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
def log_err(self, what):
|
def log_err(self, what):
|
||||||
|
@ -278,7 +277,7 @@ class BuildUtil(object):
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
class Config(object):
|
class Config:
|
||||||
|
|
||||||
def __init__(self, *obj):
|
def __init__(self, *obj):
|
||||||
for o in obj:
|
for o in obj:
|
||||||
|
@ -286,23 +285,23 @@ class Config(object):
|
||||||
else: self.__dict__.update(o.__dict__)
|
else: self.__dict__.update(o.__dict__)
|
||||||
|
|
||||||
def format(self, val):
|
def format(self, val):
|
||||||
if isinstance(val, basestring) and '%' in val:
|
if isinstance(val, str) and '%' in val:
|
||||||
return val % self.__dict__
|
return val % self.__dict__
|
||||||
elif isinstance(val, dict):
|
elif isinstance(val, dict):
|
||||||
return dict(map(lambda (k,v): (k,self.format(v)), val.iteritems()))
|
return {k: self.format(v) for k, v in val.items()}
|
||||||
else: return val
|
else: return val
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def expand(self):
|
def expand(self):
|
||||||
for k,v in self.__dict__.iteritems():
|
for k,v in self.__dict__.items():
|
||||||
self.__dict__[k] = self.format(v)
|
self.__dict__[k] = self.format(v)
|
||||||
|
|
||||||
def dump(self):
|
def dump(self):
|
||||||
ret = ''
|
ret = ''
|
||||||
for k,v in sorted(self.__dict__.iteritems()):
|
for k,v in sorted(self.__dict__.items()):
|
||||||
if isinstance(v, basestring): vv = "'" + v + "'"
|
if isinstance(v, str): vv = "'" + v + "'"
|
||||||
else: vv = str(v)
|
else: vv = str(v)
|
||||||
ret += ' '.ljust(10) + k.ljust(20) + vv + '\n'
|
ret += ' '.ljust(10) + k.ljust(20) + vv + '\n'
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -53,11 +53,9 @@ class PCodeTest(BuildUtil):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def print_all(cls):
|
def print_all(cls):
|
||||||
pct = sorted(cls.list.iteritems(), key=lambda x: x[0].lower())
|
for t,pcodetest in sorted(cls.list.items(), key=lambda x: x[0].lower()):
|
||||||
|
print(str(pcodetest))
|
||||||
for t,pcodetest in sorted(cls.list.iteritems(), key=lambda x: x[0].lower()):
|
if pcodetest.config.verbose: print(pcodetest.config.dump())
|
||||||
print str(pcodetest)
|
|
||||||
if pcodetest.config.verbose: print pcodetest.config.dump()
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
cb = 'build-all:%-5s' % ('yes' if self.config.build_all else 'no')
|
cb = 'build-all:%-5s' % ('yes' if self.config.build_all else 'no')
|
||||||
|
@ -81,7 +79,7 @@ class PCodeTestBuild(BuildUtil):
|
||||||
elif pcode_test.config.toolchain_type == 'sdcc':
|
elif pcode_test.config.toolchain_type == 'sdcc':
|
||||||
return PCodeBuildSDCC(pcode_test)
|
return PCodeBuildSDCC(pcode_test)
|
||||||
else:
|
else:
|
||||||
raise Exception(self.config.format('Toolchain type %(toolchain_type)s not known'))
|
raise Exception(pcode_test.config.format('Toolchain type %(toolchain_type)s not known'))
|
||||||
|
|
||||||
def which(self, what):
|
def which(self, what):
|
||||||
return self.config.format('%(toolchain_dir)s/%(' + what + ')s')
|
return self.config.format('%(toolchain_dir)s/%(' + what + ')s')
|
||||||
|
@ -122,7 +120,7 @@ class PCodeTestBuild(BuildUtil):
|
||||||
if not self.config.has_longlong: available_files = [x for x in available_files if not fnmatch.fnmatch(x, '*LONGLONG*')]
|
if not self.config.has_longlong: available_files = [x for x in available_files if not fnmatch.fnmatch(x, '*LONGLONG*')]
|
||||||
|
|
||||||
# compile for each optimization
|
# compile for each optimization
|
||||||
for opt_name,opt_cflag in sorted(self.config.variants.iteritems()):
|
for opt_name,opt_cflag in sorted(self.config.variants.items()):
|
||||||
|
|
||||||
kind = 'PCodeTest'
|
kind = 'PCodeTest'
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue