mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 02:09:44 +02:00
130 lines
6 KiB
Python
Executable file
130 lines
6 KiB
Python
Executable file
#!/usr/bin/python3
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import traceback
|
|
import json
|
|
|
|
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
|
from build import *
|
|
from pcodetest import *
|
|
|
|
# set default properties first, then update values from the command
|
|
# line before they are instantiated.
|
|
|
|
def test_action(action_class, deprecate=False):
|
|
class pcodeTestAction(action_class):
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
c = getattr(namespace, 'command_count', 0)
|
|
setattr(namespace, 'command_count', c+1)
|
|
|
|
if deprecate:
|
|
print('Deprecated pcodetest command\n\tuse --%s' % (self.dest))
|
|
action_class.__call__(self, parser, namespace, values, option_string)
|
|
return pcodeTestAction
|
|
|
|
from defaults import *
|
|
|
|
parser = argparse.ArgumentParser(description='''Build pcodetests.
|
|
One and only one of the following options must be given:
|
|
[--test, --all, --list]''',
|
|
epilog='(*) default properties for pcodetest instances',
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
# required alternates
|
|
required_group = parser.add_argument_group('Pcodetest Commands')
|
|
|
|
required_group.add_argument('-t', '--test', dest='test', action=test_action(argparse._StoreAction), help='the pcode test to build')
|
|
required_group.add_argument('-a', '--all', dest='all', action=test_action(argparse._StoreTrueAction), help='build all pcode tests')
|
|
required_group.add_argument('-l', '--list', dest='list', action=test_action(argparse._StoreTrueAction), help='list available pcode tests')
|
|
|
|
#Deprecated
|
|
required_group.add_argument('--pcodetest', dest='test', action=test_action(argparse._StoreAction, True), help=argparse.SUPPRESS)
|
|
required_group.add_argument('--pcodetest-all',dest='all', action=test_action(argparse._StoreTrueAction, True), help=argparse.SUPPRESS)
|
|
required_group.add_argument('--pcodetest-list', dest='list', action=test_action(argparse._StoreTrueAction, True), help=argparse.SUPPRESS)
|
|
|
|
|
|
# all-applicable arguments
|
|
|
|
parser.add_argument('-f', '--force', action='store_true', default=pcodeTestDefaults.force, help='force a build')
|
|
parser.add_argument('-v', '--verbose', action='store_true', help='verbose output where available ')
|
|
parser.add_argument('--toolchain-root', default=pcodeTestDefaults.toolchain_root, help='directory where toolchain directories can be found (*)')
|
|
parser.add_argument('--build-root', default=pcodeTestDefaults.build_root, help='temporary directory to hold build files (*)')
|
|
parser.add_argument('--gcc-version', default=pcodeTestDefaults.gcc_version, help='default version of gcc (*)')
|
|
parser.add_argument('--gcc-config', default=pcodeTestDefaults.gcc_version, help='default configuration of gcc (*)')
|
|
|
|
# pcodetest arguments
|
|
|
|
pcodetest_group = parser.add_argument_group('Pcodetest Options')
|
|
pcodetest_group.add_argument('--no-publish', action='store_true', help='do not publish pcode test binaries to pcode test root')
|
|
pcodetest_group.add_argument('--export-root', default=pcodeTestDefaults.export_root, help='location to publish pcode tests binaries (*)')
|
|
pcodetest_group.add_argument('--pcodetest-src', default=pcodeTestDefaults.pcodetest_src, help='location of pcode test .c and .h source files (*)')
|
|
pcodetest_group.add_argument('--skip-files', nargs='+', default=pcodeTestDefaults.skip_files, help='default .c files to remove from the pcode test image (*)')
|
|
pcodetest_group.add_argument('--strip-symbols', action='store_true', help='strip symbols from image')
|
|
pcodetest_group.add_argument('--add-ccflags', default='', help='additional flags to pass to compiler (must be quoted)')
|
|
pcodetest_group.add_argument('--gcc-libdir', default='', help='location of gcc libraries')
|
|
pcodetest_group.add_argument('--add-cclibs', default='', help='additional libraries to pass to the linker')
|
|
pcodetest_group.add_argument('--add-info', action='store_true', help='add data to binary with information about types and symbols')
|
|
pcodetest_group.add_argument('--build-exe', action='store_true', help='build a guest executable binary (exe)')
|
|
pcodetest_group.add_argument('--variants', default=json.dumps(pcodeTestDefaults.variants, sort_keys=True, separators=(',',':')), type=json.loads, help='build the (optimization) variants, encoded as a json dict')
|
|
|
|
|
|
sys.argv.pop(0)
|
|
args = parser.parse_args(sys.argv)
|
|
|
|
pcodeTestDefaults.skip_files = args.skip_files
|
|
pcodeTestDefaults.export_root = args.export_root
|
|
pcodeTestDefaults.pcodetest_src = args.pcodetest_src
|
|
pcodeTestDefaults.strip_symbols = args.strip_symbols
|
|
pcodeTestDefaults.add_ccflags = args.add_ccflags
|
|
pcodeTestDefaults.gcc_libdir = args.gcc_libdir
|
|
pcodeTestDefaults.add_cclibs = args.add_cclibs
|
|
pcodeTestDefaults.add_info = args.add_info
|
|
pcodeTestDefaults.build_exe = args.build_exe
|
|
pcodeTestDefaults.variants = args.variants
|
|
pcodeTestDefaults.verbose = args.verbose
|
|
pcodeTestDefaults.force = args.force
|
|
pcodeTestDefaults.no_publish = args.no_publish
|
|
pcodeTestDefaults.toolchain_root = args.toolchain_root
|
|
pcodeTestDefaults.build_root = args.build_root
|
|
pcodeTestDefaults.gcc_version = args.gcc_version
|
|
pcodeTestDefaults.gcc_config = args.gcc_config
|
|
|
|
# load the known pcodetests
|
|
from pcode_defs import *
|
|
|
|
cwd = os.getcwd()
|
|
|
|
if not hasattr(args, 'command_count'):
|
|
print('ERROR: One of [--test, --all, --list] must be given\n')
|
|
parser.print_help()
|
|
exit()
|
|
|
|
if args.command_count > 1:
|
|
print('ERROR: Two many commands given. Only one of [--test, --all, --list] must be given\n')
|
|
parser.print_help()
|
|
exit()
|
|
|
|
|
|
if args.list:
|
|
PCodeTest.print_all()
|
|
|
|
elif args.all:
|
|
for n,pct in sorted(PCodeTest.list.items(), key=lambda x: x[0].lower()):
|
|
if pct.config.build_all:
|
|
try: PCodeTestBuild.factory(pct).main()
|
|
except Exception as e:
|
|
print('unhandled exception while building %s' % n)
|
|
traceback.print_exc(file=sys.stdout)
|
|
os.chdir(cwd)
|
|
|
|
elif args.test:
|
|
if args.test in PCodeTest.list:
|
|
PCodeTest = PCodeTest.list[args.test]
|
|
PCodeTestBuild.factory(PCodeTest).main()
|
|
else:
|
|
print('the pcode test %s is not in the list' % args.test)
|
|
else:
|
|
parser.print_help()
|
|
|