Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | import subprocess |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 8 | import multiprocessing |
| 9 | import traceback |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | |
| 11 | def read_file(filename): |
| 12 | try: |
| 13 | f = open( filename, "r" ) |
| 14 | except IOError as reason: |
| 15 | return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M: |
| 16 | else: |
| 17 | data = f.read().strip() |
| 18 | f.close() |
| 19 | return data |
| 20 | return None |
| 21 | |
| 22 | def ifelse(condition, iftrue = True, iffalse = False): |
| 23 | if condition: |
| 24 | return iftrue |
| 25 | else: |
| 26 | return iffalse |
| 27 | |
| 28 | def conditional(variable, checkvalue, truevalue, falsevalue, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 29 | if d.getVar(variable) == checkvalue: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | return truevalue |
| 31 | else: |
| 32 | return falsevalue |
| 33 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | def vartrue(var, iftrue, iffalse, d): |
| 35 | import oe.types |
| 36 | if oe.types.boolean(d.getVar(var)): |
| 37 | return iftrue |
| 38 | else: |
| 39 | return iffalse |
| 40 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | def less_or_equal(variable, checkvalue, truevalue, falsevalue, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 42 | if float(d.getVar(variable)) <= float(checkvalue): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | return truevalue |
| 44 | else: |
| 45 | return falsevalue |
| 46 | |
| 47 | def version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | result = bb.utils.vercmp_string(d.getVar(variable), checkvalue) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | if result <= 0: |
| 50 | return truevalue |
| 51 | else: |
| 52 | return falsevalue |
| 53 | |
| 54 | def both_contain(variable1, variable2, checkvalue, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | val1 = d.getVar(variable1) |
| 56 | val2 = d.getVar(variable2) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | val1 = set(val1.split()) |
| 58 | val2 = set(val2.split()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 59 | if isinstance(checkvalue, str): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | checkvalue = set(checkvalue.split()) |
| 61 | else: |
| 62 | checkvalue = set(checkvalue) |
| 63 | if checkvalue.issubset(val1) and checkvalue.issubset(val2): |
| 64 | return " ".join(checkvalue) |
| 65 | else: |
| 66 | return "" |
| 67 | |
| 68 | def set_intersect(variable1, variable2, d): |
| 69 | """ |
| 70 | Expand both variables, interpret them as lists of strings, and return the |
| 71 | intersection as a flattened string. |
| 72 | |
| 73 | For example: |
| 74 | s1 = "a b c" |
| 75 | s2 = "b c d" |
| 76 | s3 = set_intersect(s1, s2) |
| 77 | => s3 = "b c" |
| 78 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | val1 = set(d.getVar(variable1).split()) |
| 80 | val2 = set(d.getVar(variable2).split()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | return " ".join(val1 & val2) |
| 82 | |
| 83 | def prune_suffix(var, suffixes, d): |
| 84 | # See if var ends with any of the suffixes listed and |
| 85 | # remove it if found |
| 86 | for suffix in suffixes: |
Brad Bishop | d89cb5f | 2019-04-10 09:02:41 -0400 | [diff] [blame] | 87 | if suffix and var.endswith(suffix): |
| 88 | var = var[:-len(suffix)] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 90 | prefix = d.getVar("MLPREFIX") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | if prefix and var.startswith(prefix): |
Brad Bishop | d89cb5f | 2019-04-10 09:02:41 -0400 | [diff] [blame] | 92 | var = var[len(prefix):] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | |
| 94 | return var |
| 95 | |
| 96 | def str_filter(f, str, d): |
| 97 | from re import match |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 98 | return " ".join([x for x in str.split() if match(f, x, 0)]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | |
| 100 | def str_filter_out(f, str, d): |
| 101 | from re import match |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 102 | return " ".join([x for x in str.split() if not match(f, x, 0)]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 104 | def build_depends_string(depends, task): |
| 105 | """Append a taskname to a string of dependencies as used by the [depends] flag""" |
| 106 | return " ".join(dep + ":" + task for dep in depends.split()) |
| 107 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | def inherits(d, *classes): |
| 109 | """Return True if the metadata inherits any of the specified classes""" |
| 110 | return any(bb.data.inherits_class(cls, d) for cls in classes) |
| 111 | |
| 112 | def features_backfill(var,d): |
| 113 | # This construct allows the addition of new features to variable specified |
| 114 | # as var |
| 115 | # Example for var = "DISTRO_FEATURES" |
| 116 | # This construct allows the addition of new features to DISTRO_FEATURES |
| 117 | # that if not present would disable existing functionality, without |
| 118 | # disturbing distributions that have already set DISTRO_FEATURES. |
| 119 | # Distributions wanting to elide a value in DISTRO_FEATURES_BACKFILL should |
| 120 | # add the feature to DISTRO_FEATURES_BACKFILL_CONSIDERED |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 121 | features = (d.getVar(var) or "").split() |
| 122 | backfill = (d.getVar(var+"_BACKFILL") or "").split() |
| 123 | considered = (d.getVar(var+"_BACKFILL_CONSIDERED") or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | |
| 125 | addfeatures = [] |
| 126 | for feature in backfill: |
| 127 | if feature not in features and feature not in considered: |
| 128 | addfeatures.append(feature) |
| 129 | |
| 130 | if addfeatures: |
| 131 | d.appendVar(var, " " + " ".join(addfeatures)) |
| 132 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 133 | def all_distro_features(d, features, truevalue="1", falsevalue=""): |
| 134 | """ |
| 135 | Returns truevalue if *all* given features are set in DISTRO_FEATURES, |
| 136 | else falsevalue. The features can be given as single string or anything |
| 137 | that can be turned into a set. |
| 138 | |
| 139 | This is a shorter, more flexible version of |
| 140 | bb.utils.contains("DISTRO_FEATURES", features, truevalue, falsevalue, d). |
| 141 | |
| 142 | Without explicit true/false values it can be used directly where |
| 143 | Python expects a boolean: |
| 144 | if oe.utils.all_distro_features(d, "foo bar"): |
| 145 | bb.fatal("foo and bar are mutually exclusive DISTRO_FEATURES") |
| 146 | |
| 147 | With just a truevalue, it can be used to include files that are meant to be |
| 148 | used only when requested via DISTRO_FEATURES: |
| 149 | require ${@ oe.utils.all_distro_features(d, "foo bar", "foo-and-bar.inc") |
| 150 | """ |
| 151 | return bb.utils.contains("DISTRO_FEATURES", features, truevalue, falsevalue, d) |
| 152 | |
| 153 | def any_distro_features(d, features, truevalue="1", falsevalue=""): |
| 154 | """ |
| 155 | Returns truevalue if at least *one* of the given features is set in DISTRO_FEATURES, |
| 156 | else falsevalue. The features can be given as single string or anything |
| 157 | that can be turned into a set. |
| 158 | |
| 159 | This is a shorter, more flexible version of |
| 160 | bb.utils.contains_any("DISTRO_FEATURES", features, truevalue, falsevalue, d). |
| 161 | |
| 162 | Without explicit true/false values it can be used directly where |
| 163 | Python expects a boolean: |
| 164 | if not oe.utils.any_distro_features(d, "foo bar"): |
| 165 | bb.fatal("foo, bar or both must be set in DISTRO_FEATURES") |
| 166 | |
| 167 | With just a truevalue, it can be used to include files that are meant to be |
| 168 | used only when requested via DISTRO_FEATURES: |
| 169 | require ${@ oe.utils.any_distro_features(d, "foo bar", "foo-or-bar.inc") |
| 170 | |
| 171 | """ |
| 172 | return bb.utils.contains_any("DISTRO_FEATURES", features, truevalue, falsevalue, d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 173 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 174 | def parallel_make(d, makeinst=False): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 175 | """ |
| 176 | Return the integer value for the number of parallel threads to use when |
| 177 | building, scraped out of PARALLEL_MAKE. If no parallelization option is |
| 178 | found, returns None |
| 179 | |
| 180 | e.g. if PARALLEL_MAKE = "-j 10", this will return 10 as an integer. |
| 181 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 182 | if makeinst: |
| 183 | pm = (d.getVar('PARALLEL_MAKEINST') or '').split() |
| 184 | else: |
| 185 | pm = (d.getVar('PARALLEL_MAKE') or '').split() |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 186 | # look for '-j' and throw other options (e.g. '-l') away |
| 187 | while pm: |
| 188 | opt = pm.pop(0) |
| 189 | if opt == '-j': |
| 190 | v = pm.pop(0) |
| 191 | elif opt.startswith('-j'): |
| 192 | v = opt[2:].strip() |
| 193 | else: |
| 194 | continue |
| 195 | |
| 196 | return int(v) |
| 197 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 198 | return '' |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 199 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 200 | def parallel_make_argument(d, fmt, limit=None, makeinst=False): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 201 | """ |
| 202 | Helper utility to construct a parallel make argument from the number of |
| 203 | parallel threads specified in PARALLEL_MAKE. |
| 204 | |
| 205 | Returns the input format string `fmt` where a single '%d' will be expanded |
| 206 | with the number of parallel threads to use. If `limit` is specified, the |
| 207 | number of parallel threads will be no larger than it. If no parallelization |
| 208 | option is found in PARALLEL_MAKE, returns an empty string |
| 209 | |
| 210 | e.g. if PARALLEL_MAKE = "-j 10", parallel_make_argument(d, "-n %d") will return |
| 211 | "-n 10" |
| 212 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 213 | v = parallel_make(d, makeinst) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 214 | if v: |
| 215 | if limit: |
| 216 | v = min(limit, v) |
| 217 | return fmt % v |
| 218 | return '' |
| 219 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 220 | def packages_filter_out_system(d): |
| 221 | """ |
| 222 | Return a list of packages from PACKAGES with the "system" packages such as |
| 223 | PN-dbg PN-doc PN-locale-eb-gb removed. |
| 224 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 225 | pn = d.getVar('PN') |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 226 | pkgfilter = [pn + suffix for suffix in ('', '-dbg', '-dev', '-doc', '-locale', '-staticdev', '-src')] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | localepkg = pn + "-locale-" |
| 228 | pkgs = [] |
| 229 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 230 | for pkg in d.getVar('PACKAGES').split(): |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 231 | if pkg not in pkgfilter and localepkg not in pkg: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 232 | pkgs.append(pkg) |
| 233 | return pkgs |
| 234 | |
| 235 | def getstatusoutput(cmd): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 236 | return subprocess.getstatusoutput(cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def trim_version(version, num_parts=2): |
| 240 | """ |
| 241 | Return just the first <num_parts> of <version>, split by periods. For |
| 242 | example, trim_version("1.2.3", 2) will return "1.2". |
| 243 | """ |
| 244 | if type(version) is not str: |
| 245 | raise TypeError("Version should be a string") |
| 246 | if num_parts < 1: |
| 247 | raise ValueError("Cannot split to parts < 1") |
| 248 | |
| 249 | parts = version.split(".") |
| 250 | trimmed = ".".join(parts[:num_parts]) |
| 251 | return trimmed |
| 252 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 253 | def cpu_count(at_least=1, at_most=64): |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 254 | cpus = len(os.sched_getaffinity(0)) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 255 | return max(min(cpus, at_most), at_least) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 256 | |
| 257 | def execute_pre_post_process(d, cmds): |
| 258 | if cmds is None: |
| 259 | return |
| 260 | |
| 261 | for cmd in cmds.strip().split(';'): |
| 262 | cmd = cmd.strip() |
| 263 | if cmd != '': |
| 264 | bb.note("Executing %s ..." % cmd) |
| 265 | bb.build.exec_func(cmd, d) |
| 266 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 267 | # For each item in items, call the function 'target' with item as the first |
| 268 | # argument, extraargs as the other arguments and handle any exceptions in the |
| 269 | # parent thread |
| 270 | def multiprocess_launch(target, items, d, extraargs=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 271 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 272 | class ProcessLaunch(multiprocessing.Process): |
| 273 | def __init__(self, *args, **kwargs): |
| 274 | multiprocessing.Process.__init__(self, *args, **kwargs) |
| 275 | self._pconn, self._cconn = multiprocessing.Pipe() |
| 276 | self._exception = None |
| 277 | self._result = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 278 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 279 | def run(self): |
| 280 | try: |
| 281 | ret = self._target(*self._args, **self._kwargs) |
| 282 | self._cconn.send((None, ret)) |
| 283 | except Exception as e: |
| 284 | tb = traceback.format_exc() |
| 285 | self._cconn.send((e, tb)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 286 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 287 | def update(self): |
| 288 | if self._pconn.poll(): |
| 289 | (e, tb) = self._pconn.recv() |
| 290 | if e is not None: |
| 291 | self._exception = (e, tb) |
| 292 | else: |
| 293 | self._result = tb |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 294 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 295 | @property |
| 296 | def exception(self): |
| 297 | self.update() |
| 298 | return self._exception |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 299 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 300 | @property |
| 301 | def result(self): |
| 302 | self.update() |
| 303 | return self._result |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 304 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 305 | max_process = int(d.getVar("BB_NUMBER_THREADS") or os.cpu_count() or 1) |
| 306 | launched = [] |
| 307 | errors = [] |
| 308 | results = [] |
| 309 | items = list(items) |
| 310 | while (items and not errors) or launched: |
| 311 | if not errors and items and len(launched) < max_process: |
| 312 | args = (items.pop(),) |
| 313 | if extraargs is not None: |
| 314 | args = args + extraargs |
| 315 | p = ProcessLaunch(target=target, args=args) |
| 316 | p.start() |
| 317 | launched.append(p) |
| 318 | for q in launched: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 319 | # Have to manually call update() to avoid deadlocks. The pipe can be full and |
| 320 | # transfer stalled until we try and read the results object but the subprocess won't exit |
| 321 | # as it still has data to write (https://bugs.python.org/issue8426) |
| 322 | q.update() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 323 | # The finished processes are joined when calling is_alive() |
| 324 | if not q.is_alive(): |
| 325 | if q.exception: |
| 326 | errors.append(q.exception) |
| 327 | if q.result: |
| 328 | results.append(q.result) |
| 329 | launched.remove(q) |
| 330 | # Paranoia doesn't hurt |
| 331 | for p in launched: |
| 332 | p.join() |
| 333 | if errors: |
| 334 | msg = "" |
| 335 | for (e, tb) in errors: |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 336 | if isinstance(e, subprocess.CalledProcessError) and e.output: |
| 337 | msg = msg + str(e) + "\n" |
| 338 | msg = msg + "Subprocess output:" |
| 339 | msg = msg + e.output.decode("utf-8", errors="ignore") |
| 340 | else: |
| 341 | msg = msg + str(e) + ": " + str(tb) + "\n" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 342 | bb.fatal("Fatal errors occurred in subprocesses:\n%s" % msg) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 343 | return results |
| 344 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 345 | def squashspaces(string): |
| 346 | import re |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 347 | return re.sub(r"\s+", " ", string).strip() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 348 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 349 | def rprovides_map(pkgdata_dir, pkg_dict): |
| 350 | # Map file -> pkg provider |
| 351 | rprov_map = {} |
| 352 | |
| 353 | for pkg in pkg_dict: |
| 354 | path_to_pkgfile = os.path.join(pkgdata_dir, 'runtime-reverse', pkg) |
| 355 | if not os.path.isfile(path_to_pkgfile): |
| 356 | continue |
| 357 | with open(path_to_pkgfile) as f: |
| 358 | for line in f: |
| 359 | if line.startswith('RPROVIDES') or line.startswith('FILERPROVIDES'): |
| 360 | # List all components provided by pkg. |
| 361 | # Exclude version strings, i.e. those starting with ( |
| 362 | provides = [x for x in line.split()[1:] if not x.startswith('(')] |
| 363 | for prov in provides: |
| 364 | if prov in rprov_map: |
| 365 | rprov_map[prov].append(pkg) |
| 366 | else: |
| 367 | rprov_map[prov] = [pkg] |
| 368 | |
| 369 | return rprov_map |
| 370 | |
| 371 | def format_pkg_list(pkg_dict, ret_format=None, pkgdata_dir=None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 372 | output = [] |
| 373 | |
| 374 | if ret_format == "arch": |
| 375 | for pkg in sorted(pkg_dict): |
| 376 | output.append("%s %s" % (pkg, pkg_dict[pkg]["arch"])) |
| 377 | elif ret_format == "file": |
| 378 | for pkg in sorted(pkg_dict): |
| 379 | output.append("%s %s %s" % (pkg, pkg_dict[pkg]["filename"], pkg_dict[pkg]["arch"])) |
| 380 | elif ret_format == "ver": |
| 381 | for pkg in sorted(pkg_dict): |
| 382 | output.append("%s %s %s" % (pkg, pkg_dict[pkg]["arch"], pkg_dict[pkg]["ver"])) |
| 383 | elif ret_format == "deps": |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 384 | rprov_map = rprovides_map(pkgdata_dir, pkg_dict) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 385 | for pkg in sorted(pkg_dict): |
| 386 | for dep in pkg_dict[pkg]["deps"]: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 387 | if dep in rprov_map: |
| 388 | # There could be multiple providers within the image |
| 389 | for pkg_provider in rprov_map[dep]: |
| 390 | output.append("%s|%s * %s [RPROVIDES]" % (pkg, pkg_provider, dep)) |
| 391 | else: |
| 392 | output.append("%s|%s" % (pkg, dep)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 393 | else: |
| 394 | for pkg in sorted(pkg_dict): |
| 395 | output.append(pkg) |
| 396 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 397 | output_str = '\n'.join(output) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 398 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 399 | if output_str: |
| 400 | # make sure last line is newline terminated |
| 401 | output_str += '\n' |
| 402 | |
| 403 | return output_str |
| 404 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 405 | |
| 406 | # Helper function to get the host compiler version |
| 407 | # Do not assume the compiler is gcc |
| 408 | def get_host_compiler_version(d, taskcontextonly=False): |
| 409 | import re, subprocess |
| 410 | |
| 411 | if taskcontextonly and d.getVar('BB_WORKERCONTEXT') != '1': |
| 412 | return |
| 413 | |
| 414 | compiler = d.getVar("BUILD_CC") |
| 415 | # Get rid of ccache since it is not present when parsing. |
| 416 | if compiler.startswith('ccache '): |
| 417 | compiler = compiler[7:] |
| 418 | try: |
| 419 | env = os.environ.copy() |
| 420 | # datastore PATH does not contain session PATH as set by environment-setup-... |
| 421 | # this breaks the install-buildtools use-case |
| 422 | # env["PATH"] = d.getVar("PATH") |
| 423 | output = subprocess.check_output("%s --version" % compiler, \ |
| 424 | shell=True, env=env, stderr=subprocess.STDOUT).decode("utf-8") |
| 425 | except subprocess.CalledProcessError as e: |
| 426 | bb.fatal("Error running %s --version: %s" % (compiler, e.output.decode("utf-8"))) |
| 427 | |
| 428 | match = re.match(r".* (\d+\.\d+)\.\d+.*", output.split('\n')[0]) |
| 429 | if not match: |
| 430 | bb.fatal("Can't get compiler version from %s --version output" % compiler) |
| 431 | |
| 432 | version = match.group(1) |
| 433 | return compiler, version |
| 434 | |
| 435 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 436 | def host_gcc_version(d, taskcontextonly=False): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 437 | import re, subprocess |
| 438 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 439 | if taskcontextonly and d.getVar('BB_WORKERCONTEXT') != '1': |
| 440 | return |
| 441 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 442 | compiler = d.getVar("BUILD_CC") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 443 | # Get rid of ccache since it is not present when parsing. |
| 444 | if compiler.startswith('ccache '): |
| 445 | compiler = compiler[7:] |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 446 | try: |
| 447 | env = os.environ.copy() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 448 | env["PATH"] = d.getVar("PATH") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 449 | output = subprocess.check_output("%s --version" % compiler, \ |
| 450 | shell=True, env=env, stderr=subprocess.STDOUT).decode("utf-8") |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 451 | except subprocess.CalledProcessError as e: |
| 452 | bb.fatal("Error running %s --version: %s" % (compiler, e.output.decode("utf-8"))) |
| 453 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 454 | match = re.match(r".* (\d+\.\d+)\.\d+.*", output.split('\n')[0]) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 455 | if not match: |
| 456 | bb.fatal("Can't get compiler version from %s --version output" % compiler) |
| 457 | |
| 458 | version = match.group(1) |
| 459 | return "-%s" % version if version in ("4.8", "4.9") else "" |
| 460 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 461 | |
| 462 | def get_multilib_datastore(variant, d): |
| 463 | localdata = bb.data.createCopy(d) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 464 | if variant: |
| 465 | overrides = localdata.getVar("OVERRIDES", False) + ":virtclass-multilib-" + variant |
| 466 | localdata.setVar("OVERRIDES", overrides) |
| 467 | localdata.setVar("MLPREFIX", variant + "-") |
| 468 | else: |
| 469 | origdefault = localdata.getVar("DEFAULTTUNE_MULTILIB_ORIGINAL") |
| 470 | if origdefault: |
| 471 | localdata.setVar("DEFAULTTUNE", origdefault) |
| 472 | overrides = localdata.getVar("OVERRIDES", False).split(":") |
| 473 | overrides = ":".join([x for x in overrides if not x.startswith("virtclass-multilib-")]) |
| 474 | localdata.setVar("OVERRIDES", overrides) |
| 475 | localdata.setVar("MLPREFIX", "") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 476 | return localdata |
| 477 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 478 | class ImageQAFailed(Exception): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 479 | def __init__(self, description, name=None, logfile=None): |
| 480 | self.description = description |
| 481 | self.name = name |
| 482 | self.logfile=logfile |
| 483 | |
| 484 | def __str__(self): |
| 485 | msg = 'Function failed: %s' % self.name |
| 486 | if self.description: |
| 487 | msg = msg + ' (%s)' % self.description |
| 488 | |
| 489 | return msg |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 490 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 491 | def sh_quote(string): |
| 492 | import shlex |
| 493 | return shlex.quote(string) |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 494 | |
| 495 | def directory_size(root, blocksize=4096): |
| 496 | """ |
| 497 | Calculate the size of the directory, taking into account hard links, |
| 498 | rounding up every size to multiples of the blocksize. |
| 499 | """ |
| 500 | def roundup(size): |
| 501 | """ |
| 502 | Round the size up to the nearest multiple of the block size. |
| 503 | """ |
| 504 | import math |
| 505 | return math.ceil(size / blocksize) * blocksize |
| 506 | |
| 507 | def getsize(filename): |
| 508 | """ |
| 509 | Get the size of the filename, not following symlinks, taking into |
| 510 | account hard links. |
| 511 | """ |
| 512 | stat = os.lstat(filename) |
| 513 | if stat.st_ino not in inodes: |
| 514 | inodes.add(stat.st_ino) |
| 515 | return stat.st_size |
| 516 | else: |
| 517 | return 0 |
| 518 | |
| 519 | inodes = set() |
| 520 | total = 0 |
| 521 | for root, dirs, files in os.walk(root): |
| 522 | total += sum(roundup(getsize(os.path.join(root, name))) for name in files) |
| 523 | total += roundup(getsize(root)) |
| 524 | return total |