Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake 'Build' implementation |
| 3 | # |
| 4 | # Core code for function execution and task handling in the |
| 5 | # BitBake build tools. |
| 6 | # |
| 7 | # Copyright (C) 2003, 2004 Chris Larson |
| 8 | # |
| 9 | # Based on Gentoo's portage.py. |
| 10 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 11 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | # |
| 13 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 14 | |
| 15 | import os |
| 16 | import sys |
| 17 | import logging |
| 18 | import shlex |
| 19 | import glob |
| 20 | import time |
| 21 | import stat |
| 22 | import bb |
| 23 | import bb.msg |
| 24 | import bb.process |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 25 | import bb.progress |
| 26 | from bb import data, event, utils |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | bblogger = logging.getLogger('BitBake') |
| 29 | logger = logging.getLogger('BitBake.Build') |
| 30 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | __mtime_cache = {} |
| 32 | |
| 33 | def cached_mtime_noerror(f): |
| 34 | if f not in __mtime_cache: |
| 35 | try: |
| 36 | __mtime_cache[f] = os.stat(f)[stat.ST_MTIME] |
| 37 | except OSError: |
| 38 | return 0 |
| 39 | return __mtime_cache[f] |
| 40 | |
| 41 | def reset_cache(): |
| 42 | global __mtime_cache |
| 43 | __mtime_cache = {} |
| 44 | |
| 45 | # When we execute a Python function, we'd like certain things |
| 46 | # in all namespaces, hence we add them to __builtins__. |
| 47 | # If we do not do this and use the exec globals, they will |
| 48 | # not be available to subfunctions. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 49 | if hasattr(__builtins__, '__setitem__'): |
| 50 | builtins = __builtins__ |
| 51 | else: |
| 52 | builtins = __builtins__.__dict__ |
| 53 | |
| 54 | builtins['bb'] = bb |
| 55 | builtins['os'] = os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
| 57 | class FuncFailed(Exception): |
| 58 | def __init__(self, name = None, logfile = None): |
| 59 | self.logfile = logfile |
| 60 | self.name = name |
| 61 | if name: |
| 62 | self.msg = 'Function failed: %s' % name |
| 63 | else: |
| 64 | self.msg = "Function failed" |
| 65 | |
| 66 | def __str__(self): |
| 67 | if self.logfile and os.path.exists(self.logfile): |
| 68 | msg = ("%s (log file is located at %s)" % |
| 69 | (self.msg, self.logfile)) |
| 70 | else: |
| 71 | msg = self.msg |
| 72 | return msg |
| 73 | |
| 74 | class TaskBase(event.Event): |
| 75 | """Base class for task events""" |
| 76 | |
| 77 | def __init__(self, t, logfile, d): |
| 78 | self._task = t |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | self._package = d.getVar("PF") |
| 80 | self._mc = d.getVar("BB_CURRENT_MC") |
| 81 | self.taskfile = d.getVar("FILE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | self.taskname = self._task |
| 83 | self.logfile = logfile |
| 84 | self.time = time.time() |
| 85 | event.Event.__init__(self) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 86 | self._message = "recipe %s: task %s: %s" % (d.getVar("PF"), t, self.getDisplayName()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
| 88 | def getTask(self): |
| 89 | return self._task |
| 90 | |
| 91 | def setTask(self, task): |
| 92 | self._task = task |
| 93 | |
| 94 | def getDisplayName(self): |
| 95 | return bb.event.getName(self)[4:] |
| 96 | |
| 97 | task = property(getTask, setTask, None, "task property") |
| 98 | |
| 99 | class TaskStarted(TaskBase): |
| 100 | """Task execution started""" |
| 101 | def __init__(self, t, logfile, taskflags, d): |
| 102 | super(TaskStarted, self).__init__(t, logfile, d) |
| 103 | self.taskflags = taskflags |
| 104 | |
| 105 | class TaskSucceeded(TaskBase): |
| 106 | """Task execution completed""" |
| 107 | |
| 108 | class TaskFailed(TaskBase): |
| 109 | """Task execution failed""" |
| 110 | |
| 111 | def __init__(self, task, logfile, metadata, errprinted = False): |
| 112 | self.errprinted = errprinted |
| 113 | super(TaskFailed, self).__init__(task, logfile, metadata) |
| 114 | |
| 115 | class TaskFailedSilent(TaskBase): |
| 116 | """Task execution failed (silently)""" |
| 117 | def getDisplayName(self): |
| 118 | # Don't need to tell the user it was silent |
| 119 | return "Failed" |
| 120 | |
| 121 | class TaskInvalid(TaskBase): |
| 122 | |
| 123 | def __init__(self, task, metadata): |
| 124 | super(TaskInvalid, self).__init__(task, None, metadata) |
| 125 | self._message = "No such task '%s'" % task |
| 126 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | class TaskProgress(event.Event): |
| 128 | """ |
| 129 | Task made some progress that could be reported to the user, usually in |
| 130 | the form of a progress bar or similar. |
| 131 | NOTE: this class does not inherit from TaskBase since it doesn't need |
| 132 | to - it's fired within the task context itself, so we don't have any of |
| 133 | the context information that you do in the case of the other events. |
| 134 | The event PID can be used to determine which task it came from. |
| 135 | The progress value is normally 0-100, but can also be negative |
| 136 | indicating that progress has been made but we aren't able to determine |
| 137 | how much. |
| 138 | The rate is optional, this is simply an extra string to display to the |
| 139 | user if specified. |
| 140 | """ |
| 141 | def __init__(self, progress, rate=None): |
| 142 | self.progress = progress |
| 143 | self.rate = rate |
| 144 | event.Event.__init__(self) |
| 145 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 146 | |
| 147 | class LogTee(object): |
| 148 | def __init__(self, logger, outfile): |
| 149 | self.outfile = outfile |
| 150 | self.logger = logger |
| 151 | self.name = self.outfile.name |
| 152 | |
| 153 | def write(self, string): |
| 154 | self.logger.plain(string) |
| 155 | self.outfile.write(string) |
| 156 | |
| 157 | def __enter__(self): |
| 158 | self.outfile.__enter__() |
| 159 | return self |
| 160 | |
| 161 | def __exit__(self, *excinfo): |
| 162 | self.outfile.__exit__(*excinfo) |
| 163 | |
| 164 | def __repr__(self): |
| 165 | return '<LogTee {0}>'.format(self.name) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 166 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 167 | def flush(self): |
| 168 | self.outfile.flush() |
| 169 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 170 | |
| 171 | class StdoutNoopContextManager: |
| 172 | """ |
| 173 | This class acts like sys.stdout, but adds noop __enter__ and __exit__ methods. |
| 174 | """ |
| 175 | def __enter__(self): |
| 176 | return sys.stdout |
| 177 | |
| 178 | def __exit__(self, *exc_info): |
| 179 | pass |
| 180 | |
| 181 | def write(self, string): |
| 182 | return sys.stdout.write(string) |
| 183 | |
| 184 | def flush(self): |
| 185 | sys.stdout.flush() |
| 186 | |
| 187 | @property |
| 188 | def name(self): |
| 189 | return sys.stdout.name |
| 190 | |
| 191 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 192 | # |
| 193 | # pythonexception allows the python exceptions generated to be raised |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 194 | # as the real exceptions (not FuncFailed) and without a backtrace at the |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 195 | # origin of the failure. |
| 196 | # |
| 197 | def exec_func(func, d, dirs = None, pythonexception=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | """Execute a BB 'function'""" |
| 199 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 200 | try: |
| 201 | oldcwd = os.getcwd() |
| 202 | except: |
| 203 | oldcwd = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 204 | |
| 205 | flags = d.getVarFlags(func) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 206 | cleandirs = flags.get('cleandirs') if flags else None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | if cleandirs: |
| 208 | for cdir in d.expand(cleandirs).split(): |
| 209 | bb.utils.remove(cdir, True) |
| 210 | bb.utils.mkdirhier(cdir) |
| 211 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 212 | if flags and dirs is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 213 | dirs = flags.get('dirs') |
| 214 | if dirs: |
| 215 | dirs = d.expand(dirs).split() |
| 216 | |
| 217 | if dirs: |
| 218 | for adir in dirs: |
| 219 | bb.utils.mkdirhier(adir) |
| 220 | adir = dirs[-1] |
| 221 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 222 | adir = None |
| 223 | |
| 224 | body = d.getVar(func, False) |
| 225 | if not body: |
| 226 | if body is None: |
| 227 | logger.warning("Function %s doesn't exist", func) |
| 228 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | |
| 230 | ispython = flags.get('python') |
| 231 | |
| 232 | lockflag = flags.get('lockfiles') |
| 233 | if lockflag: |
| 234 | lockfiles = [f for f in d.expand(lockflag).split()] |
| 235 | else: |
| 236 | lockfiles = None |
| 237 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 238 | tempdir = d.getVar('T') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | |
| 240 | # or func allows items to be executed outside of the normal |
| 241 | # task set, such as buildhistory |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 242 | task = d.getVar('BB_RUNTASK') or func |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 243 | if task == func: |
| 244 | taskfunc = task |
| 245 | else: |
| 246 | taskfunc = "%s.%s" % (task, func) |
| 247 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 248 | runfmt = d.getVar('BB_RUNFMT') or "run.{func}.{pid}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | runfn = runfmt.format(taskfunc=taskfunc, task=task, func=func, pid=os.getpid()) |
| 250 | runfile = os.path.join(tempdir, runfn) |
| 251 | bb.utils.mkdirhier(os.path.dirname(runfile)) |
| 252 | |
| 253 | # Setup the courtesy link to the runfn, only for tasks |
| 254 | # we create the link 'just' before the run script is created |
| 255 | # if we create it after, and if the run script fails, then the |
| 256 | # link won't be created as an exception would be fired. |
| 257 | if task == func: |
| 258 | runlink = os.path.join(tempdir, 'run.{0}'.format(task)) |
| 259 | if runlink: |
| 260 | bb.utils.remove(runlink) |
| 261 | |
| 262 | try: |
| 263 | os.symlink(runfn, runlink) |
| 264 | except OSError: |
| 265 | pass |
| 266 | |
| 267 | with bb.utils.fileslocked(lockfiles): |
| 268 | if ispython: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 269 | exec_func_python(func, d, runfile, cwd=adir, pythonexception=pythonexception) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 270 | else: |
| 271 | exec_func_shell(func, d, runfile, cwd=adir) |
| 272 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 273 | try: |
| 274 | curcwd = os.getcwd() |
| 275 | except: |
| 276 | curcwd = None |
| 277 | |
| 278 | if oldcwd and curcwd != oldcwd: |
| 279 | try: |
| 280 | bb.warn("Task %s changed cwd to %s" % (func, curcwd)) |
| 281 | os.chdir(oldcwd) |
| 282 | except: |
| 283 | pass |
| 284 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 285 | _functionfmt = """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 286 | {function}(d) |
| 287 | """ |
| 288 | logformatter = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 289 | def exec_func_python(func, d, runfile, cwd=None, pythonexception=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 290 | """Execute a python BB 'function'""" |
| 291 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 292 | code = _functionfmt.format(function=func) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 293 | bb.utils.mkdirhier(os.path.dirname(runfile)) |
| 294 | with open(runfile, 'w') as script: |
| 295 | bb.data.emit_func_python(func, script, d) |
| 296 | |
| 297 | if cwd: |
| 298 | try: |
| 299 | olddir = os.getcwd() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 300 | except OSError as e: |
| 301 | bb.warn("%s: Cannot get cwd: %s" % (func, e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 302 | olddir = None |
| 303 | os.chdir(cwd) |
| 304 | |
| 305 | bb.debug(2, "Executing python function %s" % func) |
| 306 | |
| 307 | try: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 308 | text = "def %s(d):\n%s" % (func, d.getVar(func, False)) |
| 309 | fn = d.getVarFlag(func, "filename", False) |
| 310 | lineno = int(d.getVarFlag(func, "lineno", False)) |
| 311 | bb.methodpool.insert_method(func, text, fn, lineno - 1) |
| 312 | |
| 313 | comp = utils.better_compile(code, func, "exec_python_func() autogenerated") |
| 314 | utils.better_exec(comp, {"d": d}, code, "exec_python_func() autogenerated", pythonexception=pythonexception) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 315 | except (bb.parse.SkipRecipe, bb.build.FuncFailed): |
| 316 | raise |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 317 | except Exception as e: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 318 | if pythonexception: |
| 319 | raise |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 320 | logger.error(str(e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 321 | raise FuncFailed(func, None) |
| 322 | finally: |
| 323 | bb.debug(2, "Python function %s finished" % func) |
| 324 | |
| 325 | if cwd and olddir: |
| 326 | try: |
| 327 | os.chdir(olddir) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 328 | except OSError as e: |
| 329 | bb.warn("%s: Cannot restore cwd %s: %s" % (func, olddir, e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 330 | |
| 331 | def shell_trap_code(): |
| 332 | return '''#!/bin/sh\n |
| 333 | # Emit a useful diagnostic if something fails: |
| 334 | bb_exit_handler() { |
| 335 | ret=$? |
| 336 | case $ret in |
| 337 | 0) ;; |
| 338 | *) case $BASH_VERSION in |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 339 | "") echo "WARNING: exit code $ret from a shell command.";; |
| 340 | *) echo "WARNING: ${BASH_SOURCE[0]}:${BASH_LINENO[0]} exit $ret from '$BASH_COMMAND'";; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 341 | esac |
| 342 | exit $ret |
| 343 | esac |
| 344 | } |
| 345 | trap 'bb_exit_handler' 0 |
| 346 | set -e |
| 347 | ''' |
| 348 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 349 | def create_progress_handler(func, progress, logfile, d): |
| 350 | if progress == 'percent': |
| 351 | # Use default regex |
| 352 | return bb.progress.BasicProgressHandler(d, outfile=logfile) |
| 353 | elif progress.startswith('percent:'): |
| 354 | # Use specified regex |
| 355 | return bb.progress.BasicProgressHandler(d, regex=progress.split(':', 1)[1], outfile=logfile) |
| 356 | elif progress.startswith('outof:'): |
| 357 | # Use specified regex |
| 358 | return bb.progress.OutOfProgressHandler(d, regex=progress.split(':', 1)[1], outfile=logfile) |
| 359 | elif progress.startswith("custom:"): |
| 360 | # Use a custom progress handler that was injected via OE_EXTRA_IMPORTS or __builtins__ |
| 361 | import functools |
| 362 | from types import ModuleType |
| 363 | |
| 364 | parts = progress.split(":", 2) |
| 365 | _, cls, otherargs = parts[0], parts[1], (parts[2] or None) if parts[2:] else None |
| 366 | if cls: |
| 367 | def resolve(x, y): |
| 368 | if not x: |
| 369 | return None |
| 370 | if isinstance(x, ModuleType): |
| 371 | return getattr(x, y, None) |
| 372 | return x.get(y) |
| 373 | cls_obj = functools.reduce(resolve, cls.split("."), bb.utils._context) |
| 374 | if not cls_obj: |
| 375 | # Fall-back on __builtins__ |
| 376 | cls_obj = functools.reduce(lambda x, y: x.get(y), cls.split("."), __builtins__) |
| 377 | if cls_obj: |
| 378 | return cls_obj(d, outfile=logfile, otherargs=otherargs) |
| 379 | bb.warn('%s: unknown custom progress handler in task progress varflag value "%s", ignoring' % (func, cls)) |
| 380 | else: |
| 381 | bb.warn('%s: invalid task progress varflag value "%s", ignoring' % (func, progress)) |
| 382 | |
| 383 | return logfile |
| 384 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 385 | def exec_func_shell(func, d, runfile, cwd=None): |
| 386 | """Execute a shell function from the metadata |
| 387 | |
| 388 | Note on directory behavior. The 'dirs' varflag should contain a list |
| 389 | of the directories you need created prior to execution. The last |
| 390 | item in the list is where we will chdir/cd to. |
| 391 | """ |
| 392 | |
| 393 | # Don't let the emitted shell script override PWD |
| 394 | d.delVarFlag('PWD', 'export') |
| 395 | |
| 396 | with open(runfile, 'w') as script: |
| 397 | script.write(shell_trap_code()) |
| 398 | |
| 399 | bb.data.emit_func(func, script, d) |
| 400 | |
| 401 | if bb.msg.loggerVerboseLogs: |
| 402 | script.write("set -x\n") |
| 403 | if cwd: |
| 404 | script.write("cd '%s'\n" % cwd) |
| 405 | script.write("%s\n" % func) |
| 406 | script.write(''' |
| 407 | # cleanup |
| 408 | ret=$? |
| 409 | trap '' 0 |
| 410 | exit $ret |
| 411 | ''') |
| 412 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 413 | os.chmod(runfile, 0o775) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 414 | |
| 415 | cmd = runfile |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 416 | if d.getVarFlag(func, 'fakeroot', False): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 417 | fakerootcmd = d.getVar('FAKEROOT') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 418 | if fakerootcmd: |
| 419 | cmd = [fakerootcmd, runfile] |
| 420 | |
| 421 | if bb.msg.loggerDefaultVerbose: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 422 | logfile = LogTee(logger, StdoutNoopContextManager()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 423 | else: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 424 | logfile = StdoutNoopContextManager() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 425 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 426 | progress = d.getVarFlag(func, 'progress') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 427 | if progress: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 428 | logfile = create_progress_handler(func, progress, logfile, d) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 429 | |
| 430 | fifobuffer = bytearray() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 431 | def readfifo(data): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 432 | nonlocal fifobuffer |
| 433 | fifobuffer.extend(data) |
| 434 | while fifobuffer: |
| 435 | message, token, nextmsg = fifobuffer.partition(b"\00") |
| 436 | if token: |
| 437 | splitval = message.split(b' ', 1) |
| 438 | cmd = splitval[0].decode("utf-8") |
| 439 | if len(splitval) > 1: |
| 440 | value = splitval[1].decode("utf-8") |
| 441 | else: |
| 442 | value = '' |
| 443 | if cmd == 'bbplain': |
| 444 | bb.plain(value) |
| 445 | elif cmd == 'bbnote': |
| 446 | bb.note(value) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 447 | elif cmd == 'bbverbnote': |
| 448 | bb.verbnote(value) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 449 | elif cmd == 'bbwarn': |
| 450 | bb.warn(value) |
| 451 | elif cmd == 'bberror': |
| 452 | bb.error(value) |
| 453 | elif cmd == 'bbfatal': |
| 454 | # The caller will call exit themselves, so bb.error() is |
| 455 | # what we want here rather than bb.fatal() |
| 456 | bb.error(value) |
| 457 | elif cmd == 'bbfatal_log': |
| 458 | bb.error(value, forcelog=True) |
| 459 | elif cmd == 'bbdebug': |
| 460 | splitval = value.split(' ', 1) |
| 461 | level = int(splitval[0]) |
| 462 | value = splitval[1] |
| 463 | bb.debug(level, value) |
| 464 | else: |
| 465 | bb.warn("Unrecognised command '%s' on FIFO" % cmd) |
| 466 | fifobuffer = nextmsg |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 467 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 468 | break |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 469 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 470 | tempdir = d.getVar('T') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 471 | fifopath = os.path.join(tempdir, 'fifo.%s' % os.getpid()) |
| 472 | if os.path.exists(fifopath): |
| 473 | os.unlink(fifopath) |
| 474 | os.mkfifo(fifopath) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 475 | with open(fifopath, 'r+b', buffering=0) as fifo: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 476 | try: |
| 477 | bb.debug(2, "Executing shell function %s" % func) |
| 478 | |
| 479 | try: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 480 | with open(os.devnull, 'r+') as stdin, logfile: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 481 | bb.process.run(cmd, shell=False, stdin=stdin, log=logfile, extrafiles=[(fifo,readfifo)]) |
| 482 | except bb.process.CmdError: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 483 | logfn = d.getVar('BB_LOGFILE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 484 | raise FuncFailed(func, logfn) |
| 485 | finally: |
| 486 | os.unlink(fifopath) |
| 487 | |
| 488 | bb.debug(2, "Shell function %s finished" % func) |
| 489 | |
| 490 | def _task_data(fn, task, d): |
| 491 | localdata = bb.data.createCopy(d) |
| 492 | localdata.setVar('BB_FILENAME', fn) |
| 493 | localdata.setVar('BB_CURRENTTASK', task[3:]) |
| 494 | localdata.setVar('OVERRIDES', 'task-%s:%s' % |
| 495 | (task[3:].replace('_', '-'), d.getVar('OVERRIDES', False))) |
| 496 | localdata.finalize() |
| 497 | bb.data.expandKeys(localdata) |
| 498 | return localdata |
| 499 | |
| 500 | def _exec_task(fn, task, d, quieterr): |
| 501 | """Execute a BB 'task' |
| 502 | |
| 503 | Execution of a task involves a bit more setup than executing a function, |
| 504 | running it with its own local metadata, and with some useful variables set. |
| 505 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 506 | if not d.getVarFlag(task, 'task', False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 507 | event.fire(TaskInvalid(task, d), d) |
| 508 | logger.error("No such task: %s" % task) |
| 509 | return 1 |
| 510 | |
| 511 | logger.debug(1, "Executing task %s", task) |
| 512 | |
| 513 | localdata = _task_data(fn, task, d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 514 | tempdir = localdata.getVar('T') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 515 | if not tempdir: |
| 516 | bb.fatal("T variable not set, unable to build") |
| 517 | |
| 518 | # Change nice level if we're asked to |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 519 | nice = localdata.getVar("BB_TASK_NICE_LEVEL") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 520 | if nice: |
| 521 | curnice = os.nice(0) |
| 522 | nice = int(nice) - curnice |
| 523 | newnice = os.nice(nice) |
| 524 | logger.debug(1, "Renice to %s " % newnice) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 525 | ionice = localdata.getVar("BB_TASK_IONICE_LEVEL") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 526 | if ionice: |
| 527 | try: |
| 528 | cls, prio = ionice.split(".", 1) |
| 529 | bb.utils.ioprio_set(os.getpid(), int(cls), int(prio)) |
| 530 | except: |
| 531 | bb.warn("Invalid ionice level %s" % ionice) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 532 | |
| 533 | bb.utils.mkdirhier(tempdir) |
| 534 | |
| 535 | # Determine the logfile to generate |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 536 | logfmt = localdata.getVar('BB_LOGFMT') or 'log.{task}.{pid}' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 537 | logbase = logfmt.format(task=task, pid=os.getpid()) |
| 538 | |
| 539 | # Document the order of the tasks... |
| 540 | logorder = os.path.join(tempdir, 'log.task_order') |
| 541 | try: |
| 542 | with open(logorder, 'a') as logorderfile: |
| 543 | logorderfile.write('{0} ({1}): {2}\n'.format(task, os.getpid(), logbase)) |
| 544 | except OSError: |
| 545 | logger.exception("Opening log file '%s'", logorder) |
| 546 | pass |
| 547 | |
| 548 | # Setup the courtesy link to the logfn |
| 549 | loglink = os.path.join(tempdir, 'log.{0}'.format(task)) |
| 550 | logfn = os.path.join(tempdir, logbase) |
| 551 | if loglink: |
| 552 | bb.utils.remove(loglink) |
| 553 | |
| 554 | try: |
| 555 | os.symlink(logbase, loglink) |
| 556 | except OSError: |
| 557 | pass |
| 558 | |
| 559 | prefuncs = localdata.getVarFlag(task, 'prefuncs', expand=True) |
| 560 | postfuncs = localdata.getVarFlag(task, 'postfuncs', expand=True) |
| 561 | |
| 562 | class ErrorCheckHandler(logging.Handler): |
| 563 | def __init__(self): |
| 564 | self.triggered = False |
| 565 | logging.Handler.__init__(self, logging.ERROR) |
| 566 | def emit(self, record): |
| 567 | if getattr(record, 'forcelog', False): |
| 568 | self.triggered = False |
| 569 | else: |
| 570 | self.triggered = True |
| 571 | |
| 572 | # Handle logfiles |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 573 | try: |
| 574 | bb.utils.mkdirhier(os.path.dirname(logfn)) |
| 575 | logfile = open(logfn, 'w') |
| 576 | except OSError: |
| 577 | logger.exception("Opening log file '%s'", logfn) |
| 578 | pass |
| 579 | |
| 580 | # Dup the existing fds so we dont lose them |
| 581 | osi = [os.dup(sys.stdin.fileno()), sys.stdin.fileno()] |
| 582 | oso = [os.dup(sys.stdout.fileno()), sys.stdout.fileno()] |
| 583 | ose = [os.dup(sys.stderr.fileno()), sys.stderr.fileno()] |
| 584 | |
| 585 | # Replace those fds with our own |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 586 | with open('/dev/null', 'r') as si: |
| 587 | os.dup2(si.fileno(), osi[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 588 | os.dup2(logfile.fileno(), oso[1]) |
| 589 | os.dup2(logfile.fileno(), ose[1]) |
| 590 | |
| 591 | # Ensure Python logging goes to the logfile |
| 592 | handler = logging.StreamHandler(logfile) |
| 593 | handler.setFormatter(logformatter) |
| 594 | # Always enable full debug output into task logfiles |
| 595 | handler.setLevel(logging.DEBUG - 2) |
| 596 | bblogger.addHandler(handler) |
| 597 | |
| 598 | errchk = ErrorCheckHandler() |
| 599 | bblogger.addHandler(errchk) |
| 600 | |
| 601 | localdata.setVar('BB_LOGFILE', logfn) |
| 602 | localdata.setVar('BB_RUNTASK', task) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 603 | localdata.setVar('BB_TASK_LOGGER', bblogger) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 604 | |
| 605 | flags = localdata.getVarFlags(task) |
| 606 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 607 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 608 | try: |
| 609 | event.fire(TaskStarted(task, logfn, flags, localdata), localdata) |
| 610 | except (bb.BBHandledException, SystemExit): |
| 611 | return 1 |
| 612 | except FuncFailed as exc: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 613 | logger.error(str(exc)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 614 | return 1 |
| 615 | |
| 616 | try: |
| 617 | for func in (prefuncs or '').split(): |
| 618 | exec_func(func, localdata) |
| 619 | exec_func(task, localdata) |
| 620 | for func in (postfuncs or '').split(): |
| 621 | exec_func(func, localdata) |
| 622 | except FuncFailed as exc: |
| 623 | if quieterr: |
| 624 | event.fire(TaskFailedSilent(task, logfn, localdata), localdata) |
| 625 | else: |
| 626 | errprinted = errchk.triggered |
| 627 | logger.error(str(exc)) |
| 628 | event.fire(TaskFailed(task, logfn, localdata, errprinted), localdata) |
| 629 | return 1 |
| 630 | except bb.BBHandledException: |
| 631 | event.fire(TaskFailed(task, logfn, localdata, True), localdata) |
| 632 | return 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 633 | finally: |
| 634 | sys.stdout.flush() |
| 635 | sys.stderr.flush() |
| 636 | |
| 637 | bblogger.removeHandler(handler) |
| 638 | |
| 639 | # Restore the backup fds |
| 640 | os.dup2(osi[0], osi[1]) |
| 641 | os.dup2(oso[0], oso[1]) |
| 642 | os.dup2(ose[0], ose[1]) |
| 643 | |
| 644 | # Close the backup fds |
| 645 | os.close(osi[0]) |
| 646 | os.close(oso[0]) |
| 647 | os.close(ose[0]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 648 | |
| 649 | logfile.close() |
| 650 | if os.path.exists(logfn) and os.path.getsize(logfn) == 0: |
| 651 | logger.debug(2, "Zero size logfn %s, removing", logfn) |
| 652 | bb.utils.remove(logfn) |
| 653 | bb.utils.remove(loglink) |
| 654 | event.fire(TaskSucceeded(task, logfn, localdata), localdata) |
| 655 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 656 | if not localdata.getVarFlag(task, 'nostamp', False) and not localdata.getVarFlag(task, 'selfstamp', False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 657 | make_stamp(task, localdata) |
| 658 | |
| 659 | return 0 |
| 660 | |
| 661 | def exec_task(fn, task, d, profile = False): |
| 662 | try: |
| 663 | quieterr = False |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 664 | if d.getVarFlag(task, "quieterrors", False) is not None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 665 | quieterr = True |
| 666 | |
| 667 | if profile: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 668 | profname = "profile-%s.log" % (d.getVar("PN") + "-" + task) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 669 | try: |
| 670 | import cProfile as profile |
| 671 | except: |
| 672 | import profile |
| 673 | prof = profile.Profile() |
| 674 | ret = profile.Profile.runcall(prof, _exec_task, fn, task, d, quieterr) |
| 675 | prof.dump_stats(profname) |
| 676 | bb.utils.process_profilelog(profname) |
| 677 | |
| 678 | return ret |
| 679 | else: |
| 680 | return _exec_task(fn, task, d, quieterr) |
| 681 | |
| 682 | except Exception: |
| 683 | from traceback import format_exc |
| 684 | if not quieterr: |
| 685 | logger.error("Build of %s failed" % (task)) |
| 686 | logger.error(format_exc()) |
| 687 | failedevent = TaskFailed(task, None, d, True) |
| 688 | event.fire(failedevent, d) |
| 689 | return 1 |
| 690 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 691 | def stamp_internal(taskname, d, file_name, baseonly=False, noextra=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 692 | """ |
| 693 | Internal stamp helper function |
| 694 | Makes sure the stamp directory exists |
| 695 | Returns the stamp path+filename |
| 696 | |
| 697 | In the bitbake core, d can be a CacheData and file_name will be set. |
| 698 | When called in task context, d will be a data store, file_name will not be set |
| 699 | """ |
| 700 | taskflagname = taskname |
| 701 | if taskname.endswith("_setscene") and taskname != "do_setscene": |
| 702 | taskflagname = taskname.replace("_setscene", "") |
| 703 | |
| 704 | if file_name: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 705 | stamp = d.stamp[file_name] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 706 | extrainfo = d.stamp_extrainfo[file_name].get(taskflagname) or "" |
| 707 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 708 | stamp = d.getVar('STAMP') |
| 709 | file_name = d.getVar('BB_FILENAME') |
| 710 | extrainfo = d.getVarFlag(taskflagname, 'stamp-extra-info') or "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 711 | |
| 712 | if baseonly: |
| 713 | return stamp |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 714 | if noextra: |
| 715 | extrainfo = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 716 | |
| 717 | if not stamp: |
| 718 | return |
| 719 | |
| 720 | stamp = bb.parse.siggen.stampfile(stamp, file_name, taskname, extrainfo) |
| 721 | |
| 722 | stampdir = os.path.dirname(stamp) |
| 723 | if cached_mtime_noerror(stampdir) == 0: |
| 724 | bb.utils.mkdirhier(stampdir) |
| 725 | |
| 726 | return stamp |
| 727 | |
| 728 | def stamp_cleanmask_internal(taskname, d, file_name): |
| 729 | """ |
| 730 | Internal stamp helper function to generate stamp cleaning mask |
| 731 | Returns the stamp path+filename |
| 732 | |
| 733 | In the bitbake core, d can be a CacheData and file_name will be set. |
| 734 | When called in task context, d will be a data store, file_name will not be set |
| 735 | """ |
| 736 | taskflagname = taskname |
| 737 | if taskname.endswith("_setscene") and taskname != "do_setscene": |
| 738 | taskflagname = taskname.replace("_setscene", "") |
| 739 | |
| 740 | if file_name: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 741 | stamp = d.stampclean[file_name] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 742 | extrainfo = d.stamp_extrainfo[file_name].get(taskflagname) or "" |
| 743 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 744 | stamp = d.getVar('STAMPCLEAN') |
| 745 | file_name = d.getVar('BB_FILENAME') |
| 746 | extrainfo = d.getVarFlag(taskflagname, 'stamp-extra-info') or "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 747 | |
| 748 | if not stamp: |
| 749 | return [] |
| 750 | |
| 751 | cleanmask = bb.parse.siggen.stampcleanmask(stamp, file_name, taskname, extrainfo) |
| 752 | |
| 753 | return [cleanmask, cleanmask.replace(taskflagname, taskflagname + "_setscene")] |
| 754 | |
| 755 | def make_stamp(task, d, file_name = None): |
| 756 | """ |
| 757 | Creates/updates a stamp for a given task |
| 758 | (d can be a data dict or dataCache) |
| 759 | """ |
| 760 | cleanmask = stamp_cleanmask_internal(task, d, file_name) |
| 761 | for mask in cleanmask: |
| 762 | for name in glob.glob(mask): |
| 763 | # Preserve sigdata files in the stamps directory |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 764 | if "sigdata" in name or "sigbasedata" in name: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 765 | continue |
| 766 | # Preserve taint files in the stamps directory |
| 767 | if name.endswith('.taint'): |
| 768 | continue |
| 769 | os.unlink(name) |
| 770 | |
| 771 | stamp = stamp_internal(task, d, file_name) |
| 772 | # Remove the file and recreate to force timestamp |
| 773 | # change on broken NFS filesystems |
| 774 | if stamp: |
| 775 | bb.utils.remove(stamp) |
| 776 | open(stamp, "w").close() |
| 777 | |
| 778 | # If we're in task context, write out a signature file for each task |
| 779 | # as it completes |
| 780 | if not task.endswith("_setscene") and task != "do_setscene" and not file_name: |
| 781 | stampbase = stamp_internal(task, d, None, True) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 782 | file_name = d.getVar('BB_FILENAME') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 783 | bb.parse.siggen.dump_sigtask(file_name, task, stampbase, True) |
| 784 | |
| 785 | def del_stamp(task, d, file_name = None): |
| 786 | """ |
| 787 | Removes a stamp for a given task |
| 788 | (d can be a data dict or dataCache) |
| 789 | """ |
| 790 | stamp = stamp_internal(task, d, file_name) |
| 791 | bb.utils.remove(stamp) |
| 792 | |
| 793 | def write_taint(task, d, file_name = None): |
| 794 | """ |
| 795 | Creates a "taint" file which will force the specified task and its |
| 796 | dependents to be re-run the next time by influencing the value of its |
| 797 | taskhash. |
| 798 | (d can be a data dict or dataCache) |
| 799 | """ |
| 800 | import uuid |
| 801 | if file_name: |
| 802 | taintfn = d.stamp[file_name] + '.' + task + '.taint' |
| 803 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 804 | taintfn = d.getVar('STAMP') + '.' + task + '.taint' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 805 | bb.utils.mkdirhier(os.path.dirname(taintfn)) |
| 806 | # The specific content of the taint file is not really important, |
| 807 | # we just need it to be random, so a random UUID is used |
| 808 | with open(taintfn, 'w') as taintf: |
| 809 | taintf.write(str(uuid.uuid4())) |
| 810 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 811 | def stampfile(taskname, d, file_name = None, noextra=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 812 | """ |
| 813 | Return the stamp for a given task |
| 814 | (d can be a data dict or dataCache) |
| 815 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 816 | return stamp_internal(taskname, d, file_name, noextra=noextra) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 817 | |
| 818 | def add_tasks(tasklist, d): |
| 819 | task_deps = d.getVar('_task_deps', False) |
| 820 | if not task_deps: |
| 821 | task_deps = {} |
| 822 | if not 'tasks' in task_deps: |
| 823 | task_deps['tasks'] = [] |
| 824 | if not 'parents' in task_deps: |
| 825 | task_deps['parents'] = {} |
| 826 | |
| 827 | for task in tasklist: |
| 828 | task = d.expand(task) |
| 829 | |
| 830 | d.setVarFlag(task, 'task', 1) |
| 831 | |
| 832 | if not task in task_deps['tasks']: |
| 833 | task_deps['tasks'].append(task) |
| 834 | |
| 835 | flags = d.getVarFlags(task) |
| 836 | def getTask(name): |
| 837 | if not name in task_deps: |
| 838 | task_deps[name] = {} |
| 839 | if name in flags: |
| 840 | deptask = d.expand(flags[name]) |
| 841 | task_deps[name][task] = deptask |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 842 | getTask('mcdepends') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 843 | getTask('depends') |
| 844 | getTask('rdepends') |
| 845 | getTask('deptask') |
| 846 | getTask('rdeptask') |
| 847 | getTask('recrdeptask') |
| 848 | getTask('recideptask') |
| 849 | getTask('nostamp') |
| 850 | getTask('fakeroot') |
| 851 | getTask('noexec') |
| 852 | getTask('umask') |
| 853 | task_deps['parents'][task] = [] |
| 854 | if 'deps' in flags: |
| 855 | for dep in flags['deps']: |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 856 | # Check and warn for "addtask task after foo" while foo does not exist |
| 857 | #if not dep in tasklist: |
| 858 | # bb.warn('%s: dependent task %s for %s does not exist' % (d.getVar('PN'), dep, task)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 859 | dep = d.expand(dep) |
| 860 | task_deps['parents'][task].append(dep) |
| 861 | |
| 862 | # don't assume holding a reference |
| 863 | d.setVar('_task_deps', task_deps) |
| 864 | |
| 865 | def addtask(task, before, after, d): |
| 866 | if task[:3] != "do_": |
| 867 | task = "do_" + task |
| 868 | |
| 869 | d.setVarFlag(task, "task", 1) |
| 870 | bbtasks = d.getVar('__BBTASKS', False) or [] |
| 871 | if task not in bbtasks: |
| 872 | bbtasks.append(task) |
| 873 | d.setVar('__BBTASKS', bbtasks) |
| 874 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 875 | existing = d.getVarFlag(task, "deps", False) or [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 876 | if after is not None: |
| 877 | # set up deps for function |
| 878 | for entry in after.split(): |
| 879 | if entry not in existing: |
| 880 | existing.append(entry) |
| 881 | d.setVarFlag(task, "deps", existing) |
| 882 | if before is not None: |
| 883 | # set up things that depend on this func |
| 884 | for entry in before.split(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 885 | existing = d.getVarFlag(entry, "deps", False) or [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 886 | if task not in existing: |
| 887 | d.setVarFlag(entry, "deps", [task] + existing) |
| 888 | |
| 889 | def deltask(task, d): |
| 890 | if task[:3] != "do_": |
| 891 | task = "do_" + task |
| 892 | |
| 893 | bbtasks = d.getVar('__BBTASKS', False) or [] |
| 894 | if task in bbtasks: |
| 895 | bbtasks.remove(task) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 896 | d.delVarFlag(task, 'task') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 897 | d.setVar('__BBTASKS', bbtasks) |
| 898 | |
| 899 | d.delVarFlag(task, 'deps') |
| 900 | for bbtask in d.getVar('__BBTASKS', False) or []: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 901 | deps = d.getVarFlag(bbtask, 'deps', False) or [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 902 | if task in deps: |
| 903 | deps.remove(task) |
| 904 | d.setVarFlag(bbtask, 'deps', deps) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 905 | |
| 906 | def preceedtask(task, with_recrdeptasks, d): |
| 907 | """ |
| 908 | Returns a set of tasks in the current recipe which were specified as |
| 909 | precondition by the task itself ("after") or which listed themselves |
| 910 | as precondition ("before"). Preceeding tasks specified via the |
| 911 | "recrdeptask" are included in the result only if requested. Beware |
| 912 | that this may lead to the task itself being listed. |
| 913 | """ |
| 914 | preceed = set() |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 915 | |
| 916 | # Ignore tasks which don't exist |
| 917 | tasks = d.getVar('__BBTASKS', False) |
| 918 | if task not in tasks: |
| 919 | return preceed |
| 920 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 921 | preceed.update(d.getVarFlag(task, 'deps') or []) |
| 922 | if with_recrdeptasks: |
| 923 | recrdeptask = d.getVarFlag(task, 'recrdeptask') |
| 924 | if recrdeptask: |
| 925 | preceed.update(recrdeptask.split()) |
| 926 | return preceed |
| 927 | |
| 928 | def tasksbetween(task_start, task_end, d): |
| 929 | """ |
| 930 | Return the list of tasks between two tasks in the current recipe, |
| 931 | where task_start is to start at and task_end is the task to end at |
| 932 | (and task_end has a dependency chain back to task_start). |
| 933 | """ |
| 934 | outtasks = [] |
| 935 | tasks = list(filter(lambda k: d.getVarFlag(k, "task"), d.keys())) |
| 936 | def follow_chain(task, endtask, chain=None): |
| 937 | if not chain: |
| 938 | chain = [] |
| 939 | chain.append(task) |
| 940 | for othertask in tasks: |
| 941 | if othertask == task: |
| 942 | continue |
| 943 | if task == endtask: |
| 944 | for ctask in chain: |
| 945 | if ctask not in outtasks: |
| 946 | outtasks.append(ctask) |
| 947 | else: |
| 948 | deps = d.getVarFlag(othertask, 'deps', False) |
| 949 | if task in deps: |
| 950 | follow_chain(othertask, endtask, chain) |
| 951 | chain.pop() |
| 952 | follow_chain(task_start, task_end) |
| 953 | return outtasks |