#!/usr/bin/env python import commands import optparse import os import re import tempfile import shutil import pprint import sys '''This script will find and copy the requested shlibs to the destination directory. If no shlib can be found, it will see if an appropriate static library (.a) exists. If it does, it will unpack it and make it into a shared library.''' def getSuffix(): makeso = 'include ${ROOTSYS}/test/Makefile.arch\n\nall:\n\t@echo $(DllSuf)\n' try: tempTuple = tempfile.mkstemp (dir='.', prefix='Makefile-') name = tempTuple[1] target = os.fdopen (tempTuple[0] , "w+b") except: raise RuntimeError, "Failed to open temporary file" target.write (makeso) target.close() suffix = commands.getoutput ('make -f %s' % name) os.unlink (name) return suffix def createShlibFromArchive (fulltarget, archive, arguments=''): dllname = os.path.basename (fulltarget) makeso = 'include ${ROOTSYS}/test/Makefile.arch\n\nall:\n\t$(LD) $(LDFLAGS) $(wildcard *.o) $(EXPLLINKLIBS) %s -shared -o %s\n' % \ (arguments, dllname) tempdir = tempfile.mkdtemp (dir='.') os.chdir (tempdir) os.system ('ar -x .%s' % archive) make = open ('Makefile', 'w') make.write (makeso) make.close() os.system ('make') os.chdir ('..') shutil.copy ('%s/%s' % (tempdir, dllname), fulltarget) os.chmod (fulltarget, 0755) if __name__ == "__main__": # Setup options parser parser = optparse.OptionParser \ ("usage: %prog [options] ") parser.add_option ('--dest', dest='dest', type='string', default='.', help = 'destination of shlibs (default: %default)') parser.add_option ('--force', dest='force', action='store_true', help = 'Force copy even if target already exists') parser.add_option ('--print', dest='printSO', action='store_true', help = 'Print out all shlibs and exit') parser.add_option ('--printAll', dest='printAll', action='store_true', help = 'Print out all shlibs and archives and exit') options, args = parser.parse_args() colonRE = re.compile (r':') suffix = getSuffix() #print "suffix", suffix soList = \ commands.getoutput ('find . -name \*.%s -print' % suffix).split('\n') aList = None if options.printAll: pprint.pprint (soList) aList = commands.getoutput ('find . -name \*.a -print').split('\n') pprint.pprint (aList) if options.printSO: pprint.pprint (soList) sys.exit for command in args: pieces = colonRE.split (command) target = "lib%s.%s" % (pieces[0], suffix) match = r"\b%s\b" % target fulltarget = '%s/%s' % (options.dest, target) if os.path.exists (fulltarget) and not options.force: print " - Target '%s' already exists. Skipping." % target continue if len (pieces) > 1 and pieces[1]: match = pieces[1] regex = re.compile (match) found = None for so in soList: if regex.search (so): found = so break if found: shutil.copy (found, fulltarget) os.chmod (fulltarget, 0755) print "Found and copied %s" % found continue if aList is None: aList = commands.getoutput ('find . -name \*.a -print').split('\n') if len (pieces) < 2 or not pieces[1]: match = r"\blib%s.a\b" % pieces[0] regex = re.compile (match) found = None for archive in aList: if regex.search (archive): found = archive break if not found: print "** Warning: %s not found. **" % target else: print "Creating from %s" % found arguments = '' if len (pieces) > 2: arguments = pieces[2] createShlibFromArchive (fulltarget, found, arguments)