Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 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 | 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) |
| 51 | files.append(w) |
| 52 | return files |
| 53 | |
| 54 | cc_flags = ['-I', '-D', '-U'] |
| 55 | cc_exts = ['.c', '.C', '.cc', '.c++'] |
| 56 | |
| 57 | def treatword(w): |
| 58 | if w[:2] in cc_flags: |
| 59 | return None |
| 60 | if w[:1] == '-': |
| 61 | return w # Assume loader flag |
| 62 | head, tail = os.path.split(w) |
| 63 | base, ext = os.path.splitext(tail) |
| 64 | if ext in cc_exts: |
| 65 | tail = base + '.o' |
| 66 | w = os.path.join(head, tail) |
| 67 | return w |
| 68 | |
| 69 | def expandvars(str, vars): |
| 70 | i = 0 |
| 71 | while i < len(str): |
| 72 | i = k = string.find(str, '$', i) |
| 73 | if i < 0: |
| 74 | break |
| 75 | i = i+1 |
| 76 | var = str[i:i+1] |
| 77 | i = i+1 |
| 78 | if var == '(': |
| 79 | j = string.find(str, ')', i) |
| 80 | if j < 0: |
| 81 | break |
| 82 | var = str[i:j] |
| 83 | i = j+1 |
| 84 | if vars.has_key(var): |
| 85 | str = str[:k] + vars[var] + str[i:] |
| 86 | i = k |
| 87 | return str |