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 | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 295 | parser.add_option("", "--runall", action="append", dest="runall", |
| 296 | help="Run the specified task for any recipe in the taskgraph of the specified target (even if it wouldn't otherwise have run).") |
| 297 | |
| 298 | parser.add_option("", "--runonly", action="append", dest="runonly", |
| 299 | help="Run only the specified task within the taskgraph of the specified targets (and any task dependencies those tasks may have).") |
| 300 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 301 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 302 | options, targets = parser.parse_args(argv) |
| 303 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 304 | if options.quiet and options.verbose: |
| 305 | parser.error("options --quiet and --verbose are mutually exclusive") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 306 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 307 | if options.quiet and options.debug: |
| 308 | parser.error("options --quiet and --debug are mutually exclusive") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 309 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 310 | # use configuration files from environment variables |
| 311 | if "BBPRECONF" in os.environ: |
| 312 | options.prefile.append(os.environ["BBPRECONF"]) |
| 313 | |
| 314 | if "BBPOSTCONF" in os.environ: |
| 315 | options.postfile.append(os.environ["BBPOSTCONF"]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 316 | |
| 317 | # fill in proper log name if not supplied |
| 318 | if options.writeeventlog is not None and len(options.writeeventlog) == 0: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 319 | from datetime import datetime |
| 320 | eventlog = "bitbake_eventlog_%s.json" % datetime.now().strftime("%Y%m%d%H%M%S") |
| 321 | options.writeeventlog = eventlog |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 322 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 323 | if options.bind: |
| 324 | try: |
| 325 | #Checking that the port is a number and is a ':' delimited value |
| 326 | (host, port) = options.bind.split(':') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 327 | port = int(port) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 328 | except (ValueError,IndexError): |
| 329 | raise BBMainException("FATAL: Malformed host:port bind parameter") |
| 330 | options.xmlrpcinterface = (host, port) |
| 331 | else: |
| 332 | options.xmlrpcinterface = (None, 0) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 333 | |
| 334 | return options, targets[1:] |
| 335 | |
| 336 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 337 | def bitbake_main(configParams, configuration): |
| 338 | |
| 339 | # Python multiprocessing requires /dev/shm on Linux |
| 340 | if sys.platform.startswith('linux') and not os.access('/dev/shm', os.W_OK | os.X_OK): |
| 341 | raise BBMainException("FATAL: /dev/shm does not exist or is not writable") |
| 342 | |
| 343 | # Unbuffer stdout to avoid log truncation in the event |
| 344 | # of an unorderly exit as well as to provide timely |
| 345 | # updates to log files for use with tail |
| 346 | try: |
| 347 | if sys.stdout.name == '<stdout>': |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 348 | # Reopen with O_SYNC (unbuffered) |
| 349 | fl = fcntl.fcntl(sys.stdout.fileno(), fcntl.F_GETFL) |
| 350 | fl |= os.O_SYNC |
| 351 | fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, fl) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 352 | except: |
| 353 | pass |
| 354 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 355 | configuration.setConfigParameters(configParams) |
| 356 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 357 | if configParams.server_only and configParams.remote_server: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 358 | raise BBMainException("FATAL: The '--server-only' option conflicts with %s.\n" % |
| 359 | ("the BBSERVER environment variable" if "BBSERVER" in os.environ \ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 360 | else "the '--remote-server' option")) |
| 361 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 362 | if configParams.observe_only and not (configParams.remote_server or configParams.bind): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 363 | raise BBMainException("FATAL: '--observe-only' can only be used by UI clients " |
| 364 | "connecting to a server.\n") |
| 365 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 366 | if "BBDEBUG" in os.environ: |
| 367 | level = int(os.environ["BBDEBUG"]) |
| 368 | if level > configuration.debug: |
| 369 | configuration.debug = level |
| 370 | |
| 371 | bb.msg.init_msgconfig(configParams.verbose, configuration.debug, |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 372 | configuration.debug_domains) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 373 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 374 | server_connection, ui_module = setup_bitbake(configParams, configuration) |
| 375 | # No server connection |
| 376 | if server_connection is None: |
| 377 | if configParams.status_only: |
| 378 | return 1 |
| 379 | if configParams.kill_server: |
| 380 | return 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 381 | |
| 382 | if not configParams.server_only: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | if configParams.status_only: |
| 384 | server_connection.terminate() |
| 385 | return 0 |
| 386 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 387 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 388 | for event in bb.event.ui_queue: |
| 389 | server_connection.events.queue_event(event) |
| 390 | bb.event.ui_queue = [] |
| 391 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 392 | return ui_module.main(server_connection.connection, server_connection.events, |
| 393 | configParams) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 394 | finally: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 395 | server_connection.terminate() |
| 396 | else: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 397 | return 0 |
| 398 | |
| 399 | return 1 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 400 | |
| 401 | def setup_bitbake(configParams, configuration, extrafeatures=None): |
| 402 | # Ensure logging messages get sent to the UI as events |
| 403 | handler = bb.event.LogHandler() |
| 404 | if not configParams.status_only: |
| 405 | # In status only mode there are no logs and no UI |
| 406 | logger.addHandler(handler) |
| 407 | |
| 408 | # Clear away any spurious environment variables while we stoke up the cooker |
| 409 | cleanedvars = bb.utils.clean_environment() |
| 410 | |
| 411 | if configParams.server_only: |
| 412 | featureset = [] |
| 413 | ui_module = None |
| 414 | else: |
| 415 | ui_module = import_extension_module(bb.ui, configParams.ui, 'main') |
| 416 | # Collect the feature set for the UI |
| 417 | featureset = getattr(ui_module, "featureSet", []) |
| 418 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 419 | if extrafeatures: |
| 420 | for feature in extrafeatures: |
| 421 | if not feature in featureset: |
| 422 | featureset.append(feature) |
| 423 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 424 | server_connection = None |
| 425 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 426 | if configParams.remote_server: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 427 | # Connect to a remote XMLRPC server |
| 428 | server_connection = bb.server.xmlrpcclient.connectXMLRPC(configParams.remote_server, featureset, |
| 429 | configParams.observe_only, configParams.xmlrpctoken) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 430 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 431 | retries = 8 |
| 432 | while retries: |
| 433 | try: |
| 434 | topdir, lock = lockBitbake() |
| 435 | sockname = topdir + "/bitbake.sock" |
| 436 | if lock: |
| 437 | if configParams.status_only or configParams.kill_server: |
| 438 | logger.info("bitbake server is not running.") |
| 439 | lock.close() |
| 440 | return None, None |
| 441 | # we start a server with a given configuration |
| 442 | logger.info("Starting bitbake server...") |
| 443 | # Clear the event queue since we already displayed messages |
| 444 | bb.event.ui_queue = [] |
| 445 | server = bb.server.process.BitBakeServer(lock, sockname, configuration, featureset) |
| 446 | |
| 447 | else: |
| 448 | logger.info("Reconnecting to bitbake server...") |
| 449 | if not os.path.exists(sockname): |
| 450 | print("Previous bitbake instance shutting down?, waiting to retry...") |
| 451 | i = 0 |
| 452 | lock = None |
| 453 | # Wait for 5s or until we can get the lock |
| 454 | while not lock and i < 50: |
| 455 | time.sleep(0.1) |
| 456 | _, lock = lockBitbake() |
| 457 | i += 1 |
| 458 | if lock: |
| 459 | bb.utils.unlockfile(lock) |
| 460 | raise bb.server.process.ProcessTimeout("Bitbake still shutting down as socket exists but no lock?") |
| 461 | if not configParams.server_only: |
| 462 | try: |
| 463 | server_connection = bb.server.process.connectProcessServer(sockname, featureset) |
| 464 | except EOFError: |
| 465 | # The server may have been shutting down but not closed the socket yet. If that happened, |
| 466 | # ignore it. |
| 467 | pass |
| 468 | |
| 469 | if server_connection or configParams.server_only: |
| 470 | break |
| 471 | except BBMainFatal: |
| 472 | raise |
| 473 | except (Exception, bb.server.process.ProcessTimeout) as e: |
| 474 | if not retries: |
| 475 | raise |
| 476 | retries -= 1 |
| 477 | if isinstance(e, (bb.server.process.ProcessTimeout, BrokenPipeError)): |
| 478 | logger.info("Retrying server connection...") |
| 479 | else: |
| 480 | logger.info("Retrying server connection... (%s)" % traceback.format_exc()) |
| 481 | if not retries: |
| 482 | bb.fatal("Unable to connect to bitbake server, or start one") |
| 483 | if retries < 5: |
| 484 | time.sleep(5) |
| 485 | |
| 486 | if configParams.kill_server: |
| 487 | server_connection.connection.terminateServer() |
| 488 | server_connection.terminate() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 489 | bb.event.ui_queue = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 490 | logger.info("Terminated bitbake server.") |
| 491 | return None, None |
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 | # Restore the environment in case the UI needs it |
| 494 | for k in cleanedvars: |
| 495 | os.environ[k] = cleanedvars[k] |
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 | logger.removeHandler(handler) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 498 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 499 | return server_connection, ui_module |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 500 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 501 | def lockBitbake(): |
| 502 | topdir = bb.cookerdata.findTopdir() |
| 503 | if not topdir: |
| 504 | bb.error("Unable to find conf/bblayers.conf or conf/bitbake.conf. BBAPTH is unset and/or not in a build directory?") |
| 505 | raise BBMainFatal |
| 506 | lockfile = topdir + "/bitbake.lock" |
| 507 | return topdir, bb.utils.lockfile(lockfile, False, False) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 508 | |