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 7from waflib import Utils, Errors 8from waflib.Configure import conf 9 10def 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 26def 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['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 51 if 'c' in exts or 'vala' in exts or 'gs' in exts: 52 feats.append('c') 53 54 for x in 'f f90 F F90 for FOR'.split(): 55 if x in exts: 56 feats.append('fc') 57 break 58 59 if 'd' in exts: 60 feats.append('d') 61 62 if 'java' in exts: 63 feats.append('java') 64 return 'java' 65 66 if typ in ('program', 'shlib', 'stlib'): 67 will_link = False 68 for x in feats: 69 if x in ('cxx', 'd', 'fc', 'c'): 70 feats.append(x + typ) 71 will_link = True 72 if not will_link and not kw.get('features', []): 73 raise Errors.WafError('Cannot link from %r, try passing eg: features="c cprogram"?' % kw) 74 return feats 75 76def set_features(kw, typ): 77 """ 78 Inserts data in the input dict *kw* based on existing data and on the type of target 79 required (typ). 80 81 :param kw: task generator parameters 82 :type kw: dict 83 :param typ: type of target 84 :type typ: string 85 """ 86 kw['typ'] = typ 87 kw['features'] = Utils.to_list(kw.get('features', [])) + Utils.to_list(sniff_features(**kw)) 88 89@conf 90def program(bld, *k, **kw): 91 """ 92 Alias for creating programs by looking at the file extensions:: 93 94 def build(bld): 95 bld.program(source='foo.c', target='app') 96 # equivalent to: 97 # bld(features='c cprogram', source='foo.c', target='app') 98 99 """ 100 set_features(kw, 'program') 101 return bld(*k, **kw) 102 103@conf 104def shlib(bld, *k, **kw): 105 """ 106 Alias for creating shared libraries by looking at the file extensions:: 107 108 def build(bld): 109 bld.shlib(source='foo.c', target='app') 110 # equivalent to: 111 # bld(features='c cshlib', source='foo.c', target='app') 112 113 """ 114 set_features(kw, 'shlib') 115 return bld(*k, **kw) 116 117@conf 118def stlib(bld, *k, **kw): 119 """ 120 Alias for creating static libraries by looking at the file extensions:: 121 122 def build(bld): 123 bld.stlib(source='foo.cpp', target='app') 124 # equivalent to: 125 # bld(features='cxx cxxstlib', source='foo.cpp', target='app') 126 127 """ 128 set_features(kw, 'stlib') 129 return bld(*k, **kw) 130 131@conf 132def objects(bld, *k, **kw): 133 """ 134 Alias for creating object files by looking at the file extensions:: 135 136 def build(bld): 137 bld.objects(source='foo.c', target='app') 138 # equivalent to: 139 # bld(features='c', source='foo.c', target='app') 140 141 """ 142 set_features(kw, 'objects') 143 return bld(*k, **kw) 144 145