ra.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8; mode: Python -*-
  2. # (c) Daniel Llorens - 2016, 2017-2019
  3. # This library is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU Lesser General Public License as published by the Free
  5. # Software Foundation; either version 3 of the License, or (at your option) any
  6. # later version.
  7. # Utilities for SConstructs
  8. import os, string
  9. # These are colorama names, but the dependence is a bother.
  10. class Fore: RED = '\x1b[31m'; YELLOW ='\x1b[33m'; RESET = '\x1b[39m'
  11. class Style: BRIGHT = '\x1b[1m'; RESET_ALL = '\x1b[0m';
  12. from os.path import join, abspath, split
  13. from subprocess import call
  14. CXXFLAGS = ['-std=c++20', '-Wall', '-Werror', '-fdiagnostics-color=always', '-Wno-unknown-pragmas',
  15. '-finput-charset=UTF-8', '-fextended-identifiers',
  16. '-Wno-error=strict-overflow', '-Werror=zero-as-null-pointer-constant',
  17. #'-Wconversion',
  18. # '-funsafe-math-optimizations', # TODO Test with this.
  19. ]
  20. def blas_flags(Configure, env, arch):
  21. env_blas = env.Clone()
  22. if arch.find('apple-darwin') >= 0:
  23. # after OS X 10.14 giving -framework Accelerate isn't enough.
  24. # cf https://github.com/shogun-toolbox/shogun/commit/6db834fb4ca9783b6e5adfde808d60ebfca0abc9
  25. # cf https://github.com/BVLC/caffe/blob/master/cmake/Modules/FindvecLib.cmake
  26. cblas_possible_paths = ['/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers/']
  27. for ppath in cblas_possible_paths:
  28. env0 = env.Clone()
  29. env0.Append(CPPPATH = [ppath])
  30. conf = Configure(env0)
  31. success = conf.CheckCHeader('cblas.h')
  32. conf.Finish()
  33. if success:
  34. print("cblas.h found at %r" % ppath)
  35. env_blas.Append(LINKFLAGS=' -framework Accelerate ')
  36. env_blas.Append(CPPPATH=[ppath])
  37. break
  38. else:
  39. print("cblas.h couldn't be found. Crossing fingers.")
  40. env_blas.Append(LINKFLAGS=' -framework Accelerate ')
  41. env_blas.Append(CCFLAGS=' -framework Accelerate ')
  42. else:
  43. env_blas.Append(LIBS=['blas'])
  44. return env_blas
  45. def ensure_ext(s, ext):
  46. "if s doesn't end with ext, append it."
  47. p = s.rfind(ext)
  48. return s if p+len(ext) == len(s) else s + ext
  49. def remove_ext(s):
  50. "clip string from the last dot until the end."
  51. p = s.rfind('.')
  52. assert p>=0, 'source must have an extension'
  53. return s[0:p]
  54. def path_parts(path):
  55. path, tail = split(path)
  56. return ([path] if path == os.sep else path_parts(path) if path else []) \
  57. + ([tail] if tail else [])
  58. def take_from_environ(env, var, wrapper=(lambda x: x), default=None):
  59. if var in os.environ and os.environ[var]!='':
  60. env[var] = wrapper(os.environ[var])
  61. elif default is not None:
  62. env[var] = default
  63. def get_value_wo_error(dictionary, key, default = ''):
  64. if key in dictionary:
  65. return dictionary[key]
  66. else:
  67. return default
  68. def dict_index_list(dictionary, list_of_keys):
  69. return dict([ (k, get_value_wo_error(dictionary, k))
  70. for k in list_of_keys ])
  71. def to_test(env, variant_dir, source, args):
  72. """
  73. Run program with args to produce a check stamp. The name of the first source
  74. is used to generate the name of the stamp.
  75. """
  76. class tester:
  77. def __init__(self, args):
  78. self.args = args
  79. def __call__(self, target, source, env):
  80. print("-> running %s \n___________________" % str(self.args))
  81. r = os.spawnl(os.P_WAIT, self.args[0], self.args[0], *self.args[1:])
  82. print("^^^^^^^^^^^^^^^^^^^")
  83. print('r ', r)
  84. if not r:
  85. print("PASSED %s" % str(self.args))
  86. call(['touch', target[0].abspath])
  87. else:
  88. print("FAILED %s" % str(self.args))
  89. return r
  90. stamp = env.File(join(variant_dir, str(source[0])) + string.join(args[1:]) + '.check')
  91. return env.Command(stamp, source, tester(args))
  92. # def to_source(env, targets, source):
  93. # main = source[0]
  94. # for target in targets: env.Notangle(target, remove_ext(main)+'.nw')
  95. # env.Noweave(remove_ext(main)+'.tex', remove_ext(main)+'.nw')
  96. # env.PDF(remove_ext(main), remove_ext(main)+'.tex')
  97. def to_source_from_noweb(env, targets, source):
  98. main = source[0]
  99. env.Noweave(remove_ext(main) + '.tex', remove_ext(main) + '.nw')
  100. env.PDF(remove_ext(main), remove_ext(main) + '.tex')
  101. return [env.Notangle(target, remove_ext(main) + '.nw') for target in targets]
  102. def to_test_ra(env_, variant_dir):
  103. def f(source, target='', cxxflags=[], cppdefines=[]):
  104. if len(cxxflags)==0 or len(cppdefines)==0:
  105. env = env_
  106. else:
  107. env = env_.Clone()
  108. env.Append(CXXFLAGS=cxxflags + ['-U' + k for k in cppdefines.keys()], CPPDEFINES=cppdefines)
  109. if len(target)==0:
  110. target = source
  111. obj = env.Object(target, [source + '.cc'])
  112. test = env.Program(target, obj)
  113. to_test(env, variant_dir, test, [test[0].abspath])
  114. return f
  115. def print_summary(GetBuildFailures, tag):
  116. test_item_tally = 0
  117. test_tally = 0
  118. build_tally = 0
  119. print('\n' + Style.BRIGHT + 'Summary for ' + tag + Style.RESET_ALL + '\n--------')
  120. for bf in GetBuildFailures():
  121. if str(bf.node).endswith('.check') and (bf.status > 0):
  122. print((Style.BRIGHT + Fore.RED + '%s ' + Style.RESET_ALL + Fore.RESET + ' failed (%d)') \
  123. % (bf.node, bf.status))
  124. test_item_tally += bf.status
  125. test_tally += 1
  126. else:
  127. print((Style.BRIGHT + Fore.YELLOW + '%s ' + Style.RESET_ALL + Fore.RESET + ' failed (%s)') \
  128. % (bf.node, bf.errstr))
  129. build_tally += 1
  130. print('%d targets failed to build.' % build_tally)
  131. print('%d tests failed with %d total failures.' % (test_tally, test_item_tally))