Use ninja with merge_libs.py on Windows and clean up.

BUG=b/8110634

Review URL: https://webrtc-codereview.appspot.com/1094006

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3571 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/build/merge_libs.py b/webrtc/build/merge_libs.py
index 31c5efb..5edae39 100644
--- a/webrtc/build/merge_libs.py
+++ b/webrtc/build/merge_libs.py
@@ -8,51 +8,72 @@
 # in the file PATENTS.  All contributing project authors may
 # be found in the AUTHORS file in the root of the source tree.
 
-# Searches for libraries and/or object files on the specified path and
-# merges them into a single library.
+# Searches for libraries or object files on the specified path and merges them
+# them into a single library. Assumes ninja is used on all platforms.
 
+import fnmatch
+import os
 import subprocess
 import sys
 
-if __name__ == '__main__':
-  if len(sys.argv) != 3:
-    sys.stderr.write('Usage: ' + sys.argv[0] + ' <search_path> <output_lib>\n')
-    sys.exit(2)
 
-  search_path = sys.argv[1]
-  output_lib = sys.argv[2]
+def FindFiles(path, pattern):
+  """Finds files matching |pattern| under |path|.
 
-  from subprocess import call, PIPE
+  Returns a list of file paths matching |pattern|, by walking the directory tree
+  under |path|. Filenames containing the string 'do_not_use' are excluded.
+
+  Args:
+    path: The root path for the search.
+    pattern: A shell-style wildcard pattern to match filenames against.
+        (e.g. '*.a')
+
+  Returns:
+    A list of file paths, relative to the current working directory.
+  """
+  files = []
+  for root, _, filenames in os.walk(path):
+    for filename in fnmatch.filter(filenames, pattern):
+      if 'do_not_use' not in filename:
+        # We use the relative path here to avoid "argument list too long"
+        # errors on Linux.
+        files.append(os.path.relpath(os.path.join(root, filename)))
+  return files
+
+
+def main(argv):
+  if len(argv) != 3:
+    sys.stderr.write('Usage: ' + argv[0] + ' <search_path> <output_lib>\n')
+    return 1
+
+  search_path = os.path.normpath(argv[1])
+  output_lib = os.path.normpath(argv[2])
+
+  if not os.path.exists(search_path):
+    sys.stderr.write('search_path does not exist: %s\n' % search_path)
+    return 1
+
+  if os.path.isfile(output_lib):
+    os.remove(output_lib)
+
   if sys.platform.startswith('linux'):
-    call(["rm -f " + output_lib], shell=True)
-    call(["rm -rf " + search_path + "/obj.target/*do_not_use*"], shell=True)
-    call(["ar crs " + output_lib + " $(find " + search_path +
-          "/obj.target -name *\.o)"], shell=True)
-    call(["ar crs " + output_lib + " $(find " + search_path +
-          "/obj/gen -name *\.o)"], shell=True)
-
+    objects = FindFiles(search_path, '*.o')
+    cmd = 'ar crs '
   elif sys.platform == 'darwin':
-    call(["rm -f " + output_lib], shell=True)
-    call(["rm -f " + search_path + "/*do_not_use*"], shell=True)
-    call(["libtool -static -v -o " + output_lib + " " + search_path + "/*.a"],
-         shell=True)
-
+    objects = FindFiles(search_path, '*.a')
+    cmd = 'libtool -static -v -o '
   elif sys.platform == 'win32':
-    # We need to execute a batch file to set some environment variables for the
-    # lib command. VS 8 uses vsvars.bat and VS 9 uses vsvars32.bat. It's
-    # required that at least one of them is in the system PATH. We try both and
-    # suppress stderr and stdout to fail silently.
-    call(["vsvars.bat"], stderr=PIPE, stdout=PIPE, shell=True)
-    call(["vsvars32.bat"], stderr=PIPE, stdout=PIPE, shell=True)
-    call(["del " + output_lib], shell=True)
-    call(["del /F /S /Q " + search_path + "/lib/*do_not_use*"],
-          shell=True)
-    call(["lib /OUT:" + output_lib + " " + search_path + "/lib/*.lib"],
-         shell=True)
-
+    objects = FindFiles(search_path, '*.lib')
+    cmd = 'lib /OUT:'
   else:
     sys.stderr.write('Platform not supported: %r\n\n' % sys.platform)
-    sys.exit(1)
+    return 1
 
-  sys.exit(0)
+  cmd += output_lib + ' ' + ' '.join(objects)
+  print cmd
+  subprocess.check_call(cmd, shell=True)
+  return 0
+
+if __name__ == '__main__':
+  sys.exit(main(sys.argv))