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