blob: 8d597bfae96796edbd3a4345b9cb12651099c6ad [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)
Guido van Rossum4a114311998-05-06 14:38:30 +000047 for w in string.split(w):
48 if skipofiles and w[-2:] == '.o':
49 continue
Guido van Rossumce769511999-06-23 21:37:57 +000050 # Assume $var expands to absolute pathname
51 if w[0] not in ('-', '$') and w[-2:] in ('.o', '.a'):
Guido van Rossum4a114311998-05-06 14:38:30 +000052 w = os.path.join(e, w)
Guido van Rossumce769511999-06-23 21:37:57 +000053 if w[:2] in ('-L', '-R') and w[2:3] != '$':
Guido van Rossum4a114311998-05-06 14:38:30 +000054 w = w[:2] + os.path.join(e, w[2:])
55 files.append(w)
Guido van Rossumd8336c21994-10-05 16:13:01 +000056 return files
57
58cc_flags = ['-I', '-D', '-U']
59cc_exts = ['.c', '.C', '.cc', '.c++']
60
61def 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
73def 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