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