jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.2KB

  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. :param lst: files to process
  10. :list lst: list of string or :py:class:`waflib.Node.Node`
  11. :return: list of file extensions
  12. :rtype: list of string
  13. """
  14. ret = []
  15. for x in Utils.to_list(lst):
  16. try:
  17. if not isinstance(x, str):
  18. x = x.name
  19. ret.append(x[x.rfind('.') + 1:])
  20. except Exception:
  21. pass
  22. return ret
  23. def sniff_features(**kw):
  24. """
  25. Look at the source files and return the features for a task generator (mainly cc and cxx)::
  26. snif_features(source=['foo.c', 'foo.cxx'], type='shlib')
  27. # returns ['cxx', 'c', 'cxxshlib', 'cshlib']
  28. :param source: source files to process
  29. :type source: list of string or :py:class:`waflib.Node.Node`
  30. :param type: object type in *program*, *shlib* or *stlib*
  31. :type type: string
  32. :return: the list of features for a task generator processing the source files
  33. :rtype: list of string
  34. """
  35. exts = get_extensions(kw['source'])
  36. type = kw['_type']
  37. feats = []
  38. # watch the order, cxx will have the precedence
  39. for x in 'cxx cpp c++ cc C'.split():
  40. if x in exts:
  41. feats.append('cxx')
  42. break
  43. if 'c' in exts or 'vala' in exts or 'gs' in exts:
  44. feats.append('c')
  45. for x in 'f f90 F F90 for FOR'.split():
  46. if x in exts:
  47. feats.append('fc')
  48. break
  49. if 'd' in exts:
  50. feats.append('d')
  51. if 'java' in exts:
  52. feats.append('java')
  53. return 'java'
  54. if type in ('program', 'shlib', 'stlib'):
  55. will_link = False
  56. for x in feats:
  57. if x in ('cxx', 'd', 'fc', 'c'):
  58. feats.append(x + type)
  59. will_link = True
  60. if not will_link and not kw.get('features', []):
  61. raise Errors.WafError('Cannot link from %r, try passing eg: features="c cprogram"?' % kw)
  62. return feats
  63. def set_features(kw, _type):
  64. kw['_type'] = _type
  65. kw['features'] = Utils.to_list(kw.get('features', [])) + Utils.to_list(sniff_features(**kw))
  66. @conf
  67. def program(bld, *k, **kw):
  68. """
  69. Alias for creating programs by looking at the file extensions::
  70. def build(bld):
  71. bld.program(source='foo.c', target='app')
  72. # equivalent to:
  73. # bld(features='c cprogram', source='foo.c', target='app')
  74. """
  75. set_features(kw, 'program')
  76. return bld(*k, **kw)
  77. @conf
  78. def shlib(bld, *k, **kw):
  79. """
  80. Alias for creating shared libraries by looking at the file extensions::
  81. def build(bld):
  82. bld.shlib(source='foo.c', target='app')
  83. # equivalent to:
  84. # bld(features='c cshlib', source='foo.c', target='app')
  85. """
  86. set_features(kw, 'shlib')
  87. return bld(*k, **kw)
  88. @conf
  89. def stlib(bld, *k, **kw):
  90. """
  91. Alias for creating static libraries by looking at the file extensions::
  92. def build(bld):
  93. bld.stlib(source='foo.cpp', target='app')
  94. # equivalent to:
  95. # bld(features='cxx cxxstlib', source='foo.cpp', target='app')
  96. """
  97. set_features(kw, 'stlib')
  98. return bld(*k, **kw)
  99. @conf
  100. def objects(bld, *k, **kw):
  101. """
  102. Alias for creating object files by looking at the file extensions::
  103. def build(bld):
  104. bld.objects(source='foo.c', target='app')
  105. # equivalent to:
  106. # bld(features='c', source='foo.c', target='app')
  107. """
  108. set_features(kw, 'objects')
  109. return bld(*k, **kw)