terminal.bbclass 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. OE_TERMINAL ?= 'auto'
  2. OE_TERMINAL[type] = 'choice'
  3. OE_TERMINAL[choices] = 'auto none \
  4. ${@oe_terminal_prioritized()}'
  5. OE_TERMINAL_EXPORTS += 'EXTRA_OEMAKE CACHED_CONFIGUREVARS CONFIGUREOPTS EXTRA_OECONF'
  6. OE_TERMINAL_EXPORTS[type] = 'list'
  7. XAUTHORITY ?= "${HOME}/.Xauthority"
  8. SHELL ?= "bash"
  9. def oe_terminal_prioritized():
  10. import oe.terminal
  11. return " ".join(o.name for o in oe.terminal.prioritized())
  12. def emit_terminal_func(command, envdata, d):
  13. import bb.build
  14. cmd_func = 'do_terminal'
  15. envdata.setVar(cmd_func, 'exec ' + command)
  16. envdata.setVarFlag(cmd_func, 'func', '1')
  17. runfmt = d.getVar('BB_RUNFMT') or "run.{func}.{pid}"
  18. runfile = runfmt.format(func=cmd_func, task=cmd_func, taskfunc=cmd_func, pid=os.getpid())
  19. runfile = os.path.join(d.getVar('T'), runfile)
  20. bb.utils.mkdirhier(os.path.dirname(runfile))
  21. with open(runfile, 'w') as script:
  22. script.write(bb.build.shell_trap_code())
  23. bb.data.emit_func(cmd_func, script, envdata)
  24. script.write(cmd_func)
  25. script.write("\n")
  26. os.chmod(runfile, 0o755)
  27. return runfile
  28. def oe_terminal(command, title, d):
  29. import oe.data
  30. import oe.terminal
  31. envdata = bb.data.init()
  32. for v in os.environ:
  33. envdata.setVar(v, os.environ[v])
  34. envdata.setVarFlag(v, 'export', '1')
  35. for export in oe.data.typed_value('OE_TERMINAL_EXPORTS', d):
  36. value = d.getVar(export)
  37. if value is not None:
  38. os.environ[export] = str(value)
  39. envdata.setVar(export, str(value))
  40. envdata.setVarFlag(export, 'export', '1')
  41. if export == "PSEUDO_DISABLED":
  42. if "PSEUDO_UNLOAD" in os.environ:
  43. del os.environ["PSEUDO_UNLOAD"]
  44. envdata.delVar("PSEUDO_UNLOAD")
  45. # Add in all variables from the user's original environment which
  46. # haven't subsequntly been set/changed
  47. origbbenv = d.getVar("BB_ORIGENV", False) or {}
  48. for key in origbbenv:
  49. if key in envdata:
  50. continue
  51. value = origbbenv.getVar(key)
  52. if value is not None:
  53. os.environ[key] = str(value)
  54. envdata.setVar(key, str(value))
  55. envdata.setVarFlag(key, 'export', '1')
  56. # Use original PATH as a fallback
  57. path = d.getVar('PATH') + ":" + origbbenv.getVar('PATH')
  58. os.environ['PATH'] = path
  59. envdata.setVar('PATH', path)
  60. # A complex PS1 might need more escaping of chars.
  61. # Lets not export PS1 instead.
  62. envdata.delVar("PS1")
  63. # Replace command with an executable wrapper script
  64. command = emit_terminal_func(command, envdata, d)
  65. terminal = oe.data.typed_value('OE_TERMINAL', d).lower()
  66. if terminal == 'none':
  67. bb.fatal('Devshell usage disabled with OE_TERMINAL')
  68. elif terminal != 'auto':
  69. try:
  70. oe.terminal.spawn(terminal, command, title, None, d)
  71. return
  72. except oe.terminal.UnsupportedTerminal:
  73. bb.warn('Unsupported terminal "%s", defaulting to "auto"' %
  74. terminal)
  75. except oe.terminal.ExecutionError as exc:
  76. bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
  77. try:
  78. oe.terminal.spawn_preferred(command, title, None, d)
  79. except oe.terminal.NoSupportedTerminals as nosup:
  80. nosup.terms.remove("false")
  81. cmds = '\n\t'.join(nosup.terms).replace("{command}",
  82. "do_terminal").replace("{title}", title)
  83. bb.fatal('No valid terminal found, unable to open devshell.\n' +
  84. 'Tried the following commands:\n\t%s' % cmds)
  85. except oe.terminal.ExecutionError as exc:
  86. bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
  87. oe_terminal[vardepsexclude] = "BB_ORIGENV"