Initial revision
[blender.git] / intern / python / freeze / checkextensions.py
1 # Check for a module in a set of extension directories.
2 # An extension directory should contain a Setup file
3 # and one or more .o files or a lib.a file.
4
5 import os
6 import string
7 import parsesetup
8
9 def checkextensions(unknown, extensions):
10         files = []
11         modules = []
12         edict = {}
13         for e in extensions:
14                 setup = os.path.join(e, 'Setup')
15                 liba = os.path.join(e, 'lib.a')
16                 if not os.path.isfile(liba):
17                         liba = None
18                 edict[e] = parsesetup.getsetupinfo(setup), liba
19         for mod in unknown:
20                 for e in extensions:
21                         (mods, vars), liba = edict[e]
22                         if not mods.has_key(mod):
23                                 continue
24                         modules.append(mod)
25                         if liba:
26                                 # If we find a lib.a, use it, ignore the
27                                 # .o files, and use *all* libraries for
28                                 # *all* modules in the Setup file
29                                 if liba in files:
30                                         break
31                                 files.append(liba)
32                                 for m in mods.keys():
33                                         files = files + select(e, mods, vars,
34                                                                m, 1)
35                                 break
36                         files = files + select(e, mods, vars, mod, 0)
37                         break
38         return files, modules
39
40 def select(e, mods, vars, mod, skipofiles):
41         files = []
42         for w in mods[mod]:
43                 w = treatword(w)
44                 if not w:
45                         continue
46                 w = expandvars(w, vars)
47                 for w in string.split(w):
48                         if skipofiles and w[-2:] == '.o':
49                                 continue
50                         # Assume $var expands to absolute pathname
51                         if w[0] not in ('-', '$') and w[-2:] in ('.o', '.a'):
52                                 w = os.path.join(e, w)
53                         if w[:2] in ('-L', '-R') and w[2:3] != '$':
54                                 w = w[:2] + os.path.join(e, w[2:])
55                         files.append(w)
56         return files
57
58 cc_flags = ['-I', '-D', '-U']
59 cc_exts = ['.c', '.C', '.cc', '.c++']
60
61 def treatword(w):
62         if w[:2] in cc_flags:
63                 return None
64         if w[:1] == '-':
65                 return w # Assume loader flag
66         head, tail = os.path.split(w)
67         base, ext = os.path.splitext(tail)
68         if ext in cc_exts:
69                 tail = base + '.o'
70                 w = os.path.join(head, tail)
71         return w
72
73 def expandvars(str, vars):
74         i = 0
75         while i < len(str):
76                 i = k = string.find(str, '$', i)
77                 if i < 0:
78                         break
79                 i = i+1
80                 var = str[i:i+1]
81                 i = i+1
82                 if var == '(':
83                         j = string.find(str, ')', i)
84                         if j < 0:
85                                 break
86                         var = str[i:j]
87                         i = j+1
88                 if vars.has_key(var):
89                         str = str[:k] + vars[var] + str[i:]
90                         i = k
91         return str