75ab5defcdca1caf26edce3b42cec4c6180680f6
[samba.git] / buildtools / wafsamba / samba_wildcard.py
1 #! /usr/bin/env python
2
3 # based on playground/evil in the waf svn tree
4
5 import os, datetime
6 import Scripting, Utils, Options, Logs, Environment, fnmatch
7 from Constants import *
8 from samba_utils import *
9
10 def run_task(t, k):
11         '''run a single build task'''
12         ret = t.run()
13         if ret:
14                 raise Utils.WafError("Failed to build %s: %u" % (k, ret))
15
16
17 def run_named_build_task(cmd):
18         '''run a named build task, matching the cmd name using fnmatch
19         wildcards against inputs and outputs of all build tasks'''
20         bld = fake_build_environment(info=False)
21         found = False
22         cwd_node = bld.root.find_dir(os.getcwd())
23         top_node = bld.root.find_dir(bld.srcnode.abspath())
24
25         cmd = os.path.normpath(cmd)
26
27         # cope with builds of bin/*/*
28         if os.path.islink(cmd):
29                 cmd = os_path_relpath(os.readlink(cmd), os.getcwd())
30
31         if cmd[0:12] == "bin/default/":
32                 cmd = cmd[12:]
33
34         for g in bld.task_manager.groups:
35                 for attr in ['outputs', 'inputs']:
36                         for t in g.tasks:
37                                 s = getattr(t, attr, [])
38                                 for k in s:
39                                         relpath1 = k.relpath_gen(cwd_node)
40                                         relpath2 = k.relpath_gen(top_node)
41                                         if (fnmatch.fnmatch(relpath1, cmd) or
42                                             fnmatch.fnmatch(relpath2, cmd)):
43                                                 t.position = [0,0]
44                                                 print(t.display())
45                                                 run_task(t, k)
46                                                 found = True
47
48
49         if not found:
50                 raise Utils.WafError("Unable to find build target matching %s" % cmd)
51
52
53 def rewrite_compile_targets():
54         '''cope with the bin/ form of compile target'''
55         if not Options.options.compile_targets:
56                 return
57
58         bld = fake_build_environment(info=False)
59         targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
60         tlist = []
61
62         for t in Options.options.compile_targets.split(','):
63                 if not os.path.islink(t):
64                         tlist.append(t)
65                         continue
66                 link = os.readlink(t)
67                 list = link.split('/')
68                 for name in [list[-1], '/'.join(list[-2:])]:
69                         if name in targets:
70                                 tlist.append(name)
71                                 continue
72         Options.options.compile_targets = ",".join(tlist)
73
74
75
76 def wildcard_main(missing_cmd_fn):
77         '''this replaces main from Scripting, allowing us to override the
78            behaviour for unknown commands
79
80            If a unknown command is found, then missing_cmd_fn() is called with
81            the name of the requested command
82            '''
83         Scripting.commands = Options.arg_line[:]
84
85         # rewrite the compile targets to cope with the bin/xx form
86         rewrite_compile_targets()
87
88         while Scripting.commands:
89                 x = Scripting.commands.pop(0)
90
91                 ini = datetime.datetime.now()
92                 if x == 'configure':
93                         fun = Scripting.configure
94                 elif x == 'build':
95                         fun = Scripting.build
96                 else:
97                         fun = getattr(Utils.g_module, x, None)
98
99                 # this is the new addition on top of main from Scripting.py
100                 if not fun:
101                         missing_cmd_fn(x)
102                         break
103
104                 ctx = getattr(Utils.g_module, x + '_context', Utils.Context)()
105
106                 if x in ['init', 'shutdown', 'dist', 'distclean', 'distcheck']:
107                         try:
108                                 fun(ctx)
109                         except TypeError:
110                                 fun()
111                 else:
112                         fun(ctx)
113
114                 ela = ''
115                 if not Options.options.progress_bar:
116                         ela = ' (%s)' % Utils.get_elapsed_time(ini)
117
118                 if x != 'init' and x != 'shutdown':
119                         Logs.info('%r finished successfully%s' % (x, ela))
120
121                 if not Scripting.commands and x != 'shutdown':
122                         Scripting.commands.append('shutdown')
123
124
125
126
127 def fake_build_environment(info=True, flush=False):
128         """create all the tasks for the project, but do not run the build
129         return the build context in use"""
130         bld = getattr(Utils.g_module, 'build_context', Utils.Context)()
131         bld = Scripting.check_configured(bld)
132
133         Options.commands['install'] = False
134         Options.commands['uninstall'] = False
135         Options.is_install = False
136
137         bld.is_install = 0 # False
138
139         try:
140                 proj = Environment.Environment(Options.lockfile)
141         except IOError:
142                 raise Utils.WafError("Project not configured (run 'waf configure' first)")
143
144         bld.load_dirs(proj[SRCDIR], proj[BLDDIR])
145         bld.load_envs()
146
147         if info:
148                 Logs.info("Waf: Entering directory `%s'" % bld.bldnode.abspath())
149         bld.add_subdirs([os.path.split(Utils.g_module.root_path)[0]])
150
151         bld.pre_build()
152         if flush:
153                 bld.flush()
154         return bld
155