third_party:waf: update to upstream 2.0.4 release
[bbaumbach/samba.git] / third_party / waf / waflib / extras / cfg_altoptions.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
4
5 #!/usr/bin/python
6 # -*- coding: utf-8 -*-
7 # Tool to extend c_config.check_cfg()
8
9 __author__ = __maintainer__ = "Jérôme Carretero <cJ-waf@zougloub.eu>"
10 __copyright__ = "Jérôme Carretero, 2014"
11
12 """
13
14 This tool allows to work around the absence of ``*-config`` programs
15 on systems, by keeping the same clean configuration syntax but inferring
16 values or permitting their modification via the options interface.
17
18 Note that pkg-config can also support setting ``PKG_CONFIG_PATH``,
19 so you can put custom files in a folder containing new .pc files.
20 This tool could also be implemented by taking advantage of this fact.
21
22 Usage::
23
24    def options(opt):
25      opt.load('c_config_alt')
26      opt.add_package_option('package')
27
28    def configure(cfg):
29      conf.load('c_config_alt')
30      conf.check_cfg(...)
31
32 Known issues:
33
34 - Behavior with different build contexts...
35
36 """
37
38 import os
39 import functools
40 from waflib import Configure, Options, Errors
41
42 def name_to_dest(x):
43         return x.lower().replace('-', '_')
44
45
46 def options(opt):
47         def x(opt, param):
48                 dest = name_to_dest(param)
49                 gr = opt.get_option_group("configure options")
50                 gr.add_option('--%s-root' % dest,
51                  help="path containing include and lib subfolders for %s" \
52                   % param,
53                 )
54
55         opt.add_package_option = functools.partial(x, opt)
56
57
58 check_cfg_old = getattr(Configure.ConfigurationContext, 'check_cfg')
59
60 @Configure.conf
61 def check_cfg(conf, *k, **kw):
62         if k:
63                 lst = k[0].split()
64                 kw['package'] = lst[0]
65                 kw['args'] = ' '.join(lst[1:])
66
67         if not 'package' in kw:
68                 return check_cfg_old(conf, **kw)
69
70         package = kw['package']
71
72         package_lo = name_to_dest(package)
73         package_hi = package.upper().replace('-', '_') # TODO FIXME
74         package_hi = kw.get('uselib_store', package_hi)
75
76         def check_folder(path, name):
77                 try:
78                         assert os.path.isdir(path)
79                 except AssertionError:
80                         raise Errors.ConfigurationError(
81                                 "%s_%s (%s) is not a folder!" \
82                                 % (package_lo, name, path))
83                 return path
84
85         root = getattr(Options.options, '%s_root' % package_lo, None)
86
87         if root is None:
88                 return check_cfg_old(conf, **kw)
89         else:
90                 def add_manual_var(k, v):
91                         conf.start_msg('Adding for %s a manual var' % (package))
92                         conf.env["%s_%s" % (k, package_hi)] = v
93                         conf.end_msg("%s = %s" % (k, v))
94
95
96                 check_folder(root, 'root')
97
98                 pkg_inc = check_folder(os.path.join(root, "include"), 'inc')
99                 add_manual_var('INCLUDES', [pkg_inc])
100                 pkg_lib = check_folder(os.path.join(root, "lib"), 'libpath')
101                 add_manual_var('LIBPATH', [pkg_lib])
102                 add_manual_var('LIB', [package])
103
104                 for x in kw.get('manual_deps', []):
105                         for k, v in sorted(conf.env.get_merged_dict().items()):
106                                 if k.endswith('_%s' % x):
107                                         k = k.replace('_%s' % x, '')
108                                         conf.start_msg('Adding for %s a manual dep' \
109                                          %(package))
110                                         conf.env["%s_%s" % (k, package_hi)] += v
111                                         conf.end_msg('%s += %s' % (k, v))
112
113                 return True
114