blob: 68f7ff893ed3c5b234ea88ac1e55405995843024 [file] [log] [blame]
Guido van Rossumd8336c21994-10-05 16:13:01 +00001# 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
5import os
6import string
7import parsesetup
8
9def 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
40def 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 if skipofiles and w[-2:] == '.o':
48 continue
49 if w[0] != '-' and w[-2:] in ('.o', '.a'):
50 w = os.path.join(e, w)
Guido van Rossumdf194071998-04-23 14:38:46 +000051 if w[:2] in ('-L', '-R'):
52 w = w[:2] + os.path.join(e, w[2:])
Guido van Rossumd8336c21994-10-05 16:13:01 +000053 files.append(w)
54 return files
55
56cc_flags = ['-I', '-D', '-U']
57cc_exts = ['.c', '.C', '.cc', '.c++']
58
59def treatword(w):
60 if w[:2] in cc_flags:
61 return None
62 if w[:1] == '-':
63 return w # Assume loader flag
64 head, tail = os.path.split(w)
65 base, ext = os.path.splitext(tail)
66 if ext in cc_exts:
67 tail = base + '.o'
68 w = os.path.join(head, tail)
69 return w
70
71def expandvars(str, vars):
72 i = 0
73 while i < len(str):
74 i = k = string.find(str, '$', i)
75 if i < 0:
76 break
77 i = i+1
78 var = str[i:i+1]
79 i = i+1
80 if var == '(':
81 j = string.find(str, ')', i)
82 if j < 0:
83 break
84 var = str[i:j]
85 i = j+1
86 if vars.has_key(var):
87 str = str[:k] + vars[var] + str[i:]
88 i = k
89 return str