blob: 7711b290de99c76d15af92591148795784f50ff5 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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
25import os
26import sys
27import logging
28import optparse
29import warnings
Patrick Williamsc0f7c042017-02-23 20:41:17 -060030import fcntl
Brad Bishopd7bf8c12018-02-25 22:55:05 -050031import time
32import traceback
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
34import bb
35from bb import event
36import bb.msg
37from bb import cooker
38from bb import ui
39from bb import server
40from bb import cookerdata
41
Brad Bishopd7bf8c12018-02-25 22:55:05 -050042import bb.server.process
43import bb.server.xmlrpcclient
44
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045logger = logging.getLogger("BitBake")
46
47class BBMainException(Exception):
48 pass
49
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050class BBMainFatal(bb.BBHandledException):
51 pass
52
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053def present_options(optionlist):
54 if len(optionlist) > 1:
55 return ' or '.join([', '.join(optionlist[:-1]), optionlist[-1]])
56 else:
57 return optionlist[0]
58
59class 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 Williamsc124f4f2015-09-15 14:41:29 -050069
70 return optparse.IndentedHelpFormatter.format_option(self, option)
71
72def 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
104def 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 Williamsc0f7c042017-02-23 20:41:17 -0600109 module = __import__(pkg.__name__, fromlist=[modulename])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 return getattr(module, modulename)
111 except AttributeError:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600112 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 Williamsc124f4f2015-09-15 14:41:29 -0500115
116# Display bitbake/OE warnings via the BitBake.Warnings logger, ignoring others"""
117warnlog = logging.getLogger("BitBake.Warnings")
118_warnings_showwarning = warnings.showwarning
119def _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 Williamsc0f7c042017-02-23 20:41:17 -0600125 warnlog.warning(s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126
127warnings.showwarning = _showwarning
128warnings.filterwarnings("ignore")
129warnings.filterwarnings("default", module="(<string>$|(oe|bb)\.)")
130warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
131warnings.filterwarnings("ignore", category=ImportWarning)
132warnings.filterwarnings("ignore", category=DeprecationWarning, module="<string>$")
133warnings.filterwarnings("ignore", message="With-statements now directly support multiple context managers")
134
135class BitBakeConfigParameters(cookerdata.ConfigParameters):
136
137 def parseCommandLine(self, argv=sys.argv):
138 parser = optparse.OptionParser(
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600139 formatter=BitbakeHelpFormatter(),
140 version="BitBake Build Tool Core version %s" % bb.__version__,
141 usage="""%prog [options] [recipename/target recipe:do_task ...]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142
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 Williamsc0f7c042017-02-23 20:41:17 -0600147 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 Williamsc124f4f2015-09-15 14:41:29 -0500150
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600151 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 Williamsc124f4f2015-09-15 14:41:29 -0500155
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156 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 Williamsc124f4f2015-09-15 14:41:29 -0500159
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600160 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 Williamsc124f4f2015-09-15 14:41:29 -0500165
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600166 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 Williamsc124f4f2015-09-15 14:41:29 -0500169
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600170 parser.add_option("-r", "--read", action="append", dest="prefile", default=[],
171 help="Read the specified file before bitbake.conf.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600173 parser.add_option("-R", "--postread", action="append", dest="postfile", default=[],
174 help="Read the specified file after bitbake.conf.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600176 parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 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 Williamsc124f4f2015-09-15 14:41:29 -0500180
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600181 parser.add_option("-D", "--debug", action="count", dest="debug", default=0,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 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 Williamsc124f4f2015-09-15 14:41:29 -0500192
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500193 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 Williamsc124f4f2015-09-15 14:41:29 -0500195
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600196 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 Williamsc124f4f2015-09-15 14:41:29 -0500198
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600199 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 Williamsc124f4f2015-09-15 14:41:29 -0500206
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207 parser.add_option("-p", "--parse-only", action="store_true",
208 dest="parse_only", default=False,
209 help="Quit after parsing the BB recipes.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600211 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 Williamsc124f4f2015-09-15 14:41:29 -0500214
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600215 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 Williamsc124f4f2015-09-15 14:41:29 -0500219
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600220 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 Williamsc124f4f2015-09-15 14:41:29 -0500223
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600224 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 Williamsc124f4f2015-09-15 14:41:29 -0500229
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600230 parser.add_option("-l", "--log-domains", action="append", dest="debug_domains", default=[],
231 help="Show debug logging for the specified logging domains")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233 parser.add_option("-P", "--profile", action="store_true", dest="profile", default=False,
234 help="Profile the command and save reports.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500235
236 # @CHOICES@ is substituted out by BitbakeHelpFormatter above
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600237 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 Williamsc124f4f2015-09-15 14:41:29 -0500240
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600241 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 Williamsc124f4f2015-09-15 14:41:29 -0500245
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600246 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 Williamsc124f4f2015-09-15 14:41:29 -0500250
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600251 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 Williamsc124f4f2015-09-15 14:41:29 -0500255
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600256 parser.add_option("-B", "--bind", action="store", dest="bind", default=False,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500257 help="The name/address for the bitbake xmlrpc server to bind to.")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500258
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500259 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 Williamsc124f4f2015-09-15 14:41:29 -0500264
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600265 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 Williamsc124f4f2015-09-15 14:41:29 -0500269
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600270 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 Williamsc124f4f2015-09-15 14:41:29 -0500273
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600274 parser.add_option("", "--remote-server", action="store", dest="remote_server",
275 default=os.environ.get("BBSERVER"),
276 help="Connect to the specified server.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600278 parser.add_option("-m", "--kill-server", action="store_true",
279 dest="kill_server", default=False,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500280 help="Terminate any running bitbake server.")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600281
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 Williamsc124f4f2015-09-15 14:41:29 -0500294
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500295 parser.add_option("", "--runall", action="store", dest="runall",
296 help="Run the specified task for all build targets and their dependencies.")
297
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500298 options, targets = parser.parse_args(argv)
299
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600300 if options.quiet and options.verbose:
301 parser.error("options --quiet and --verbose are mutually exclusive")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500302
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600303 if options.quiet and options.debug:
304 parser.error("options --quiet and --debug are mutually exclusive")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600306 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500312
313 # fill in proper log name if not supplied
314 if options.writeeventlog is not None and len(options.writeeventlog) == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600315 from datetime import datetime
316 eventlog = "bitbake_eventlog_%s.json" % datetime.now().strftime("%Y%m%d%H%M%S")
317 options.writeeventlog = eventlog
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500318
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500319 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 Williamsc0f7c042017-02-23 20:41:17 -0600323 port = int(port)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324 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 Williamsc124f4f2015-09-15 14:41:29 -0500329
330 return options, targets[1:]
331
332
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500333def 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 Williamsc0f7c042017-02-23 20:41:17 -0600344 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500348 except:
349 pass
350
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351 configuration.setConfigParameters(configParams)
352
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500353 if configParams.server_only and configParams.remote_server:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354 raise BBMainException("FATAL: The '--server-only' option conflicts with %s.\n" %
355 ("the BBSERVER environment variable" if "BBSERVER" in os.environ \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600356 else "the '--remote-server' option"))
357
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500358 if configParams.observe_only and not (configParams.remote_server or configParams.bind):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500359 raise BBMainException("FATAL: '--observe-only' can only be used by UI clients "
360 "connecting to a server.\n")
361
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500362 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 Williamsc0f7c042017-02-23 20:41:17 -0600368 configuration.debug_domains)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500370 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 Williamsc124f4f2015-09-15 14:41:29 -0500377
378 if not configParams.server_only:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500379 if configParams.status_only:
380 server_connection.terminate()
381 return 0
382
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500384 for event in bb.event.ui_queue:
385 server_connection.events.queue_event(event)
386 bb.event.ui_queue = []
387
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600388 return ui_module.main(server_connection.connection, server_connection.events,
389 configParams)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500390 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500391 server_connection.terminate()
392 else:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500393 return 0
394
395 return 1
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500396
397def 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 Bishop6e60e8b2018-02-01 10:27:11 -0500415 if extrafeatures:
416 for feature in extrafeatures:
417 if not feature in featureset:
418 featureset.append(feature)
419
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500420 server_connection = None
421
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500422 if configParams.remote_server:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500423 # Connect to a remote XMLRPC server
424 server_connection = bb.server.xmlrpcclient.connectXMLRPC(configParams.remote_server, featureset,
425 configParams.observe_only, configParams.xmlrpctoken)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500426 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500427 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 Bishop6e60e8b2018-02-01 10:27:11 -0500485 bb.event.ui_queue = []
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500486 logger.info("Terminated bitbake server.")
487 return None, None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500488
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500489 # Restore the environment in case the UI needs it
490 for k in cleanedvars:
491 os.environ[k] = cleanedvars[k]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500492
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500493 logger.removeHandler(handler)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500494
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500495 return server_connection, ui_module
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500496
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500497def 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 Bishop6e60e8b2018-02-01 10:27:11 -0500504