7 import Task, Utils, Logs
8 from TaskGen import extension
9 from Configure import conf
12 SWIG_EXTS = ['.swig', '.i']
14 swig_str = '${SWIG} ${SWIGFLAGS} ${SRC}'
15 cls = Task.simple_task_type('swig', swig_str, color='BLUE', before='cc cxx')
17 re_module = re.compile('%module(?:\s*\(.*\))?\s+(.+)', re.M)
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)
24 "scan for swig dependencies, climb the .i files"
30 to_see = [self.inputs[0]]
41 code = preproc.re_nl.sub('', code)
42 code = preproc.re_cpp.sub(preproc.repl, code)
44 # find .i files and project headers
45 names = re_2.findall(code) + re_3.findall(code)
47 for d in self.generator.swig_dir_nodes + [node.parent]:
48 u = d.find_resource(n)
53 Logs.warn('could not find %r' % n)
55 # list of nodes this one depends on, and module name if present
57 Logs.debug('deps: deps for %s: %s' % (str(self), str(lst_src)))
61 # provide additional language processing
64 swig_langs[fun.__name__.replace('swig_', '')] = fun
68 tsk.set_outputs(tsk.inputs[0].parent.find_or_declare(tsk.module + '.py'))
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'))
75 def add_swig_paths(self):
76 if getattr(self, 'add_swig_paths_done', None):
78 self.add_swig_paths_done = True
80 self.swig_dir_nodes = []
81 for x in self.to_list(self.includes):
82 node = self.path.find_dir(x)
84 Logs.warn('could not find the include %r' % x)
86 self.swig_dir_nodes.append(node)
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
95 def i_file(self, node):
96 flags = self.to_list(getattr(self, 'swig_flags', []))
98 ext = '.swigwrap_%d.c' % self.idx
102 # the user might specify the module directly
103 module = getattr(self, 'swig_module', None)
105 # else, open the files and search
106 txt = node.read(self.env)
107 m = re_module.search(txt)
109 raise "for now we are expecting a module name in the main swig file"
111 out_node = node.parent.find_or_declare(module + ext)
114 tsk = self.create_task('swig')
116 tsk.set_outputs(out_node)
118 tsk.env['SWIGFLAGS'] = flags
120 if not '-outdir' in flags:
121 flags.append('-outdir')
122 flags.append(node.parent.abspath(self.env))
124 if not '-o' in flags:
126 flags.append(out_node.abspath(self.env))
128 # add the language-specific output files as nodes
129 # call funs in the dict swig_langs
131 # obtain the language
140 self.allnodes.append(out_node)
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)
150 swig_out = Utils.cmd_output('%s -version' % conf.env['SWIG'])
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))
160 conf.env['SWIG_VERSION'] = swigver_full
161 minver_str = '.'.join(map(str, minver))
163 conf.check_message_custom('swig version', '', swigver_full)
165 conf.check_message('swig version', '>= %s' % (minver_str,), result, option=swigver_full)
169 swig = conf.find_program('swig', var='SWIG', mandatory=True)