terminal.bbclass 3.9 KB

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