3 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file
5 import os,shlex,sys,time
6 from waflib import ConfigSet,Utils,Options,Logs,Context,Build,Errors
8 from urllib import request
10 from urllib import urlopen
12 urlopen=request.urlopen
15 WAF_CONFIG_LOG='config.log'
17 conf_template='''# project %(app)s configured on %(now)s by
18 # waf %(wafver)s (abi %(abi)s, python %(pyver)x on %(systype)s)
21 def download_check(node):
23 def download_tool(tool,force=False,ctx=None):
24 for x in Utils.to_list(Context.remote_repo):
25 for sub in Utils.to_list(Context.remote_locs):
26 url='/'.join((x,sub,tool+'.py'))
30 if web.getcode()!=200:
32 except AttributeError:
37 tmp=ctx.root.make_node(os.sep.join((Context.waf_dir,'waflib','extras',tool+'.py')))
38 tmp.write(web.read(),'wb')
39 Logs.warn('Downloaded %s from %s'%(tool,url))
42 module=Context.load_tool(tool)
44 Logs.warn('The tool %s from %s is unusable'%(tool,url))
51 raise Errors.WafError('Could not load the Waf tool')
52 class ConfigurationContext(Context.Context):
53 '''configures the project'''
56 def __init__(self,**kw):
57 super(ConfigurationContext,self).__init__(**kw)
58 self.environ=dict(os.environ)
67 def setenv(self,name,env=None):
68 if name not in self.all_envs or env:
70 env=ConfigSet.ConfigSet()
74 self.all_envs[name]=env
77 return self.all_envs[self.variant]
78 def set_env(self,val):
79 self.all_envs[self.variant]=val
80 env=property(get_env,set_env)
84 top=Options.options.top
86 top=getattr(Context.g_module,Context.TOP,None)
88 top=self.path.abspath()
89 top=os.path.abspath(top)
90 self.srcnode=(os.path.isabs(top)and self.root or self.path).find_dir(top)
94 out=Options.options.out
96 out=getattr(Context.g_module,Context.OUT,None)
98 out=Options.lockfile.replace('.lock-waf_%s_'%sys.platform,'').replace('.lock-waf','')
99 self.bldnode=(os.path.isabs(out)and self.root or self.path).make_node(out)
101 if not os.path.isdir(self.bldnode.abspath()):
102 conf.fatal('Could not create the build directory %s'%self.bldnode.abspath())
105 self.cachedir=self.bldnode.make_node(Build.CACHE_DIR)
106 self.cachedir.mkdir()
107 path=os.path.join(self.bldnode.abspath(),WAF_CONFIG_LOG)
108 self.logger=Logs.make_logger(path,'cfg')
109 app=getattr(Context.g_module,'APPNAME','')
111 ver=getattr(Context.g_module,'VERSION','')
113 app="%s (%s)"%(app,ver)
117 args=" ".join(sys.argv)
118 wafver=Context.WAFVERSION
120 self.to_log(conf_template%vars())
121 self.msg('Setting top to',self.srcnode.abspath())
122 self.msg('Setting out to',self.bldnode.abspath())
123 if id(self.srcnode)==id(self.bldnode):
124 Logs.warn('Setting top == out (remember to use "update_outputs")')
125 elif id(self.path)!=id(self.srcnode):
126 if self.srcnode.is_child_of(self.path):
127 Logs.warn('Are you certain that you do not want to set top="." ?')
128 super(ConfigurationContext,self).execute()
130 Context.top_dir=self.srcnode.abspath()
131 Context.out_dir=self.bldnode.abspath()
132 env=ConfigSet.ConfigSet()
134 env['options']=Options.options.__dict__
135 env.run_dir=Context.run_dir
136 env.top_dir=Context.top_dir
137 env.out_dir=Context.out_dir
138 env['hash']=self.hash
139 env['files']=self.files
140 env['environ']=dict(self.environ)
141 if not self.env.NO_LOCK_IN_RUN:
142 env.store(Context.run_dir+os.sep+Options.lockfile)
143 if not self.env.NO_LOCK_IN_TOP:
144 env.store(Context.top_dir+os.sep+Options.lockfile)
145 if not self.env.NO_LOCK_IN_OUT:
146 env.store(Context.out_dir+os.sep+Options.lockfile)
147 def prepare_env(self,env):
149 if Options.options.prefix or Utils.is_win32:
150 env.PREFIX=os.path.abspath(os.path.expanduser(Options.options.prefix))
154 env.BINDIR=Utils.subst_vars('${PREFIX}/bin',env)
156 env.LIBDIR=Utils.subst_vars('${PREFIX}/lib',env)
158 n=self.cachedir.make_node('build.config.py')
159 n.write('version = 0x%x\ntools = %r\n'%(Context.HEXVERSION,self.tools))
160 if not self.all_envs:
161 self.fatal('nothing to store in the configuration context!')
162 for key in self.all_envs:
163 tmpenv=self.all_envs[key]
164 tmpenv.store(os.path.join(self.cachedir.abspath(),key+Build.CACHE_SUFFIX))
165 def load(self,input,tooldir=None,funs=None,download=True):
166 tools=Utils.to_list(input)
167 if tooldir:tooldir=Utils.to_list(tooldir)
169 mag=(tool,id(self.env),funs)
170 if mag in self.tool_cache:
171 self.to_log('(tool %s is already loaded, skipping)'%tool)
173 self.tool_cache.append(mag)
176 module=Context.load_tool(tool,tooldir)
177 except ImportError ,e:
178 if Options.options.download:
179 module=download_tool(tool,ctx=self)
181 self.fatal('Could not load the Waf tool %r or download a suitable replacement from the repository (sys.path %r)\n%s'%(tool,sys.path,e))
183 self.fatal('Could not load the Waf tool %r from %r (try the --download option?):\n%s'%(tool,sys.path,e))
185 self.to_log('imp %r (%r & %r)'%(tool,tooldir,funs))
186 self.to_log(Utils.ex_stack())
189 self.eval_rules(funs)
191 func=getattr(module,'configure',None)
193 if type(func)is type(Utils.readf):func(self)
194 else:self.eval_rules(func)
195 self.tools.append({'tool':tool,'tooldir':tooldir,'funs':funs})
196 def post_recurse(self,node):
197 super(ConfigurationContext,self).post_recurse(node)
198 self.hash=hash((self.hash,node.read('rb')))
199 self.files.append(node.abspath())
200 def eval_rules(self,rules):
201 self.rules=Utils.to_list(rules)
204 if not f:self.fatal("No such method '%s'."%x)
208 ret=self.err_handler(x,e)
215 def err_handler(self,fun,error):
221 mandatory=kw['mandatory']
225 except Errors.ConfigurationError:
228 setattr(ConfigurationContext,f.__name__,fun)
229 setattr(Build.BuildContext,f.__name__,fun)
232 def add_os_flags(self,var,dest=None):
233 try:self.env.append_value(dest or var,shlex.split(self.environ[var]))
236 def cmd_to_list(self,cmd):
237 if isinstance(cmd,str)and cmd.find(' '):
241 return shlex.split(cmd)
246 def check_waf_version(self,mini='1.6.99',maxi='1.8.0'):
247 self.start_msg('Checking for waf version in %s-%s'%(str(mini),str(maxi)))
248 ver=Context.HEXVERSION
249 if Utils.num2ver(mini)>ver:
250 self.fatal('waf version should be at least %r (%r found)'%(Utils.num2ver(mini),ver))
251 if Utils.num2ver(maxi)<ver:
252 self.fatal('waf version should be at most %r (%r found)'%(Utils.num2ver(maxi),ver))
255 def find_file(self,filename,path_list=[]):
256 for n in Utils.to_list(filename):
257 for d in Utils.to_list(path_list):
259 if os.path.exists(p):
261 self.fatal('Could not find %r'%filename)
263 def find_program(self,filename,**kw):
264 exts=kw.get('exts',Utils.is_win32 and'.exe,.com,.bat,.cmd'or',.sh,.pl,.py')
265 environ=kw.get('environ',os.environ)
267 filename=Utils.to_list(filename)
270 var=filename[0].upper()
275 path_list=kw.get('path_list','')
278 path_list=Utils.to_list(path_list)
280 path_list=environ.get('PATH','').split(os.pathsep)
281 if not isinstance(filename,list):
283 for a in exts.split(','):
292 x=os.path.expanduser(os.path.join(c,b+a))
293 if os.path.isfile(x):
295 if not ret and Utils.winreg:
296 ret=Utils.get_registry_app_path(Utils.winreg.HKEY_CURRENT_USER,filename)
297 if not ret and Utils.winreg:
298 ret=Utils.get_registry_app_path(Utils.winreg.HKEY_LOCAL_MACHINE,filename)
299 self.msg('Checking for program '+','.join(filename),ret or False)
300 self.to_log('find program=%r paths=%r var=%r -> %r'%(filename,path_list,var,ret))
302 self.fatal(kw.get('errmsg','')or'Could not find the program %s'%','.join(filename))
307 def find_perl_program(self,filename,path_list=[],var=None,environ=None,exts=''):
309 app=self.find_program(filename,path_list=path_list,var=var,environ=environ,exts=exts)
311 self.find_program('perl',var='PERL')
312 app=self.find_file(filename,os.environ['PATH'].split(os.pathsep))
316 self.env[var]=Utils.to_list(self.env['PERL'])+[app]
317 self.msg('Checking for %r'%filename,app)