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