diff options
Diffstat (limited to 'bitbake/lib/bb/main.py')
| -rwxr-xr-x | bitbake/lib/bb/main.py | 390 |
1 files changed, 390 insertions, 0 deletions
diff --git a/bitbake/lib/bb/main.py b/bitbake/lib/bb/main.py new file mode 100755 index 0000000000..591f32bae9 --- /dev/null +++ b/bitbake/lib/bb/main.py | |||
| @@ -0,0 +1,390 @@ | |||
| 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 | ||
| 30 | |||
| 31 | import bb | ||
| 32 | from bb import event | ||
| 33 | import bb.msg | ||
| 34 | from bb import cooker | ||
| 35 | from bb import ui | ||
| 36 | from bb import server | ||
| 37 | from bb import cookerdata | ||
| 38 | |||
| 39 | __version__ = "1.25.0" | ||
| 40 | logger = logging.getLogger("BitBake") | ||
| 41 | |||
| 42 | class BBMainException(bb.BBHandledException): | ||
| 43 | pass | ||
| 44 | |||
| 45 | def get_ui(config): | ||
| 46 | if not config.ui: | ||
| 47 | # modify 'ui' attribute because it is also read by cooker | ||
| 48 | config.ui = os.environ.get('BITBAKE_UI', 'knotty') | ||
| 49 | |||
| 50 | interface = config.ui | ||
| 51 | |||
| 52 | try: | ||
| 53 | # Dynamically load the UI based on the ui name. Although we | ||
| 54 | # suggest a fixed set this allows you to have flexibility in which | ||
| 55 | # ones are available. | ||
| 56 | module = __import__("bb.ui", fromlist = [interface]) | ||
| 57 | return getattr(module, interface) | ||
| 58 | except AttributeError: | ||
| 59 | raise BBMainException("FATAL: Invalid user interface '%s' specified.\n" | ||
| 60 | "Valid interfaces: depexp, goggle, ncurses, hob, knotty [default]." % interface) | ||
| 61 | |||
| 62 | |||
| 63 | # Display bitbake/OE warnings via the BitBake.Warnings logger, ignoring others""" | ||
| 64 | warnlog = logging.getLogger("BitBake.Warnings") | ||
| 65 | _warnings_showwarning = warnings.showwarning | ||
| 66 | def _showwarning(message, category, filename, lineno, file=None, line=None): | ||
| 67 | if file is not None: | ||
| 68 | if _warnings_showwarning is not None: | ||
| 69 | _warnings_showwarning(message, category, filename, lineno, file, line) | ||
| 70 | else: | ||
| 71 | s = warnings.formatwarning(message, category, filename, lineno) | ||
| 72 | warnlog.warn(s) | ||
| 73 | |||
| 74 | warnings.showwarning = _showwarning | ||
| 75 | warnings.filterwarnings("ignore") | ||
| 76 | warnings.filterwarnings("default", module="(<string>$|(oe|bb)\.)") | ||
| 77 | warnings.filterwarnings("ignore", category=PendingDeprecationWarning) | ||
| 78 | warnings.filterwarnings("ignore", category=ImportWarning) | ||
| 79 | warnings.filterwarnings("ignore", category=DeprecationWarning, module="<string>$") | ||
| 80 | warnings.filterwarnings("ignore", message="With-statements now directly support multiple context managers") | ||
| 81 | |||
| 82 | class BitBakeConfigParameters(cookerdata.ConfigParameters): | ||
| 83 | |||
| 84 | def parseCommandLine(self, argv=sys.argv): | ||
| 85 | parser = optparse.OptionParser( | ||
| 86 | version = "BitBake Build Tool Core version %s, %%prog version %s" % (bb.__version__, __version__), | ||
| 87 | usage = """%prog [options] [recipename/target ...] | ||
| 88 | |||
| 89 | Executes the specified task (default is 'build') for a given set of target recipes (.bb files). | ||
| 90 | It is assumed there is a conf/bblayers.conf available in cwd or in BBPATH which | ||
| 91 | will provide the layer, BBFILES and other configuration information.""") | ||
| 92 | |||
| 93 | parser.add_option("-b", "--buildfile", help = "Execute tasks from a specific .bb recipe directly. WARNING: Does not handle any dependencies from other recipes.", | ||
| 94 | action = "store", dest = "buildfile", default = None) | ||
| 95 | |||
| 96 | parser.add_option("-k", "--continue", help = "Continue as much as possible after an error. While the target that failed and anything depending on it cannot be built, as much as possible will be built before stopping.", | ||
| 97 | action = "store_false", dest = "abort", default = True) | ||
| 98 | |||
| 99 | parser.add_option("-a", "--tryaltconfigs", help = "Continue with builds by trying to use alternative providers where possible.", | ||
| 100 | action = "store_true", dest = "tryaltconfigs", default = False) | ||
| 101 | |||
| 102 | parser.add_option("-f", "--force", help = "Force the specified targets/task to run (invalidating any existing stamp file).", | ||
| 103 | action = "store_true", dest = "force", default = False) | ||
| 104 | |||
| 105 | parser.add_option("-c", "--cmd", help = "Specify the task to execute. The exact options available depend on the metadata. Some examples might be 'compile' or 'populate_sysroot' or 'listtasks' may give a list of the tasks available.", | ||
| 106 | action = "store", dest = "cmd") | ||
| 107 | |||
| 108 | parser.add_option("-C", "--clear-stamp", help = "Invalidate the stamp for the specified task such as 'compile' and then run the default task for the specified target(s).", | ||
| 109 | action = "store", dest = "invalidate_stamp") | ||
| 110 | |||
| 111 | parser.add_option("-r", "--read", help = "Read the specified file before bitbake.conf.", | ||
| 112 | action = "append", dest = "prefile", default = []) | ||
| 113 | |||
| 114 | parser.add_option("-R", "--postread", help = "Read the specified file after bitbake.conf.", | ||
| 115 | action = "append", dest = "postfile", default = []) | ||
| 116 | |||
| 117 | parser.add_option("-v", "--verbose", help = "Output more log message data to the terminal.", | ||
| 118 | action = "store_true", dest = "verbose", default = False) | ||
| 119 | |||
| 120 | parser.add_option("-D", "--debug", help = "Increase the debug level. You can specify this more than once.", | ||
| 121 | action = "count", dest="debug", default = 0) | ||
| 122 | |||
| 123 | parser.add_option("-n", "--dry-run", help = "Don't execute, just go through the motions.", | ||
| 124 | action = "store_true", dest = "dry_run", default = False) | ||
| 125 | |||
| 126 | parser.add_option("-S", "--dump-signatures", help = "Dump out the signature construction information, with no task execution. The SIGNATURE_HANDLER parameter is passed to the handler. Two common values are none and printdiff but the handler may define more/less. none means only dump the signature, printdiff means compare the dumped signature with the cached one.", | ||
| 127 | action = "append", dest = "dump_signatures", default = [], metavar="SIGNATURE_HANDLER") | ||
| 128 | |||
| 129 | parser.add_option("-p", "--parse-only", help = "Quit after parsing the BB recipes.", | ||
| 130 | action = "store_true", dest = "parse_only", default = False) | ||
| 131 | |||
| 132 | parser.add_option("-s", "--show-versions", help = "Show current and preferred versions of all recipes.", | ||
| 133 | action = "store_true", dest = "show_versions", default = False) | ||
| 134 | |||
| 135 | parser.add_option("-e", "--environment", help = "Show the global or per-recipe environment complete with information about where variables were set/changed.", | ||
| 136 | action = "store_true", dest = "show_environment", default = False) | ||
| 137 | |||
| 138 | parser.add_option("-g", "--graphviz", help = "Save dependency tree information for the specified targets in the dot syntax.", | ||
| 139 | action = "store_true", dest = "dot_graph", default = False) | ||
| 140 | |||
| 141 | parser.add_option("-I", "--ignore-deps", help = """Assume these dependencies don't exist and are already provided (equivalent to ASSUME_PROVIDED). Useful to make dependency graphs more appealing""", | ||
| 142 | action = "append", dest = "extra_assume_provided", default = []) | ||
| 143 | |||
| 144 | parser.add_option("-l", "--log-domains", help = """Show debug logging for the specified logging domains""", | ||
| 145 | action = "append", dest = "debug_domains", default = []) | ||
| 146 | |||
| 147 | parser.add_option("-P", "--profile", help = "Profile the command and save reports.", | ||
| 148 | action = "store_true", dest = "profile", default = False) | ||
| 149 | |||
| 150 | parser.add_option("-u", "--ui", help = "The user interface to use (e.g. knotty, hob, depexp).", | ||
| 151 | action = "store", dest = "ui") | ||
| 152 | |||
| 153 | parser.add_option("-t", "--servertype", help = "Choose which server to use, process or xmlrpc.", | ||
| 154 | action = "store", dest = "servertype") | ||
| 155 | |||
| 156 | parser.add_option("", "--token", help = "Specify the connection token to be used when connecting to a remote server.", | ||
| 157 | action = "store", dest = "xmlrpctoken") | ||
| 158 | |||
| 159 | parser.add_option("", "--revisions-changed", help = "Set the exit code depending on whether upstream floating revisions have changed or not.", | ||
| 160 | action = "store_true", dest = "revisions_changed", default = False) | ||
| 161 | |||
| 162 | parser.add_option("", "--server-only", help = "Run bitbake without a UI, only starting a server (cooker) process.", | ||
| 163 | action = "store_true", dest = "server_only", default = False) | ||
| 164 | |||
| 165 | parser.add_option("-B", "--bind", help = "The name/address for the bitbake server to bind to.", | ||
| 166 | action = "store", dest = "bind", default = False) | ||
| 167 | |||
| 168 | parser.add_option("", "--no-setscene", help = "Do not run any setscene tasks. sstate will be ignored and everything needed, built.", | ||
| 169 | action = "store_true", dest = "nosetscene", default = False) | ||
| 170 | |||
| 171 | parser.add_option("", "--remote-server", help = "Connect to the specified server.", | ||
| 172 | action = "store", dest = "remote_server", default = False) | ||
| 173 | |||
| 174 | parser.add_option("-m", "--kill-server", help = "Terminate the remote server.", | ||
| 175 | action = "store_true", dest = "kill_server", default = False) | ||
| 176 | |||
| 177 | parser.add_option("", "--observe-only", help = "Connect to a server as an observing-only client.", | ||
| 178 | action = "store_true", dest = "observe_only", default = False) | ||
| 179 | |||
| 180 | parser.add_option("", "--status-only", help = "Check the status of the remote bitbake server.", | ||
| 181 | action = "store_true", dest = "status_only", default = False) | ||
| 182 | |||
| 183 | parser.add_option("-w", "--write-log", help = "Writes the event log of the build to a bitbake event json file. Use '' (empty string) to assign the name automatically.", | ||
| 184 | action = "store", dest = "writeeventlog") | ||
| 185 | |||
| 186 | options, targets = parser.parse_args(argv) | ||
| 187 | |||
| 188 | # some environmental variables set also configuration options | ||
| 189 | if "BBSERVER" in os.environ: | ||
| 190 | options.servertype = "xmlrpc" | ||
| 191 | options.remote_server = os.environ["BBSERVER"] | ||
| 192 | |||
| 193 | if "BBTOKEN" in os.environ: | ||
| 194 | options.xmlrpctoken = os.environ["BBTOKEN"] | ||
| 195 | |||
| 196 | if "BBEVENTLOG" is os.environ: | ||
| 197 | options.writeeventlog = os.environ["BBEVENTLOG"] | ||
| 198 | |||
| 199 | # fill in proper log name if not supplied | ||
| 200 | if options.writeeventlog is not None and len(options.writeeventlog) == 0: | ||
| 201 | import datetime | ||
| 202 | options.writeeventlog = "bitbake_eventlog_%s.json" % datetime.datetime.now().strftime("%Y%m%d%H%M%S") | ||
| 203 | |||
| 204 | # if BBSERVER says to autodetect, let's do that | ||
| 205 | if options.remote_server: | ||
| 206 | [host, port] = options.remote_server.split(":", 2) | ||
| 207 | port = int(port) | ||
| 208 | # use automatic port if port set to -1, means read it from | ||
| 209 | # the bitbake.lock file; this is a bit tricky, but we always expect | ||
| 210 | # to be in the base of the build directory if we need to have a | ||
| 211 | # chance to start the server later, anyway | ||
| 212 | if port == -1: | ||
| 213 | lock_location = "./bitbake.lock" | ||
| 214 | # we try to read the address at all times; if the server is not started, | ||
| 215 | # we'll try to start it after the first connect fails, below | ||
| 216 | try: | ||
| 217 | lf = open(lock_location, 'r') | ||
| 218 | remotedef = lf.readline() | ||
| 219 | [host, port] = remotedef.split(":") | ||
| 220 | port = int(port) | ||
| 221 | lf.close() | ||
| 222 | options.remote_server = remotedef | ||
| 223 | except Exception as e: | ||
| 224 | raise BBMainException("Failed to read bitbake.lock (%s), invalid port" % str(e)) | ||
| 225 | |||
| 226 | return options, targets[1:] | ||
| 227 | |||
| 228 | |||
| 229 | def start_server(servermodule, configParams, configuration, features): | ||
| 230 | server = servermodule.BitBakeServer() | ||
| 231 | if configParams.bind: | ||
| 232 | (host, port) = configParams.bind.split(':') | ||
| 233 | server.initServer((host, int(port))) | ||
| 234 | configuration.interface = [ server.serverImpl.host, server.serverImpl.port ] | ||
| 235 | else: | ||
| 236 | server.initServer() | ||
| 237 | configuration.interface = [] | ||
| 238 | |||
| 239 | try: | ||
| 240 | configuration.setServerRegIdleCallback(server.getServerIdleCB()) | ||
| 241 | |||
| 242 | cooker = bb.cooker.BBCooker(configuration, features) | ||
| 243 | |||
| 244 | server.addcooker(cooker) | ||
| 245 | server.saveConnectionDetails() | ||
| 246 | except Exception as e: | ||
| 247 | exc_info = sys.exc_info() | ||
| 248 | while hasattr(server, "event_queue"): | ||
| 249 | try: | ||
| 250 | import queue | ||
| 251 | except ImportError: | ||
| 252 | import Queue as queue | ||
| 253 | try: | ||
| 254 | event = server.event_queue.get(block=False) | ||
| 255 | except (queue.Empty, IOError): | ||
| 256 | break | ||
| 257 | if isinstance(event, logging.LogRecord): | ||
| 258 | logger.handle(event) | ||
| 259 | raise exc_info[1], None, exc_info[2] | ||
| 260 | server.detach() | ||
| 261 | return server | ||
| 262 | |||
| 263 | |||
| 264 | def bitbake_main(configParams, configuration): | ||
| 265 | |||
| 266 | # Python multiprocessing requires /dev/shm on Linux | ||
| 267 | if sys.platform.startswith('linux') and not os.access('/dev/shm', os.W_OK | os.X_OK): | ||
| 268 | raise BBMainException("FATAL: /dev/shm does not exist or is not writable") | ||
| 269 | |||
| 270 | # Unbuffer stdout to avoid log truncation in the event | ||
| 271 | # of an unorderly exit as well as to provide timely | ||
| 272 | # updates to log files for use with tail | ||
| 273 | try: | ||
| 274 | if sys.stdout.name == '<stdout>': | ||
| 275 | sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) | ||
| 276 | except: | ||
| 277 | pass | ||
| 278 | |||
| 279 | |||
| 280 | configuration.setConfigParameters(configParams) | ||
| 281 | |||
| 282 | ui_module = get_ui(configParams) | ||
| 283 | |||
| 284 | # Server type can be xmlrpc or process currently, if nothing is specified, | ||
| 285 | # the default server is process | ||
| 286 | if configParams.servertype: | ||
| 287 | server_type = configParams.servertype | ||
| 288 | else: | ||
| 289 | server_type = 'process' | ||
| 290 | |||
| 291 | try: | ||
| 292 | module = __import__("bb.server", fromlist = [server_type]) | ||
| 293 | servermodule = getattr(module, server_type) | ||
| 294 | except AttributeError: | ||
| 295 | raise BBMainException("FATAL: Invalid server type '%s' specified.\n" | ||
| 296 | "Valid interfaces: xmlrpc, process [default]." % server_type) | ||
| 297 | |||
| 298 | if configParams.server_only: | ||
| 299 | if configParams.servertype != "xmlrpc": | ||
| 300 | raise BBMainException("FATAL: If '--server-only' is defined, we must set the " | ||
| 301 | "servertype as 'xmlrpc'.\n") | ||
| 302 | if not configParams.bind: | ||
| 303 | raise BBMainException("FATAL: The '--server-only' option requires a name/address " | ||
| 304 | "to bind to with the -B option.\n") | ||
| 305 | if configParams.remote_server: | ||
| 306 | raise BBMainException("FATAL: The '--server-only' option conflicts with %s.\n" % | ||
| 307 | ("the BBSERVER environment variable" if "BBSERVER" in os.environ \ | ||
| 308 | else "the '--remote-server' option" )) | ||
| 309 | |||
| 310 | if configParams.bind and configParams.servertype != "xmlrpc": | ||
| 311 | raise BBMainException("FATAL: If '-B' or '--bind' is defined, we must " | ||
| 312 | "set the servertype as 'xmlrpc'.\n") | ||
| 313 | |||
| 314 | if configParams.remote_server and configParams.servertype != "xmlrpc": | ||
| 315 | raise BBMainException("FATAL: If '--remote-server' is defined, we must " | ||
| 316 | "set the servertype as 'xmlrpc'.\n") | ||
| 317 | |||
| 318 | if configParams.observe_only and (not configParams.remote_server or configParams.bind): | ||
| 319 | raise BBMainException("FATAL: '--observe-only' can only be used by UI clients " | ||
| 320 | "connecting to a server.\n") | ||
| 321 | |||
| 322 | if configParams.kill_server and not configParams.remote_server: | ||
| 323 | raise BBMainException("FATAL: '--kill-server' can only be used to terminate a remote server") | ||
| 324 | |||
| 325 | if "BBDEBUG" in os.environ: | ||
| 326 | level = int(os.environ["BBDEBUG"]) | ||
| 327 | if level > configuration.debug: | ||
| 328 | configuration.debug = level | ||
| 329 | |||
| 330 | bb.msg.init_msgconfig(configParams.verbose, configuration.debug, | ||
| 331 | configuration.debug_domains) | ||
| 332 | |||
| 333 | # Ensure logging messages get sent to the UI as events | ||
| 334 | handler = bb.event.LogHandler() | ||
| 335 | if not configParams.status_only: | ||
| 336 | # In status only mode there are no logs and no UI | ||
| 337 | logger.addHandler(handler) | ||
| 338 | |||
| 339 | # Clear away any spurious environment variables while we stoke up the cooker | ||
| 340 | cleanedvars = bb.utils.clean_environment() | ||
| 341 | |||
| 342 | featureset = [] | ||
| 343 | if not configParams.server_only: | ||
| 344 | # Collect the feature set for the UI | ||
| 345 | featureset = getattr(ui_module, "featureSet", []) | ||
| 346 | |||
| 347 | if not configParams.remote_server: | ||
| 348 | # we start a server with a given configuration | ||
| 349 | server = start_server(servermodule, configParams, configuration, featureset) | ||
| 350 | bb.event.ui_queue = [] | ||
| 351 | else: | ||
| 352 | # we start a stub server that is actually a XMLRPClient that connects to a real server | ||
| 353 | server = servermodule.BitBakeXMLRPCClient(configParams.observe_only, configParams.xmlrpctoken) | ||
| 354 | server.saveConnectionDetails(configParams.remote_server) | ||
| 355 | |||
| 356 | |||
| 357 | if not configParams.server_only: | ||
| 358 | try: | ||
| 359 | server_connection = server.establishConnection(featureset) | ||
| 360 | except Exception as e: | ||
| 361 | if configParams.kill_server: | ||
| 362 | return 0 | ||
| 363 | bb.fatal("Could not connect to server %s: %s" % (configParams.remote_server, str(e))) | ||
| 364 | |||
| 365 | # Restore the environment in case the UI needs it | ||
| 366 | for k in cleanedvars: | ||
| 367 | os.environ[k] = cleanedvars[k] | ||
| 368 | |||
| 369 | logger.removeHandler(handler) | ||
| 370 | |||
| 371 | |||
| 372 | if configParams.status_only: | ||
| 373 | server_connection.terminate() | ||
| 374 | return 0 | ||
| 375 | |||
| 376 | if configParams.kill_server: | ||
| 377 | server_connection.connection.terminateServer() | ||
| 378 | bb.event.ui_queue = [] | ||
| 379 | return 0 | ||
| 380 | |||
| 381 | try: | ||
| 382 | return ui_module.main(server_connection.connection, server_connection.events, configParams) | ||
| 383 | finally: | ||
| 384 | bb.event.ui_queue = [] | ||
| 385 | server_connection.terminate() | ||
| 386 | else: | ||
| 387 | print("Bitbake server address: %s, server port: %s" % (server.serverImpl.host, server.serverImpl.port)) | ||
| 388 | return 0 | ||
| 389 | |||
| 390 | return 1 | ||
