3 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file
5 import os,sys,errno,re,shutil
9 import pickle as cPickle
10 from waflib import Runner,TaskGen,Utils,ConfigSet,Task,Logs,Options,Context,Errors
13 CACHE_SUFFIX='_cache.py'
16 SAVED_ATTRS='root node_deps raw_deps task_sigs'.split()
21 class BuildContext(Context.Context):
22 '''executes the build'''
25 def __init__(self,**kw):
26 super(BuildContext,self).__init__(**kw)
28 self.top_dir=kw.get('top_dir',Context.top_dir)
29 self.run_dir=kw.get('run_dir',Context.run_dir)
30 self.post_mode=POST_AT_ONCE
31 self.out_dir=kw.get('out_dir',Context.out_dir)
32 self.cache_dir=kw.get('cache_dir',None)
33 if not self.cache_dir:
34 self.cache_dir=self.out_dir+os.sep+CACHE_DIR
39 self.cache_dir_contents={}
40 self.task_gen_cache_names={}
41 self.launch_dir=Context.launch_dir
42 self.jobs=Options.options.jobs
43 self.targets=Options.options.targets
44 self.keep=Options.options.keep
45 self.cache_global=Options.cache_global
46 self.nocache=Options.options.nocache
47 self.progress_bar=Options.options.progress_bar
48 self.deps_man=Utils.defaultdict(list)
52 def get_variant_dir(self):
55 return os.path.join(self.out_dir,self.variant)
56 variant_dir=property(get_variant_dir,None)
57 def __call__(self,*k,**kw):
59 ret=TaskGen.task_gen(*k,**kw)
60 self.task_gen_cache_names={}
61 self.add_to_group(ret,group=kw.get('group',None))
63 def rule(self,*k,**kw):
70 raise Errors.WafError('build contexts are not supposed to be copied')
71 def install_files(self,*k,**kw):
73 def install_as(self,*k,**kw):
75 def symlink_as(self,*k,**kw):
78 node=self.root.find_node(self.cache_dir)
80 raise Errors.WafError('The project was not configured: run "waf configure" first!')
81 lst=node.ant_glob('**/*%s'%CACHE_SUFFIX,quiet=True)
83 raise Errors.WafError('The cache directory is empty: reconfigure the project')
85 name=x.path_from(node).replace(CACHE_SUFFIX,'').replace('\\','/')
86 env=ConfigSet.ConfigSet(x.abspath())
87 self.all_envs[name]=env
88 for f in env[CFG_FILES]:
89 newnode=self.root.find_resource(f)
91 h=Utils.h_file(newnode.abspath())
92 except(IOError,AttributeError):
93 Logs.error('cannot find %r'%f)
97 if not(os.path.isabs(self.top_dir)and os.path.isabs(self.out_dir)):
98 raise Errors.WafError('The project was not configured: run "waf configure" first!')
99 self.path=self.srcnode=self.root.find_dir(self.top_dir)
100 self.bldnode=self.root.make_node(self.variant_dir)
104 if not self.all_envs:
107 def execute_build(self):
108 Logs.info("Waf: Entering directory `%s'"%self.variant_dir)
109 self.recurse([self.run_dir])
111 self.timer=Utils.Timer()
112 if self.progress_bar:
113 sys.stderr.write(Logs.colors.cursor_off)
117 if self.progress_bar==1:
118 c=len(self.returned_tasks)or 1
119 self.to_log(self.progress_line(c,c,Logs.colors.BLUE,Logs.colors.NORMAL))
122 sys.stderr.write(Logs.colors.cursor_on)
123 Logs.info("Waf: Leaving directory `%s'"%self.variant_dir)
127 env=ConfigSet.ConfigSet(os.path.join(self.cache_dir,'build.config.py'))
128 except(IOError,OSError):
131 if env['version']<Context.HEXVERSION:
132 raise Errors.WafError('Version mismatch! reconfigure the project')
133 for t in env['tools']:
137 dbfn=os.path.join(self.variant_dir,Context.DBFILE)
140 except(IOError,EOFError):
141 Logs.debug('build: Could not load the build cache %s (missing)'%dbfn)
144 waflib.Node.pickle_lock.acquire()
145 waflib.Node.Nod3=self.node_class
149 Logs.debug('build: Could not pickle the build cache %s: %r'%(dbfn,e))
151 for x in SAVED_ATTRS:
152 setattr(self,x,data[x])
154 waflib.Node.pickle_lock.release()
161 for x in SAVED_ATTRS:
162 data[x]=getattr(self,x)
163 db=os.path.join(self.variant_dir,Context.DBFILE)
165 waflib.Node.pickle_lock.acquire()
166 waflib.Node.Nod3=self.node_class
169 f=open(db+'.tmp','wb')
170 cPickle.dump(data,f,-1)
175 waflib.Node.pickle_lock.release()
179 if not Utils.is_win32:
180 os.chown(db+'.tmp',st.st_uid,st.st_gid)
181 except(AttributeError,OSError):
183 os.rename(db+'.tmp',db)
185 Logs.debug('build: compile()')
186 self.producer=Runner.Parallel(self,self.jobs)
187 self.producer.biter=self.get_build_iterator()
188 self.returned_tasks=[]
190 self.producer.start()
191 except KeyboardInterrupt:
195 if self.producer.dirty:
197 if self.producer.error:
198 raise Errors.BuildError(self.producer.error)
199 def setup(self,tool,tooldir=None,funs=None):
200 if isinstance(tool,list):
201 for i in tool:self.setup(i,tooldir)
203 module=Context.load_tool(tool,tooldir)
204 if hasattr(module,"setup"):module.setup(self)
207 return self.all_envs[self.variant]
209 return self.all_envs['']
210 def set_env(self,val):
211 self.all_envs[self.variant]=val
212 env=property(get_env,set_env)
213 def add_manual_dependency(self,path,value):
215 raise ValueError('Invalid input')
216 if isinstance(path,waflib.Node.Node):
218 elif os.path.isabs(path):
219 node=self.root.find_resource(path)
221 node=self.path.find_resource(path)
222 if isinstance(value,list):
223 self.deps_man[id(node)].extend(value)
225 self.deps_man[id(node)].append(value)
226 def launch_node(self):
229 except AttributeError:
230 self.p_ln=self.root.find_dir(self.launch_dir)
232 def hash_env_vars(self,env,vars_lst):
237 idx=str(id(env))+str(vars_lst)
240 except AttributeError:
241 cache=self.cache_env={}
244 return self.cache_env[idx]
247 lst=[env[a]for a in vars_lst]
248 ret=Utils.h_list(lst)
249 Logs.debug('envhash: %s %r',Utils.to_hex(ret),lst)
252 def get_tgen_by_name(self,name):
253 cache=self.task_gen_cache_names
255 for g in self.groups:
259 except AttributeError:
264 raise Errors.WafError('Could not find a task generator for the name %r'%name)
265 def progress_line(self,state,total,col1,col2):
268 ind=Utils.rot_chr[Utils.rot_idx%4]
269 pc=(100.*state)/total
271 fs="[%%%dd/%%%dd][%%s%%2d%%%%%%s][%s]["%(n,n,ind)
272 left=fs%(state,total,col1,pc,col2)
273 right='][%s%s%s]'%(col1,eta,col2)
274 cols=Logs.get_term_cols()-len(left)-len(right)+2*len(col1)+2*len(col2)
276 ratio=((cols*state)//total)-1
277 bar=('='*ratio+'>').ljust(cols)
278 msg=Utils.indicator%(left,bar,right)
280 def declare_chain(self,*k,**kw):
281 return TaskGen.declare_chain(*k,**kw)
283 for m in getattr(self,'pre_funs',[]):
285 def post_build(self):
286 for m in getattr(self,'post_funs',[]):
288 def add_pre_fun(self,meth):
290 self.pre_funs.append(meth)
291 except AttributeError:
293 def add_post_fun(self,meth):
295 self.post_funs.append(meth)
296 except AttributeError:
297 self.post_funs=[meth]
298 def get_group(self,x):
302 return self.groups[self.current_group]
303 if x in self.group_names:
304 return self.group_names[x]
305 return self.groups[x]
306 def add_to_group(self,tgen,group=None):
307 assert(isinstance(tgen,TaskGen.task_gen)or isinstance(tgen,Task.TaskBase))
309 self.get_group(group).append(tgen)
310 def get_group_name(self,g):
311 if not isinstance(g,list):
313 for x in self.group_names:
314 if id(self.group_names[x])==id(g):
317 def get_group_idx(self,tg):
319 for i in range(len(self.groups)):
320 for t in self.groups[i]:
324 def add_group(self,name=None,move=True):
325 if name and name in self.group_names:
326 Logs.error('add_group: name %s already present'%name)
328 self.group_names[name]=g
329 self.groups.append(g)
331 self.current_group=len(self.groups)-1
332 def set_group(self,idx):
333 if isinstance(idx,str):
334 g=self.group_names[idx]
335 for i in range(len(self.groups)):
336 if id(g)==id(self.groups[i]):
339 self.current_group=idx
342 for group in self.groups:
346 except AttributeError:
349 def get_targets(self):
352 for name in self.targets.split(','):
353 tg=self.get_tgen_by_name(name)
355 raise Errors.WafError('target %r does not exist'%name)
356 m=self.get_group_idx(tg)
362 return(min_grp,to_post)
363 def get_all_task_gen(self):
365 for g in self.groups:
368 def post_group(self):
369 if self.targets=='*':
370 for tg in self.groups[self.cur]:
373 except AttributeError:
378 if self.cur<self._min_grp:
379 for tg in self.groups[self.cur]:
382 except AttributeError:
387 for tg in self._exact_tg:
390 ln=self.launch_node()
391 if ln.is_child_of(self.bldnode):
392 Logs.warn('Building from the build directory, forcing --targets=*')
394 elif not ln.is_child_of(self.srcnode):
395 Logs.warn('CWD %s is not under %s, forcing --targets=* (run distclean?)'%(ln.abspath(),self.srcnode.abspath()))
397 for tg in self.groups[self.cur]:
400 except AttributeError:
403 if tg.path.is_child_of(ln):
405 def get_tasks_group(self,idx):
407 for tg in self.groups[idx]:
409 tasks.extend(tg.tasks)
410 except AttributeError:
413 def get_build_iterator(self):
415 if self.targets and self.targets!='*':
416 (self._min_grp,self._exact_tg)=self.get_targets()
418 if self.post_mode!=POST_LAZY:
419 while self.cur<len(self.groups):
423 while self.cur<len(self.groups):
424 if self.post_mode!=POST_AT_ONCE:
426 tasks=self.get_tasks_group(self.cur)
427 Task.set_file_constraints(tasks)
428 Task.set_precedence_constraints(tasks)
436 class inst(Task.Task):
439 lst=[self.dest,self.path]+self.source
440 return Utils.h_list(repr(lst))
443 for x in self.source:
444 if isinstance(x,waflib.Node.Node):
447 y=self.path.find_resource(x)
450 Logs.warn('Could not find %s immediately (may cause broken builds)'%x)
451 idx=self.generator.bld.get_group_idx(self)
452 for tg in self.generator.bld.groups[idx]:
453 if not isinstance(tg,inst)and id(tg)!=id(self):
455 y=self.path.find_resource(x)
459 raise Errors.WafError('Could not find %r in %r'%(x,self.path))
462 def runnable_status(self):
463 ret=super(inst,self).runnable_status()
464 if ret==Task.SKIP_ME:
470 return self.generator.exec_task()
471 def get_install_path(self,destdir=True):
472 dest=Utils.subst_vars(self.dest,self.env)
473 dest=dest.replace('/',os.sep)
474 if destdir and Options.options.destdir:
475 dest=os.path.join(Options.options.destdir,os.path.splitdrive(dest)[1].lstrip(os.sep))
477 def exec_install_files(self):
478 destpath=self.get_install_path()
480 raise Errors.WafError('unknown installation path %r'%self.generator)
481 for x,y in zip(self.source,self.inputs):
482 if self.relative_trick:
483 destfile=os.path.join(destpath,y.path_from(self.path))
484 Utils.check_dir(os.path.dirname(destfile))
486 destfile=os.path.join(destpath,y.name)
487 self.generator.bld.do_install(y.abspath(),destfile,self.chmod)
488 def exec_install_as(self):
489 destfile=self.get_install_path()
490 self.generator.bld.do_install(self.inputs[0].abspath(),destfile,self.chmod)
491 def exec_symlink_as(self):
492 destfile=self.get_install_path()
494 if self.relative_trick:
495 src=os.path.relpath(src,os.path.dirname(destfile))
496 self.generator.bld.do_link(src,destfile)
497 class InstallContext(BuildContext):
498 '''installs the targets on the system'''
500 def __init__(self,**kw):
501 super(InstallContext,self).__init__(**kw)
503 self.is_install=INSTALL
504 def do_install(self,src,tgt,chmod=Utils.O644):
505 d,_=os.path.split(tgt)
507 raise Errors.WafError('Invalid installation given %r->%r'%(src,tgt))
509 srclbl=src.replace(self.srcnode.abspath()+os.sep,'')
510 if not Options.options.force:
517 if st1.st_mtime+2>=st2.st_mtime and st1.st_size==st2.st_size:
518 if not self.progress_bar:
519 Logs.info('- install %s (from %s)'%(tgt,srclbl))
521 if not self.progress_bar:
522 Logs.info('+ install %s (from %s)'%(tgt,srclbl))
528 shutil.copy2(src,tgt)
533 except(OSError,IOError):
534 Logs.error('File %r does not exist'%src)
535 raise Errors.WafError('Could not install the file %r'%tgt)
536 def do_link(self,src,tgt):
537 d,_=os.path.split(tgt)
540 if not os.path.islink(tgt):
542 elif os.readlink(tgt)!=src:
547 if not self.progress_bar:
548 Logs.info('+ symlink %s (to %s)'%(tgt,src))
551 if not self.progress_bar:
552 Logs.info('- symlink %s (to %s)'%(tgt,src))
553 def run_task_now(self,tsk,postpone):
556 if tsk.runnable_status()==Task.ASK_LATER:
557 raise self.WafError('cannot post the task %r'%tsk)
559 def install_files(self,dest,files,env=None,chmod=Utils.O644,relative_trick=False,cwd=None,add=True,postpone=True):
560 tsk=inst(env=env or self.env)
562 tsk.path=cwd or self.path
564 if isinstance(files,waflib.Node.Node):
567 tsk.source=Utils.to_list(files)
569 tsk.exec_task=tsk.exec_install_files
570 tsk.relative_trick=relative_trick
571 if add:self.add_to_group(tsk)
572 self.run_task_now(tsk,postpone)
574 def install_as(self,dest,srcfile,env=None,chmod=Utils.O644,cwd=None,add=True,postpone=True):
575 tsk=inst(env=env or self.env)
577 tsk.path=cwd or self.path
581 tsk.exec_task=tsk.exec_install_as
582 if add:self.add_to_group(tsk)
583 self.run_task_now(tsk,postpone)
585 def symlink_as(self,dest,src,env=None,cwd=None,add=True,postpone=True,relative_trick=False):
588 tsk=inst(env=env or self.env)
591 tsk.path=cwd or self.path
594 tsk.relative_trick=relative_trick
595 tsk.exec_task=tsk.exec_symlink_as
596 if add:self.add_to_group(tsk)
597 self.run_task_now(tsk,postpone)
599 class UninstallContext(InstallContext):
600 '''removes the targets installed'''
602 def __init__(self,**kw):
603 super(UninstallContext,self).__init__(**kw)
604 self.is_install=UNINSTALL
605 def do_install(self,src,tgt,chmod=Utils.O644):
606 if not self.progress_bar:
607 Logs.info('- remove %s'%tgt)
608 self.uninstall.append(tgt)
612 if e.errno!=errno.ENOENT:
613 if not getattr(self,'uninstall_error',None):
614 self.uninstall_error=True
615 Logs.warn('build: some files could not be uninstalled (retry with -vv to list them)')
617 Logs.warn('Could not remove %s (error code %r)'%(e.filename,e.errno))
619 tgt=os.path.dirname(tgt)
624 def do_link(self,src,tgt):
626 if not self.progress_bar:
627 Logs.info('- unlink %s'%tgt)
632 tgt=os.path.dirname(tgt)
639 def runnable_status(self):
641 setattr(Task.Task,'runnable_status_back',Task.Task.runnable_status)
642 setattr(Task.Task,'runnable_status',runnable_status)
643 super(UninstallContext,self).execute()
645 setattr(Task.Task,'runnable_status',Task.Task.runnable_status_back)
646 class CleanContext(BuildContext):
647 '''cleans the project'''
651 if not self.all_envs:
653 self.recurse([self.run_dir])
659 Logs.debug('build: clean called')
660 if self.bldnode!=self.srcnode:
662 for e in self.all_envs.values():
663 lst.extend(self.root.find_or_declare(f)for f in e[CFG_FILES])
664 for n in self.bldnode.ant_glob('**/*',excl='.lock* *conf_check_*/** config.log c4che/*',quiet=True):
668 self.root.children={}
669 for v in'node_deps task_sigs raw_deps'.split():
671 class ListContext(BuildContext):
672 '''lists the targets to execute'''
676 if not self.all_envs:
678 self.recurse([self.run_dir])
680 self.timer=Utils.Timer()
681 for g in self.groups:
685 except AttributeError:
690 self.get_tgen_by_name('')
693 lst=list(self.task_gen_cache_names.keys())
696 Logs.pprint('GREEN',k)
697 class StepContext(BuildContext):
698 '''executes tasks in a step-by-step fashion, for debugging'''
700 def __init__(self,**kw):
701 super(StepContext,self).__init__(**kw)
702 self.files=Options.options.files
705 Logs.warn('Add a pattern for the debug build, for example "waf step --files=main.c,app"')
706 BuildContext.compile(self)
709 if self.targets and self.targets!='*':
710 targets=self.targets.split(',')
711 for g in self.groups:
713 if targets and tg.name not in targets:
717 except AttributeError:
721 for pat in self.files.split(','):
722 matcher=self.get_matcher(pat)
724 if isinstance(tg,Task.TaskBase):
730 for node in getattr(tsk,'inputs',[]):
731 if matcher(node,output=False):
734 for node in getattr(tsk,'outputs',[]):
735 if matcher(node,output=True):
740 Logs.info('%s -> exit %r'%(str(tsk),ret))
741 def get_matcher(self,pat):
744 if pat.startswith('in:'):
746 pat=pat.replace('in:','')
747 elif pat.startswith('out:'):
749 pat=pat.replace('out:','')
750 anode=self.root.find_node(pat)
753 if not pat.startswith('^'):
755 if not pat.endswith('$'):
757 pattern=re.compile(pat)
758 def match(node,output):
759 if output==True and not out:
761 if output==False and not inn:
766 return pattern.match(node.abspath())
768 BuildContext.store=Utils.nogc(BuildContext.store)
769 BuildContext.restore=Utils.nogc(BuildContext.restore)