3 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file
6 from waflib import Utils,Options,Errors,Logs
7 from waflib.TaskGen import extension,before_method,after_method,feature
8 from waflib.Configure import conf
14 void Py_Initialize(void);
15 void Py_Finalize(void);
19 int main(int argc, char **argv)
21 (void)argc; (void)argv;
28 import sys, py_compile
29 py_compile.compile(sys.argv[1], sys.argv[2], sys.argv[3])
31 DISTUTILS_IMP=['from distutils.sysconfig import get_config_var, get_python_lib']
33 def process_py(self,node):
35 if not self.bld.is_install:
37 except AttributeError:
40 if not self.install_path:
42 except AttributeError:
43 self.install_path='${PYTHONDIR}'
45 install_from=getattr(self,'install_from',None)
47 install_from=self.path.find_dir(install_from)
48 install_pyfile(self,node,install_from)
49 self.bld.add_post_fun(inst_py)
50 def install_pyfile(self,node,install_from=None):
51 from_node=install_from or node.parent
52 tsk=self.bld.install_as(self.install_path+'/'+node.path_from(from_node),node,postpone=False)
53 path=tsk.get_install_path()
54 if self.bld.is_install<0:
55 Logs.info("+ removing byte compiled python files")
61 if self.bld.is_install>0:
65 Logs.error('The python file is missing, this should not happen')
67 do_inst=self.env['PY'+x.upper()]
73 if st1.st_mtime<=st2.st_mtime:
76 lst=(x=='o')and[self.env['PYFLAGS_OPT']]or[]
77 (a,b,c)=(path,path+x,tsk.get_install_path(destdir=False)+x)
78 argv=self.env['PYTHON']+lst+['-c',INST,a,b,c]
79 Logs.info('+ byte compiling %r'%(path+x))
80 env=self.env.env or None
81 ret=Utils.subprocess.Popen(argv,env=env).wait()
83 raise Errors.WafError('py%s compilation failed %r'%(x,path))
88 @before_method('propagate_uselib_vars','apply_link')
89 @after_method('apply_bundle')
91 self.uselib=self.to_list(getattr(self,'uselib',[]))
92 if not'PYEXT'in self.uselib:
93 self.uselib.append('PYEXT')
94 self.env.cshlib_PATTERN=self.env.cxxshlib_PATTERN=self.env.macbundle_PATTERN=self.env.pyext_PATTERN
95 self.env.fcshlib_PATTERN=self.env.dshlib_PATTERN=self.env.pyext_PATTERN
97 if not self.install_path:
99 except AttributeError:
100 self.install_path='${PYTHONARCHDIR}'
102 @before_method('apply_link','apply_bundle')
103 def set_bundle(self):
104 if Utils.unversioned_sys_platform()=='darwin':
106 @before_method('propagate_uselib_vars')
108 def init_pyembed(self):
109 self.uselib=self.to_list(getattr(self,'uselib',[]))
110 if not'PYEMBED'in self.uselib:
111 self.uselib.append('PYEMBED')
113 def get_python_variables(self,variables,imports=None):
116 imports=self.python_imports
117 except AttributeError:
118 imports=DISTUTILS_IMP
119 program=list(imports)
122 program.append("print(repr(%s))"%v)
123 os_env=dict(os.environ)
125 del os_env['MACOSX_DEPLOYMENT_TARGET']
129 out=self.cmd_and_log(self.env.PYTHON+['-c','\n'.join(program)],env=os_env)
130 except Errors.WafError:
131 self.fatal('The distutils module is unusable: install "python-devel"?')
134 for s in out.split('\n'):
139 return_values.append(None)
140 elif(s[0]=="'"and s[-1]=="'")or(s[0]=='"'and s[-1]=='"'):
141 return_values.append(eval(s))
143 return_values.append(int(s))
147 def check_python_headers(conf):
149 if not env['CC_NAME']and not env['CXX_NAME']:
150 conf.fatal('load a compiler first (gcc, g++, ..)')
151 if not env['PYTHON_VERSION']:
152 conf.check_python_version()
153 pybin=conf.env.PYTHON
155 conf.fatal('Could not find the python executable')
156 v='prefix SO LDFLAGS LIBDIR LIBPL INCLUDEPY Py_ENABLE_SHARED MACOSX_DEPLOYMENT_TARGET LDSHARED CFLAGS'.split()
158 lst=conf.get_python_variables(["get_config_var('%s') or ''"%x for x in v])
160 conf.fatal("Python development headers not found (-v for details).")
161 vals=['%s = %r'%(x,y)for(x,y)in zip(v,lst)]
162 conf.to_log("Configuration returned from %r:\n%r\n"%(pybin,'\n'.join(vals)))
164 x='MACOSX_DEPLOYMENT_TARGET'
166 conf.env[x]=conf.environ[x]=dct[x]
167 env['pyext_PATTERN']='%s'+dct['SO']
168 all_flags=dct['LDFLAGS']+' '+dct['CFLAGS']
169 conf.parse_flags(all_flags,'PYEMBED')
170 all_flags=dct['LDFLAGS']+' '+dct['LDSHARED']+' '+dct['CFLAGS']
171 conf.parse_flags(all_flags,'PYEXT')
173 for name in('python'+env['PYTHON_VERSION'],'python'+env['PYTHON_VERSION']+'m','python'+env['PYTHON_VERSION'].replace('.','')):
174 if not result and env['LIBPATH_PYEMBED']:
175 path=env['LIBPATH_PYEMBED']
176 conf.to_log("\n\n# Trying default LIBPATH_PYEMBED: %r\n"%path)
177 result=conf.check(lib=name,uselib='PYEMBED',libpath=path,mandatory=False,msg='Checking for library %s in LIBPATH_PYEMBED'%name)
178 if not result and dct['LIBDIR']:
180 conf.to_log("\n\n# try again with -L$python_LIBDIR: %r\n"%path)
181 result=conf.check(lib=name,uselib='PYEMBED',libpath=path,mandatory=False,msg='Checking for library %s in LIBDIR'%name)
182 if not result and dct['LIBPL']:
184 conf.to_log("\n\n# try again with -L$python_LIBPL (some systems don't install the python library in $prefix/lib)\n")
185 result=conf.check(lib=name,uselib='PYEMBED',libpath=path,mandatory=False,msg='Checking for library %s in python_LIBPL'%name)
187 path=[os.path.join(dct['prefix'],"libs")]
188 conf.to_log("\n\n# try again with -L$prefix/libs, and pythonXY name rather than pythonX.Y (win32)\n")
189 result=conf.check(lib=name,uselib='PYEMBED',libpath=path,mandatory=False,msg='Checking for library %s in $prefix/libs'%name)
193 env['LIBPATH_PYEMBED']=path
194 env.append_value('LIB_PYEMBED',[name])
196 conf.to_log("\n\n### LIB NOT FOUND\n")
197 if(Utils.is_win32 or sys.platform.startswith('os2')or dct['Py_ENABLE_SHARED']):
198 env['LIBPATH_PYEXT']=env['LIBPATH_PYEMBED']
199 env['LIB_PYEXT']=env['LIB_PYEMBED']
200 num='.'.join(env['PYTHON_VERSION'].split('.')[:2])
201 conf.find_program([''.join(pybin)+'-config','python%s-config'%num,'python-config-%s'%num,'python%sm-config'%num],var='PYTHON_CONFIG',mandatory=False)
203 if conf.env.PYTHON_CONFIG:
204 for incstr in conf.cmd_and_log([conf.env.PYTHON_CONFIG,'--includes']).strip().split():
205 if(incstr.startswith('-I')or incstr.startswith('/I')):
207 if incstr not in includes:
208 includes.append(incstr)
209 conf.to_log("Include path for Python extensions (found via python-config --includes): %r\n"%(includes,))
210 env['INCLUDES_PYEXT']=includes
211 env['INCLUDES_PYEMBED']=includes
213 conf.to_log("Include path for Python extensions ""(found via distutils module): %r\n"%(dct['INCLUDEPY'],))
214 env['INCLUDES_PYEXT']=[dct['INCLUDEPY']]
215 env['INCLUDES_PYEMBED']=[dct['INCLUDEPY']]
216 if env['CC_NAME']=='gcc':
217 env.append_value('CFLAGS_PYEMBED',['-fno-strict-aliasing'])
218 env.append_value('CFLAGS_PYEXT',['-fno-strict-aliasing'])
219 if env['CXX_NAME']=='gcc':
220 env.append_value('CXXFLAGS_PYEMBED',['-fno-strict-aliasing'])
221 env.append_value('CXXFLAGS_PYEXT',['-fno-strict-aliasing'])
222 if env.CC_NAME=="msvc":
223 from distutils.msvccompiler import MSVCCompiler
224 dist_compiler=MSVCCompiler()
225 dist_compiler.initialize()
226 env.append_value('CFLAGS_PYEXT',dist_compiler.compile_options)
227 env.append_value('CXXFLAGS_PYEXT',dist_compiler.compile_options)
228 env.append_value('LINKFLAGS_PYEXT',dist_compiler.ldflags_shared)
230 conf.check(header_name='Python.h',define_name='HAVE_PYTHON_H',uselib='PYEMBED',fragment=FRAG,errmsg=':-(')
231 except conf.errors.ConfigurationError:
232 xx=conf.env.CXX_NAME and'cxx'or'c'
233 flags=['--cflags','--libs','--ldflags']
235 conf.check_cfg(msg='Asking python-config for pyembed %s flags'%f,path=conf.env.PYTHON_CONFIG,package='',uselib_store='PYEMBED',args=[f])
236 conf.check(header_name='Python.h',define_name='HAVE_PYTHON_H',msg='Getting pyembed flags from python-config',fragment=FRAG,errmsg='Could not build a python embedded interpreter',features='%s %sprogram pyembed'%(xx,xx))
238 conf.check_cfg(msg='Asking python-config for pyext %s flags'%f,path=conf.env.PYTHON_CONFIG,package='',uselib_store='PYEXT',args=[f])
239 conf.check(header_name='Python.h',define_name='HAVE_PYTHON_H',msg='Getting pyext flags from python-config',features='%s %sshlib pyext'%(xx,xx),fragment=FRAG,errmsg='Could not build python extensions')
241 def check_python_version(conf,minver=None):
242 assert minver is None or isinstance(minver,tuple)
243 pybin=conf.env['PYTHON']
245 conf.fatal('could not find the python executable')
246 cmd=pybin+['-c','import sys\nfor x in sys.version_info: print(str(x))']
247 Logs.debug('python: Running python command %r'%cmd)
248 lines=conf.cmd_and_log(cmd).split()
249 assert len(lines)==5,"found %i lines, expected 5: %r"%(len(lines),lines)
250 pyver_tuple=(int(lines[0]),int(lines[1]),int(lines[2]),lines[3],int(lines[4]))
251 result=(minver is None)or(pyver_tuple>=minver)
253 pyver='.'.join([str(x)for x in pyver_tuple[:2]])
254 conf.env['PYTHON_VERSION']=pyver
255 if'PYTHONDIR'in conf.environ:
256 pydir=conf.environ['PYTHONDIR']
259 (python_LIBDEST,pydir)=conf.get_python_variables(["get_config_var('LIBDEST') or ''","get_python_lib(standard_lib=0, prefix=%r) or ''"%conf.env['PREFIX']])
262 (pydir,)=conf.get_python_variables(["get_python_lib(standard_lib=0, prefix=%r) or ''"%conf.env['PREFIX']])
263 if python_LIBDEST is None:
264 if conf.env['LIBDIR']:
265 python_LIBDEST=os.path.join(conf.env['LIBDIR'],"python"+pyver)
267 python_LIBDEST=os.path.join(conf.env['PREFIX'],"lib","python"+pyver)
268 if'PYTHONARCHDIR'in conf.environ:
269 pyarchdir=conf.environ['PYTHONARCHDIR']
271 (pyarchdir,)=conf.get_python_variables(["get_python_lib(plat_specific=1, standard_lib=0, prefix=%r) or ''"%conf.env['PREFIX']])
274 if hasattr(conf,'define'):
275 conf.define('PYTHONDIR',pydir)
276 conf.define('PYTHONARCHDIR',pyarchdir)
277 conf.env['PYTHONDIR']=pydir
278 conf.env['PYTHONARCHDIR']=pyarchdir
279 pyver_full='.'.join(map(str,pyver_tuple[:3]))
281 conf.msg('Checking for python version',pyver_full)
283 minver_str='.'.join(map(str,minver))
284 conf.msg('Checking for python version',pyver_tuple,">= %s"%(minver_str,)and'GREEN'or'YELLOW')
286 conf.fatal('The python version is too old, expecting %r'%(minver,))
287 PYTHON_MODULE_TEMPLATE='''
288 import %s as current_module
289 version = getattr(current_module, '__version__', None)
290 if version is not None:
293 print('unknown version')
296 def check_python_module(conf,module_name,condition=''):
297 msg='Python module %s'%module_name
299 msg='%s (%s)'%(msg,condition)
302 ret=conf.cmd_and_log(conf.env['PYTHON']+['-c',PYTHON_MODULE_TEMPLATE%module_name])
305 conf.fatal('Could not find the python module %r'%module_name)
309 if ret=='unknown version':
310 conf.fatal('Could not check the %s version'%module_name)
311 from distutils.version import LooseVersion
313 if isinstance(k[0],int):
314 return LooseVersion('.'.join([str(x)for x in k]))
316 return LooseVersion(k[0])
317 d={'num':num,'ver':LooseVersion(ret)}
318 ev=eval(condition,{},d)
320 conf.fatal('The %s version does not satisfy the requirements'%module_name)
322 if ret=='unknown version':
328 conf.find_program('python',var='PYTHON')
329 except conf.errors.ConfigurationError:
330 Logs.warn("could not find a python executable, setting to sys.executable '%s'"%sys.executable)
331 conf.env.PYTHON=sys.executable
332 if conf.env.PYTHON!=sys.executable:
333 Logs.warn("python executable %r differs from system %r"%(conf.env.PYTHON,sys.executable))
334 conf.env.PYTHON=conf.cmd_to_list(conf.env.PYTHON)
336 v['PYCMD']='"import sys, py_compile;py_compile.compile(sys.argv[1], sys.argv[2])"'
338 v['PYFLAGS_OPT']='-O'
339 v['PYC']=getattr(Options.options,'pyc',1)
340 v['PYO']=getattr(Options.options,'pyo',1)
342 opt.add_option('--nopyc',action='store_false',default=1,help='Do not install bytecode compiled .pyc files (configuration) [Default:install]',dest='pyc')
343 opt.add_option('--nopyo',action='store_false',default=1,help='Do not install optimised compiled .pyo files (configuration) [Default:install]',dest='pyo')