blob: 2dfc7db255124c8147698549ec9af7e8b3ace5da [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007OE_TERMINAL ?= 'auto'
8OE_TERMINAL[type] = 'choice'
9OE_TERMINAL[choices] = 'auto none \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050010 ${@oe_terminal_prioritized()}'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
Brad Bishop6e60e8b2018-02-01 10:27:11 -050012OE_TERMINAL_EXPORTS += 'EXTRA_OEMAKE CACHED_CONFIGUREVARS CONFIGUREOPTS EXTRA_OECONF'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013OE_TERMINAL_EXPORTS[type] = 'list'
14
15XAUTHORITY ?= "${HOME}/.Xauthority"
16SHELL ?= "bash"
17
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050018def oe_terminal_prioritized():
19 import oe.terminal
20 return " ".join(o.name for o in oe.terminal.prioritized())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
22def emit_terminal_func(command, envdata, d):
Brad Bishop19323692019-04-05 15:28:33 -040023 import bb.build
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024 cmd_func = 'do_terminal'
25
26 envdata.setVar(cmd_func, 'exec ' + command)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050027 envdata.setVarFlag(cmd_func, 'func', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029 runfmt = d.getVar('BB_RUNFMT') or "run.{func}.{pid}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030 runfile = runfmt.format(func=cmd_func, task=cmd_func, taskfunc=cmd_func, pid=os.getpid())
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 runfile = os.path.join(d.getVar('T'), runfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032 bb.utils.mkdirhier(os.path.dirname(runfile))
33
34 with open(runfile, 'w') as script:
Patrick Williams0ca19cc2021-08-16 14:03:13 -050035 # 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 Bishop19323692019-04-05 15:28:33 -040038 script.write(bb.build.shell_trap_code())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 bb.data.emit_func(cmd_func, script, envdata)
40 script.write(cmd_func)
41 script.write("\n")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060042 os.chmod(runfile, 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043
44 return runfile
45
46def oe_terminal(command, title, d):
47 import oe.data
48 import oe.terminal
Patrick Williams0ca19cc2021-08-16 14:03:13 -050049
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 envdata = bb.data.init()
51
52 for v in os.environ:
53 envdata.setVar(v, os.environ[v])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050054 envdata.setVarFlag(v, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055
56 for export in oe.data.typed_value('OE_TERMINAL_EXPORTS', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050057 value = d.getVar(export)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 if value is not None:
59 os.environ[export] = str(value)
60 envdata.setVar(export, str(value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050061 envdata.setVarFlag(export, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 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 Bishop6e60e8b2018-02-01 10:27:11 -050073 value = origbbenv.getVar(key)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 if value is not None:
75 os.environ[key] = str(value)
76 envdata.setVar(key, str(value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050077 envdata.setVarFlag(key, 'export', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 # 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 Williamsc124f4f2015-09-15 14:41:29 -050084 # 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 Bishop6e60e8b2018-02-01 10:27:11 -0500106 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 Williamsc124f4f2015-09-15 14:41:29 -0500112 except oe.terminal.ExecutionError as exc:
113 bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
114
115oe_terminal[vardepsexclude] = "BB_ORIGENV"