crosscc.py 2.3 KB

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