[000b090] | 1 | #! /usr/bin/env python |
---|
| 2 | # encoding: UTF-8 |
---|
| 3 | # Petar Forai |
---|
| 4 | # Thomas Nagy 2008 |
---|
| 5 | |
---|
| 6 | import re |
---|
| 7 | import Task, Utils, Logs |
---|
| 8 | from TaskGen import extension |
---|
| 9 | from Configure import conf |
---|
| 10 | import preproc |
---|
| 11 | |
---|
| 12 | SWIG_EXTS = ['.swig', '.i'] |
---|
| 13 | |
---|
| 14 | swig_str = '${SWIG} ${SWIGFLAGS} ${SRC}' |
---|
| 15 | cls = Task.simple_task_type('swig', swig_str, color='BLUE', before='cc cxx') |
---|
| 16 | |
---|
| 17 | re_module = re.compile('%module(?:\s*\(.*\))?\s+(.+)', re.M) |
---|
| 18 | |
---|
| 19 | re_1 = re.compile(r'^%module.*?\s+([\w]+)\s*?$', re.M) |
---|
| 20 | re_2 = re.compile('%include "(.*)"', re.M) |
---|
| 21 | re_3 = re.compile('#include "(.*)"', re.M) |
---|
| 22 | |
---|
| 23 | def scan(self): |
---|
| 24 | "scan for swig dependencies, climb the .i files" |
---|
| 25 | env = self.env |
---|
| 26 | |
---|
| 27 | lst_src = [] |
---|
| 28 | |
---|
| 29 | seen = [] |
---|
| 30 | to_see = [self.inputs[0]] |
---|
| 31 | |
---|
| 32 | while to_see: |
---|
| 33 | node = to_see.pop(0) |
---|
| 34 | if node.id in seen: |
---|
| 35 | continue |
---|
| 36 | seen.append(node.id) |
---|
| 37 | lst_src.append(node) |
---|
| 38 | |
---|
| 39 | # read the file |
---|
| 40 | code = node.read(env) |
---|
| 41 | code = preproc.re_nl.sub('', code) |
---|
| 42 | code = preproc.re_cpp.sub(preproc.repl, code) |
---|
| 43 | |
---|
| 44 | # find .i files and project headers |
---|
| 45 | names = re_2.findall(code) + re_3.findall(code) |
---|
| 46 | for n in names: |
---|
| 47 | for d in self.generator.swig_dir_nodes + [node.parent]: |
---|
| 48 | u = d.find_resource(n) |
---|
| 49 | if u: |
---|
| 50 | to_see.append(u) |
---|
| 51 | break |
---|
| 52 | else: |
---|
| 53 | Logs.warn('could not find %r' % n) |
---|
| 54 | |
---|
| 55 | # list of nodes this one depends on, and module name if present |
---|
| 56 | if Logs.verbose: |
---|
| 57 | Logs.debug('deps: deps for %s: %s' % (str(self), str(lst_src))) |
---|
| 58 | return (lst_src, []) |
---|
| 59 | cls.scan = scan |
---|
| 60 | |
---|
| 61 | # provide additional language processing |
---|
| 62 | swig_langs = {} |
---|
| 63 | def swig(fun): |
---|
| 64 | swig_langs[fun.__name__.replace('swig_', '')] = fun |
---|
| 65 | |
---|
| 66 | @swig |
---|
| 67 | def swig_python(tsk): |
---|
| 68 | tsk.set_outputs(tsk.inputs[0].parent.find_or_declare(tsk.module + '.py')) |
---|
| 69 | |
---|
| 70 | @swig |
---|
| 71 | def swig_ocaml(tsk): |
---|
| 72 | tsk.set_outputs(tsk.inputs[0].parent.find_or_declare(tsk.module + '.ml')) |
---|
| 73 | tsk.set_outputs(tsk.inputs[0].parent.find_or_declare(tsk.module + '.mli')) |
---|
| 74 | |
---|
| 75 | def add_swig_paths(self): |
---|
| 76 | if getattr(self, 'add_swig_paths_done', None): |
---|
| 77 | return |
---|
| 78 | self.add_swig_paths_done = True |
---|
| 79 | |
---|
| 80 | self.swig_dir_nodes = [] |
---|
| 81 | for x in self.to_list(self.includes): |
---|
| 82 | node = self.path.find_dir(x) |
---|
| 83 | if not node: |
---|
| 84 | Logs.warn('could not find the include %r' % x) |
---|
| 85 | continue |
---|
| 86 | self.swig_dir_nodes.append(node) |
---|
| 87 | |
---|
| 88 | # add the top-level, it is likely to be added |
---|
| 89 | self.swig_dir_nodes.append(self.bld.srcnode) |
---|
| 90 | for x in self.swig_dir_nodes: |
---|
| 91 | self.env.append_unique('SWIGFLAGS', '-I%s' % x.abspath(self.env)) # build dir |
---|
| 92 | self.env.append_unique('SWIGFLAGS', '-I%s' % x.abspath()) # source dir |
---|
| 93 | |
---|
| 94 | @extension(SWIG_EXTS) |
---|
| 95 | def i_file(self, node): |
---|
| 96 | flags = self.to_list(getattr(self, 'swig_flags', [])) |
---|
| 97 | |
---|
| 98 | ext = '.swigwrap_%d.c' % self.idx |
---|
| 99 | if '-c++' in flags: |
---|
| 100 | ext += 'xx' |
---|
| 101 | |
---|
| 102 | # the user might specify the module directly |
---|
| 103 | module = getattr(self, 'swig_module', None) |
---|
| 104 | if not module: |
---|
| 105 | # else, open the files and search |
---|
| 106 | txt = node.read(self.env) |
---|
| 107 | m = re_module.search(txt) |
---|
| 108 | if not m: |
---|
| 109 | raise "for now we are expecting a module name in the main swig file" |
---|
| 110 | module = m.group(1) |
---|
| 111 | out_node = node.parent.find_or_declare(module + ext) |
---|
| 112 | |
---|
| 113 | # the task instance |
---|
| 114 | tsk = self.create_task('swig') |
---|
| 115 | tsk.set_inputs(node) |
---|
| 116 | tsk.set_outputs(out_node) |
---|
| 117 | tsk.module = module |
---|
| 118 | tsk.env['SWIGFLAGS'] = flags |
---|
| 119 | |
---|
| 120 | if not '-outdir' in flags: |
---|
| 121 | flags.append('-outdir') |
---|
| 122 | flags.append(node.parent.abspath(self.env)) |
---|
| 123 | |
---|
| 124 | if not '-o' in flags: |
---|
| 125 | flags.append('-o') |
---|
| 126 | flags.append(out_node.abspath(self.env)) |
---|
| 127 | |
---|
| 128 | # add the language-specific output files as nodes |
---|
| 129 | # call funs in the dict swig_langs |
---|
| 130 | for x in flags: |
---|
| 131 | # obtain the language |
---|
| 132 | x = x[1:] |
---|
| 133 | try: |
---|
| 134 | fun = swig_langs[x] |
---|
| 135 | except KeyError: |
---|
| 136 | pass |
---|
| 137 | else: |
---|
| 138 | fun(tsk) |
---|
| 139 | |
---|
| 140 | self.allnodes.append(out_node) |
---|
| 141 | |
---|
| 142 | add_swig_paths(self) |
---|
| 143 | |
---|
| 144 | @conf |
---|
| 145 | def check_swig_version(conf, minver=None): |
---|
| 146 | """Check for a minimum swig version like conf.check_swig_version('1.3.28') |
---|
| 147 | or conf.check_swig_version((1,3,28)) """ |
---|
| 148 | reg_swig = re.compile(r'SWIG Version\s(.*)', re.M) |
---|
| 149 | |
---|
| 150 | swig_out = Utils.cmd_output('%s -version' % conf.env['SWIG']) |
---|
| 151 | |
---|
| 152 | swigver = [int(s) for s in reg_swig.findall(swig_out)[0].split('.')] |
---|
| 153 | if isinstance(minver, basestring): |
---|
| 154 | minver = [int(s) for s in minver.split(".")] |
---|
| 155 | if isinstance(minver, tuple): |
---|
| 156 | minver = [int(s) for s in minver] |
---|
| 157 | result = (minver is None) or (minver[:3] <= swigver[:3]) |
---|
| 158 | swigver_full = '.'.join(map(str, swigver)) |
---|
| 159 | if result: |
---|
| 160 | conf.env['SWIG_VERSION'] = swigver_full |
---|
| 161 | minver_str = '.'.join(map(str, minver)) |
---|
| 162 | if minver is None: |
---|
| 163 | conf.check_message_custom('swig version', '', swigver_full) |
---|
| 164 | else: |
---|
| 165 | conf.check_message('swig version', '>= %s' % (minver_str,), result, option=swigver_full) |
---|
| 166 | return result |
---|
| 167 | |
---|
| 168 | def detect(conf): |
---|
| 169 | swig = conf.find_program('swig', var='SWIG', mandatory=True) |
---|
| 170 | |
---|