c_aliases.py 3.4 KB

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