blob: 7990195eac2de985b3538156f5f59081e428182d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Copyright (C) 2003, 2004 Chris Larson
3# Copyright (C) 2003, 2004 Phil Blundell
4# Copyright (C) 2003 - 2005 Michael 'Mickey' Lauer
5# Copyright (C) 2005 Holger Hans Peter Freyther
6# Copyright (C) 2005 ROAD GmbH
7# Copyright (C) 2006 Richard Purdie
8#
Brad Bishopc342db32019-05-15 21:57:59 -04009# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
12import os
13import sys
14import logging
15import optparse
16import warnings
Patrick Williamsc0f7c042017-02-23 20:41:17 -060017import fcntl
Brad Bishopd7bf8c12018-02-25 22:55:05 -050018import time
19import traceback
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020
21import bb
22from bb import event
23import bb.msg
24from bb import cooker
25from bb import ui
26from bb import server
27from bb import cookerdata
28
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029import bb.server.process
30import bb.server.xmlrpcclient
31
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032logger = logging.getLogger("BitBake")
33
34class BBMainException(Exception):
35 pass
36
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037class BBMainFatal(bb.BBHandledException):
38 pass
39
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040def present_options(optionlist):
41 if len(optionlist) > 1:
42 return ' or '.join([', '.join(optionlist[:-1]), optionlist[-1]])
43 else:
44 return optionlist[0]
45
46class BitbakeHelpFormatter(optparse.IndentedHelpFormatter):
47 def format_option(self, option):
48 # We need to do this here rather than in the text we supply to
49 # add_option() because we don't want to call list_extension_modules()
50 # on every execution (since it imports all of the modules)
51 # Note also that we modify option.help rather than the returned text
52 # - this is so that we don't have to re-format the text ourselves
53 if option.dest == 'ui':
54 valid_uis = list_extension_modules(bb.ui, 'main')
55 option.help = option.help.replace('@CHOICES@', present_options(valid_uis))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056
57 return optparse.IndentedHelpFormatter.format_option(self, option)
58
59def list_extension_modules(pkg, checkattr):
60 """
61 Lists extension modules in a specific Python package
62 (e.g. UIs, servers). NOTE: Calling this function will import all of the
63 submodules of the specified module in order to check for the specified
64 attribute; this can have unusual side-effects. As a result, this should
65 only be called when displaying help text or error messages.
66 Parameters:
67 pkg: previously imported Python package to list
68 checkattr: attribute to look for in module to determine if it's valid
69 as the type of extension you are looking for
70 """
71 import pkgutil
72 pkgdir = os.path.dirname(pkg.__file__)
73
74 modules = []
75 for _, modulename, _ in pkgutil.iter_modules([pkgdir]):
76 if os.path.isdir(os.path.join(pkgdir, modulename)):
77 # ignore directories
78 continue
79 try:
80 module = __import__(pkg.__name__, fromlist=[modulename])
81 except:
82 # If we can't import it, it's not valid
83 continue
84 module_if = getattr(module, modulename)
85 if getattr(module_if, 'hidden_extension', False):
86 continue
87 if not checkattr or hasattr(module_if, checkattr):
88 modules.append(modulename)
89 return modules
90
91def import_extension_module(pkg, modulename, checkattr):
92 try:
93 # Dynamically load the UI based on the ui name. Although we
94 # suggest a fixed set this allows you to have flexibility in which
95 # ones are available.
Patrick Williamsc0f7c042017-02-23 20:41:17 -060096 module = __import__(pkg.__name__, fromlist=[modulename])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 return getattr(module, modulename)
98 except AttributeError:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060099 modules = present_options(list_extension_modules(pkg, checkattr))
100 raise BBMainException('FATAL: Unable to import extension module "%s" from %s. '
101 'Valid extension modules: %s' % (modulename, pkg.__name__, modules))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
103# Display bitbake/OE warnings via the BitBake.Warnings logger, ignoring others"""
104warnlog = logging.getLogger("BitBake.Warnings")
105_warnings_showwarning = warnings.showwarning
106def _showwarning(message, category, filename, lineno, file=None, line=None):
107 if file is not None:
108 if _warnings_showwarning is not None:
109 _warnings_showwarning(message, category, filename, lineno, file, line)
110 else:
111 s = warnings.formatwarning(message, category, filename, lineno)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600112 warnlog.warning(s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113
114warnings.showwarning = _showwarning
115warnings.filterwarnings("ignore")
116warnings.filterwarnings("default", module="(<string>$|(oe|bb)\.)")
117warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
118warnings.filterwarnings("ignore", category=ImportWarning)
119warnings.filterwarnings("ignore", category=DeprecationWarning, module="<string>$")
120warnings.filterwarnings("ignore", message="With-statements now directly support multiple context managers")
121
122class BitBakeConfigParameters(cookerdata.ConfigParameters):
123
124 def parseCommandLine(self, argv=sys.argv):
125 parser = optparse.OptionParser(
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600126 formatter=BitbakeHelpFormatter(),
127 version="BitBake Build Tool Core version %s" % bb.__version__,
128 usage="""%prog [options] [recipename/target recipe:do_task ...]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129
130 Executes the specified task (default is 'build') for a given set of target recipes (.bb files).
131 It is assumed there is a conf/bblayers.conf available in cwd or in BBPATH which
132 will provide the layer, BBFILES and other configuration information.""")
133
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600134 parser.add_option("-b", "--buildfile", action="store", dest="buildfile", default=None,
135 help="Execute tasks from a specific .bb recipe directly. WARNING: Does "
136 "not handle any dependencies from other recipes.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600138 parser.add_option("-k", "--continue", action="store_false", dest="abort", default=True,
139 help="Continue as much as possible after an error. While the target that "
140 "failed and anything depending on it cannot be built, as much as "
141 "possible will be built before stopping.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600143 parser.add_option("-f", "--force", action="store_true", dest="force", default=False,
144 help="Force the specified targets/task to run (invalidating any "
145 "existing stamp file).")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600147 parser.add_option("-c", "--cmd", action="store", dest="cmd",
148 help="Specify the task to execute. The exact options available "
149 "depend on the metadata. Some examples might be 'compile'"
150 " or 'populate_sysroot' or 'listtasks' may give a list of "
151 "the tasks available.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600153 parser.add_option("-C", "--clear-stamp", action="store", dest="invalidate_stamp",
154 help="Invalidate the stamp for the specified task such as 'compile' "
155 "and then run the default task for the specified target(s).")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600157 parser.add_option("-r", "--read", action="append", dest="prefile", default=[],
158 help="Read the specified file before bitbake.conf.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600160 parser.add_option("-R", "--postread", action="append", dest="postfile", default=[],
161 help="Read the specified file after bitbake.conf.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600163 parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 help="Enable tracing of shell tasks (with 'set -x'). "
165 "Also print bb.note(...) messages to stdout (in "
166 "addition to writing them to ${T}/log.do_<task>).")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600168 parser.add_option("-D", "--debug", action="count", dest="debug", default=0,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500169 help="Increase the debug level. You can specify this "
170 "more than once. -D sets the debug level to 1, "
171 "where only bb.debug(1, ...) messages are printed "
172 "to stdout; -DD sets the debug level to 2, where "
173 "both bb.debug(1, ...) and bb.debug(2, ...) "
174 "messages are printed; etc. Without -D, no debug "
175 "messages are printed. Note that -D only affects "
176 "output to stdout. All debug messages are written "
177 "to ${T}/log.do_taskname, regardless of the debug "
178 "level.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500180 parser.add_option("-q", "--quiet", action="count", dest="quiet", default=0,
181 help="Output less log message data to the terminal. You can specify this more than once.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600183 parser.add_option("-n", "--dry-run", action="store_true", dest="dry_run", default=False,
184 help="Don't execute, just go through the motions.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600186 parser.add_option("-S", "--dump-signatures", action="append", dest="dump_signatures",
187 default=[], metavar="SIGNATURE_HANDLER",
188 help="Dump out the signature construction information, with no task "
189 "execution. The SIGNATURE_HANDLER parameter is passed to the "
190 "handler. Two common values are none and printdiff but the handler "
191 "may define more/less. none means only dump the signature, printdiff"
192 " means compare the dumped signature with the cached one.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600194 parser.add_option("-p", "--parse-only", action="store_true",
195 dest="parse_only", default=False,
196 help="Quit after parsing the BB recipes.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600198 parser.add_option("-s", "--show-versions", action="store_true",
199 dest="show_versions", default=False,
200 help="Show current and preferred versions of all recipes.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202 parser.add_option("-e", "--environment", action="store_true",
203 dest="show_environment", default=False,
204 help="Show the global or per-recipe environment complete with information"
205 " about where variables were set/changed.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207 parser.add_option("-g", "--graphviz", action="store_true", dest="dot_graph", default=False,
208 help="Save dependency tree information for the specified "
209 "targets in the dot syntax.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600211 parser.add_option("-I", "--ignore-deps", action="append",
212 dest="extra_assume_provided", default=[],
213 help="Assume these dependencies don't exist and are already provided "
214 "(equivalent to ASSUME_PROVIDED). Useful to make dependency "
215 "graphs more appealing")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500216
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600217 parser.add_option("-l", "--log-domains", action="append", dest="debug_domains", default=[],
218 help="Show debug logging for the specified logging domains")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500219
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600220 parser.add_option("-P", "--profile", action="store_true", dest="profile", default=False,
221 help="Profile the command and save reports.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222
223 # @CHOICES@ is substituted out by BitbakeHelpFormatter above
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600224 parser.add_option("-u", "--ui", action="store", dest="ui",
225 default=os.environ.get('BITBAKE_UI', 'knotty'),
226 help="The user interface to use (@CHOICES@ - default %default).")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600228 parser.add_option("", "--token", action="store", dest="xmlrpctoken",
229 default=os.environ.get("BBTOKEN"),
230 help="Specify the connection token to be used when connecting "
231 "to a remote server.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233 parser.add_option("", "--revisions-changed", action="store_true",
234 dest="revisions_changed", default=False,
235 help="Set the exit code depending on whether upstream floating "
236 "revisions have changed or not.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600238 parser.add_option("", "--server-only", action="store_true",
239 dest="server_only", default=False,
240 help="Run bitbake without a UI, only starting a server "
241 "(cooker) process.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600243 parser.add_option("-B", "--bind", action="store", dest="bind", default=False,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500244 help="The name/address for the bitbake xmlrpc server to bind to.")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500246 parser.add_option("-T", "--idle-timeout", type=float, dest="server_timeout",
247 default=os.getenv("BB_SERVER_TIMEOUT"),
248 help="Set timeout to unload bitbake server due to inactivity, "
249 "set to -1 means no unload, "
250 "default: Environment variable BB_SERVER_TIMEOUT.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600252 parser.add_option("", "--no-setscene", action="store_true",
253 dest="nosetscene", default=False,
254 help="Do not run any setscene tasks. sstate will be ignored and "
255 "everything needed, built.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256
Brad Bishop96ff1982019-08-19 13:50:42 -0400257 parser.add_option("", "--skip-setscene", action="store_true",
258 dest="skipsetscene", default=False,
259 help="Skip setscene tasks if they would be executed. Tasks previously "
260 "restored from sstate will be kept, unlike --no-setscene")
261
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600262 parser.add_option("", "--setscene-only", action="store_true",
263 dest="setsceneonly", default=False,
264 help="Only run setscene tasks, don't run any real tasks.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500265
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600266 parser.add_option("", "--remote-server", action="store", dest="remote_server",
267 default=os.environ.get("BBSERVER"),
268 help="Connect to the specified server.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500269
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600270 parser.add_option("-m", "--kill-server", action="store_true",
271 dest="kill_server", default=False,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500272 help="Terminate any running bitbake server.")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600273
274 parser.add_option("", "--observe-only", action="store_true",
275 dest="observe_only", default=False,
276 help="Connect to a server as an observing-only client.")
277
278 parser.add_option("", "--status-only", action="store_true",
279 dest="status_only", default=False,
280 help="Check the status of the remote bitbake server.")
281
282 parser.add_option("-w", "--write-log", action="store", dest="writeeventlog",
283 default=os.environ.get("BBEVENTLOG"),
284 help="Writes the event log of the build to a bitbake event json file. "
285 "Use '' (empty string) to assign the name automatically.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286
Brad Bishop316dfdd2018-06-25 12:45:53 -0400287 parser.add_option("", "--runall", action="append", dest="runall",
288 help="Run the specified task for any recipe in the taskgraph of the specified target (even if it wouldn't otherwise have run).")
289
290 parser.add_option("", "--runonly", action="append", dest="runonly",
291 help="Run only the specified task within the taskgraph of the specified targets (and any task dependencies those tasks may have).")
292
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500293
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500294 options, targets = parser.parse_args(argv)
295
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600296 if options.quiet and options.verbose:
297 parser.error("options --quiet and --verbose are mutually exclusive")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500298
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600299 if options.quiet and options.debug:
300 parser.error("options --quiet and --debug are mutually exclusive")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500301
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600302 # use configuration files from environment variables
303 if "BBPRECONF" in os.environ:
304 options.prefile.append(os.environ["BBPRECONF"])
305
306 if "BBPOSTCONF" in os.environ:
307 options.postfile.append(os.environ["BBPOSTCONF"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500308
309 # fill in proper log name if not supplied
310 if options.writeeventlog is not None and len(options.writeeventlog) == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600311 from datetime import datetime
312 eventlog = "bitbake_eventlog_%s.json" % datetime.now().strftime("%Y%m%d%H%M%S")
313 options.writeeventlog = eventlog
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500314
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500315 if options.bind:
316 try:
317 #Checking that the port is a number and is a ':' delimited value
318 (host, port) = options.bind.split(':')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600319 port = int(port)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500320 except (ValueError,IndexError):
321 raise BBMainException("FATAL: Malformed host:port bind parameter")
322 options.xmlrpcinterface = (host, port)
323 else:
324 options.xmlrpcinterface = (None, 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500325
326 return options, targets[1:]
327
328
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500329def bitbake_main(configParams, configuration):
330
331 # Python multiprocessing requires /dev/shm on Linux
332 if sys.platform.startswith('linux') and not os.access('/dev/shm', os.W_OK | os.X_OK):
333 raise BBMainException("FATAL: /dev/shm does not exist or is not writable")
334
335 # Unbuffer stdout to avoid log truncation in the event
336 # of an unorderly exit as well as to provide timely
337 # updates to log files for use with tail
338 try:
339 if sys.stdout.name == '<stdout>':
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600340 # Reopen with O_SYNC (unbuffered)
341 fl = fcntl.fcntl(sys.stdout.fileno(), fcntl.F_GETFL)
342 fl |= os.O_SYNC
343 fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, fl)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500344 except:
345 pass
346
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500347 if configParams.server_only and configParams.remote_server:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500348 raise BBMainException("FATAL: The '--server-only' option conflicts with %s.\n" %
349 ("the BBSERVER environment variable" if "BBSERVER" in os.environ \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600350 else "the '--remote-server' option"))
351
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500352 if configParams.observe_only and not (configParams.remote_server or configParams.bind):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500353 raise BBMainException("FATAL: '--observe-only' can only be used by UI clients "
354 "connecting to a server.\n")
355
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500356 if "BBDEBUG" in os.environ:
357 level = int(os.environ["BBDEBUG"])
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500358 if level > configParams.debug:
359 configParams.debug = level
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500360
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500361 bb.msg.init_msgconfig(configParams.verbose, configParams.debug,
362 configParams.debug_domains)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500363
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500364 server_connection, ui_module = setup_bitbake(configParams)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500365 # No server connection
366 if server_connection is None:
367 if configParams.status_only:
368 return 1
369 if configParams.kill_server:
370 return 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500371
372 if not configParams.server_only:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500373 if configParams.status_only:
374 server_connection.terminate()
375 return 0
376
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500377 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500378 for event in bb.event.ui_queue:
379 server_connection.events.queue_event(event)
380 bb.event.ui_queue = []
381
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600382 return ui_module.main(server_connection.connection, server_connection.events,
383 configParams)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500384 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500385 server_connection.terminate()
386 else:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387 return 0
388
389 return 1
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500390
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500391def setup_bitbake(configParams, extrafeatures=None):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500392 # Ensure logging messages get sent to the UI as events
393 handler = bb.event.LogHandler()
394 if not configParams.status_only:
395 # In status only mode there are no logs and no UI
396 logger.addHandler(handler)
397
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500398 if configParams.server_only:
399 featureset = []
400 ui_module = None
401 else:
402 ui_module = import_extension_module(bb.ui, configParams.ui, 'main')
403 # Collect the feature set for the UI
404 featureset = getattr(ui_module, "featureSet", [])
405
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500406 if extrafeatures:
407 for feature in extrafeatures:
408 if not feature in featureset:
409 featureset.append(feature)
410
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500411 server_connection = None
412
Brad Bishop6ef32652018-10-09 18:59:25 +0100413 # Clear away any spurious environment variables while we stoke up the cooker
414 # (done after import_extension_module() above since for example import gi triggers env var usage)
415 cleanedvars = bb.utils.clean_environment()
416
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500417 if configParams.remote_server:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500418 # Connect to a remote XMLRPC server
419 server_connection = bb.server.xmlrpcclient.connectXMLRPC(configParams.remote_server, featureset,
420 configParams.observe_only, configParams.xmlrpctoken)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500421 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500422 retries = 8
423 while retries:
424 try:
425 topdir, lock = lockBitbake()
426 sockname = topdir + "/bitbake.sock"
427 if lock:
428 if configParams.status_only or configParams.kill_server:
429 logger.info("bitbake server is not running.")
430 lock.close()
431 return None, None
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500432 # we start a server with a given featureset
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500433 logger.info("Starting bitbake server...")
434 # Clear the event queue since we already displayed messages
435 bb.event.ui_queue = []
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500436 server = bb.server.process.BitBakeServer(lock, sockname, featureset, configParams.server_timeout, configParams.xmlrpcinterface)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500437
438 else:
439 logger.info("Reconnecting to bitbake server...")
440 if not os.path.exists(sockname):
Brad Bishope2d5b612018-11-23 10:55:50 +1300441 logger.info("Previous bitbake instance shutting down?, waiting to retry...")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500442 i = 0
443 lock = None
444 # Wait for 5s or until we can get the lock
445 while not lock and i < 50:
446 time.sleep(0.1)
447 _, lock = lockBitbake()
448 i += 1
449 if lock:
450 bb.utils.unlockfile(lock)
451 raise bb.server.process.ProcessTimeout("Bitbake still shutting down as socket exists but no lock?")
452 if not configParams.server_only:
Brad Bishop96ff1982019-08-19 13:50:42 -0400453 server_connection = bb.server.process.connectProcessServer(sockname, featureset)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500454
455 if server_connection or configParams.server_only:
456 break
457 except BBMainFatal:
458 raise
459 except (Exception, bb.server.process.ProcessTimeout) as e:
460 if not retries:
461 raise
462 retries -= 1
Brad Bishop19323692019-04-05 15:28:33 -0400463 tryno = 8 - retries
Brad Bishop96ff1982019-08-19 13:50:42 -0400464 if isinstance(e, (bb.server.process.ProcessTimeout, BrokenPipeError, EOFError)):
Brad Bishop19323692019-04-05 15:28:33 -0400465 logger.info("Retrying server connection (#%d)..." % tryno)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500466 else:
Brad Bishop19323692019-04-05 15:28:33 -0400467 logger.info("Retrying server connection (#%d)... (%s)" % (tryno, traceback.format_exc()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500468 if not retries:
Brad Bishop96ff1982019-08-19 13:50:42 -0400469 bb.fatal("Unable to connect to bitbake server, or start one (server startup failures would be in bitbake-cookerdaemon.log).")
470 bb.event.print_ui_queue()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500471 if retries < 5:
472 time.sleep(5)
473
474 if configParams.kill_server:
475 server_connection.connection.terminateServer()
476 server_connection.terminate()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500477 bb.event.ui_queue = []
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500478 logger.info("Terminated bitbake server.")
479 return None, None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500480
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500481 # Restore the environment in case the UI needs it
482 for k in cleanedvars:
483 os.environ[k] = cleanedvars[k]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500484
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500485 logger.removeHandler(handler)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500486
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500487 return server_connection, ui_module
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500488
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500489def lockBitbake():
490 topdir = bb.cookerdata.findTopdir()
491 if not topdir:
Brad Bishop15ae2502019-06-18 21:44:24 -0400492 bb.error("Unable to find conf/bblayers.conf or conf/bitbake.conf. BBPATH is unset and/or not in a build directory?")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500493 raise BBMainFatal
494 lockfile = topdir + "/bitbake.lock"
495 return topdir, bb.utils.lockfile(lockfile, False, False)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500496