third_party:waf: update to upstream 2.0.4 release
[samba.git] / third_party / waf / waflib / Tools / c_aliases.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/env python
6 # encoding: utf-8
7 # Thomas Nagy, 2005-2015 (ita)
8
9 "base for all c/c++ programs and libraries"
10
11 from waflib import Utils, Errors
12 from waflib.Configure import conf
13
14 def get_extensions(lst):
15         """
16         Returns the file extensions for the list of files given as input
17
18         :param lst: files to process
19         :list lst: list of string or :py:class:`waflib.Node.Node`
20         :return: list of file extensions
21         :rtype: list of string
22         """
23         ret = []
24         for x in Utils.to_list(lst):
25                 if not isinstance(x, str):
26                         x = x.name
27                 ret.append(x[x.rfind('.') + 1:])
28         return ret
29
30 def sniff_features(**kw):
31         """
32         Computes and returns the features required for a task generator by
33         looking at the file extensions. This aimed for C/C++ mainly::
34
35                 snif_features(source=['foo.c', 'foo.cxx'], type='shlib')
36                 # returns  ['cxx', 'c', 'cxxshlib', 'cshlib']
37
38         :param source: source files to process
39         :type source: list of string or :py:class:`waflib.Node.Node`
40         :param type: object type in *program*, *shlib* or *stlib*
41         :type type: string
42         :return: the list of features for a task generator processing the source files
43         :rtype: list of string
44         """
45         exts = get_extensions(kw['source'])
46         typ = kw['typ']
47         feats = []
48
49         # watch the order, cxx will have the precedence
50         for x in 'cxx cpp c++ cc C'.split():
51                 if x in exts:
52                         feats.append('cxx')
53                         break
54
55         if 'c' in exts or 'vala' in exts or 'gs' in exts:
56                 feats.append('c')
57
58         for x in 'f f90 F F90 for FOR'.split():
59                 if x in exts:
60                         feats.append('fc')
61                         break
62
63         if 'd' in exts:
64                 feats.append('d')
65
66         if 'java' in exts:
67                 feats.append('java')
68                 return 'java'
69
70         if typ in ('program', 'shlib', 'stlib'):
71                 will_link = False
72                 for x in feats:
73                         if x in ('cxx', 'd', 'fc', 'c'):
74                                 feats.append(x + typ)
75                                 will_link = True
76                 if not will_link and not kw.get('features', []):
77                         raise Errors.WafError('Cannot link from %r, try passing eg: features="c cprogram"?' % kw)
78         return feats
79
80 def set_features(kw, typ):
81         """
82         Inserts data in the input dict *kw* based on existing data and on the type of target
83         required (typ).
84
85         :param kw: task generator parameters
86         :type kw: dict
87         :param typ: type of target
88         :type typ: string
89         """
90         kw['typ'] = typ
91         kw['features'] = Utils.to_list(kw.get('features', [])) + Utils.to_list(sniff_features(**kw))
92
93 @conf
94 def program(bld, *k, **kw):
95         """
96         Alias for creating programs by looking at the file extensions::
97
98                 def build(bld):
99                         bld.program(source='foo.c', target='app')
100                         # equivalent to:
101                         # bld(features='c cprogram', source='foo.c', target='app')
102
103         """
104         set_features(kw, 'program')
105         return bld(*k, **kw)
106
107 @conf
108 def shlib(bld, *k, **kw):
109         """
110         Alias for creating shared libraries by looking at the file extensions::
111
112                 def build(bld):
113                         bld.shlib(source='foo.c', target='app')
114                         # equivalent to:
115                         # bld(features='c cshlib', source='foo.c', target='app')
116
117         """
118         set_features(kw, 'shlib')
119         return bld(*k, **kw)
120
121 @conf
122 def stlib(bld, *k, **kw):
123         """
124         Alias for creating static libraries by looking at the file extensions::
125
126                 def build(bld):
127                         bld.stlib(source='foo.cpp', target='app')
128                         # equivalent to:
129                         # bld(features='cxx cxxstlib', source='foo.cpp', target='app')
130
131         """
132         set_features(kw, 'stlib')
133         return bld(*k, **kw)
134
135 @conf
136 def objects(bld, *k, **kw):
137         """
138         Alias for creating object files by looking at the file extensions::
139
140                 def build(bld):
141                         bld.objects(source='foo.c', target='app')
142                         # equivalent to:
143                         # bld(features='c', source='foo.c', target='app')
144
145         """
146         set_features(kw, 'objects')
147         return bld(*k, **kw)
148