import argparse
import os
import subprocess
import sys
import wrapper_utils
BAT_PREFIX = 'cmd /c call '
def CommandToRun(command):
if command[0].startswith(BAT_PREFIX):
command = command[0].split(None, 3) + command[1:]
return command
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--strip',
help='The strip binary to run',
metavar='PATH')
parser.add_argument('--unstripped-file',
help='Executable file produced by linking command',
metavar='FILE')
parser.add_argument('--map-file',
help=('Use --Wl,-Map to generate a map file. Will be '
'gzipped if extension ends with .gz'),
metavar='FILE')
parser.add_argument('--dwp', help=('The dwp binary to run'), metavar='FILE')
parser.add_argument('--output',
required=True,
help='Final output executable file',
metavar='FILE')
parser.add_argument('command', nargs='+',
help='Linking command')
args = parser.parse_args()
fast_env = dict(os.environ)
fast_env['LC_ALL'] = 'C'
result = wrapper_utils.RunLinkWithOptionalMapFile(args.command, env=fast_env,
map_file=args.map_file)
if result != 0:
return result
dwp_proc = None
if args.dwp:
exe_file = args.output
if args.unstripped_file:
exe_file = args.unstripped_file
dwp_proc = subprocess.Popen(wrapper_utils.CommandToRun(
[args.dwp, '-e', exe_file, '-o', exe_file + '.dwp']),
stderr=subprocess.DEVNULL)
if args.strip:
result = subprocess.call(
CommandToRun([args.strip, '-o', args.output, args.unstripped_file]))
if dwp_proc:
dwp_result = dwp_proc.wait()
if dwp_result != 0:
sys.stderr.write('dwp failed with error code {}\n'.format(dwp_result))
return dwp_result
return result
if __name__ == "__main__":
sys.exit(main())