Package mbuild :: Module arar
[frames] | no frames]

Source Code for Module mbuild.arar

 1  #!/usr/bin/env python 
 2  # -*- python -*- 
 3  # Repackage a bunch of static libs as one big static library. 
 4  #BEGIN_LEGAL 
 5  # 
 6  #Copyright (c) 2016 Intel Corporation 
 7  # 
 8  #  Licensed under the Apache License, Version 2.0 (the "License"); 
 9  #  you may not use this file except in compliance with the License. 
10  #  You may obtain a copy of the License at 
11  # 
12  #      http://www.apache.org/licenses/LICENSE-2.0 
13  # 
14  #  Unless required by applicable law or agreed to in writing, software 
15  #  distributed under the License is distributed on an "AS IS" BASIS, 
16  #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  #  See the License for the specific language governing permissions and 
18  #  limitations under the License. 
19  #   
20  #END_LEGAL 
21  import os 
22  import sys 
23  import shutil 
24  import re 
25   
26 -class arar_error(Exception):
27 - def __init__(self, value):
28 self.value = value
29 - def _str__(self):
30 return repr(self.value)
31
32 -def repack(files, ar='ar', target='liball.a', verbose=False):
33 """For linux only. Repackage the list of files using ar as the 34 archiver program. The input files list can contain .a or .o 35 files. The output library name is supplied by the target keyword 36 argument. This will raise an exception arar_error in the event of 37 a problem, setting the exception value field with an explanation.""" 38 import glob 39 pid= os.getpid() 40 #error=os.system(ar + " --version") 41 tdir = 'tmp.arar.%d' % (pid) 42 if os.path.exists(tdir): 43 raise arar_error('Conflict with existing temporary directory: %s' % \ 44 (tdir)) 45 os.mkdir(tdir) 46 # work in a temporary subdirectory 47 os.chdir(tdir) 48 doto = [] 49 for arg in files: 50 if re.search(r'[.]o$', arg): 51 if arg[0] == '/': 52 doto.append(arg) 53 else: 54 doto.append(os.path.join('..',arg)) 55 continue 56 if arg[0] == '/': 57 cmd = "%s x %s" % (ar,arg) 58 else: 59 cmd = "%s x ../%s" % (ar,arg) 60 if verbose: 61 print "EXTRACTING %s" % (cmd) 62 error= os.system(cmd) 63 if error: 64 raise arar_error('Extract failed for command %s' % (cmd)) 65 files = glob.glob('*.o') + doto 66 local_target = os.path.basename(target) 67 cmd = "%s rcv %s %s" % (ar, local_target, " ".join(files)) 68 if verbose: 69 print "RECOMBINING %s" % (cmd) 70 error=os.system(cmd) 71 if error: 72 raise arar_error('Recombine failed') 73 74 os.chdir('..') 75 os.rename(os.path.join(tdir,local_target), target) 76 if verbose: 77 print "CREATED %s" % (target) 78 shutil.rmtree(tdir)
79