1 # Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 # TODO(slightlyoff): move to using shared version of this script.
7 '''This script makes it easy to combine libs and object files to a new lib,
8 optionally removing some of the object files in the input libs by regular
10 For usage information, run the script with a --help argument.
20 '''Runs the program and args in args, returns the output from the program.'''
21 process
= subprocess
.Popen(args
,
23 stdout
= subprocess
.PIPE
,
24 stderr
= subprocess
.STDOUT
)
25 output
= process
.stdout
.readlines()
27 retcode
= process
.returncode
29 raise RuntimeError('%s exited with status %d' % (args
[0], retcode
))
33 def CollectRemovals(remove_re
, inputs
):
34 '''Returns a list of all object files in inputs that match remove_re.'''
37 output
= Shell('lib.exe', '/list', input)
41 if remove_re
.search(line
):
47 def CombineLibraries(output
, remove_re
, inputs
):
48 '''Combines all the libraries and objects in inputs, while removing any
49 object files that match remove_re.
53 removals
= CollectRemovals(remove_re
, inputs
)
57 args
= ['lib.exe', '/out:%s' % output
]
58 args
+= ['/remove:%s' % obj
for obj
in removals
]
63 USAGE
= '''usage: %prog [options] <lib or obj>+
65 Combines input libraries or objects into an output library, while removing
66 any object file (in the input libraries) that matches a given regular
70 def GetOptionParser():
71 parser
= optparse
.OptionParser(USAGE
)
72 parser
.add_option('-o', '--output', dest
= 'output',
73 help = 'write to this output library')
74 parser
.add_option('-r', '--remove', dest
= 'remove',
75 help = 'object files matching this regexp will be removed '
76 'from the output library')
81 '''Main function for this script'''
82 parser
= GetOptionParser()
83 (opt
, args
) = parser
.parse_args()
87 parser
.error('You must specify an output file')
90 parser
.error('You must specify at least one object or library')
92 output
= output
.strip()
93 remove
= remove
.strip()
97 remove_re
= re
.compile(opt
.remove
)
99 parser
.error('%s is not a valid regular expression' % opt
.remove
)
103 if sys
.platform
!= 'win32' and sys
.platform
!= 'cygwin':
104 parser
.error('this script only works on Windows for now')
106 # If this is set, we can't capture lib.exe's output.
107 if 'VS_UNICODE_OUTPUT' in os
.environ
:
108 del os
.environ
['VS_UNICODE_OUTPUT']
110 CombineLibraries(output
, remove_re
, args
)
113 if __name__
== '__main__':