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