Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | This script determines the given package's openbmc dependencies from its |
| 5 | configure.ac file where it downloads, configures, builds, and installs each of |
| 6 | these dependencies. Then the given package is configured, built, and installed |
| 7 | prior to executing its unit tests. |
| 8 | """ |
| 9 | |
Matthew Barth | d181037 | 2016-12-19 16:57:21 -0600 | [diff] [blame] | 10 | from git import Repo |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 11 | from urlparse import urljoin |
Andrew Jeffery | a4e31c6 | 2018-03-08 13:45:28 +1030 | [diff] [blame] | 12 | from subprocess import check_call, call, CalledProcessError |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 13 | import os |
| 14 | import sys |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 15 | import argparse |
William A. Kennington III | a215673 | 2018-06-30 18:38:09 -0700 | [diff] [blame] | 16 | import multiprocessing |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 17 | import re |
William A. Kennington III | 9a32d5e | 2018-12-06 17:38:53 -0800 | [diff] [blame] | 18 | import sets |
William A. Kennington III | e67f5fc | 2018-12-06 17:40:30 -0800 | [diff] [blame] | 19 | import subprocess |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 20 | import shutil |
William A. Kennington III | 4e1d0a1 | 2018-07-16 12:04:03 -0700 | [diff] [blame] | 21 | import platform |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class DepTree(): |
| 25 | """ |
| 26 | Represents package dependency tree, where each node is a DepTree with a |
| 27 | name and DepTree children. |
| 28 | """ |
| 29 | |
| 30 | def __init__(self, name): |
| 31 | """ |
| 32 | Create new DepTree. |
| 33 | |
| 34 | Parameter descriptions: |
| 35 | name Name of new tree node. |
| 36 | """ |
| 37 | self.name = name |
| 38 | self.children = list() |
| 39 | |
| 40 | def AddChild(self, name): |
| 41 | """ |
| 42 | Add new child node to current node. |
| 43 | |
| 44 | Parameter descriptions: |
| 45 | name Name of new child |
| 46 | """ |
| 47 | new_child = DepTree(name) |
| 48 | self.children.append(new_child) |
| 49 | return new_child |
| 50 | |
| 51 | def AddChildNode(self, node): |
| 52 | """ |
| 53 | Add existing child node to current node. |
| 54 | |
| 55 | Parameter descriptions: |
| 56 | node Tree node to add |
| 57 | """ |
| 58 | self.children.append(node) |
| 59 | |
| 60 | def RemoveChild(self, name): |
| 61 | """ |
| 62 | Remove child node. |
| 63 | |
| 64 | Parameter descriptions: |
| 65 | name Name of child to remove |
| 66 | """ |
| 67 | for child in self.children: |
| 68 | if child.name == name: |
| 69 | self.children.remove(child) |
| 70 | return |
| 71 | |
| 72 | def GetNode(self, name): |
| 73 | """ |
| 74 | Return node with matching name. Return None if not found. |
| 75 | |
| 76 | Parameter descriptions: |
| 77 | name Name of node to return |
| 78 | """ |
| 79 | if self.name == name: |
| 80 | return self |
| 81 | for child in self.children: |
| 82 | node = child.GetNode(name) |
| 83 | if node: |
| 84 | return node |
| 85 | return None |
| 86 | |
| 87 | def GetParentNode(self, name, parent_node=None): |
| 88 | """ |
| 89 | Return parent of node with matching name. Return none if not found. |
| 90 | |
| 91 | Parameter descriptions: |
| 92 | name Name of node to get parent of |
| 93 | parent_node Parent of current node |
| 94 | """ |
| 95 | if self.name == name: |
| 96 | return parent_node |
| 97 | for child in self.children: |
| 98 | found_node = child.GetParentNode(name, self) |
| 99 | if found_node: |
| 100 | return found_node |
| 101 | return None |
| 102 | |
| 103 | def GetPath(self, name, path=None): |
| 104 | """ |
| 105 | Return list of node names from head to matching name. |
| 106 | Return None if not found. |
| 107 | |
| 108 | Parameter descriptions: |
| 109 | name Name of node |
| 110 | path List of node names from head to current node |
| 111 | """ |
| 112 | if not path: |
| 113 | path = [] |
| 114 | if self.name == name: |
| 115 | path.append(self.name) |
| 116 | return path |
| 117 | for child in self.children: |
| 118 | match = child.GetPath(name, path + [self.name]) |
| 119 | if match: |
| 120 | return match |
| 121 | return None |
| 122 | |
| 123 | def GetPathRegex(self, name, regex_str, path=None): |
| 124 | """ |
| 125 | Return list of node paths that end in name, or match regex_str. |
| 126 | Return empty list if not found. |
| 127 | |
| 128 | Parameter descriptions: |
| 129 | name Name of node to search for |
| 130 | regex_str Regex string to match node names |
| 131 | path Path of node names from head to current node |
| 132 | """ |
| 133 | new_paths = [] |
| 134 | if not path: |
| 135 | path = [] |
| 136 | match = re.match(regex_str, self.name) |
| 137 | if (self.name == name) or (match): |
| 138 | new_paths.append(path + [self.name]) |
| 139 | for child in self.children: |
| 140 | return_paths = None |
| 141 | full_path = path + [self.name] |
| 142 | return_paths = child.GetPathRegex(name, regex_str, full_path) |
| 143 | for i in return_paths: |
| 144 | new_paths.append(i) |
| 145 | return new_paths |
| 146 | |
| 147 | def MoveNode(self, from_name, to_name): |
| 148 | """ |
| 149 | Mode existing from_name node to become child of to_name node. |
| 150 | |
| 151 | Parameter descriptions: |
| 152 | from_name Name of node to make a child of to_name |
| 153 | to_name Name of node to make parent of from_name |
| 154 | """ |
| 155 | parent_from_node = self.GetParentNode(from_name) |
| 156 | from_node = self.GetNode(from_name) |
| 157 | parent_from_node.RemoveChild(from_name) |
| 158 | to_node = self.GetNode(to_name) |
| 159 | to_node.AddChildNode(from_node) |
| 160 | |
| 161 | def ReorderDeps(self, name, regex_str): |
| 162 | """ |
| 163 | Reorder dependency tree. If tree contains nodes with names that |
| 164 | match 'name' and 'regex_str', move 'regex_str' nodes that are |
| 165 | to the right of 'name' node, so that they become children of the |
| 166 | 'name' node. |
| 167 | |
| 168 | Parameter descriptions: |
| 169 | name Name of node to look for |
| 170 | regex_str Regex string to match names to |
| 171 | """ |
| 172 | name_path = self.GetPath(name) |
| 173 | if not name_path: |
| 174 | return |
| 175 | paths = self.GetPathRegex(name, regex_str) |
| 176 | is_name_in_paths = False |
| 177 | name_index = 0 |
| 178 | for i in range(len(paths)): |
| 179 | path = paths[i] |
| 180 | if path[-1] == name: |
| 181 | is_name_in_paths = True |
| 182 | name_index = i |
| 183 | break |
| 184 | if not is_name_in_paths: |
| 185 | return |
| 186 | for i in range(name_index + 1, len(paths)): |
| 187 | path = paths[i] |
| 188 | if name in path: |
| 189 | continue |
| 190 | from_name = path[-1] |
| 191 | self.MoveNode(from_name, name) |
| 192 | |
| 193 | def GetInstallList(self): |
| 194 | """ |
| 195 | Return post-order list of node names. |
| 196 | |
| 197 | Parameter descriptions: |
| 198 | """ |
| 199 | install_list = [] |
| 200 | for child in self.children: |
| 201 | child_install_list = child.GetInstallList() |
| 202 | install_list.extend(child_install_list) |
| 203 | install_list.append(self.name) |
| 204 | return install_list |
| 205 | |
| 206 | def PrintTree(self, level=0): |
| 207 | """ |
| 208 | Print pre-order node names with indentation denoting node depth level. |
| 209 | |
| 210 | Parameter descriptions: |
| 211 | level Current depth level |
| 212 | """ |
| 213 | INDENT_PER_LEVEL = 4 |
| 214 | print ' ' * (level * INDENT_PER_LEVEL) + self.name |
| 215 | for child in self.children: |
| 216 | child.PrintTree(level + 1) |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 217 | |
| 218 | |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 219 | def check_call_cmd(*cmd): |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 220 | """ |
| 221 | Verbose prints the directory location the given command is called from and |
| 222 | the command, then executes the command using check_call. |
| 223 | |
| 224 | Parameter descriptions: |
| 225 | dir Directory location command is to be called from |
| 226 | cmd List of parameters constructing the complete command |
| 227 | """ |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 228 | printline(os.getcwd(), ">", " ".join(cmd)) |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 229 | check_call(cmd) |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 230 | |
| 231 | |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 232 | def clone_pkg(pkg, branch): |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 233 | """ |
| 234 | Clone the given openbmc package's git repository from gerrit into |
| 235 | the WORKSPACE location |
| 236 | |
| 237 | Parameter descriptions: |
| 238 | pkg Name of the package to clone |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 239 | branch Branch to clone from pkg |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 240 | """ |
Andrew Jeffery | 7be94ca | 2018-03-08 13:15:33 +1030 | [diff] [blame] | 241 | pkg_dir = os.path.join(WORKSPACE, pkg) |
| 242 | if os.path.exists(os.path.join(pkg_dir, '.git')): |
| 243 | return pkg_dir |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 244 | pkg_repo = urljoin('https://gerrit.openbmc-project.xyz/openbmc/', pkg) |
Andrew Jeffery | 7be94ca | 2018-03-08 13:15:33 +1030 | [diff] [blame] | 245 | os.mkdir(pkg_dir) |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 246 | printline(pkg_dir, "> git clone", pkg_repo, branch, "./") |
| 247 | try: |
| 248 | # first try the branch |
| 249 | repo_inst = Repo.clone_from(pkg_repo, pkg_dir, |
| 250 | branch=branch).working_dir |
| 251 | except: |
| 252 | printline("Input branch not found, default to master") |
| 253 | repo_inst = Repo.clone_from(pkg_repo, pkg_dir, |
| 254 | branch="master").working_dir |
| 255 | return repo_inst |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 256 | |
| 257 | |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 258 | def get_autoconf_deps(pkgdir): |
| 259 | """ |
| 260 | Parse the given 'configure.ac' file for package dependencies and return |
| 261 | a list of the dependencies found. If the package is not autoconf it is just |
| 262 | ignored. |
| 263 | |
| 264 | Parameter descriptions: |
| 265 | pkgdir Directory where package source is located |
| 266 | """ |
| 267 | configure_ac = os.path.join(pkgdir, 'configure.ac') |
| 268 | if not os.path.exists(configure_ac): |
| 269 | return [] |
| 270 | |
William A. Kennington III | e67f5fc | 2018-12-06 17:40:30 -0800 | [diff] [blame] | 271 | configure_ac_contents = '' |
| 272 | # Prepend some special function overrides so we can parse out dependencies |
| 273 | for macro in DEPENDENCIES.iterkeys(): |
| 274 | configure_ac_contents += ('m4_define([' + macro + '], [' + |
| 275 | macro + '_START$' + str(DEPENDENCIES_OFFSET[macro] + 1) + |
| 276 | macro + '_END])\n') |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 277 | with open(configure_ac, "rt") as f: |
William A. Kennington III | e67f5fc | 2018-12-06 17:40:30 -0800 | [diff] [blame] | 278 | configure_ac_contents += f.read() |
| 279 | |
| 280 | autoconf_process = subprocess.Popen(['autoconf', '-Wno-undefined', '-'], |
| 281 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 282 | stderr=subprocess.PIPE) |
| 283 | (stdout, stderr) = autoconf_process.communicate(input=configure_ac_contents) |
| 284 | if not stdout: |
| 285 | print(stderr) |
| 286 | raise Exception("Failed to run autoconf for parsing dependencies") |
| 287 | |
| 288 | # Parse out all of the dependency text |
| 289 | matches = [] |
| 290 | for macro in DEPENDENCIES.iterkeys(): |
| 291 | pattern = '(' + macro + ')_START(.*?)' + macro + '_END' |
| 292 | for match in re.compile(pattern).finditer(stdout): |
| 293 | matches.append((match.group(1), match.group(2))) |
| 294 | |
| 295 | # Look up dependencies from the text |
| 296 | found_deps = [] |
| 297 | for macro, deptext in matches: |
| 298 | for potential_dep in deptext.split(' '): |
| 299 | for known_dep in DEPENDENCIES[macro].iterkeys(): |
| 300 | if potential_dep.startswith(known_dep): |
| 301 | found_deps.append(DEPENDENCIES[macro][known_dep]) |
| 302 | |
| 303 | return found_deps |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 304 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 305 | def get_meson_deps(pkgdir): |
| 306 | """ |
| 307 | Parse the given 'meson.build' file for package dependencies and return |
| 308 | a list of the dependencies found. If the package is not meson compatible |
| 309 | it is just ignored. |
| 310 | |
| 311 | Parameter descriptions: |
| 312 | pkgdir Directory where package source is located |
| 313 | """ |
| 314 | meson_build = os.path.join(pkgdir, 'meson.build') |
| 315 | if not os.path.exists(meson_build): |
| 316 | return [] |
| 317 | |
| 318 | found_deps = [] |
| 319 | for root, dirs, files in os.walk(pkgdir): |
| 320 | if 'meson.build' not in files: |
| 321 | continue |
| 322 | with open(os.path.join(root, 'meson.build'), 'rt') as f: |
| 323 | build_contents = f.read() |
| 324 | for match in re.finditer(r"dependency\('([^']*)'.*?\)\n", build_contents): |
| 325 | maybe_dep = DEPENDENCIES['PKG_CHECK_MODULES'].get(match.group(1)) |
| 326 | if maybe_dep is not None: |
| 327 | found_deps.append(maybe_dep) |
| 328 | |
| 329 | return found_deps |
| 330 | |
William A. Kennington III | a215673 | 2018-06-30 18:38:09 -0700 | [diff] [blame] | 331 | make_parallel = [ |
| 332 | 'make', |
| 333 | # Run enough jobs to saturate all the cpus |
| 334 | '-j', str(multiprocessing.cpu_count()), |
| 335 | # Don't start more jobs if the load avg is too high |
| 336 | '-l', str(multiprocessing.cpu_count()), |
| 337 | # Synchronize the output so logs aren't intermixed in stdout / stderr |
| 338 | '-O', |
| 339 | ] |
| 340 | |
William A. Kennington III | a045491 | 2018-12-06 14:47:16 -0800 | [diff] [blame] | 341 | def enFlag(flag, enabled): |
| 342 | """ |
| 343 | Returns an configure flag as a string |
| 344 | |
| 345 | Parameters: |
| 346 | flag The name of the flag |
| 347 | enabled Whether the flag is enabled or disabled |
| 348 | """ |
| 349 | return '--' + ('enable' if enabled else 'disable') + '-' + flag |
| 350 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 351 | def mesonFeature(val): |
| 352 | """ |
| 353 | Returns the meson flag which signifies the value |
| 354 | |
| 355 | True is enabled which requires the feature. |
| 356 | False is disabled which disables the feature. |
| 357 | None is auto which autodetects the feature. |
| 358 | |
| 359 | Parameters: |
| 360 | val The value being converted |
| 361 | """ |
| 362 | if val is True: |
| 363 | return "enabled" |
| 364 | elif val is False: |
| 365 | return "disabled" |
| 366 | elif val is None: |
| 367 | return "auto" |
| 368 | else: |
| 369 | raise Exception("Bad meson feature value") |
| 370 | |
William A. Kennington III | 6764d5f | 2018-12-13 12:27:03 -0800 | [diff] [blame] | 371 | def parse_meson_options(options_file): |
| 372 | """ |
| 373 | Returns a set of options defined in the provides meson_options.txt file |
| 374 | |
| 375 | Parameters: |
| 376 | options_file The file containing options |
| 377 | """ |
| 378 | options_contents = '' |
| 379 | with open(options_file, "rt") as f: |
| 380 | options_contents += f.read() |
| 381 | options = sets.Set() |
| 382 | pattern = 'option\\(\\s*\'([^\']*)\'' |
| 383 | for match in re.compile(pattern).finditer(options_contents): |
| 384 | options.add(match.group(1)) |
| 385 | return options |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 386 | |
William A. Kennington III | a045491 | 2018-12-06 14:47:16 -0800 | [diff] [blame] | 387 | def build_and_install(pkg, build_for_testing=False): |
William A. Kennington III | 780ec09 | 2018-12-06 14:46:50 -0800 | [diff] [blame] | 388 | """ |
| 389 | Builds and installs the package in the environment. Optionally |
| 390 | builds the examples and test cases for package. |
| 391 | |
| 392 | Parameter description: |
| 393 | pkg The package we are building |
William A. Kennington III | a045491 | 2018-12-06 14:47:16 -0800 | [diff] [blame] | 394 | build_for_testing Enable options related to testing on the package? |
William A. Kennington III | 780ec09 | 2018-12-06 14:46:50 -0800 | [diff] [blame] | 395 | """ |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 396 | os.chdir(os.path.join(WORKSPACE, pkg)) |
William A. Kennington III | 54d4faf | 2018-12-06 17:46:24 -0800 | [diff] [blame] | 397 | |
| 398 | # Refresh dynamic linker run time bindings for dependencies |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 399 | check_call_cmd('sudo', '-n', '--', 'ldconfig') |
William A. Kennington III | 54d4faf | 2018-12-06 17:46:24 -0800 | [diff] [blame] | 400 | |
William A. Kennington III | 780ec09 | 2018-12-06 14:46:50 -0800 | [diff] [blame] | 401 | # Build & install this package |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 402 | # Always try using meson first |
| 403 | if os.path.exists('meson.build'): |
Brad Bishop | f5a9829 | 2019-03-29 16:44:36 -0400 | [diff] [blame] | 404 | meson_options = sets.Set() |
| 405 | if os.path.exists("meson_options.txt"): |
| 406 | meson_options = parse_meson_options("meson_options.txt") |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 407 | meson_flags = [ |
| 408 | '-Db_colorout=never', |
William A. Kennington III | 73b0d6e | 2019-01-17 14:35:41 -0800 | [diff] [blame] | 409 | '-Dwerror=true', |
| 410 | '-Dwarning_level=3', |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 411 | ] |
William A. Kennington III | 25e5814 | 2018-12-13 14:30:43 -0800 | [diff] [blame] | 412 | if build_for_testing: |
| 413 | meson_flags.append('--buildtype=debug') |
| 414 | else: |
| 415 | meson_flags.append('--buildtype=debugoptimized') |
William A. Kennington III | 6764d5f | 2018-12-13 12:27:03 -0800 | [diff] [blame] | 416 | if 'tests' in meson_options: |
| 417 | meson_flags.append('-Dtests=' + mesonFeature(build_for_testing)) |
| 418 | if 'examples' in meson_options: |
| 419 | meson_flags.append('-Dexamples=' + str(build_for_testing).lower()) |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 420 | if MESON_FLAGS.get(pkg) is not None: |
| 421 | meson_flags.extend(MESON_FLAGS.get(pkg)) |
| 422 | try: |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 423 | check_call_cmd('meson', 'setup', '--reconfigure', 'build', *meson_flags) |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 424 | except: |
| 425 | shutil.rmtree('build') |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 426 | check_call_cmd('meson', 'setup', 'build', *meson_flags) |
| 427 | check_call_cmd('ninja', '-C', 'build') |
| 428 | check_call_cmd('sudo', '-n', '--', 'ninja', '-C', 'build', 'install') |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 429 | # Assume we are autoconf otherwise |
| 430 | else: |
| 431 | conf_flags = [ |
| 432 | enFlag('silent-rules', False), |
| 433 | enFlag('examples', build_for_testing), |
| 434 | enFlag('tests', build_for_testing), |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 435 | ] |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 436 | if not TEST_ONLY: |
| 437 | conf_flags.extend([ |
| 438 | enFlag('code-coverage', build_for_testing), |
| 439 | enFlag('valgrind', build_for_testing), |
| 440 | ]) |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 441 | # Add any necessary configure flags for package |
| 442 | if CONFIGURE_FLAGS.get(pkg) is not None: |
| 443 | conf_flags.extend(CONFIGURE_FLAGS.get(pkg)) |
| 444 | for bootstrap in ['bootstrap.sh', 'bootstrap', 'autogen.sh']: |
| 445 | if os.path.exists(bootstrap): |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 446 | check_call_cmd('./' + bootstrap) |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 447 | break |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 448 | check_call_cmd('./configure', *conf_flags) |
| 449 | check_call_cmd(*make_parallel) |
| 450 | check_call_cmd('sudo', '-n', '--', *(make_parallel + [ 'install' ])) |
William A. Kennington III | 780ec09 | 2018-12-06 14:46:50 -0800 | [diff] [blame] | 451 | |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 452 | def build_dep_tree(pkg, pkgdir, dep_added, head, branch, dep_tree=None): |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 453 | """ |
| 454 | For each package(pkg), starting with the package to be unit tested, |
| 455 | parse its 'configure.ac' file from within the package's directory(pkgdir) |
| 456 | for each package dependency defined recursively doing the same thing |
| 457 | on each package found as a dependency. |
| 458 | |
| 459 | Parameter descriptions: |
| 460 | pkg Name of the package |
| 461 | pkgdir Directory where package source is located |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 462 | dep_added Current dict of dependencies and added status |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 463 | head Head node of the dependency tree |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 464 | branch Branch to clone from pkg |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 465 | dep_tree Current dependency tree node |
| 466 | """ |
| 467 | if not dep_tree: |
| 468 | dep_tree = head |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 469 | |
William A. Kennington III | be6aab2 | 2018-12-06 15:01:54 -0800 | [diff] [blame] | 470 | with open("/tmp/depcache", "r") as depcache: |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 471 | cache = depcache.readline() |
| 472 | |
| 473 | # Read out pkg dependencies |
| 474 | pkg_deps = [] |
| 475 | pkg_deps += get_autoconf_deps(pkgdir) |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 476 | pkg_deps += get_meson_deps(pkgdir) |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 477 | |
William A. Kennington III | 9a32d5e | 2018-12-06 17:38:53 -0800 | [diff] [blame] | 478 | for dep in sets.Set(pkg_deps): |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 479 | if dep in cache: |
| 480 | continue |
| 481 | # Dependency package not already known |
| 482 | if dep_added.get(dep) is None: |
| 483 | # Dependency package not added |
| 484 | new_child = dep_tree.AddChild(dep) |
| 485 | dep_added[dep] = False |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 486 | dep_pkgdir = clone_pkg(dep,branch) |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 487 | # Determine this dependency package's |
| 488 | # dependencies and add them before |
| 489 | # returning to add this package |
| 490 | dep_added = build_dep_tree(dep, |
| 491 | dep_pkgdir, |
| 492 | dep_added, |
| 493 | head, |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 494 | branch, |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 495 | new_child) |
| 496 | else: |
| 497 | # Dependency package known and added |
| 498 | if dep_added[dep]: |
Andrew Jeffery | 2cb0c7a | 2018-03-08 13:19:08 +1030 | [diff] [blame] | 499 | continue |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 500 | else: |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 501 | # Cyclic dependency failure |
| 502 | raise Exception("Cyclic dependencies found in "+pkg) |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 503 | |
| 504 | if not dep_added[pkg]: |
| 505 | dep_added[pkg] = True |
| 506 | |
| 507 | return dep_added |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 508 | |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 509 | def make_target_exists(target): |
| 510 | """ |
| 511 | Runs a check against the makefile in the current directory to determine |
| 512 | if the target exists so that it can be built. |
| 513 | |
| 514 | Parameter descriptions: |
| 515 | target The make target we are checking |
| 516 | """ |
| 517 | try: |
| 518 | cmd = [ 'make', '-n', target ] |
| 519 | with open(os.devnull, 'w') as devnull: |
| 520 | check_call(cmd, stdout=devnull, stderr=devnull) |
| 521 | return True |
| 522 | except CalledProcessError: |
| 523 | return False |
| 524 | |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 525 | def run_unit_tests(): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 526 | """ |
| 527 | Runs the unit tests for the package via `make check` |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 528 | """ |
| 529 | try: |
| 530 | cmd = make_parallel + [ 'check' ] |
| 531 | for i in range(0, args.repeat): |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 532 | check_call_cmd(*cmd) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 533 | except CalledProcessError: |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 534 | for root, _, files in os.walk(os.getcwd()): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 535 | if 'test-suite.log' not in files: |
| 536 | continue |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 537 | check_call_cmd('cat', os.path.join(root, 'test-suite.log')) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 538 | raise Exception('Unit tests failed') |
| 539 | |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 540 | def run_cppcheck(): |
Brad Bishop | 48424d4 | 2020-01-07 13:01:31 -0500 | [diff] [blame] | 541 | match_re = re.compile('((?!\.mako\.).)*\.[ch](?:pp)?$', re.I) |
| 542 | cppcheck_files = [] |
| 543 | stdout = subprocess.check_output(['git', 'ls-files']) |
Patrick Venture | ad4354e | 2018-10-12 16:59:54 -0700 | [diff] [blame] | 544 | |
Brad Bishop | 48424d4 | 2020-01-07 13:01:31 -0500 | [diff] [blame] | 545 | for f in stdout.decode('utf-8').split(): |
| 546 | if match_re.match(f): |
| 547 | cppcheck_files.append(f) |
| 548 | |
| 549 | if not cppcheck_files: |
| 550 | # skip cppcheck if there arent' any c or cpp sources. |
| 551 | print("no files") |
| 552 | return None |
| 553 | |
| 554 | # http://cppcheck.sourceforge.net/manual.pdf |
| 555 | params = ['cppcheck', '-j', str(multiprocessing.cpu_count()), |
| 556 | '--enable=all', '--file-list=-'] |
| 557 | |
| 558 | cppcheck_process = subprocess.Popen( |
| 559 | params, |
| 560 | stdout=subprocess.PIPE, |
| 561 | stderr=subprocess.PIPE, |
| 562 | stdin=subprocess.PIPE) |
| 563 | (stdout, stderr) = cppcheck_process.communicate( |
| 564 | input='\n'.join(cppcheck_files)) |
| 565 | |
| 566 | if cppcheck_process.wait(): |
Patrick Venture | ad4354e | 2018-10-12 16:59:54 -0700 | [diff] [blame] | 567 | raise Exception('Cppcheck failed') |
Brad Bishop | 48424d4 | 2020-01-07 13:01:31 -0500 | [diff] [blame] | 568 | print(stdout) |
| 569 | print(stderr) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 570 | |
William A. Kennington III | 37a89a2 | 2018-12-13 14:32:02 -0800 | [diff] [blame] | 571 | def is_valgrind_safe(): |
| 572 | """ |
| 573 | Returns whether it is safe to run valgrind on our platform |
| 574 | """ |
William A. Kennington III | 0326ded | 2019-02-07 00:33:28 -0800 | [diff] [blame] | 575 | src = 'unit-test-vg.c' |
| 576 | exe = './unit-test-vg' |
| 577 | with open(src, 'w') as h: |
William A. Kennington III | afb0f98 | 2019-04-26 17:30:28 -0700 | [diff] [blame] | 578 | h.write('#include <errno.h>\n') |
| 579 | h.write('#include <stdio.h>\n') |
William A. Kennington III | 0326ded | 2019-02-07 00:33:28 -0800 | [diff] [blame] | 580 | h.write('#include <stdlib.h>\n') |
| 581 | h.write('#include <string.h>\n') |
| 582 | h.write('int main() {\n') |
| 583 | h.write('char *heap_str = malloc(16);\n') |
| 584 | h.write('strcpy(heap_str, "RandString");\n') |
| 585 | h.write('int res = strcmp("RandString", heap_str);\n') |
| 586 | h.write('free(heap_str);\n') |
William A. Kennington III | afb0f98 | 2019-04-26 17:30:28 -0700 | [diff] [blame] | 587 | h.write('char errstr[64];\n') |
| 588 | h.write('strerror_r(EINVAL, errstr, sizeof(errstr));\n') |
| 589 | h.write('printf("%s\\n", errstr);\n') |
William A. Kennington III | 0326ded | 2019-02-07 00:33:28 -0800 | [diff] [blame] | 590 | h.write('return res;\n') |
| 591 | h.write('}\n') |
| 592 | try: |
| 593 | with open(os.devnull, 'w') as devnull: |
| 594 | check_call(['gcc', '-O2', '-o', exe, src], |
| 595 | stdout=devnull, stderr=devnull) |
| 596 | check_call(['valgrind', '--error-exitcode=99', exe], |
| 597 | stdout=devnull, stderr=devnull) |
| 598 | return True |
| 599 | except: |
| 600 | sys.stderr.write("###### Platform is not valgrind safe ######\n") |
| 601 | return False |
| 602 | finally: |
| 603 | os.remove(src) |
| 604 | os.remove(exe) |
William A. Kennington III | 37a89a2 | 2018-12-13 14:32:02 -0800 | [diff] [blame] | 605 | |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 606 | def is_sanitize_safe(): |
| 607 | """ |
| 608 | Returns whether it is safe to run sanitizers on our platform |
| 609 | """ |
William A. Kennington III | 0b7fb2b | 2019-02-07 00:33:42 -0800 | [diff] [blame] | 610 | src = 'unit-test-sanitize.c' |
| 611 | exe = './unit-test-sanitize' |
| 612 | with open(src, 'w') as h: |
| 613 | h.write('int main() { return 0; }\n') |
| 614 | try: |
| 615 | with open(os.devnull, 'w') as devnull: |
| 616 | check_call(['gcc', '-O2', '-fsanitize=address', |
| 617 | '-fsanitize=undefined', '-o', exe, src], |
| 618 | stdout=devnull, stderr=devnull) |
| 619 | check_call([exe], stdout=devnull, stderr=devnull) |
| 620 | return True |
| 621 | except: |
| 622 | sys.stderr.write("###### Platform is not sanitize safe ######\n") |
| 623 | return False |
| 624 | finally: |
| 625 | os.remove(src) |
| 626 | os.remove(exe) |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 627 | |
William A. Kennington III | 49d4e59 | 2019-02-06 17:59:27 -0800 | [diff] [blame] | 628 | def meson_setup_exists(setup): |
| 629 | """ |
| 630 | Returns whether the meson build supports the named test setup. |
| 631 | |
| 632 | Parameter descriptions: |
| 633 | setup The setup target to check |
| 634 | """ |
| 635 | try: |
| 636 | with open(os.devnull, 'w') as devnull: |
| 637 | output = subprocess.check_output( |
| 638 | ['meson', 'test', '-C', 'build', |
| 639 | '--setup', setup, '-t', '0'], |
| 640 | stderr=subprocess.STDOUT) |
| 641 | except CalledProcessError as e: |
| 642 | output = e.output |
| 643 | return not re.search('Test setup .* not found from project', output) |
| 644 | |
Andrew Geissler | 9ced4ed | 2019-11-18 14:33:58 -0600 | [diff] [blame] | 645 | def run_unit_tests_meson(): |
| 646 | """ |
| 647 | Runs the unit tests for the meson based package |
| 648 | """ |
| 649 | try: |
| 650 | check_call_cmd('meson', 'test', '-C', 'build') |
| 651 | except CalledProcessError: |
| 652 | for root, _, files in os.walk(os.getcwd()): |
| 653 | if 'testlog.txt' not in files: |
| 654 | continue |
| 655 | check_call_cmd('cat', os.path.join(root, 'testlog.txt')) |
| 656 | raise Exception('Unit tests failed') |
| 657 | |
William A. Kennington III | 49d4e59 | 2019-02-06 17:59:27 -0800 | [diff] [blame] | 658 | def maybe_meson_valgrind(): |
| 659 | """ |
| 660 | Potentially runs the unit tests through valgrind for the package |
| 661 | via `meson test`. The package can specify custom valgrind configurations |
| 662 | by utilizing add_test_setup() in a meson.build |
| 663 | """ |
| 664 | if not is_valgrind_safe(): |
William A. Kennington III | 7513019 | 2019-02-07 00:34:14 -0800 | [diff] [blame] | 665 | sys.stderr.write("###### Skipping valgrind ######\n") |
William A. Kennington III | 49d4e59 | 2019-02-06 17:59:27 -0800 | [diff] [blame] | 666 | return |
| 667 | if meson_setup_exists('valgrind'): |
| 668 | check_call_cmd('meson', 'test', '-C', 'build', |
| 669 | '--setup', 'valgrind') |
| 670 | else: |
| 671 | check_call_cmd('meson', 'test', '-C', 'build', |
| 672 | '--wrapper', 'valgrind') |
| 673 | |
William A. Kennington III | eaff24a | 2019-02-06 16:57:42 -0800 | [diff] [blame] | 674 | def maybe_make_valgrind(): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 675 | """ |
| 676 | Potentially runs the unit tests through valgrind for the package |
| 677 | via `make check-valgrind`. If the package does not have valgrind testing |
| 678 | then it just skips over this. |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 679 | """ |
William A. Kennington III | 4e1d0a1 | 2018-07-16 12:04:03 -0700 | [diff] [blame] | 680 | # Valgrind testing is currently broken by an aggressive strcmp optimization |
| 681 | # that is inlined into optimized code for POWER by gcc 7+. Until we find |
| 682 | # a workaround, just don't run valgrind tests on POWER. |
| 683 | # https://github.com/openbmc/openbmc/issues/3315 |
William A. Kennington III | 37a89a2 | 2018-12-13 14:32:02 -0800 | [diff] [blame] | 684 | if not is_valgrind_safe(): |
William A. Kennington III | 7513019 | 2019-02-07 00:34:14 -0800 | [diff] [blame] | 685 | sys.stderr.write("###### Skipping valgrind ######\n") |
William A. Kennington III | 4e1d0a1 | 2018-07-16 12:04:03 -0700 | [diff] [blame] | 686 | return |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 687 | if not make_target_exists('check-valgrind'): |
| 688 | return |
| 689 | |
| 690 | try: |
| 691 | cmd = make_parallel + [ 'check-valgrind' ] |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 692 | check_call_cmd(*cmd) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 693 | except CalledProcessError: |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 694 | for root, _, files in os.walk(os.getcwd()): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 695 | for f in files: |
| 696 | if re.search('test-suite-[a-z]+.log', f) is None: |
| 697 | continue |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 698 | check_call_cmd('cat', os.path.join(root, f)) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 699 | raise Exception('Valgrind tests failed') |
| 700 | |
William A. Kennington III | eaff24a | 2019-02-06 16:57:42 -0800 | [diff] [blame] | 701 | def maybe_make_coverage(): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 702 | """ |
| 703 | Potentially runs the unit tests through code coverage for the package |
| 704 | via `make check-code-coverage`. If the package does not have code coverage |
| 705 | testing then it just skips over this. |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 706 | """ |
| 707 | if not make_target_exists('check-code-coverage'): |
| 708 | return |
| 709 | |
| 710 | # Actually run code coverage |
| 711 | try: |
| 712 | cmd = make_parallel + [ 'check-code-coverage' ] |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 713 | check_call_cmd(*cmd) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 714 | except CalledProcessError: |
| 715 | raise Exception('Code coverage failed') |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 716 | |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 717 | def find_file(filename, basedir): |
| 718 | """ |
| 719 | Finds all occurrences of a file in the base directory |
| 720 | and passes them back with their relative paths. |
| 721 | |
| 722 | Parameter descriptions: |
| 723 | filename The name of the file to find |
| 724 | basedir The base directory search in |
| 725 | """ |
| 726 | |
| 727 | filepaths = [] |
| 728 | for root, dirs, files in os.walk(basedir): |
| 729 | if filename in files: |
| 730 | filepaths.append(os.path.join(root, filename)) |
| 731 | return filepaths |
| 732 | |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 733 | if __name__ == '__main__': |
| 734 | # CONFIGURE_FLAGS = [GIT REPO]:[CONFIGURE FLAGS] |
| 735 | CONFIGURE_FLAGS = { |
Matthew Barth | 1d1c673 | 2017-03-24 10:00:28 -0500 | [diff] [blame] | 736 | 'sdbusplus': ['--enable-transaction'], |
| 737 | 'phosphor-logging': |
Matt Spinler | 0744bb8 | 2020-01-16 08:23:35 -0600 | [diff] [blame^] | 738 | ['--enable-metadata-processing', '--enable-openpower-pel-extension', |
Deepak Kodihalli | 3a4e1b4 | 2017-06-08 09:52:35 -0500 | [diff] [blame] | 739 | 'YAML_DIR=/usr/local/share/phosphor-dbus-yaml/yaml'] |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 740 | } |
| 741 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 742 | # MESON_FLAGS = [GIT REPO]:[MESON FLAGS] |
| 743 | MESON_FLAGS = { |
| 744 | } |
| 745 | |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 746 | # DEPENDENCIES = [MACRO]:[library/header]:[GIT REPO] |
| 747 | DEPENDENCIES = { |
| 748 | 'AC_CHECK_LIB': {'mapper': 'phosphor-objmgr'}, |
Matthew Barth | 710f3f0 | 2017-01-18 15:20:19 -0600 | [diff] [blame] | 749 | 'AC_CHECK_HEADER': { |
| 750 | 'host-ipmid': 'phosphor-host-ipmid', |
Patrick Venture | b41a446 | 2018-10-03 17:27:38 -0700 | [diff] [blame] | 751 | 'blobs-ipmid': 'phosphor-ipmi-blobs', |
Matthew Barth | 710f3f0 | 2017-01-18 15:20:19 -0600 | [diff] [blame] | 752 | 'sdbusplus': 'sdbusplus', |
William A. Kennington III | b4f730a | 2018-09-12 11:21:20 -0700 | [diff] [blame] | 753 | 'sdeventplus': 'sdeventplus', |
William A. Kennington III | 2370524 | 2019-01-15 18:17:25 -0800 | [diff] [blame] | 754 | 'stdplus': 'stdplus', |
Patrick Venture | 2232996 | 2018-09-14 10:23:04 -0700 | [diff] [blame] | 755 | 'gpioplus': 'gpioplus', |
Saqib Khan | 6614505 | 2017-02-14 12:02:07 -0600 | [diff] [blame] | 756 | 'phosphor-logging/log.hpp': 'phosphor-logging', |
Patrick Williams | eab8a37 | 2017-01-30 11:21:32 -0600 | [diff] [blame] | 757 | }, |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 758 | 'AC_PATH_PROG': {'sdbus++': 'sdbusplus'}, |
Patrick Williams | eab8a37 | 2017-01-30 11:21:32 -0600 | [diff] [blame] | 759 | 'PKG_CHECK_MODULES': { |
Matthew Barth | 19e261e | 2017-02-01 12:55:22 -0600 | [diff] [blame] | 760 | 'phosphor-dbus-interfaces': 'phosphor-dbus-interfaces', |
Patrick Williams | f128b40 | 2017-03-29 06:45:59 -0500 | [diff] [blame] | 761 | 'openpower-dbus-interfaces': 'openpower-dbus-interfaces', |
Matt Spinler | 7be1903 | 2018-04-13 09:43:14 -0500 | [diff] [blame] | 762 | 'ibm-dbus-interfaces': 'ibm-dbus-interfaces', |
William A. Kennington III | 993ab33 | 2019-02-07 02:12:31 -0800 | [diff] [blame] | 763 | 'libipmid': 'phosphor-host-ipmid', |
| 764 | 'libipmid-host': 'phosphor-host-ipmid', |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 765 | 'sdbusplus': 'sdbusplus', |
William A. Kennington III | b4f730a | 2018-09-12 11:21:20 -0700 | [diff] [blame] | 766 | 'sdeventplus': 'sdeventplus', |
William A. Kennington III | 2370524 | 2019-01-15 18:17:25 -0800 | [diff] [blame] | 767 | 'stdplus': 'stdplus', |
Patrick Venture | 2232996 | 2018-09-14 10:23:04 -0700 | [diff] [blame] | 768 | 'gpioplus': 'gpioplus', |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 769 | 'phosphor-logging': 'phosphor-logging', |
Marri Devender Rao | a3eee8a | 2018-08-13 05:34:27 -0500 | [diff] [blame] | 770 | 'phosphor-snmp': 'phosphor-snmp', |
Patrick Venture | e584c3b | 2019-03-07 09:44:16 -0800 | [diff] [blame] | 771 | 'ipmiblob': 'ipmi-blob-tool', |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 772 | }, |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 773 | } |
| 774 | |
William A. Kennington III | e67f5fc | 2018-12-06 17:40:30 -0800 | [diff] [blame] | 775 | # Offset into array of macro parameters MACRO(0, 1, ...N) |
| 776 | DEPENDENCIES_OFFSET = { |
| 777 | 'AC_CHECK_LIB': 0, |
| 778 | 'AC_CHECK_HEADER': 0, |
| 779 | 'AC_PATH_PROG': 1, |
| 780 | 'PKG_CHECK_MODULES': 1, |
| 781 | } |
| 782 | |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 783 | # DEPENDENCIES_REGEX = [GIT REPO]:[REGEX STRING] |
| 784 | DEPENDENCIES_REGEX = { |
Patrick Venture | d37b805 | 2018-10-16 16:03:03 -0700 | [diff] [blame] | 785 | 'phosphor-logging': r'\S+-dbus-interfaces$' |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 786 | } |
| 787 | |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 788 | # Set command line arguments |
| 789 | parser = argparse.ArgumentParser() |
| 790 | parser.add_argument("-w", "--workspace", dest="WORKSPACE", required=True, |
| 791 | help="Workspace directory location(i.e. /home)") |
| 792 | parser.add_argument("-p", "--package", dest="PACKAGE", required=True, |
| 793 | help="OpenBMC package to be unit tested") |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 794 | parser.add_argument("-t", "--test-only", dest="TEST_ONLY", |
| 795 | action="store_true", required=False, default=False, |
| 796 | help="Only run test cases, no other validation") |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 797 | parser.add_argument("-v", "--verbose", action="store_true", |
| 798 | help="Print additional package status messages") |
Andrew Jeffery | 468309d | 2018-03-08 13:46:33 +1030 | [diff] [blame] | 799 | parser.add_argument("-r", "--repeat", help="Repeat tests N times", |
| 800 | type=int, default=1) |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 801 | parser.add_argument("-b", "--branch", dest="BRANCH", required=False, |
| 802 | help="Branch to target for dependent repositories", |
| 803 | default="master") |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 804 | parser.add_argument("-n", "--noformat", dest="FORMAT", |
| 805 | action="store_false", required=False, |
| 806 | help="Whether or not to run format code") |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 807 | args = parser.parse_args(sys.argv[1:]) |
| 808 | WORKSPACE = args.WORKSPACE |
| 809 | UNIT_TEST_PKG = args.PACKAGE |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 810 | TEST_ONLY = args.TEST_ONLY |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 811 | BRANCH = args.BRANCH |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 812 | FORMAT_CODE = args.FORMAT |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 813 | if args.verbose: |
| 814 | def printline(*line): |
| 815 | for arg in line: |
| 816 | print arg, |
| 817 | print |
| 818 | else: |
| 819 | printline = lambda *l: None |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 820 | |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 821 | CODE_SCAN_DIR = WORKSPACE + "/" + UNIT_TEST_PKG |
| 822 | |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 823 | # First validate code formatting if repo has style formatting files. |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 824 | # The format-code.sh checks for these files. |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 825 | if FORMAT_CODE: |
| 826 | check_call_cmd("./format-code.sh", CODE_SCAN_DIR) |
Andrew Geissler | a28286d | 2018-01-10 11:00:00 -0800 | [diff] [blame] | 827 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 828 | # Automake and meson |
| 829 | if (os.path.isfile(CODE_SCAN_DIR + "/configure.ac") or |
| 830 | os.path.isfile(CODE_SCAN_DIR + '/meson.build')): |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 831 | prev_umask = os.umask(000) |
| 832 | # Determine dependencies and add them |
| 833 | dep_added = dict() |
| 834 | dep_added[UNIT_TEST_PKG] = False |
| 835 | # Create dependency tree |
| 836 | dep_tree = DepTree(UNIT_TEST_PKG) |
| 837 | build_dep_tree(UNIT_TEST_PKG, |
| 838 | os.path.join(WORKSPACE, UNIT_TEST_PKG), |
| 839 | dep_added, |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 840 | dep_tree, |
| 841 | BRANCH) |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 842 | |
| 843 | # Reorder Dependency Tree |
| 844 | for pkg_name, regex_str in DEPENDENCIES_REGEX.iteritems(): |
| 845 | dep_tree.ReorderDeps(pkg_name, regex_str) |
| 846 | if args.verbose: |
| 847 | dep_tree.PrintTree() |
| 848 | install_list = dep_tree.GetInstallList() |
William A. Kennington III | d61316d | 2018-12-06 14:56:12 -0800 | [diff] [blame] | 849 | # We don't want to treat our package as a dependency |
| 850 | install_list.remove(UNIT_TEST_PKG) |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 851 | # install reordered dependencies |
William A. Kennington III | d61316d | 2018-12-06 14:56:12 -0800 | [diff] [blame] | 852 | for dep in install_list: |
| 853 | build_and_install(dep, False) |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 854 | os.chdir(os.path.join(WORKSPACE, UNIT_TEST_PKG)) |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 855 | # Run package unit tests |
William A. Kennington III | d61316d | 2018-12-06 14:56:12 -0800 | [diff] [blame] | 856 | build_and_install(UNIT_TEST_PKG, True) |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 857 | if os.path.isfile(CODE_SCAN_DIR + '/meson.build'): |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 858 | if not TEST_ONLY: |
William A. Kennington III | 49d4e59 | 2019-02-06 17:59:27 -0800 | [diff] [blame] | 859 | maybe_meson_valgrind() |
William A. Kennington III | 40d5c7c | 2018-12-13 14:37:59 -0800 | [diff] [blame] | 860 | |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 861 | # Run clang-tidy only if the project has a configuration |
| 862 | if os.path.isfile('.clang-tidy'): |
William A. Kennington III | f676941 | 2019-06-26 12:14:51 -0700 | [diff] [blame] | 863 | check_call_cmd('run-clang-tidy-8.py', '-p', |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 864 | 'build') |
| 865 | # Run the basic clang static analyzer otherwise |
| 866 | else: |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 867 | check_call_cmd('ninja', '-C', 'build', |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 868 | 'scan-build') |
| 869 | |
| 870 | # Run tests through sanitizers |
| 871 | # b_lundef is needed if clang++ is CXX since it resolves the |
| 872 | # asan symbols at runtime only. We don't want to set it earlier |
| 873 | # in the build process to ensure we don't have undefined |
| 874 | # runtime code. |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 875 | if is_sanitize_safe(): |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 876 | check_call_cmd('meson', 'configure', 'build', |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 877 | '-Db_sanitize=address,undefined', |
| 878 | '-Db_lundef=false') |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 879 | check_call_cmd('meson', 'test', '-C', 'build', |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 880 | '--logbase', 'testlog-ubasan') |
| 881 | # TODO: Fix memory sanitizer |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 882 | #check_call_cmd('meson', 'configure', 'build', |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 883 | # '-Db_sanitize=memory') |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 884 | #check_call_cmd('meson', 'test', '-C', 'build' |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 885 | # '--logbase', 'testlog-msan') |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 886 | check_call_cmd('meson', 'configure', 'build', |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 887 | '-Db_sanitize=none', '-Db_lundef=true') |
William A. Kennington III | 7513019 | 2019-02-07 00:34:14 -0800 | [diff] [blame] | 888 | else: |
| 889 | sys.stderr.write("###### Skipping sanitizers ######\n") |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 890 | |
| 891 | # Run coverage checks |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 892 | check_call_cmd('meson', 'configure', 'build', |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 893 | '-Db_coverage=true') |
Andrew Geissler | 9ced4ed | 2019-11-18 14:33:58 -0600 | [diff] [blame] | 894 | run_unit_tests_meson() |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 895 | # Only build coverage HTML if coverage files were produced |
| 896 | for root, dirs, files in os.walk('build'): |
| 897 | if any([f.endswith('.gcda') for f in files]): |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 898 | check_call_cmd('ninja', '-C', 'build', |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 899 | 'coverage-html') |
| 900 | break |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 901 | check_call_cmd('meson', 'configure', 'build', |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 902 | '-Db_coverage=false') |
| 903 | else: |
Andrew Geissler | 9ced4ed | 2019-11-18 14:33:58 -0600 | [diff] [blame] | 904 | run_unit_tests_meson() |
| 905 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 906 | else: |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 907 | run_unit_tests() |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 908 | if not TEST_ONLY: |
William A. Kennington III | eaff24a | 2019-02-06 16:57:42 -0800 | [diff] [blame] | 909 | maybe_make_valgrind() |
| 910 | maybe_make_coverage() |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 911 | if not TEST_ONLY: |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 912 | run_cppcheck() |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 913 | |
| 914 | os.umask(prev_umask) |
| 915 | |
| 916 | # Cmake |
| 917 | elif os.path.isfile(CODE_SCAN_DIR + "/CMakeLists.txt"): |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 918 | os.chdir(os.path.join(WORKSPACE, UNIT_TEST_PKG)) |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 919 | check_call_cmd('cmake', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', '.') |
| 920 | check_call_cmd('cmake', '--build', '.', '--', '-j', |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 921 | str(multiprocessing.cpu_count())) |
| 922 | if make_target_exists('test'): |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 923 | check_call_cmd('ctest', '.') |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 924 | if not TEST_ONLY: |
William A. Kennington III | eaff24a | 2019-02-06 16:57:42 -0800 | [diff] [blame] | 925 | maybe_make_valgrind() |
| 926 | maybe_make_coverage() |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 927 | run_cppcheck() |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 928 | if os.path.isfile('.clang-tidy'): |
William A. Kennington III | f676941 | 2019-06-26 12:14:51 -0700 | [diff] [blame] | 929 | check_call_cmd('run-clang-tidy-8.py', '-p', '.') |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 930 | |
| 931 | else: |
Andrew Geissler | 71a7cc1 | 2018-01-31 14:18:37 -0800 | [diff] [blame] | 932 | print "Not a supported repo for CI Tests, exit" |
| 933 | quit() |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 934 | |
| 935 | # Run any custom CI scripts the repo has, of which there can be |
| 936 | # multiple of and anywhere in the repository. |
| 937 | ci_scripts = find_file('run-ci.sh', os.path.join(WORKSPACE, UNIT_TEST_PKG)) |
| 938 | if ci_scripts: |
| 939 | os.chdir(os.path.join(WORKSPACE, UNIT_TEST_PKG)) |
| 940 | for ci_script in ci_scripts: |
| 941 | check_call_cmd('sh', ci_script) |