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.

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