Bug 1047621 - Have link.py import and call expandlibs_exec.py; r=gps a=NPOTB
authorMike Shal <mshal@mozilla.com>
Wed, 13 Aug 2014 14:24:15 -0400
changeset 208335 bd02db1d22d0
parent 208334 a09c51fcbd98
child 208336 2309c50ccc6c
push id3829
push userryanvm@gmail.com
push date2014-08-19 11:52 +0000
treeherdermozilla-beta@bd02db1d22d0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps, NPOTB
bugs1047621
milestone32.0
Bug 1047621 - Have link.py import and call expandlibs_exec.py; r=gps a=NPOTB
config/expandlibs_exec.py
config/link.py
toolkit/library/build/Makefile.in
--- a/config/expandlibs_exec.py
+++ b/config/expandlibs_exec.py
@@ -308,32 +308,32 @@ class SectionFinder(object):
 def print_command(out, args):
     print >>out, "Executing: " + " ".join(args)
     for tmp in [f for f in args.tmp if os.path.isfile(f)]:
         print >>out, tmp + ":"
         with open(tmp) as file:
             print >>out, "".join(["    " + l for l in file.readlines()])
     out.flush()
 
-def main():
+def main(args, proc_callback=None):
     parser = OptionParser()
     parser.add_option("--depend", dest="depend", metavar="FILE",
         help="generate dependencies for the given execution and store it in the given file")
     parser.add_option("--target", dest="target", metavar="FILE",
         help="designate the target for dependencies")
     parser.add_option("--extract", action="store_true", dest="extract",
         help="when a library has no descriptor file, extract it first, when possible")
     parser.add_option("--uselist", action="store_true", dest="uselist",
         help="use a list file for objects when executing a command")
     parser.add_option("--verbose", action="store_true", dest="verbose",
         help="display executed command and temporary files content")
     parser.add_option("--symbol-order", dest="symbol_order", metavar="FILE",
         help="use the given list of symbols to order symbols in the resulting binary when using with a linker")
 
-    (options, args) = parser.parse_args()
+    (options, args) = parser.parse_args(args)
 
     if not options.target:
         options.depend = False
     if options.depend:
         deps = ExpandLibsDeps(args)
         # Filter out common command wrappers
         while os.path.basename(deps[0]) in ['ccache', 'distcc']:
             deps.pop(0)
@@ -346,34 +346,37 @@ def main():
             args.orderSymbols(options.symbol_order)
         if options.uselist:
             args.makelist()
 
         if options.verbose:
             print_command(sys.stderr, args)
         try:
             proc = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
+            if proc_callback:
+                proc_callback(proc)
         except Exception, e:
             print >>sys.stderr, 'error: Launching', args, ':', e
             raise e
         (stdout, stderr) = proc.communicate()
         if proc.returncode and not options.verbose:
             print_command(sys.stderr, args)
         sys.stderr.write(stdout)
         sys.stderr.flush()
         if proc.returncode:
-            exit(proc.returncode)
+            return proc.returncode
     if not options.depend:
-        return
+        return 0
     ensureParentDir(options.depend)
     mk = Makefile()
     deps = [dep for dep in deps if os.path.isfile(dep) and dep != options.target
             and os.path.abspath(dep) != os.path.abspath(options.depend)]
     no_dynamic_lib = [dep for dep in deps if not isDynamicLib(dep)]
     mk.create_rule([options.target]).add_dependencies(no_dynamic_lib)
     if len(deps) != len(no_dynamic_lib):
         mk.create_rule(['%s_order_only' % options.target]).add_dependencies(dep for dep in deps if isDynamicLib(dep))
 
     with open(options.depend, 'w') as depfile:
         mk.dump(depfile, removal_guard=True)
+    return 0
 
 if __name__ == '__main__':
-    main()
+    exit(main(sys.argv[1:]))
--- a/config/link.py
+++ b/config/link.py
@@ -1,14 +1,17 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
-from __future__ import with_statement
-import os, subprocess, sys, threading, time
+import expandlibs_exec
+import sys
+import threading
+import time
+
 from win32 import procmem
 
 def measure_vsize_threadfunc(proc, output_file):
     """
     Measure the virtual memory usage of |proc| at regular intervals
     until it exits, then print the maximum value and write it to
     |output_file|.  Also, print something to the console every
     half an hour to prevent the build job from getting killed when
@@ -28,26 +31,30 @@ def measure_vsize_threadfunc(proc, outpu
     with open(output_file, "w") as f:
         f.write("%d\n" % maxvsize)
 
 def measure_link_vsize(output_file, args):
     """
     Execute |args|, and measure the maximum virtual memory usage of the process,
     printing it to stdout when finished.
     """
-    proc = subprocess.Popen(args)
-    t = threading.Thread(target=measure_vsize_threadfunc,
-                         args=(proc, output_file))
-    t.start()
-    # Wait for the linker to finish.
-    exitcode = proc.wait()
-    # ...and then wait for the background thread to finish.
-    t.join()
+
+    # This needs to be a list in order for the callback to set the
+    # variable properly with python-2's scoping rules.
+    t = [None]
+    def callback(proc):
+        t[0] = threading.Thread(target=measure_vsize_threadfunc,
+                             args=(proc, output_file))
+        t[0].start()
+    exitcode = expandlibs_exec.main(args, proc_callback=callback)
+    # Wait for the background thread to finish.
+    t[0].join()
     return exitcode
 
 if __name__ == "__main__":
     if sys.platform != "win32":
         print >>sys.stderr, "link.py is only for use on Windows!"
         sys.exit(1)
     if len(sys.argv) < 3:
         print >>sys.stderr, "Usage: link.py <output filename> <commandline>"
         sys.exit(1)
-    sys.exit(measure_link_vsize(sys.argv[1], sys.argv[2:]))
+    output_file = sys.argv.pop(1)
+    sys.exit(measure_link_vsize(output_file, sys.argv[1:]))
--- a/toolkit/library/build/Makefile.in
+++ b/toolkit/library/build/Makefile.in
@@ -1,17 +1,19 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
 # You can obtain one at http://mozilla.org/MPL/2.0/.
 
 include $(topsrcdir)/toolkit/library/libxul.mk
 
+include $(topsrcdir)/config/config.mk
+
 ifeq (WINNT_1,$(OS_TARGET)_$(MOZ_PROFILE_USE))
 # Wrap linker to measure peak virtual memory usage.
-LD := $(PYTHON) $(topsrcdir)/config/link.py $(DEPTH)/toolkit/library/linker-vsize $(LD)
+EXPAND_LIBS_EXEC := $(PYTHON) $(topsrcdir)/config/link.py $(DEPTH)/toolkit/library/linker-vsize
 endif
 
 ifneq (,$(filter WINNT,$(OS_ARCH)))
 SDK_LIBRARY = $(IMPORT_LIBRARY)
 else
 SDK_LIBRARY = $(SHARED_LIBRARY)
 endif