blob: a564ee7494722744f1abae9b93a1ca73678ea689 [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):
Brad Bishop19323692019-04-05 15:28:33 -040017 import bb.build
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018 cmd_func = 'do_terminal'
19
20 envdata.setVar(cmd_func, 'exec ' + command)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050021 envdata.setVarFlag(cmd_func, 'func', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022
Brad Bishop6e60e8b2018-02-01 10:27:11 -050023 runfmt = d.getVar('BB_RUNFMT') or "run.{func}.{pid}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024 runfile = runfmt.format(func=cmd_func, task=cmd_func, taskfunc=cmd_func, pid=os.getpid())
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 runfile = os.path.join(d.getVar('T'), runfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 bb.utils.mkdirhier(os.path.dirname(runfile))
27
28 with open(runfile, 'w') as script:
Patrick Williams0ca19cc2021-08-16 14:03:13 -050029 # Override the shell shell_trap_code specifies.
30 # If our shell is bash, we might well face silent death.
31 script.write("#!/bin/bash\n")
Brad Bishop19323692019-04-05 15:28:33 -040032 script.write(bb.build.shell_trap_code())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 bb.data.emit_func(cmd_func, script, envdata)
34 script.write(cmd_func)
35 script.write("\n")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060036 os.chmod(runfile, 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
38 return runfile
39
40def oe_terminal(command, title, d):
41 import oe.data
42 import oe.terminal
Patrick Williams0ca19cc2021-08-16 14:03:13 -050043
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 envdata = bb.data.init()
45
46 for v in os.environ:
47 envdata.setVar(v, os.environ[v])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050048 envdata.setVarFlag(v, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049
50 for export in oe.data.typed_value('OE_TERMINAL_EXPORTS', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 value = d.getVar(export)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 if value is not None:
53 os.environ[export] = str(value)
54 envdata.setVar(export, str(value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050055 envdata.setVarFlag(export, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 if export == "PSEUDO_DISABLED":
57 if "PSEUDO_UNLOAD" in os.environ:
58 del os.environ["PSEUDO_UNLOAD"]
59 envdata.delVar("PSEUDO_UNLOAD")
60
61 # Add in all variables from the user's original environment which
62 # haven't subsequntly been set/changed
63 origbbenv = d.getVar("BB_ORIGENV", False) or {}
64 for key in origbbenv:
65 if key in envdata:
66 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -050067 value = origbbenv.getVar(key)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 if value is not None:
69 os.environ[key] = str(value)
70 envdata.setVar(key, str(value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050071 envdata.setVarFlag(key, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 # Use original PATH as a fallback
74 path = d.getVar('PATH') + ":" + origbbenv.getVar('PATH')
75 os.environ['PATH'] = path
76 envdata.setVar('PATH', path)
77
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 # A complex PS1 might need more escaping of chars.
79 # Lets not export PS1 instead.
80 envdata.delVar("PS1")
81
82 # Replace command with an executable wrapper script
83 command = emit_terminal_func(command, envdata, d)
84
85 terminal = oe.data.typed_value('OE_TERMINAL', d).lower()
86 if terminal == 'none':
87 bb.fatal('Devshell usage disabled with OE_TERMINAL')
88 elif terminal != 'auto':
89 try:
90 oe.terminal.spawn(terminal, command, title, None, d)
91 return
92 except oe.terminal.UnsupportedTerminal:
93 bb.warn('Unsupported terminal "%s", defaulting to "auto"' %
94 terminal)
95 except oe.terminal.ExecutionError as exc:
96 bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
97
98 try:
99 oe.terminal.spawn_preferred(command, title, None, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 except oe.terminal.NoSupportedTerminals as nosup:
101 nosup.terms.remove("false")
102 cmds = '\n\t'.join(nosup.terms).replace("{command}",
103 "do_terminal").replace("{title}", title)
104 bb.fatal('No valid terminal found, unable to open devshell.\n' +
105 'Tried the following commands:\n\t%s' % cmds)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 except oe.terminal.ExecutionError as exc:
107 bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
108
109oe_terminal[vardepsexclude] = "BB_ORIGENV"