blob: 41dd3b9e03ac85a06b369e4bf639e46c7f765b95 [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 Bishop316dfdd2018-06-25 12:45:53 -0400295 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 Bishop6e60e8b2018-02-01 10:27:11 -0500301
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500302 options, targets = parser.parse_args(argv)
303
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600304 if options.quiet and options.verbose:
305 parser.error("options --quiet and --verbose are mutually exclusive")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500306
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600307 if options.quiet and options.debug:
308 parser.error("options --quiet and --debug are mutually exclusive")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600310 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500316
317 # fill in proper log name if not supplied
318 if options.writeeventlog is not None and len(options.writeeventlog) == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600319 from datetime import datetime
320 eventlog = "bitbake_eventlog_%s.json" % datetime.now().strftime("%Y%m%d%H%M%S")
321 options.writeeventlog = eventlog
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500322
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323 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 Williamsc0f7c042017-02-23 20:41:17 -0600327 port = int(port)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500328 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 Williamsc124f4f2015-09-15 14:41:29 -0500333
334 return options, targets[1:]
335
336
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500337def 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 Williamsc0f7c042017-02-23 20:41:17 -0600348 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500352 except:
353 pass
354
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500355 configuration.setConfigParameters(configParams)
356
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500357 if configParams.server_only and configParams.remote_server:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500358 raise BBMainException("FATAL: The '--server-only' option conflicts with %s.\n" %
359 ("the BBSERVER environment variable" if "BBSERVER" in os.environ \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600360 else "the '--remote-server' option"))
361
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500362 if configParams.observe_only and not (configParams.remote_server or configParams.bind):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500363 raise BBMainException("FATAL: '--observe-only' can only be used by UI clients "
364 "connecting to a server.\n")
365
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500366 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 Williamsc0f7c042017-02-23 20:41:17 -0600372 configuration.debug_domains)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500373
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500374 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 Williamsc124f4f2015-09-15 14:41:29 -0500381
382 if not configParams.server_only:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383 if configParams.status_only:
384 server_connection.terminate()
385 return 0
386
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500388 for event in bb.event.ui_queue:
389 server_connection.events.queue_event(event)
390 bb.event.ui_queue = []
391
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600392 return ui_module.main(server_connection.connection, server_connection.events,
393 configParams)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500394 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500395 server_connection.terminate()
396 else:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500397 return 0
398
399 return 1
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500400
401def 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500408 if configParams.server_only:
409 featureset = []
410 ui_module = None
411 else:
412 ui_module = import_extension_module(bb.ui, configParams.ui, 'main')
413 # Collect the feature set for the UI
414 featureset = getattr(ui_module, "featureSet", [])
415
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500416 if extrafeatures:
417 for feature in extrafeatures:
418 if not feature in featureset:
419 featureset.append(feature)
420
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500421 server_connection = None
422
Brad Bishop6ef32652018-10-09 18:59:25 +0100423 # Clear away any spurious environment variables while we stoke up the cooker
424 # (done after import_extension_module() above since for example import gi triggers env var usage)
425 cleanedvars = bb.utils.clean_environment()
426
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500427 if configParams.remote_server:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500428 # Connect to a remote XMLRPC server
429 server_connection = bb.server.xmlrpcclient.connectXMLRPC(configParams.remote_server, featureset,
430 configParams.observe_only, configParams.xmlrpctoken)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500431 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500432 retries = 8
433 while retries:
434 try:
435 topdir, lock = lockBitbake()
436 sockname = topdir + "/bitbake.sock"
437 if lock:
438 if configParams.status_only or configParams.kill_server:
439 logger.info("bitbake server is not running.")
440 lock.close()
441 return None, None
442 # we start a server with a given configuration
443 logger.info("Starting bitbake server...")
444 # Clear the event queue since we already displayed messages
445 bb.event.ui_queue = []
446 server = bb.server.process.BitBakeServer(lock, sockname, configuration, featureset)
447
448 else:
449 logger.info("Reconnecting to bitbake server...")
450 if not os.path.exists(sockname):
Brad Bishope2d5b612018-11-23 10:55:50 +1300451 logger.info("Previous bitbake instance shutting down?, waiting to retry...")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500452 i = 0
453 lock = None
454 # Wait for 5s or until we can get the lock
455 while not lock and i < 50:
456 time.sleep(0.1)
457 _, lock = lockBitbake()
458 i += 1
459 if lock:
460 bb.utils.unlockfile(lock)
461 raise bb.server.process.ProcessTimeout("Bitbake still shutting down as socket exists but no lock?")
462 if not configParams.server_only:
463 try:
464 server_connection = bb.server.process.connectProcessServer(sockname, featureset)
465 except EOFError:
466 # The server may have been shutting down but not closed the socket yet. If that happened,
467 # ignore it.
468 pass
469
470 if server_connection or configParams.server_only:
471 break
472 except BBMainFatal:
473 raise
474 except (Exception, bb.server.process.ProcessTimeout) as e:
475 if not retries:
476 raise
477 retries -= 1
Brad Bishop19323692019-04-05 15:28:33 -0400478 tryno = 8 - retries
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500479 if isinstance(e, (bb.server.process.ProcessTimeout, BrokenPipeError)):
Brad Bishop19323692019-04-05 15:28:33 -0400480 logger.info("Retrying server connection (#%d)..." % tryno)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500481 else:
Brad Bishop19323692019-04-05 15:28:33 -0400482 logger.info("Retrying server connection (#%d)... (%s)" % (tryno, traceback.format_exc()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500483 if not retries:
484 bb.fatal("Unable to connect to bitbake server, or start one")
485 if retries < 5:
486 time.sleep(5)
487
488 if configParams.kill_server:
489 server_connection.connection.terminateServer()
490 server_connection.terminate()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500491 bb.event.ui_queue = []
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500492 logger.info("Terminated bitbake server.")
493 return None, None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500494
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500495 # Restore the environment in case the UI needs it
496 for k in cleanedvars:
497 os.environ[k] = cleanedvars[k]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500498
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500499 logger.removeHandler(handler)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500500
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500501 return server_connection, ui_module
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500502
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500503def lockBitbake():
504 topdir = bb.cookerdata.findTopdir()
505 if not topdir:
506 bb.error("Unable to find conf/bblayers.conf or conf/bitbake.conf. BBAPTH is unset and/or not in a build directory?")
507 raise BBMainFatal
508 lockfile = topdir + "/bitbake.lock"
509 return topdir, bb.utils.lockfile(lockfile, False, False)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500510