waf: make mkdir_p on a empty string not recurse forever
[amitay/samba.git] / buildtools / wafsamba / samba_utils.py
1 # a waf tool to add autoconf-like macros to the configure section
2 # and for SAMBA_ macros for building libraries, binaries etc
3
4 import Build, os, sys, Options, Utils, Task, re, fnmatch, Logs
5 from TaskGen import feature, before
6 from Configure import conf
7 from Logs import debug
8 import shlex
9
10 # TODO: make this a --option
11 LIB_PATH="shared"
12
13
14 # sigh, python octal constants are a mess
15 MODE_644 = int('644', 8)
16 MODE_755 = int('755', 8)
17
18 @conf
19 def SET_TARGET_TYPE(ctx, target, value):
20     '''set the target type of a target'''
21     cache = LOCAL_CACHE(ctx, 'TARGET_TYPE')
22     if target in cache and cache[target] != 'EMPTY':
23         Logs.error("ERROR: Target '%s' in directory %s re-defined as %s - was %s" % (target, ctx.curdir, value, cache[target]))
24         sys.exit(1)
25     LOCAL_CACHE_SET(ctx, 'TARGET_TYPE', target, value)
26     debug("task_gen: Target '%s' created of type '%s' in %s" % (target, value, ctx.curdir))
27     return True
28
29
30 def GET_TARGET_TYPE(ctx, target):
31     '''get target type from cache'''
32     cache = LOCAL_CACHE(ctx, 'TARGET_TYPE')
33     if not target in cache:
34         return None
35     return cache[target]
36
37
38 ######################################################
39 # this is used as a decorator to make functions only
40 # run once. Based on the idea from
41 # http://stackoverflow.com/questions/815110/is-there-a-decorator-to-simply-cache-function-return-values
42 runonce_ret = {}
43 def runonce(function):
44     def runonce_wrapper(*args):
45         if args in runonce_ret:
46             return runonce_ret[args]
47         else:
48             ret = function(*args)
49             runonce_ret[args] = ret
50             return ret
51     return runonce_wrapper
52
53
54 def ADD_LD_LIBRARY_PATH(path):
55     '''add something to LD_LIBRARY_PATH'''
56     if 'LD_LIBRARY_PATH' in os.environ:
57         oldpath = os.environ['LD_LIBRARY_PATH']
58     else:
59         oldpath = ''
60     newpath = oldpath.split(':')
61     if not path in newpath:
62         newpath.append(path)
63         os.environ['LD_LIBRARY_PATH'] = ':'.join(newpath)
64
65
66 def needs_private_lib(bld, target):
67     '''return True if a target links to a private library'''
68     for lib in getattr(target, "uselib_local", []):
69         t = bld.name_to_obj(lib, bld.env)
70         if t and getattr(t, 'private_library', False):
71             return True
72     return False
73
74
75 def install_rpath(target):
76     '''the rpath value for installation'''
77     bld = target.bld
78     bld.env['RPATH'] = []
79     ret = set()
80     if bld.env.RPATH_ON_INSTALL:
81         ret.add(bld.EXPAND_VARIABLES(bld.env.LIBDIR))
82     if bld.env.RPATH_ON_INSTALL_PRIVATE and needs_private_lib(bld, target):
83         ret.add(bld.EXPAND_VARIABLES(bld.env.PRIVATELIBDIR))
84     return list(ret)
85
86
87 def build_rpath(bld):
88     '''the rpath value for build'''
89     rpaths = [os.path.normpath('%s/%s' % (bld.env.BUILD_DIRECTORY, d)) for d in ("shared", "shared/private")]
90     bld.env['RPATH'] = []
91     if bld.env.RPATH_ON_BUILD:
92         return rpaths
93     for rpath in rpaths:
94         ADD_LD_LIBRARY_PATH(rpath)
95     return []
96
97
98 @conf
99 def LOCAL_CACHE(ctx, name):
100     '''return a named build cache dictionary, used to store
101        state inside other functions'''
102     if name in ctx.env:
103         return ctx.env[name]
104     ctx.env[name] = {}
105     return ctx.env[name]
106
107
108 @conf
109 def LOCAL_CACHE_SET(ctx, cachename, key, value):
110     '''set a value in a local cache'''
111     cache = LOCAL_CACHE(ctx, cachename)
112     cache[key] = value
113
114
115 @conf
116 def ASSERT(ctx, expression, msg):
117     '''a build assert call'''
118     if not expression:
119         raise Utils.WafError("ERROR: %s\n" % msg)
120 Build.BuildContext.ASSERT = ASSERT
121
122
123 def SUBDIR(bld, subdir, list):
124     '''create a list of files by pre-pending each with a subdir name'''
125     ret = ''
126     for l in TO_LIST(list):
127         ret = ret + os.path.normpath(os.path.join(subdir, l)) + ' '
128     return ret
129 Build.BuildContext.SUBDIR = SUBDIR
130
131
132 def dict_concat(d1, d2):
133     '''concatenate two dictionaries d1 += d2'''
134     for t in d2:
135         if t not in d1:
136             d1[t] = d2[t]
137
138
139 def exec_command(self, cmd, **kw):
140     '''this overrides the 'waf -v' debug output to be in a nice
141     unix like format instead of a python list.
142     Thanks to ita on #waf for this'''
143     import Utils, Logs
144     _cmd = cmd
145     if isinstance(cmd, list):
146         _cmd = ' '.join(cmd)
147     debug('runner: %s' % _cmd)
148     if self.log:
149         self.log.write('%s\n' % cmd)
150         kw['log'] = self.log
151     try:
152         if not kw.get('cwd', None):
153             kw['cwd'] = self.cwd
154     except AttributeError:
155         self.cwd = kw['cwd'] = self.bldnode.abspath()
156     return Utils.exec_command(cmd, **kw)
157 Build.BuildContext.exec_command = exec_command
158
159
160 def ADD_COMMAND(opt, name, function):
161     '''add a new top level command to waf'''
162     Utils.g_module.__dict__[name] = function
163     opt.name = function
164 Options.Handler.ADD_COMMAND = ADD_COMMAND
165
166
167 @feature('cc', 'cshlib', 'cprogram')
168 @before('apply_core','exec_rule')
169 def process_depends_on(self):
170     '''The new depends_on attribute for build rules
171        allow us to specify a dependency on output from
172        a source generation rule'''
173     if getattr(self , 'depends_on', None):
174         lst = self.to_list(self.depends_on)
175         for x in lst:
176             y = self.bld.name_to_obj(x, self.env)
177             self.bld.ASSERT(y is not None, "Failed to find dependency %s of %s" % (x, self.name))
178             y.post()
179             if getattr(y, 'more_includes', None):
180                   self.includes += " " + y.more_includes
181
182
183 os_path_relpath = getattr(os.path, 'relpath', None)
184 if os_path_relpath is None:
185     # Python < 2.6 does not have os.path.relpath, provide a replacement
186     # (imported from Python2.6.5~rc2)
187     def os_path_relpath(path, start):
188         """Return a relative version of a path"""
189         start_list = os.path.abspath(start).split("/")
190         path_list = os.path.abspath(path).split("/")
191
192         # Work out how much of the filepath is shared by start and path.
193         i = len(os.path.commonprefix([start_list, path_list]))
194
195         rel_list = ['..'] * (len(start_list)-i) + path_list[i:]
196         if not rel_list:
197             return start
198         return os.path.join(*rel_list)
199
200
201 def unique_list(seq):
202     '''return a uniquified list in the same order as the existing list'''
203     seen = {}
204     result = []
205     for item in seq:
206         if item in seen: continue
207         seen[item] = True
208         result.append(item)
209     return result
210
211
212 def TO_LIST(str, delimiter=None):
213     '''Split a list, preserving quoted strings and existing lists'''
214     if str is None:
215         return []
216     if isinstance(str, list):
217         return str
218     lst = str.split(delimiter)
219     # the string may have had quotes in it, now we
220     # check if we did have quotes, and use the slower shlex
221     # if we need to
222     for e in lst:
223         if e[0] == '"':
224             return shlex.split(str)
225     return lst
226
227
228 def subst_vars_error(string, env):
229     '''substitute vars, throw an error if a variable is not defined'''
230     lst = re.split('(\$\{\w+\})', string)
231     out = []
232     for v in lst:
233         if re.match('\$\{\w+\}', v):
234             vname = v[2:-1]
235             if not vname in env:
236                 Logs.error("Failed to find variable %s in %s" % (vname, string))
237                 sys.exit(1)
238             v = env[vname]
239         out.append(v)
240     return ''.join(out)
241
242
243 @conf
244 def SUBST_ENV_VAR(ctx, varname):
245     '''Substitute an environment variable for any embedded variables'''
246     return subst_vars_error(ctx.env[varname], ctx.env)
247 Build.BuildContext.SUBST_ENV_VAR = SUBST_ENV_VAR
248
249
250 def ENFORCE_GROUP_ORDERING(bld):
251     '''enforce group ordering for the project. This
252        makes the group ordering apply only when you specify
253        a target with --target'''
254     if Options.options.compile_targets:
255         @feature('*')
256         @before('exec_rule', 'apply_core', 'collect')
257         def force_previous_groups(self):
258             if getattr(self.bld, 'enforced_group_ordering', False) == True:
259                 return
260             self.bld.enforced_group_ordering = True
261
262             def group_name(g):
263                 tm = self.bld.task_manager
264                 return [x for x in tm.groups_names if id(tm.groups_names[x]) == id(g)][0]
265
266             my_id = id(self)
267             bld = self.bld
268             stop = None
269             for g in bld.task_manager.groups:
270                 for t in g.tasks_gen:
271                     if id(t) == my_id:
272                         stop = id(g)
273                         debug('group: Forcing up to group %s for target %s',
274                               group_name(g), self.name or self.target)
275                         break
276                 if stop != None:
277                     break
278             if stop is None:
279                 return
280
281             for i in xrange(len(bld.task_manager.groups)):
282                 g = bld.task_manager.groups[i]
283                 bld.task_manager.current_group = i
284                 if id(g) == stop:
285                     break
286                 debug('group: Forcing group %s', group_name(g))
287                 for t in g.tasks_gen:
288                     if not getattr(t, 'forced_groups', False):
289                         debug('group: Posting %s', t.name or t.target)
290                         t.forced_groups = True
291                         t.post()
292 Build.BuildContext.ENFORCE_GROUP_ORDERING = ENFORCE_GROUP_ORDERING
293
294
295 def recursive_dirlist(dir, relbase, pattern=None):
296     '''recursive directory list'''
297     ret = []
298     for f in os.listdir(dir):
299         f2 = dir + '/' + f
300         if os.path.isdir(f2):
301             ret.extend(recursive_dirlist(f2, relbase))
302         else:
303             if pattern and not fnmatch.fnmatch(f, pattern):
304                 continue
305             ret.append(os_path_relpath(f2, relbase))
306     return ret
307
308
309 def mkdir_p(dir):
310     '''like mkdir -p'''
311     if not dir or os.path.isdir(dir):
312         return
313     mkdir_p(os.path.dirname(dir))
314     os.mkdir(dir)
315
316
317 def SUBST_VARS_RECURSIVE(string, env):
318     '''recursively expand variables'''
319     if string is None:
320         return string
321     limit=100
322     while (string.find('${') != -1 and limit > 0):
323         string = subst_vars_error(string, env)
324         limit -= 1
325     return string
326
327
328 @conf
329 def EXPAND_VARIABLES(ctx, varstr, vars=None):
330     '''expand variables from a user supplied dictionary
331
332     This is most useful when you pass vars=locals() to expand
333     all your local variables in strings
334     '''
335
336     if isinstance(varstr, list):
337         ret = []
338         for s in varstr:
339             ret.append(EXPAND_VARIABLES(ctx, s, vars=vars))
340         return ret
341
342     if not isinstance(varstr, str):
343         return varstr
344
345     import Environment
346     env = Environment.Environment()
347     ret = varstr
348     # substitute on user supplied dict if avaiilable
349     if vars is not None:
350         for v in vars.keys():
351             env[v] = vars[v]
352         ret = SUBST_VARS_RECURSIVE(ret, env)
353
354     # if anything left, subst on the environment as well
355     if ret.find('${') != -1:
356         ret = SUBST_VARS_RECURSIVE(ret, ctx.env)
357     # make sure there is nothing left. Also check for the common
358     # typo of $( instead of ${
359     if ret.find('${') != -1 or ret.find('$(') != -1:
360         Logs.error('Failed to substitute all variables in varstr=%s' % ret)
361         sys.exit(1)
362     return ret
363 Build.BuildContext.EXPAND_VARIABLES = EXPAND_VARIABLES
364
365
366 def RUN_COMMAND(cmd,
367                 env=None,
368                 shell=False):
369     '''run a external command, return exit code or signal'''
370     if env:
371         cmd = SUBST_VARS_RECURSIVE(cmd, env)
372
373     status = os.system(cmd)
374     if os.WIFEXITED(status):
375         return os.WEXITSTATUS(status)
376     if os.WIFSIGNALED(status):
377         return - os.WTERMSIG(status)
378     Logs.error("Unknown exit reason %d for command: %s" (status, cmd))
379     return -1
380
381
382 # make sure we have md5. some systems don't have it
383 try:
384     from hashlib import md5
385 except:
386     try:
387         import md5
388     except:
389         import Constants
390         Constants.SIG_NIL = hash('abcd')
391         class replace_md5(object):
392             def __init__(self):
393                 self.val = None
394             def update(self, val):
395                 self.val = hash((self.val, val))
396             def digest(self):
397                 return str(self.val)
398             def hexdigest(self):
399                 return self.digest().encode('hex')
400         def replace_h_file(filename):
401             f = open(filename, 'rb')
402             m = replace_md5()
403             while (filename):
404                 filename = f.read(100000)
405                 m.update(filename)
406             f.close()
407             return m.digest()
408         Utils.md5 = replace_md5
409         Task.md5 = replace_md5
410         Utils.h_file = replace_h_file
411
412
413 def LOAD_ENVIRONMENT():
414     '''load the configuration environment, allowing access to env vars
415        from new commands'''
416     import Environment
417     env = Environment.Environment()
418     try:
419         env.load('.lock-wscript')
420         env.load(env.blddir + '/c4che/default.cache.py')
421     except:
422         pass
423     return env
424
425
426 def IS_NEWER(bld, file1, file2):
427     '''return True if file1 is newer than file2'''
428     t1 = os.stat(os.path.join(bld.curdir, file1)).st_mtime
429     t2 = os.stat(os.path.join(bld.curdir, file2)).st_mtime
430     return t1 > t2
431 Build.BuildContext.IS_NEWER = IS_NEWER
432
433
434 @conf
435 def RECURSE(ctx, directory):
436     '''recurse into a directory, relative to the curdir or top level'''
437     try:
438         visited_dirs = ctx.visited_dirs
439     except:
440         visited_dirs = ctx.visited_dirs = set()
441     d = os.path.join(ctx.curdir, directory)
442     if os.path.exists(d):
443         abspath = os.path.abspath(d)
444     else:
445         abspath = os.path.abspath(os.path.join(Utils.g_module.srcdir, directory))
446     ctxclass = ctx.__class__.__name__
447     key = ctxclass + ':' + abspath
448     if key in visited_dirs:
449         # already done it
450         return
451     visited_dirs.add(key)
452     relpath = os_path_relpath(abspath, ctx.curdir)
453     if ctxclass == 'Handler':
454         return ctx.sub_options(relpath)
455     if ctxclass == 'ConfigurationContext':
456         return ctx.sub_config(relpath)
457     if ctxclass == 'BuildContext':
458         return ctx.add_subdirs(relpath)
459     Logs.error('Unknown RECURSE context class', ctxclass)
460     raise
461 Options.Handler.RECURSE = RECURSE
462 Build.BuildContext.RECURSE = RECURSE
463
464
465 def CHECK_MAKEFLAGS(bld):
466     '''check for MAKEFLAGS environment variable in case we are being
467     called from a Makefile try to honor a few make command line flags'''
468     if not 'WAF_MAKE' in os.environ:
469         return
470     makeflags = os.environ.get('MAKEFLAGS')
471     jobs_set = False
472     # we need to use shlex.split to cope with the escaping of spaces
473     # in makeflags
474     for opt in shlex.split(makeflags):
475         # options can come either as -x or as x
476         if opt[0:2] == 'V=':
477             Options.options.verbose = Logs.verbose = int(opt[2:])
478             if Logs.verbose > 0:
479                 Logs.zones = ['runner']
480             if Logs.verbose > 2:
481                 Logs.zones = ['*']
482         elif opt[0].isupper() and opt.find('=') != -1:
483             loc = opt.find('=')
484             setattr(Options.options, opt[0:loc], opt[loc+1:])
485         elif opt[0] != '-':
486             for v in opt:
487                 if v == 'j':
488                     jobs_set = True
489                 elif v == 'k':
490                     Options.options.keep = True                
491         elif opt == '-j':
492             jobs_set = True
493         elif opt == '-k':
494             Options.options.keep = True                
495     if not jobs_set:
496         # default to one job
497         Options.options.jobs = 1
498             
499 Build.BuildContext.CHECK_MAKEFLAGS = CHECK_MAKEFLAGS
500
501 option_groups = {}
502
503 def option_group(opt, name):
504     '''find or create an option group'''
505     global option_groups
506     if name in option_groups:
507         return option_groups[name]
508     gr = opt.add_option_group(name)
509     option_groups[name] = gr
510     return gr
511 Options.Handler.option_group = option_group
512
513
514 def save_file(filename, contents, create_dir=False):
515     '''save data to a file'''
516     if create_dir:
517         mkdir_p(os.path.dirname(filename))
518     try:
519         f = open(filename, 'w')
520         f.write(contents)
521         f.close()
522     except:
523         return False
524     return True
525
526
527 def load_file(filename):
528     '''return contents of a file'''
529     try:
530         f = open(filename, 'r')
531         r = f.read()
532         f.close()
533     except:
534         return None
535     return r
536
537
538 def reconfigure(ctx):
539     '''rerun configure if necessary'''
540     import Configure, samba_wildcard, Scripting
541     if not os.path.exists(".lock-wscript"):
542         raise Utils.WafError('configure has not been run')
543     bld = samba_wildcard.fake_build_environment()
544     Configure.autoconfig = True
545     Scripting.check_configured(bld)
546
547
548 def map_shlib_extension(ctx, name, python=False):
549     '''map a filename with a shared library extension of .so to the real shlib name'''
550     if name is None:
551         return None
552     if name[-1:].isdigit():
553         # some libraries have specified versions in the wscript rule
554         return name
555     (root1, ext1) = os.path.splitext(name)
556     if python:
557         (root2, ext2) = os.path.splitext(ctx.env.pyext_PATTERN)
558     else:
559         (root2, ext2) = os.path.splitext(ctx.env.shlib_PATTERN)
560     return root1+ext2
561 Build.BuildContext.map_shlib_extension = map_shlib_extension
562
563
564 def make_libname(ctx, name, nolibprefix=False, version=None, python=False):
565     """make a library filename
566          Options:
567               nolibprefix: don't include the lib prefix
568               version    : add a version number
569               python     : if we should use python module name conventions"""
570
571     if python:
572         libname = ctx.env.pyext_PATTERN % name
573     else:
574         libname = ctx.env.shlib_PATTERN % name
575     if nolibprefix and libname[0:3] == 'lib':
576         libname = libname[3:]
577     if version:
578         if version[0] == '.':
579             version = version[1:]
580         (root, ext) = os.path.splitext(libname)
581         if ext == ".dylib":
582             # special case - version goes before the prefix
583             libname = "%s.%s%s" % (root, version, ext)
584         else:
585             libname = "%s%s.%s" % (root, ext, version)
586     return libname
587 Build.BuildContext.make_libname = make_libname
588
589
590 def get_tgt_list(bld):
591     '''return a list of build objects for samba'''
592
593     targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
594
595     # build a list of task generators we are interested in
596     tgt_list = []
597     for tgt in targets:
598         type = targets[tgt]
599         if not type in ['SUBSYSTEM', 'MODULE', 'BINARY', 'LIBRARY', 'ASN1', 'PYTHON']:
600             continue
601         t = bld.name_to_obj(tgt, bld.env)
602         if t is None:
603             Logs.error("Target %s of type %s has no task generator" % (tgt, type))
604             sys.exit(1)
605         tgt_list.append(t)
606     return tgt_list