Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # ex:ts=4:sw=4:sts=4:et |
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 4 | # |
| 5 | # Copyright (C) 2003, 2004 Chris Larson |
| 6 | # Copyright (C) 2003, 2004 Phil Blundell |
| 7 | # Copyright (C) 2003 - 2005 Michael 'Mickey' Lauer |
| 8 | # Copyright (C) 2005 Holger Hans Peter Freyther |
| 9 | # Copyright (C) 2005 ROAD GmbH |
| 10 | # Copyright (C) 2006 Richard Purdie |
| 11 | # |
| 12 | # This program is free software; you can redistribute it and/or modify |
| 13 | # it under the terms of the GNU General Public License version 2 as |
| 14 | # published by the Free Software Foundation. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
| 21 | # You should have received a copy of the GNU General Public License along |
| 22 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | |
| 25 | import os, sys |
| 26 | from functools import wraps |
| 27 | import logging |
| 28 | import bb |
| 29 | from bb import data |
| 30 | import bb.parse |
| 31 | |
| 32 | logger = logging.getLogger("BitBake") |
| 33 | parselog = logging.getLogger("BitBake.Parsing") |
| 34 | |
| 35 | class ConfigParameters(object): |
| 36 | def __init__(self, argv=sys.argv): |
| 37 | self.options, targets = self.parseCommandLine(argv) |
| 38 | self.environment = self.parseEnvironment() |
| 39 | |
| 40 | self.options.pkgs_to_build = targets or [] |
| 41 | |
| 42 | self.options.tracking = False |
| 43 | if hasattr(self.options, "show_environment") and self.options.show_environment: |
| 44 | self.options.tracking = True |
| 45 | |
| 46 | for key, val in self.options.__dict__.items(): |
| 47 | setattr(self, key, val) |
| 48 | |
| 49 | def parseCommandLine(self, argv=sys.argv): |
| 50 | raise Exception("Caller must implement commandline option parsing") |
| 51 | |
| 52 | def parseEnvironment(self): |
| 53 | return os.environ.copy() |
| 54 | |
| 55 | def updateFromServer(self, server): |
| 56 | if not self.options.cmd: |
| 57 | defaulttask, error = server.runCommand(["getVariable", "BB_DEFAULT_TASK"]) |
| 58 | if error: |
| 59 | raise Exception("Unable to get the value of BB_DEFAULT_TASK from the server: %s" % error) |
| 60 | self.options.cmd = defaulttask or "build" |
| 61 | _, error = server.runCommand(["setConfig", "cmd", self.options.cmd]) |
| 62 | if error: |
| 63 | raise Exception("Unable to set configuration option 'cmd' on the server: %s" % error) |
| 64 | |
| 65 | if not self.options.pkgs_to_build: |
| 66 | bbpkgs, error = server.runCommand(["getVariable", "BBPKGS"]) |
| 67 | if error: |
| 68 | raise Exception("Unable to get the value of BBPKGS from the server: %s" % error) |
| 69 | if bbpkgs: |
| 70 | self.options.pkgs_to_build.extend(bbpkgs.split()) |
| 71 | |
| 72 | def updateToServer(self, server, environment): |
| 73 | options = {} |
| 74 | for o in ["abort", "tryaltconfigs", "force", "invalidate_stamp", |
| 75 | "verbose", "debug", "dry_run", "dump_signatures", |
| 76 | "debug_domains", "extra_assume_provided", "profile", |
| 77 | "prefile", "postfile"]: |
| 78 | options[o] = getattr(self.options, o) |
| 79 | |
| 80 | ret, error = server.runCommand(["updateConfig", options, environment]) |
| 81 | if error: |
| 82 | raise Exception("Unable to update the server configuration with local parameters: %s" % error) |
| 83 | |
| 84 | def parseActions(self): |
| 85 | # Parse any commandline into actions |
| 86 | action = {'action':None, 'msg':None} |
| 87 | if self.options.show_environment: |
| 88 | if 'world' in self.options.pkgs_to_build: |
| 89 | action['msg'] = "'world' is not a valid target for --environment." |
| 90 | elif 'universe' in self.options.pkgs_to_build: |
| 91 | action['msg'] = "'universe' is not a valid target for --environment." |
| 92 | elif len(self.options.pkgs_to_build) > 1: |
| 93 | action['msg'] = "Only one target can be used with the --environment option." |
| 94 | elif self.options.buildfile and len(self.options.pkgs_to_build) > 0: |
| 95 | action['msg'] = "No target should be used with the --environment and --buildfile options." |
| 96 | elif len(self.options.pkgs_to_build) > 0: |
| 97 | action['action'] = ["showEnvironmentTarget", self.options.pkgs_to_build] |
| 98 | else: |
| 99 | action['action'] = ["showEnvironment", self.options.buildfile] |
| 100 | elif self.options.buildfile is not None: |
| 101 | action['action'] = ["buildFile", self.options.buildfile, self.options.cmd] |
| 102 | elif self.options.revisions_changed: |
| 103 | action['action'] = ["compareRevisions"] |
| 104 | elif self.options.show_versions: |
| 105 | action['action'] = ["showVersions"] |
| 106 | elif self.options.parse_only: |
| 107 | action['action'] = ["parseFiles"] |
| 108 | elif self.options.dot_graph: |
| 109 | if self.options.pkgs_to_build: |
| 110 | action['action'] = ["generateDotGraph", self.options.pkgs_to_build, self.options.cmd] |
| 111 | else: |
| 112 | action['msg'] = "Please specify a package name for dependency graph generation." |
| 113 | else: |
| 114 | if self.options.pkgs_to_build: |
| 115 | action['action'] = ["buildTargets", self.options.pkgs_to_build, self.options.cmd] |
| 116 | else: |
| 117 | #action['msg'] = "Nothing to do. Use 'bitbake world' to build everything, or run 'bitbake --help' for usage information." |
| 118 | action = None |
| 119 | self.options.initialaction = action |
| 120 | return action |
| 121 | |
| 122 | class CookerConfiguration(object): |
| 123 | """ |
| 124 | Manages build options and configurations for one run |
| 125 | """ |
| 126 | |
| 127 | def __init__(self): |
| 128 | self.debug_domains = [] |
| 129 | self.extra_assume_provided = [] |
| 130 | self.prefile = [] |
| 131 | self.postfile = [] |
| 132 | self.debug = 0 |
| 133 | self.cmd = None |
| 134 | self.abort = True |
| 135 | self.force = False |
| 136 | self.profile = False |
| 137 | self.nosetscene = False |
| 138 | self.invalidate_stamp = False |
| 139 | self.dump_signatures = [] |
| 140 | self.dry_run = False |
| 141 | self.tracking = False |
| 142 | self.interface = [] |
| 143 | self.writeeventlog = False |
| 144 | |
| 145 | self.env = {} |
| 146 | |
| 147 | def setConfigParameters(self, parameters): |
| 148 | for key in self.__dict__.keys(): |
| 149 | if key in parameters.options.__dict__: |
| 150 | setattr(self, key, parameters.options.__dict__[key]) |
| 151 | self.env = parameters.environment.copy() |
| 152 | self.tracking = parameters.tracking |
| 153 | |
| 154 | def setServerRegIdleCallback(self, srcb): |
| 155 | self.server_register_idlecallback = srcb |
| 156 | |
| 157 | def __getstate__(self): |
| 158 | state = {} |
| 159 | for key in self.__dict__.keys(): |
| 160 | if key == "server_register_idlecallback": |
| 161 | state[key] = None |
| 162 | else: |
| 163 | state[key] = getattr(self, key) |
| 164 | return state |
| 165 | |
| 166 | def __setstate__(self,state): |
| 167 | for k in state: |
| 168 | setattr(self, k, state[k]) |
| 169 | |
| 170 | |
| 171 | def catch_parse_error(func): |
| 172 | """Exception handling bits for our parsing""" |
| 173 | @wraps(func) |
| 174 | def wrapped(fn, *args): |
| 175 | try: |
| 176 | return func(fn, *args) |
| 177 | except IOError as exc: |
| 178 | import traceback |
| 179 | parselog.critical(traceback.format_exc()) |
| 180 | parselog.critical("Unable to parse %s: %s" % (fn, exc)) |
| 181 | sys.exit(1) |
| 182 | except (bb.parse.ParseError, bb.data_smart.ExpansionError) as exc: |
| 183 | import traceback |
| 184 | |
| 185 | bbdir = os.path.dirname(__file__) + os.sep |
| 186 | exc_class, exc, tb = sys.exc_info() |
| 187 | for tb in iter(lambda: tb.tb_next, None): |
| 188 | # Skip frames in bitbake itself, we only want the metadata |
| 189 | fn, _, _, _ = traceback.extract_tb(tb, 1)[0] |
| 190 | if not fn.startswith(bbdir): |
| 191 | break |
| 192 | parselog.critical("Unable to parse %s", fn, exc_info=(exc_class, exc, tb)) |
| 193 | sys.exit(1) |
| 194 | return wrapped |
| 195 | |
| 196 | @catch_parse_error |
| 197 | def parse_config_file(fn, data, include=True): |
| 198 | return bb.parse.handle(fn, data, include) |
| 199 | |
| 200 | @catch_parse_error |
| 201 | def _inherit(bbclass, data): |
| 202 | bb.parse.BBHandler.inherit(bbclass, "configuration INHERITs", 0, data) |
| 203 | return data |
| 204 | |
| 205 | def findConfigFile(configfile, data): |
| 206 | search = [] |
| 207 | bbpath = data.getVar("BBPATH", True) |
| 208 | if bbpath: |
| 209 | for i in bbpath.split(":"): |
| 210 | search.append(os.path.join(i, "conf", configfile)) |
| 211 | path = os.getcwd() |
| 212 | while path != "/": |
| 213 | search.append(os.path.join(path, "conf", configfile)) |
| 214 | path, _ = os.path.split(path) |
| 215 | |
| 216 | for i in search: |
| 217 | if os.path.exists(i): |
| 218 | return i |
| 219 | |
| 220 | return None |
| 221 | |
| 222 | class CookerDataBuilder(object): |
| 223 | |
| 224 | def __init__(self, cookercfg, worker = False): |
| 225 | |
| 226 | self.prefiles = cookercfg.prefile |
| 227 | self.postfiles = cookercfg.postfile |
| 228 | self.tracking = cookercfg.tracking |
| 229 | |
| 230 | bb.utils.set_context(bb.utils.clean_context()) |
| 231 | bb.event.set_class_handlers(bb.event.clean_class_handlers()) |
| 232 | self.data = bb.data.init() |
| 233 | if self.tracking: |
| 234 | self.data.enableTracking() |
| 235 | |
| 236 | # Keep a datastore of the initial environment variables and their |
| 237 | # values from when BitBake was launched to enable child processes |
| 238 | # to use environment variables which have been cleaned from the |
| 239 | # BitBake processes env |
| 240 | self.savedenv = bb.data.init() |
| 241 | for k in cookercfg.env: |
| 242 | self.savedenv.setVar(k, cookercfg.env[k]) |
| 243 | |
| 244 | filtered_keys = bb.utils.approved_variables() |
| 245 | bb.data.inheritFromOS(self.data, self.savedenv, filtered_keys) |
| 246 | self.data.setVar("BB_ORIGENV", self.savedenv) |
| 247 | |
| 248 | if worker: |
| 249 | self.data.setVar("BB_WORKERCONTEXT", "1") |
| 250 | |
| 251 | def parseBaseConfiguration(self): |
| 252 | try: |
| 253 | self.parseConfigurationFiles(self.prefiles, self.postfiles) |
| 254 | except SyntaxError: |
| 255 | raise bb.BBHandledException |
| 256 | except bb.data_smart.ExpansionError as e: |
| 257 | logger.error(str(e)) |
| 258 | raise bb.BBHandledException |
| 259 | except Exception: |
| 260 | logger.exception("Error parsing configuration files") |
| 261 | raise bb.BBHandledException |
| 262 | |
| 263 | def _findLayerConf(self, data): |
| 264 | return findConfigFile("bblayers.conf", data) |
| 265 | |
| 266 | def parseConfigurationFiles(self, prefiles, postfiles): |
| 267 | data = self.data |
| 268 | bb.parse.init_parser(data) |
| 269 | |
| 270 | # Parse files for loading *before* bitbake.conf and any includes |
| 271 | for f in prefiles: |
| 272 | data = parse_config_file(f, data) |
| 273 | |
| 274 | layerconf = self._findLayerConf(data) |
| 275 | if layerconf: |
| 276 | parselog.debug(2, "Found bblayers.conf (%s)", layerconf) |
| 277 | # By definition bblayers.conf is in conf/ of TOPDIR. |
| 278 | # We may have been called with cwd somewhere else so reset TOPDIR |
| 279 | data.setVar("TOPDIR", os.path.dirname(os.path.dirname(layerconf))) |
| 280 | data = parse_config_file(layerconf, data) |
| 281 | |
| 282 | layers = (data.getVar('BBLAYERS', True) or "").split() |
| 283 | |
| 284 | data = bb.data.createCopy(data) |
| 285 | approved = bb.utils.approved_variables() |
| 286 | for layer in layers: |
| 287 | parselog.debug(2, "Adding layer %s", layer) |
| 288 | if 'HOME' in approved and '~' in layer: |
| 289 | layer = os.path.expanduser(layer) |
| 290 | data.setVar('LAYERDIR', layer) |
| 291 | data = parse_config_file(os.path.join(layer, "conf", "layer.conf"), data) |
| 292 | data.expandVarref('LAYERDIR') |
| 293 | |
| 294 | data.delVar('LAYERDIR') |
| 295 | |
| 296 | if not data.getVar("BBPATH", True): |
| 297 | msg = "The BBPATH variable is not set" |
| 298 | if not layerconf: |
| 299 | msg += (" and bitbake did not find a conf/bblayers.conf file in" |
| 300 | " the expected location.\nMaybe you accidentally" |
| 301 | " invoked bitbake from the wrong directory?") |
| 302 | raise SystemExit(msg) |
| 303 | |
| 304 | data = parse_config_file(os.path.join("conf", "bitbake.conf"), data) |
| 305 | |
| 306 | # Parse files for loading *after* bitbake.conf and any includes |
| 307 | for p in postfiles: |
| 308 | data = parse_config_file(p, data) |
| 309 | |
| 310 | # Handle any INHERITs and inherit the base class |
| 311 | bbclasses = ["base"] + (data.getVar('INHERIT', True) or "").split() |
| 312 | for bbclass in bbclasses: |
| 313 | data = _inherit(bbclass, data) |
| 314 | |
| 315 | # Nomally we only register event handlers at the end of parsing .bb files |
| 316 | # We register any handlers we've found so far here... |
| 317 | for var in data.getVar('__BBHANDLERS', False) or []: |
| 318 | bb.event.register(var, data.getVar(var, False), (data.getVarFlag(var, "eventmask", True) or "").split()) |
| 319 | |
| 320 | if data.getVar("BB_WORKERCONTEXT", False) is None: |
| 321 | bb.fetch.fetcher_init(data) |
| 322 | bb.codeparser.parser_cache_init(data) |
| 323 | bb.event.fire(bb.event.ConfigParsed(), data) |
| 324 | |
| 325 | if data.getVar("BB_INVALIDCONF", False) is True: |
| 326 | data.setVar("BB_INVALIDCONF", False) |
| 327 | self.parseConfigurationFiles(self.prefiles, self.postfiles) |
| 328 | return |
| 329 | |
| 330 | bb.parse.init_parser(data) |
| 331 | data.setVar('BBINCLUDED',bb.parse.get_file_depends(data)) |
| 332 | self.data = data |
| 333 | self.data_hash = data.get_hash() |
| 334 | |
| 335 | |
| 336 | |