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