Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2003, 2004 Chris Larson |
| 3 | # Copyright (C) 2003, 2004 Phil Blundell |
| 4 | # Copyright (C) 2003 - 2005 Michael 'Mickey' Lauer |
| 5 | # Copyright (C) 2005 Holger Hans Peter Freyther |
| 6 | # Copyright (C) 2005 ROAD GmbH |
| 7 | # Copyright (C) 2006 Richard Purdie |
| 8 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | |
| 12 | import os |
| 13 | import sys |
| 14 | import logging |
| 15 | import optparse |
| 16 | import warnings |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 17 | import fcntl |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 18 | import time |
| 19 | import traceback |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | |
| 21 | import bb |
| 22 | from bb import event |
| 23 | import bb.msg |
| 24 | from bb import cooker |
| 25 | from bb import ui |
| 26 | from bb import server |
| 27 | from bb import cookerdata |
| 28 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 29 | import bb.server.process |
| 30 | import bb.server.xmlrpcclient |
| 31 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | logger = logging.getLogger("BitBake") |
| 33 | |
| 34 | class BBMainException(Exception): |
| 35 | pass |
| 36 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 37 | class BBMainFatal(bb.BBHandledException): |
| 38 | pass |
| 39 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | def present_options(optionlist): |
| 41 | if len(optionlist) > 1: |
| 42 | return ' or '.join([', '.join(optionlist[:-1]), optionlist[-1]]) |
| 43 | else: |
| 44 | return optionlist[0] |
| 45 | |
| 46 | class BitbakeHelpFormatter(optparse.IndentedHelpFormatter): |
| 47 | def format_option(self, option): |
| 48 | # We need to do this here rather than in the text we supply to |
| 49 | # add_option() because we don't want to call list_extension_modules() |
| 50 | # on every execution (since it imports all of the modules) |
| 51 | # Note also that we modify option.help rather than the returned text |
| 52 | # - this is so that we don't have to re-format the text ourselves |
| 53 | if option.dest == 'ui': |
| 54 | valid_uis = list_extension_modules(bb.ui, 'main') |
| 55 | option.help = option.help.replace('@CHOICES@', present_options(valid_uis)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
| 57 | return optparse.IndentedHelpFormatter.format_option(self, option) |
| 58 | |
| 59 | def list_extension_modules(pkg, checkattr): |
| 60 | """ |
| 61 | Lists extension modules in a specific Python package |
| 62 | (e.g. UIs, servers). NOTE: Calling this function will import all of the |
| 63 | submodules of the specified module in order to check for the specified |
| 64 | attribute; this can have unusual side-effects. As a result, this should |
| 65 | only be called when displaying help text or error messages. |
| 66 | Parameters: |
| 67 | pkg: previously imported Python package to list |
| 68 | checkattr: attribute to look for in module to determine if it's valid |
| 69 | as the type of extension you are looking for |
| 70 | """ |
| 71 | import pkgutil |
| 72 | pkgdir = os.path.dirname(pkg.__file__) |
| 73 | |
| 74 | modules = [] |
| 75 | for _, modulename, _ in pkgutil.iter_modules([pkgdir]): |
| 76 | if os.path.isdir(os.path.join(pkgdir, modulename)): |
| 77 | # ignore directories |
| 78 | continue |
| 79 | try: |
| 80 | module = __import__(pkg.__name__, fromlist=[modulename]) |
| 81 | except: |
| 82 | # If we can't import it, it's not valid |
| 83 | continue |
| 84 | module_if = getattr(module, modulename) |
| 85 | if getattr(module_if, 'hidden_extension', False): |
| 86 | continue |
| 87 | if not checkattr or hasattr(module_if, checkattr): |
| 88 | modules.append(modulename) |
| 89 | return modules |
| 90 | |
| 91 | def import_extension_module(pkg, modulename, checkattr): |
| 92 | try: |
| 93 | # Dynamically load the UI based on the ui name. Although we |
| 94 | # suggest a fixed set this allows you to have flexibility in which |
| 95 | # ones are available. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 96 | module = __import__(pkg.__name__, fromlist=[modulename]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | return getattr(module, modulename) |
| 98 | except AttributeError: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 99 | modules = present_options(list_extension_modules(pkg, checkattr)) |
| 100 | raise BBMainException('FATAL: Unable to import extension module "%s" from %s. ' |
| 101 | 'Valid extension modules: %s' % (modulename, pkg.__name__, modules)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | |
| 103 | # Display bitbake/OE warnings via the BitBake.Warnings logger, ignoring others""" |
| 104 | warnlog = logging.getLogger("BitBake.Warnings") |
| 105 | _warnings_showwarning = warnings.showwarning |
| 106 | def _showwarning(message, category, filename, lineno, file=None, line=None): |
| 107 | if file is not None: |
| 108 | if _warnings_showwarning is not None: |
| 109 | _warnings_showwarning(message, category, filename, lineno, file, line) |
| 110 | else: |
| 111 | s = warnings.formatwarning(message, category, filename, lineno) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 112 | warnlog.warning(s) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | |
| 114 | warnings.showwarning = _showwarning |
| 115 | warnings.filterwarnings("ignore") |
| 116 | warnings.filterwarnings("default", module="(<string>$|(oe|bb)\.)") |
| 117 | warnings.filterwarnings("ignore", category=PendingDeprecationWarning) |
| 118 | warnings.filterwarnings("ignore", category=ImportWarning) |
| 119 | warnings.filterwarnings("ignore", category=DeprecationWarning, module="<string>$") |
| 120 | warnings.filterwarnings("ignore", message="With-statements now directly support multiple context managers") |
| 121 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 123 | def create_bitbake_parser(): |
| 124 | parser = optparse.OptionParser( |
| 125 | formatter=BitbakeHelpFormatter(), |
| 126 | version="BitBake Build Tool Core version %s" % bb.__version__, |
| 127 | usage="""%prog [options] [recipename/target recipe:do_task ...] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | |
| 129 | Executes the specified task (default is 'build') for a given set of target recipes (.bb files). |
| 130 | It is assumed there is a conf/bblayers.conf available in cwd or in BBPATH which |
| 131 | will provide the layer, BBFILES and other configuration information.""") |
| 132 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 133 | parser.add_option("-b", "--buildfile", action="store", dest="buildfile", default=None, |
| 134 | help="Execute tasks from a specific .bb recipe directly. WARNING: Does " |
| 135 | "not handle any dependencies from other recipes.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 137 | parser.add_option("-k", "--continue", action="store_false", dest="abort", default=True, |
| 138 | help="Continue as much as possible after an error. While the target that " |
| 139 | "failed and anything depending on it cannot be built, as much as " |
| 140 | "possible will be built before stopping.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 142 | parser.add_option("-f", "--force", action="store_true", dest="force", default=False, |
| 143 | help="Force the specified targets/task to run (invalidating any " |
| 144 | "existing stamp file).") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 146 | parser.add_option("-c", "--cmd", action="store", dest="cmd", |
| 147 | help="Specify the task to execute. The exact options available " |
| 148 | "depend on the metadata. Some examples might be 'compile'" |
| 149 | " or 'populate_sysroot' or 'listtasks' may give a list of " |
| 150 | "the tasks available.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 152 | parser.add_option("-C", "--clear-stamp", action="store", dest="invalidate_stamp", |
| 153 | help="Invalidate the stamp for the specified task such as 'compile' " |
| 154 | "and then run the default task for the specified target(s).") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 156 | parser.add_option("-r", "--read", action="append", dest="prefile", default=[], |
| 157 | help="Read the specified file before bitbake.conf.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 159 | parser.add_option("-R", "--postread", action="append", dest="postfile", default=[], |
| 160 | help="Read the specified file after bitbake.conf.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 161 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 162 | parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, |
| 163 | help="Enable tracing of shell tasks (with 'set -x'). " |
| 164 | "Also print bb.note(...) messages to stdout (in " |
| 165 | "addition to writing them to ${T}/log.do_<task>).") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 167 | parser.add_option("-D", "--debug", action="count", dest="debug", default=0, |
| 168 | help="Increase the debug level. You can specify this " |
| 169 | "more than once. -D sets the debug level to 1, " |
| 170 | "where only bb.debug(1, ...) messages are printed " |
| 171 | "to stdout; -DD sets the debug level to 2, where " |
| 172 | "both bb.debug(1, ...) and bb.debug(2, ...) " |
| 173 | "messages are printed; etc. Without -D, no debug " |
| 174 | "messages are printed. Note that -D only affects " |
| 175 | "output to stdout. All debug messages are written " |
| 176 | "to ${T}/log.do_taskname, regardless of the debug " |
| 177 | "level.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 178 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 179 | parser.add_option("-q", "--quiet", action="count", dest="quiet", default=0, |
| 180 | help="Output less log message data to the terminal. You can specify this more than once.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 181 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 182 | parser.add_option("-n", "--dry-run", action="store_true", dest="dry_run", default=False, |
| 183 | help="Don't execute, just go through the motions.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 184 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 185 | parser.add_option("-S", "--dump-signatures", action="append", dest="dump_signatures", |
| 186 | default=[], metavar="SIGNATURE_HANDLER", |
| 187 | help="Dump out the signature construction information, with no task " |
| 188 | "execution. The SIGNATURE_HANDLER parameter is passed to the " |
| 189 | "handler. Two common values are none and printdiff but the handler " |
| 190 | "may define more/less. none means only dump the signature, printdiff" |
| 191 | " means compare the dumped signature with the cached one.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 192 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 193 | parser.add_option("-p", "--parse-only", action="store_true", |
| 194 | dest="parse_only", default=False, |
| 195 | help="Quit after parsing the BB recipes.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 196 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 197 | parser.add_option("-s", "--show-versions", action="store_true", |
| 198 | dest="show_versions", default=False, |
| 199 | help="Show current and preferred versions of all recipes.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 200 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 201 | parser.add_option("-e", "--environment", action="store_true", |
| 202 | dest="show_environment", default=False, |
| 203 | help="Show the global or per-recipe environment complete with information" |
| 204 | " about where variables were set/changed.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 205 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 206 | parser.add_option("-g", "--graphviz", action="store_true", dest="dot_graph", default=False, |
| 207 | help="Save dependency tree information for the specified " |
| 208 | "targets in the dot syntax.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 209 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 210 | parser.add_option("-I", "--ignore-deps", action="append", |
| 211 | dest="extra_assume_provided", default=[], |
| 212 | help="Assume these dependencies don't exist and are already provided " |
| 213 | "(equivalent to ASSUME_PROVIDED). Useful to make dependency " |
| 214 | "graphs more appealing") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 215 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 216 | parser.add_option("-l", "--log-domains", action="append", dest="debug_domains", default=[], |
| 217 | help="Show debug logging for the specified logging domains") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 218 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 219 | parser.add_option("-P", "--profile", action="store_true", dest="profile", default=False, |
| 220 | help="Profile the command and save reports.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 221 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 222 | # @CHOICES@ is substituted out by BitbakeHelpFormatter above |
| 223 | parser.add_option("-u", "--ui", action="store", dest="ui", |
| 224 | default=os.environ.get('BITBAKE_UI', 'knotty'), |
| 225 | help="The user interface to use (@CHOICES@ - default %default).") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 226 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 227 | parser.add_option("", "--token", action="store", dest="xmlrpctoken", |
| 228 | default=os.environ.get("BBTOKEN"), |
| 229 | help="Specify the connection token to be used when connecting " |
| 230 | "to a remote server.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 231 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 232 | parser.add_option("", "--revisions-changed", action="store_true", |
| 233 | dest="revisions_changed", default=False, |
| 234 | help="Set the exit code depending on whether upstream floating " |
| 235 | "revisions have changed or not.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 236 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 237 | parser.add_option("", "--server-only", action="store_true", |
| 238 | dest="server_only", default=False, |
| 239 | help="Run bitbake without a UI, only starting a server " |
| 240 | "(cooker) process.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 241 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 242 | parser.add_option("-B", "--bind", action="store", dest="bind", default=False, |
| 243 | help="The name/address for the bitbake xmlrpc server to bind to.") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 244 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 245 | parser.add_option("-T", "--idle-timeout", type=float, dest="server_timeout", |
| 246 | default=os.getenv("BB_SERVER_TIMEOUT"), |
| 247 | help="Set timeout to unload bitbake server due to inactivity, " |
| 248 | "set to -1 means no unload, " |
| 249 | "default: Environment variable BB_SERVER_TIMEOUT.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 250 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 251 | parser.add_option("", "--no-setscene", action="store_true", |
| 252 | dest="nosetscene", default=False, |
| 253 | help="Do not run any setscene tasks. sstate will be ignored and " |
| 254 | "everything needed, built.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 255 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 256 | parser.add_option("", "--skip-setscene", action="store_true", |
| 257 | dest="skipsetscene", default=False, |
| 258 | help="Skip setscene tasks if they would be executed. Tasks previously " |
| 259 | "restored from sstate will be kept, unlike --no-setscene") |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 260 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 261 | parser.add_option("", "--setscene-only", action="store_true", |
| 262 | dest="setsceneonly", default=False, |
| 263 | help="Only run setscene tasks, don't run any real tasks.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 264 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 265 | parser.add_option("", "--remote-server", action="store", dest="remote_server", |
| 266 | default=os.environ.get("BBSERVER"), |
| 267 | help="Connect to the specified server.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 268 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 269 | parser.add_option("-m", "--kill-server", action="store_true", |
| 270 | dest="kill_server", default=False, |
| 271 | help="Terminate any running bitbake server.") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 272 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 273 | parser.add_option("", "--observe-only", action="store_true", |
| 274 | dest="observe_only", default=False, |
| 275 | help="Connect to a server as an observing-only client.") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 276 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 277 | parser.add_option("", "--status-only", action="store_true", |
| 278 | dest="status_only", default=False, |
| 279 | help="Check the status of the remote bitbake server.") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 280 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 281 | parser.add_option("-w", "--write-log", action="store", dest="writeeventlog", |
| 282 | default=os.environ.get("BBEVENTLOG"), |
| 283 | help="Writes the event log of the build to a bitbake event json file. " |
| 284 | "Use '' (empty string) to assign the name automatically.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 285 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 286 | parser.add_option("", "--runall", action="append", dest="runall", |
| 287 | help="Run the specified task for any recipe in the taskgraph of the specified target (even if it wouldn't otherwise have run).") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 288 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 289 | parser.add_option("", "--runonly", action="append", dest="runonly", |
| 290 | help="Run only the specified task within the taskgraph of the specified targets (and any task dependencies those tasks may have).") |
| 291 | return parser |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 292 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 293 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 294 | class BitBakeConfigParameters(cookerdata.ConfigParameters): |
| 295 | def parseCommandLine(self, argv=sys.argv): |
| 296 | parser = create_bitbake_parser() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 297 | options, targets = parser.parse_args(argv) |
| 298 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 299 | if options.quiet and options.verbose: |
| 300 | parser.error("options --quiet and --verbose are mutually exclusive") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 301 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 302 | if options.quiet and options.debug: |
| 303 | parser.error("options --quiet and --debug are mutually exclusive") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 304 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 305 | # use configuration files from environment variables |
| 306 | if "BBPRECONF" in os.environ: |
| 307 | options.prefile.append(os.environ["BBPRECONF"]) |
| 308 | |
| 309 | if "BBPOSTCONF" in os.environ: |
| 310 | options.postfile.append(os.environ["BBPOSTCONF"]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 311 | |
| 312 | # fill in proper log name if not supplied |
| 313 | if options.writeeventlog is not None and len(options.writeeventlog) == 0: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 314 | from datetime import datetime |
| 315 | eventlog = "bitbake_eventlog_%s.json" % datetime.now().strftime("%Y%m%d%H%M%S") |
| 316 | options.writeeventlog = eventlog |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 317 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 318 | if options.bind: |
| 319 | try: |
| 320 | #Checking that the port is a number and is a ':' delimited value |
| 321 | (host, port) = options.bind.split(':') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 322 | port = int(port) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 323 | except (ValueError,IndexError): |
| 324 | raise BBMainException("FATAL: Malformed host:port bind parameter") |
| 325 | options.xmlrpcinterface = (host, port) |
| 326 | else: |
| 327 | options.xmlrpcinterface = (None, 0) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 328 | |
| 329 | return options, targets[1:] |
| 330 | |
| 331 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 332 | def bitbake_main(configParams, configuration): |
| 333 | |
| 334 | # Python multiprocessing requires /dev/shm on Linux |
| 335 | if sys.platform.startswith('linux') and not os.access('/dev/shm', os.W_OK | os.X_OK): |
| 336 | raise BBMainException("FATAL: /dev/shm does not exist or is not writable") |
| 337 | |
| 338 | # Unbuffer stdout to avoid log truncation in the event |
| 339 | # of an unorderly exit as well as to provide timely |
| 340 | # updates to log files for use with tail |
| 341 | try: |
| 342 | if sys.stdout.name == '<stdout>': |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 343 | # Reopen with O_SYNC (unbuffered) |
| 344 | fl = fcntl.fcntl(sys.stdout.fileno(), fcntl.F_GETFL) |
| 345 | fl |= os.O_SYNC |
| 346 | fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, fl) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 347 | except: |
| 348 | pass |
| 349 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 350 | if configParams.server_only and configParams.remote_server: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 351 | raise BBMainException("FATAL: The '--server-only' option conflicts with %s.\n" % |
| 352 | ("the BBSERVER environment variable" if "BBSERVER" in os.environ \ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 353 | else "the '--remote-server' option")) |
| 354 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 355 | if configParams.observe_only and not (configParams.remote_server or configParams.bind): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 356 | raise BBMainException("FATAL: '--observe-only' can only be used by UI clients " |
| 357 | "connecting to a server.\n") |
| 358 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 359 | if "BBDEBUG" in os.environ: |
| 360 | level = int(os.environ["BBDEBUG"]) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 361 | if level > configParams.debug: |
| 362 | configParams.debug = level |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 363 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 364 | bb.msg.init_msgconfig(configParams.verbose, configParams.debug, |
| 365 | configParams.debug_domains) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 366 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 367 | server_connection, ui_module = setup_bitbake(configParams) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 368 | # No server connection |
| 369 | if server_connection is None: |
| 370 | if configParams.status_only: |
| 371 | return 1 |
| 372 | if configParams.kill_server: |
| 373 | return 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 374 | |
| 375 | if not configParams.server_only: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 376 | if configParams.status_only: |
| 377 | server_connection.terminate() |
| 378 | return 0 |
| 379 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 380 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 381 | for event in bb.event.ui_queue: |
| 382 | server_connection.events.queue_event(event) |
| 383 | bb.event.ui_queue = [] |
| 384 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 385 | return ui_module.main(server_connection.connection, server_connection.events, |
| 386 | configParams) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 387 | finally: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 388 | server_connection.terminate() |
| 389 | else: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 390 | return 0 |
| 391 | |
| 392 | return 1 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 393 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 394 | def setup_bitbake(configParams, extrafeatures=None): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 395 | # Ensure logging messages get sent to the UI as events |
| 396 | handler = bb.event.LogHandler() |
| 397 | if not configParams.status_only: |
| 398 | # In status only mode there are no logs and no UI |
| 399 | logger.addHandler(handler) |
| 400 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 401 | if configParams.server_only: |
| 402 | featureset = [] |
| 403 | ui_module = None |
| 404 | else: |
| 405 | ui_module = import_extension_module(bb.ui, configParams.ui, 'main') |
| 406 | # Collect the feature set for the UI |
| 407 | featureset = getattr(ui_module, "featureSet", []) |
| 408 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 409 | if extrafeatures: |
| 410 | for feature in extrafeatures: |
| 411 | if not feature in featureset: |
| 412 | featureset.append(feature) |
| 413 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 414 | server_connection = None |
| 415 | |
Brad Bishop | 6ef3265 | 2018-10-09 18:59:25 +0100 | [diff] [blame] | 416 | # Clear away any spurious environment variables while we stoke up the cooker |
| 417 | # (done after import_extension_module() above since for example import gi triggers env var usage) |
| 418 | cleanedvars = bb.utils.clean_environment() |
| 419 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 420 | if configParams.remote_server: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 421 | # Connect to a remote XMLRPC server |
| 422 | server_connection = bb.server.xmlrpcclient.connectXMLRPC(configParams.remote_server, featureset, |
| 423 | configParams.observe_only, configParams.xmlrpctoken) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 424 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 425 | retries = 8 |
| 426 | while retries: |
| 427 | try: |
| 428 | topdir, lock = lockBitbake() |
| 429 | sockname = topdir + "/bitbake.sock" |
| 430 | if lock: |
| 431 | if configParams.status_only or configParams.kill_server: |
| 432 | logger.info("bitbake server is not running.") |
| 433 | lock.close() |
| 434 | return None, None |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 435 | # we start a server with a given featureset |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 436 | logger.info("Starting bitbake server...") |
| 437 | # Clear the event queue since we already displayed messages |
| 438 | bb.event.ui_queue = [] |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 439 | server = bb.server.process.BitBakeServer(lock, sockname, featureset, configParams.server_timeout, configParams.xmlrpcinterface) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 440 | |
| 441 | else: |
| 442 | logger.info("Reconnecting to bitbake server...") |
| 443 | if not os.path.exists(sockname): |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 444 | logger.info("Previous bitbake instance shutting down?, waiting to retry...") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 445 | i = 0 |
| 446 | lock = None |
| 447 | # Wait for 5s or until we can get the lock |
| 448 | while not lock and i < 50: |
| 449 | time.sleep(0.1) |
| 450 | _, lock = lockBitbake() |
| 451 | i += 1 |
| 452 | if lock: |
| 453 | bb.utils.unlockfile(lock) |
| 454 | raise bb.server.process.ProcessTimeout("Bitbake still shutting down as socket exists but no lock?") |
| 455 | if not configParams.server_only: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 456 | server_connection = bb.server.process.connectProcessServer(sockname, featureset) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 457 | |
| 458 | if server_connection or configParams.server_only: |
| 459 | break |
| 460 | except BBMainFatal: |
| 461 | raise |
Andrew Geissler | 4c19ea1 | 2020-10-27 13:52:24 -0500 | [diff] [blame] | 462 | except (Exception, bb.server.process.ProcessTimeout, SystemExit) as e: |
| 463 | # SystemExit does not inherit from the Exception class, needs to be included explicitly |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 464 | if not retries: |
| 465 | raise |
| 466 | retries -= 1 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 467 | tryno = 8 - retries |
Andrew Geissler | 4c19ea1 | 2020-10-27 13:52:24 -0500 | [diff] [blame] | 468 | if isinstance(e, (bb.server.process.ProcessTimeout, BrokenPipeError, EOFError, SystemExit)): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 469 | logger.info("Retrying server connection (#%d)..." % tryno) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 470 | else: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 471 | logger.info("Retrying server connection (#%d)... (%s)" % (tryno, traceback.format_exc())) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 472 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 473 | if not retries: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 474 | bb.fatal("Unable to connect to bitbake server, or start one (server startup failures would be in bitbake-cookerdaemon.log).") |
| 475 | bb.event.print_ui_queue() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 476 | if retries < 5: |
| 477 | time.sleep(5) |
| 478 | |
| 479 | if configParams.kill_server: |
| 480 | server_connection.connection.terminateServer() |
| 481 | server_connection.terminate() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 482 | bb.event.ui_queue = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 483 | logger.info("Terminated bitbake server.") |
| 484 | return None, None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 485 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 486 | # Restore the environment in case the UI needs it |
| 487 | for k in cleanedvars: |
| 488 | os.environ[k] = cleanedvars[k] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 489 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 490 | logger.removeHandler(handler) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 491 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 492 | return server_connection, ui_module |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 493 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 494 | def lockBitbake(): |
| 495 | topdir = bb.cookerdata.findTopdir() |
| 496 | if not topdir: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 497 | bb.error("Unable to find conf/bblayers.conf or conf/bitbake.conf. BBPATH is unset and/or not in a build directory?") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 498 | raise BBMainFatal |
| 499 | lockfile = topdir + "/bitbake.lock" |
| 500 | return topdir, bb.utils.lockfile(lockfile, False, False) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 501 | |