2 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
10 from driver_env import env
11 from driver_log import Log
18 'OPT_FLAGS_all' : '-disable-opt --strip',
19 'OPT_FLAGS_debug' : '-disable-opt --strip-debug',
20 'STRIP_FLAGS_all' : '-s',
21 'STRIP_FLAGS_debug' : '-S',
23 'OPT_FLAGS' : '${OPT_FLAGS_%MODE%}',
24 'STRIP_FLAGS' : '${STRIP_FLAGS_%MODE%}',
26 'RUN_OPT' : '${LLVM_OPT} ${OPT_FLAGS} ${input} -o ${output}',
27 'RUN_STRIP' : '${STRIP} ${STRIP_FLAGS} ${input} -o ${output}',
31 ( ('-o','(.*)'), "env.set('OUTPUT', pathtools.normalize($0))"),
32 ( ('-o','(.*)'), "env.set('OUTPUT', pathtools.normalize($0))"),
34 ( '--strip-all', "env.set('MODE', 'all')"),
35 ( '-s', "env.set('MODE', 'all')"),
37 ( '--strip-debug', "env.set('MODE', 'debug')"),
38 ( '-S', "env.set('MODE', 'debug')"),
39 ( '-g', "env.set('MODE', 'debug')"),
40 ( '-d', "env.set('MODE', 'debug')"),
42 ( '(-p)', "env.append('STRIP_FLAGS', $0)"),
43 ( '(--info)', "env.append('STRIP_FLAGS', $0)"),
45 ( '(-.*)', driver_tools.UnrecognizedOption),
47 ( '(.*)', "env.append('INPUTS', pathtools.normalize($0))"),
52 driver_tools.ParseArgs(argv, StripPatterns)
53 inputs = env.get('INPUTS')
54 output = env.getone('OUTPUT')
55 for path in inputs + [output]:
56 driver_tools.CheckPathLength(path)
58 if len(inputs) > 1 and output != '':
59 Log.Fatal('Cannot have -o with multiple inputs')
61 if '--info' in env.get('STRIP_FLAGS'):
62 code, _, _ = driver_tools.Run('${STRIP} ${STRIP_FLAGS}')
70 if filetype.IsPNaClBitcode(f):
71 # PNaCl-format bitcode has no symbols, i.e. it is already stripped.
73 shutil.copyfile(f, f_output)
74 elif filetype.IsLLVMBitcode(f):
75 driver_tools.RunWithEnv('${RUN_OPT}', input=f, output=f_output)
76 elif filetype.IsELF(f) or filetype.IsNativeArchive(f):
77 driver_tools.RunWithEnv('${RUN_STRIP}', input=f, output=f_output)
78 elif filetype.IsBitcodeArchive(f):
79 # The strip tool supports native archives, but it does not support the
80 # LLVM gold plugin so cannot handle bitcode. There is also no bitcode
81 # tool like opt that support archives.
82 Log.Fatal('%s: strip does not support bitcode archives',
85 Log.Fatal('%s: File is neither ELF, nor bitcode', pathtools.touser(f))
89 def get_help(unused_argv):
90 script = env.getone('SCRIPT_NAME')
91 return """Usage: %s <option(s)> in-file(s)
92 Removes symbols and sections from bitcode or native code.
95 -p --preserve-dates Copy modified/access timestamps to the output
96 (only native code for now)
97 -s --strip-all Remove all symbol and relocation information
98 -g -S -d --strip-debug Remove all debugging symbols & sections
99 -h --help Display this output
100 --info List object formats & architectures supported
101 -o <file> Place stripped output into <file>
103 %s: supported targets: bitcode, native code (see --info).
104 """ % (script, script)