"""Runs a linking command and optionally a strip command.
This script exists to avoid using complex shell commands in
gcc_toolchain.gni's tool("link"), in case the host running the compiler
does not have a POSIX-like shell (e.g. Windows).
"""
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')
parser.add_argument('--mini-debug',
action='store_true',
default=False,
help='Add .gnu_debugdata section for stripped sofile')
parser.add_argument('--clang-base-dir', help='')
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
if args.mini_debug:
unstripped_libfile = os.path.abspath(args.unstripped_file)
ohos_root_path = os.path.join(os.path.dirname(__file__), '../..')
script_path = os.path.join(ohos_root_path, 'arkweb/build/toolchain', 'mini_debug_info.py')
result = subprocess.call(
wrapper_utils.CommandToRun(
['python3', script_path, '--unstripped-path', unstripped_libfile, '--stripped-path', args.output,
'--root-path', ohos_root_path, '--clang-base-dir', args.clang_base_dir]))
return result
if __name__ == "__main__":
sys.exit(main())