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