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.

147 lines
3.5KB

  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.get('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. if 's' in exts or 'S' in exts:
  45. feats.append('asm')
  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 typ in ('program', 'shlib', 'stlib'):
  56. will_link = False
  57. for x in feats:
  58. if x in ('cxx', 'd', 'fc', 'c', 'asm'):
  59. feats.append(x + typ)
  60. will_link = True
  61. if not will_link and not kw.get('features', []):
  62. raise Errors.WafError('Unable to determine how to link %r, try adding eg: features="c cshlib"?' % kw)
  63. return feats
  64. def set_features(kw, typ):
  65. """
  66. Inserts data in the input dict *kw* based on existing data and on the type of target
  67. required (typ).
  68. :param kw: task generator parameters
  69. :type kw: dict
  70. :param typ: type of target
  71. :type typ: string
  72. """
  73. kw['typ'] = typ
  74. kw['features'] = Utils.to_list(kw.get('features', [])) + Utils.to_list(sniff_features(**kw))
  75. @conf
  76. def program(bld, *k, **kw):
  77. """
  78. Alias for creating programs by looking at the file extensions::
  79. def build(bld):
  80. bld.program(source='foo.c', target='app')
  81. # equivalent to:
  82. # bld(features='c cprogram', source='foo.c', target='app')
  83. """
  84. set_features(kw, 'program')
  85. return bld(*k, **kw)
  86. @conf
  87. def shlib(bld, *k, **kw):
  88. """
  89. Alias for creating shared libraries by looking at the file extensions::
  90. def build(bld):
  91. bld.shlib(source='foo.c', target='app')
  92. # equivalent to:
  93. # bld(features='c cshlib', source='foo.c', target='app')
  94. """
  95. set_features(kw, 'shlib')
  96. return bld(*k, **kw)
  97. @conf
  98. def stlib(bld, *k, **kw):
  99. """
  100. Alias for creating static libraries by looking at the file extensions::
  101. def build(bld):
  102. bld.stlib(source='foo.cpp', target='app')
  103. # equivalent to:
  104. # bld(features='cxx cxxstlib', source='foo.cpp', target='app')
  105. """
  106. set_features(kw, 'stlib')
  107. return bld(*k, **kw)
  108. @conf
  109. def objects(bld, *k, **kw):
  110. """
  111. Alias for creating object files by looking at the file extensions::
  112. def build(bld):
  113. bld.objects(source='foo.c', target='app')
  114. # equivalent to:
  115. # bld(features='c', source='foo.c', target='app')
  116. """
  117. set_features(kw, 'objects')
  118. return bld(*k, **kw)