crosscc.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from SCons.Errors import StopError
  2. from SCons.Tool import asm
  3. from SCons.Tool import gcc
  4. from SCons.Tool import gxx
  5. from SCons.Tool import ar
  6. from SCons.Tool import gnulink
  7. import strip
  8. import gdb
  9. import objdump
  10. import size
  11. from SCons.Action import _subproc
  12. import subprocess
  13. def prefix_commands(env, command_prefix, cmd_list):
  14. for command in cmd_list:
  15. if command in env:
  16. env[command] = command_prefix + env[command]
  17. def _get_tool_version(env, tool):
  18. verstr = "version unknown"
  19. proc = _subproc(
  20. env,
  21. env.subst("${%s} --version" % tool),
  22. stdout=subprocess.PIPE,
  23. stderr="devnull",
  24. stdin="devnull",
  25. universal_newlines=True,
  26. error="raise",
  27. shell=True,
  28. )
  29. if proc:
  30. verstr = proc.stdout.readline()
  31. proc.communicate()
  32. return verstr
  33. def generate(env, **kw):
  34. for orig_tool in (asm, gcc, gxx, ar, gnulink, strip, gdb, objdump, size):
  35. orig_tool.generate(env)
  36. env.SetDefault(
  37. TOOLCHAIN_PREFIX=kw.get("toolchain_prefix"),
  38. )
  39. prefix_commands(
  40. env,
  41. env.subst("$TOOLCHAIN_PREFIX"),
  42. [
  43. "AR",
  44. "AS",
  45. "CC",
  46. "CXX",
  47. "OBJCOPY",
  48. "RANLIB",
  49. "STRIP",
  50. "GDB",
  51. "GDBPY",
  52. "OBJDUMP",
  53. "SIZE",
  54. ],
  55. )
  56. # Call CC to check version
  57. if whitelisted_versions := kw.get("versions", ()):
  58. cc_version = _get_tool_version(env, "CC")
  59. # print("CC version =", cc_version)
  60. # print(list(filter(lambda v: v in cc_version, whitelisted_versions)))
  61. if not any(filter(lambda v: v in cc_version, whitelisted_versions)):
  62. raise StopError(
  63. f"Toolchain version is not supported. Allowed: {whitelisted_versions}, toolchain: {cc_version} "
  64. )
  65. def exists(env):
  66. return True