1OE_TERMINAL ?= 'auto'
2OE_TERMINAL[type] = 'choice'
3OE_TERMINAL[choices] = 'auto none \
4                        ${@oe_terminal_prioritized()}'
5
6OE_TERMINAL_EXPORTS += 'EXTRA_OEMAKE CACHED_CONFIGUREVARS CONFIGUREOPTS EXTRA_OECONF'
7OE_TERMINAL_EXPORTS[type] = 'list'
8
9XAUTHORITY ?= "${HOME}/.Xauthority"
10SHELL ?= "bash"
11
12def oe_terminal_prioritized():
13    import oe.terminal
14    return " ".join(o.name for o in oe.terminal.prioritized())
15
16def emit_terminal_func(command, envdata, d):
17    import bb.build
18    cmd_func = 'do_terminal'
19
20    envdata.setVar(cmd_func, 'exec ' + command)
21    envdata.setVarFlag(cmd_func, 'func', '1')
22
23    runfmt = d.getVar('BB_RUNFMT') or "run.{func}.{pid}"
24    runfile = runfmt.format(func=cmd_func, task=cmd_func, taskfunc=cmd_func, pid=os.getpid())
25    runfile = os.path.join(d.getVar('T'), runfile)
26    bb.utils.mkdirhier(os.path.dirname(runfile))
27
28    with open(runfile, 'w') as script:
29        # 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")
32        script.write(bb.build.shell_trap_code())
33        bb.data.emit_func(cmd_func, script, envdata)
34        script.write(cmd_func)
35        script.write("\n")
36    os.chmod(runfile, 0o755)
37
38    return runfile
39
40def oe_terminal(command, title, d):
41    import oe.data
42    import oe.terminal
43
44    envdata = bb.data.init()
45
46    for v in os.environ:
47        envdata.setVar(v, os.environ[v])
48        envdata.setVarFlag(v, 'export', '1')
49
50    for export in oe.data.typed_value('OE_TERMINAL_EXPORTS', d):
51        value = d.getVar(export)
52        if value is not None:
53            os.environ[export] = str(value)
54            envdata.setVar(export, str(value))
55            envdata.setVarFlag(export, 'export', '1')
56        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
67        value = origbbenv.getVar(key)
68        if value is not None:
69            os.environ[key] = str(value)
70            envdata.setVar(key, str(value))
71            envdata.setVarFlag(key, 'export', '1')
72
73    # 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
78    # 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)
100    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)
106    except oe.terminal.ExecutionError as exc:
107        bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
108
109oe_terminal[vardepsexclude] = "BB_ORIGENV"
110