blob: a27e10c6ecbbc55a0a48ec810ccfd5b2c4264d55 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001OE_TERMINAL ?= 'auto'
2OE_TERMINAL[type] = 'choice'
3OE_TERMINAL[choices] = 'auto none \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05004 ${@oe_terminal_prioritized()}'
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006OE_TERMINAL_EXPORTS += 'EXTRA_OEMAKE CACHED_CONFIGUREVARS CONFIGUREOPTS EXTRA_OECONF'
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007OE_TERMINAL_EXPORTS[type] = 'list'
8
9XAUTHORITY ?= "${HOME}/.Xauthority"
10SHELL ?= "bash"
11
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050012def oe_terminal_prioritized():
13 import oe.terminal
14 return " ".join(o.name for o in oe.terminal.prioritized())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015
16def emit_terminal_func(command, envdata, d):
17 cmd_func = 'do_terminal'
18
19 envdata.setVar(cmd_func, 'exec ' + command)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020 envdata.setVarFlag(cmd_func, 'func', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022 runfmt = d.getVar('BB_RUNFMT') or "run.{func}.{pid}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023 runfile = runfmt.format(func=cmd_func, task=cmd_func, taskfunc=cmd_func, pid=os.getpid())
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024 runfile = os.path.join(d.getVar('T'), runfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 bb.utils.mkdirhier(os.path.dirname(runfile))
26
27 with open(runfile, 'w') as script:
28 script.write('#!/bin/sh -e\n')
29 bb.data.emit_func(cmd_func, script, envdata)
30 script.write(cmd_func)
31 script.write("\n")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060032 os.chmod(runfile, 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
34 return runfile
35
36def oe_terminal(command, title, d):
37 import oe.data
38 import oe.terminal
39
40 envdata = bb.data.init()
41
42 for v in os.environ:
43 envdata.setVar(v, os.environ[v])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050044 envdata.setVarFlag(v, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
46 for export in oe.data.typed_value('OE_TERMINAL_EXPORTS', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 value = d.getVar(export)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 if value is not None:
49 os.environ[export] = str(value)
50 envdata.setVar(export, str(value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050051 envdata.setVarFlag(export, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 if export == "PSEUDO_DISABLED":
53 if "PSEUDO_UNLOAD" in os.environ:
54 del os.environ["PSEUDO_UNLOAD"]
55 envdata.delVar("PSEUDO_UNLOAD")
56
57 # Add in all variables from the user's original environment which
58 # haven't subsequntly been set/changed
59 origbbenv = d.getVar("BB_ORIGENV", False) or {}
60 for key in origbbenv:
61 if key in envdata:
62 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -050063 value = origbbenv.getVar(key)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 if value is not None:
65 os.environ[key] = str(value)
66 envdata.setVar(key, str(value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050067 envdata.setVarFlag(key, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
Brad Bishop6e60e8b2018-02-01 10:27:11 -050069 # Use original PATH as a fallback
70 path = d.getVar('PATH') + ":" + origbbenv.getVar('PATH')
71 os.environ['PATH'] = path
72 envdata.setVar('PATH', path)
73
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 # A complex PS1 might need more escaping of chars.
75 # Lets not export PS1 instead.
76 envdata.delVar("PS1")
77
78 # Replace command with an executable wrapper script
79 command = emit_terminal_func(command, envdata, d)
80
81 terminal = oe.data.typed_value('OE_TERMINAL', d).lower()
82 if terminal == 'none':
83 bb.fatal('Devshell usage disabled with OE_TERMINAL')
84 elif terminal != 'auto':
85 try:
86 oe.terminal.spawn(terminal, command, title, None, d)
87 return
88 except oe.terminal.UnsupportedTerminal:
89 bb.warn('Unsupported terminal "%s", defaulting to "auto"' %
90 terminal)
91 except oe.terminal.ExecutionError as exc:
92 bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
93
94 try:
95 oe.terminal.spawn_preferred(command, title, None, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050096 except oe.terminal.NoSupportedTerminals as nosup:
97 nosup.terms.remove("false")
98 cmds = '\n\t'.join(nosup.terms).replace("{command}",
99 "do_terminal").replace("{title}", title)
100 bb.fatal('No valid terminal found, unable to open devshell.\n' +
101 'Tried the following commands:\n\t%s' % cmds)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102 except oe.terminal.ExecutionError as exc:
103 bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
104
105oe_terminal[vardepsexclude] = "BB_ORIGENV"