Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 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 |
William A. Kennington III | 3992d10 | 2021-05-17 01:41:04 -0700 | [diff] [blame] | 11 | # interpreter is not used directly but this resolves dependency ordering |
| 12 | # that would be broken if we didn't include it. |
| 13 | from mesonbuild import interpreter |
William A. Kennington III | fcd7077 | 2020-06-04 00:50:23 -0700 | [diff] [blame] | 14 | from mesonbuild import coredata, optinterpreter |
Patrick Williams | e95626d | 2021-04-13 11:13:53 -0500 | [diff] [blame] | 15 | from mesonbuild.mesonlib import OptionKey |
Patrick Williams | 95095f1 | 2021-04-14 14:42:35 -0500 | [diff] [blame] | 16 | from mesonbuild.mesonlib import version_compare as meson_version_compare |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 17 | from urllib.parse import urljoin |
Andrew Jeffery | a4e31c6 | 2018-03-08 13:45:28 +1030 | [diff] [blame] | 18 | from subprocess import check_call, call, CalledProcessError |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 19 | import os |
| 20 | import sys |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 21 | import argparse |
William A. Kennington III | a215673 | 2018-06-30 18:38:09 -0700 | [diff] [blame] | 22 | import multiprocessing |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 23 | import re |
William A. Kennington III | e67f5fc | 2018-12-06 17:40:30 -0800 | [diff] [blame] | 24 | import subprocess |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 25 | import shutil |
William A. Kennington III | 4e1d0a1 | 2018-07-16 12:04:03 -0700 | [diff] [blame] | 26 | import platform |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class DepTree(): |
| 30 | """ |
| 31 | Represents package dependency tree, where each node is a DepTree with a |
| 32 | name and DepTree children. |
| 33 | """ |
| 34 | |
| 35 | def __init__(self, name): |
| 36 | """ |
| 37 | Create new DepTree. |
| 38 | |
| 39 | Parameter descriptions: |
| 40 | name Name of new tree node. |
| 41 | """ |
| 42 | self.name = name |
| 43 | self.children = list() |
| 44 | |
| 45 | def AddChild(self, name): |
| 46 | """ |
| 47 | Add new child node to current node. |
| 48 | |
| 49 | Parameter descriptions: |
| 50 | name Name of new child |
| 51 | """ |
| 52 | new_child = DepTree(name) |
| 53 | self.children.append(new_child) |
| 54 | return new_child |
| 55 | |
| 56 | def AddChildNode(self, node): |
| 57 | """ |
| 58 | Add existing child node to current node. |
| 59 | |
| 60 | Parameter descriptions: |
| 61 | node Tree node to add |
| 62 | """ |
| 63 | self.children.append(node) |
| 64 | |
| 65 | def RemoveChild(self, name): |
| 66 | """ |
| 67 | Remove child node. |
| 68 | |
| 69 | Parameter descriptions: |
| 70 | name Name of child to remove |
| 71 | """ |
| 72 | for child in self.children: |
| 73 | if child.name == name: |
| 74 | self.children.remove(child) |
| 75 | return |
| 76 | |
| 77 | def GetNode(self, name): |
| 78 | """ |
| 79 | Return node with matching name. Return None if not found. |
| 80 | |
| 81 | Parameter descriptions: |
| 82 | name Name of node to return |
| 83 | """ |
| 84 | if self.name == name: |
| 85 | return self |
| 86 | for child in self.children: |
| 87 | node = child.GetNode(name) |
| 88 | if node: |
| 89 | return node |
| 90 | return None |
| 91 | |
| 92 | def GetParentNode(self, name, parent_node=None): |
| 93 | """ |
| 94 | Return parent of node with matching name. Return none if not found. |
| 95 | |
| 96 | Parameter descriptions: |
| 97 | name Name of node to get parent of |
| 98 | parent_node Parent of current node |
| 99 | """ |
| 100 | if self.name == name: |
| 101 | return parent_node |
| 102 | for child in self.children: |
| 103 | found_node = child.GetParentNode(name, self) |
| 104 | if found_node: |
| 105 | return found_node |
| 106 | return None |
| 107 | |
| 108 | def GetPath(self, name, path=None): |
| 109 | """ |
| 110 | Return list of node names from head to matching name. |
| 111 | Return None if not found. |
| 112 | |
| 113 | Parameter descriptions: |
| 114 | name Name of node |
| 115 | path List of node names from head to current node |
| 116 | """ |
| 117 | if not path: |
| 118 | path = [] |
| 119 | if self.name == name: |
| 120 | path.append(self.name) |
| 121 | return path |
| 122 | for child in self.children: |
| 123 | match = child.GetPath(name, path + [self.name]) |
| 124 | if match: |
| 125 | return match |
| 126 | return None |
| 127 | |
| 128 | def GetPathRegex(self, name, regex_str, path=None): |
| 129 | """ |
| 130 | Return list of node paths that end in name, or match regex_str. |
| 131 | Return empty list if not found. |
| 132 | |
| 133 | Parameter descriptions: |
| 134 | name Name of node to search for |
| 135 | regex_str Regex string to match node names |
| 136 | path Path of node names from head to current node |
| 137 | """ |
| 138 | new_paths = [] |
| 139 | if not path: |
| 140 | path = [] |
| 141 | match = re.match(regex_str, self.name) |
| 142 | if (self.name == name) or (match): |
| 143 | new_paths.append(path + [self.name]) |
| 144 | for child in self.children: |
| 145 | return_paths = None |
| 146 | full_path = path + [self.name] |
| 147 | return_paths = child.GetPathRegex(name, regex_str, full_path) |
| 148 | for i in return_paths: |
| 149 | new_paths.append(i) |
| 150 | return new_paths |
| 151 | |
| 152 | def MoveNode(self, from_name, to_name): |
| 153 | """ |
| 154 | Mode existing from_name node to become child of to_name node. |
| 155 | |
| 156 | Parameter descriptions: |
| 157 | from_name Name of node to make a child of to_name |
| 158 | to_name Name of node to make parent of from_name |
| 159 | """ |
| 160 | parent_from_node = self.GetParentNode(from_name) |
| 161 | from_node = self.GetNode(from_name) |
| 162 | parent_from_node.RemoveChild(from_name) |
| 163 | to_node = self.GetNode(to_name) |
| 164 | to_node.AddChildNode(from_node) |
| 165 | |
| 166 | def ReorderDeps(self, name, regex_str): |
| 167 | """ |
| 168 | Reorder dependency tree. If tree contains nodes with names that |
| 169 | match 'name' and 'regex_str', move 'regex_str' nodes that are |
| 170 | to the right of 'name' node, so that they become children of the |
| 171 | 'name' node. |
| 172 | |
| 173 | Parameter descriptions: |
| 174 | name Name of node to look for |
| 175 | regex_str Regex string to match names to |
| 176 | """ |
| 177 | name_path = self.GetPath(name) |
| 178 | if not name_path: |
| 179 | return |
| 180 | paths = self.GetPathRegex(name, regex_str) |
| 181 | is_name_in_paths = False |
| 182 | name_index = 0 |
| 183 | for i in range(len(paths)): |
| 184 | path = paths[i] |
| 185 | if path[-1] == name: |
| 186 | is_name_in_paths = True |
| 187 | name_index = i |
| 188 | break |
| 189 | if not is_name_in_paths: |
| 190 | return |
| 191 | for i in range(name_index + 1, len(paths)): |
| 192 | path = paths[i] |
| 193 | if name in path: |
| 194 | continue |
| 195 | from_name = path[-1] |
| 196 | self.MoveNode(from_name, name) |
| 197 | |
| 198 | def GetInstallList(self): |
| 199 | """ |
| 200 | Return post-order list of node names. |
| 201 | |
| 202 | Parameter descriptions: |
| 203 | """ |
| 204 | install_list = [] |
| 205 | for child in self.children: |
| 206 | child_install_list = child.GetInstallList() |
| 207 | install_list.extend(child_install_list) |
| 208 | install_list.append(self.name) |
| 209 | return install_list |
| 210 | |
| 211 | def PrintTree(self, level=0): |
| 212 | """ |
| 213 | Print pre-order node names with indentation denoting node depth level. |
| 214 | |
| 215 | Parameter descriptions: |
| 216 | level Current depth level |
| 217 | """ |
| 218 | INDENT_PER_LEVEL = 4 |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 219 | print(' ' * (level * INDENT_PER_LEVEL) + self.name) |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 220 | for child in self.children: |
| 221 | child.PrintTree(level + 1) |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 222 | |
| 223 | |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 224 | def check_call_cmd(*cmd): |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 225 | """ |
| 226 | Verbose prints the directory location the given command is called from and |
| 227 | the command, then executes the command using check_call. |
| 228 | |
| 229 | Parameter descriptions: |
| 230 | dir Directory location command is to be called from |
| 231 | cmd List of parameters constructing the complete command |
| 232 | """ |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 233 | printline(os.getcwd(), ">", " ".join(cmd)) |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 234 | check_call(cmd) |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 235 | |
| 236 | |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 237 | def clone_pkg(pkg, branch): |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 238 | """ |
| 239 | Clone the given openbmc package's git repository from gerrit into |
| 240 | the WORKSPACE location |
| 241 | |
| 242 | Parameter descriptions: |
| 243 | pkg Name of the package to clone |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 244 | branch Branch to clone from pkg |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 245 | """ |
Andrew Jeffery | 7be94ca | 2018-03-08 13:15:33 +1030 | [diff] [blame] | 246 | pkg_dir = os.path.join(WORKSPACE, pkg) |
| 247 | if os.path.exists(os.path.join(pkg_dir, '.git')): |
| 248 | return pkg_dir |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 249 | pkg_repo = urljoin('https://gerrit.openbmc-project.xyz/openbmc/', pkg) |
Andrew Jeffery | 7be94ca | 2018-03-08 13:15:33 +1030 | [diff] [blame] | 250 | os.mkdir(pkg_dir) |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 251 | printline(pkg_dir, "> git clone", pkg_repo, branch, "./") |
| 252 | try: |
| 253 | # first try the branch |
Andrew Jeffery | 7d4a26f | 2020-03-13 11:42:34 +1030 | [diff] [blame] | 254 | clone = Repo.clone_from(pkg_repo, pkg_dir, branch=branch) |
| 255 | repo_inst = clone.working_dir |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 256 | except: |
| 257 | printline("Input branch not found, default to master") |
Andrew Jeffery | 7d4a26f | 2020-03-13 11:42:34 +1030 | [diff] [blame] | 258 | clone = Repo.clone_from(pkg_repo, pkg_dir, branch="master") |
| 259 | repo_inst = clone.working_dir |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 260 | return repo_inst |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 261 | |
| 262 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 263 | def make_target_exists(target): |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 264 | """ |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 265 | Runs a check against the makefile in the current directory to determine |
| 266 | if the target exists so that it can be built. |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 267 | |
| 268 | Parameter descriptions: |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 269 | target The make target we are checking |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 270 | """ |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 271 | try: |
Andrew Jeffery | 881041f | 2020-03-13 11:46:30 +1030 | [diff] [blame] | 272 | cmd = ['make', '-n', target] |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 273 | with open(os.devnull, 'w') as devnull: |
| 274 | check_call(cmd, stdout=devnull, stderr=devnull) |
| 275 | return True |
| 276 | except CalledProcessError: |
| 277 | return False |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 278 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 279 | |
William A. Kennington III | a215673 | 2018-06-30 18:38:09 -0700 | [diff] [blame] | 280 | make_parallel = [ |
| 281 | 'make', |
| 282 | # Run enough jobs to saturate all the cpus |
| 283 | '-j', str(multiprocessing.cpu_count()), |
| 284 | # Don't start more jobs if the load avg is too high |
| 285 | '-l', str(multiprocessing.cpu_count()), |
| 286 | # Synchronize the output so logs aren't intermixed in stdout / stderr |
| 287 | '-O', |
| 288 | ] |
| 289 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 290 | |
Andrew Jeffery | ff5c5d5 | 2020-03-13 10:12:14 +1030 | [diff] [blame] | 291 | def build_and_install(name, build_for_testing=False): |
William A. Kennington III | 780ec09 | 2018-12-06 14:46:50 -0800 | [diff] [blame] | 292 | """ |
| 293 | Builds and installs the package in the environment. Optionally |
| 294 | builds the examples and test cases for package. |
| 295 | |
| 296 | Parameter description: |
Andrew Jeffery | ff5c5d5 | 2020-03-13 10:12:14 +1030 | [diff] [blame] | 297 | name The name of the package we are building |
William A. Kennington III | a045491 | 2018-12-06 14:47:16 -0800 | [diff] [blame] | 298 | 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] | 299 | """ |
Andrew Jeffery | ff5c5d5 | 2020-03-13 10:12:14 +1030 | [diff] [blame] | 300 | os.chdir(os.path.join(WORKSPACE, name)) |
William A. Kennington III | 54d4faf | 2018-12-06 17:46:24 -0800 | [diff] [blame] | 301 | |
| 302 | # Refresh dynamic linker run time bindings for dependencies |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 303 | check_call_cmd('sudo', '-n', '--', 'ldconfig') |
William A. Kennington III | 54d4faf | 2018-12-06 17:46:24 -0800 | [diff] [blame] | 304 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 305 | pkg = Package() |
| 306 | if build_for_testing: |
| 307 | pkg.test() |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 308 | else: |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 309 | pkg.install() |
| 310 | |
William A. Kennington III | 780ec09 | 2018-12-06 14:46:50 -0800 | [diff] [blame] | 311 | |
Andrew Jeffery | ccf85d6 | 2020-03-13 10:25:42 +1030 | [diff] [blame] | 312 | def build_dep_tree(name, pkgdir, dep_added, head, branch, dep_tree=None): |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 313 | """ |
Andrew Jeffery | ccf85d6 | 2020-03-13 10:25:42 +1030 | [diff] [blame] | 314 | For each package (name), starting with the package to be unit tested, |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 315 | extract its dependencies. For each package dependency defined, recursively |
| 316 | apply the same strategy |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 317 | |
| 318 | Parameter descriptions: |
Andrew Jeffery | ccf85d6 | 2020-03-13 10:25:42 +1030 | [diff] [blame] | 319 | name Name of the package |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 320 | pkgdir Directory where package source is located |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 321 | dep_added Current dict of dependencies and added status |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 322 | head Head node of the dependency tree |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 323 | branch Branch to clone from pkg |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 324 | dep_tree Current dependency tree node |
| 325 | """ |
| 326 | if not dep_tree: |
| 327 | dep_tree = head |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 328 | |
William A. Kennington III | be6aab2 | 2018-12-06 15:01:54 -0800 | [diff] [blame] | 329 | with open("/tmp/depcache", "r") as depcache: |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 330 | cache = depcache.readline() |
| 331 | |
| 332 | # Read out pkg dependencies |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 333 | pkg = Package(name, pkgdir) |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 334 | |
Patrick Williams | 38c6b7d | 2021-02-18 07:26:14 -0600 | [diff] [blame] | 335 | build = pkg.build_system() |
| 336 | if build == None: |
| 337 | raise Exception(f"Unable to find build system for {name}.") |
| 338 | |
| 339 | for dep in set(build.dependencies()): |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 340 | if dep in cache: |
| 341 | continue |
| 342 | # Dependency package not already known |
| 343 | if dep_added.get(dep) is None: |
Patrick Williams | 38c6b7d | 2021-02-18 07:26:14 -0600 | [diff] [blame] | 344 | print(f"Adding {dep} dependency to {name}.") |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 345 | # Dependency package not added |
| 346 | new_child = dep_tree.AddChild(dep) |
| 347 | dep_added[dep] = False |
Andrew Jeffery | 3b92fdd | 2020-03-13 11:49:18 +1030 | [diff] [blame] | 348 | dep_pkgdir = clone_pkg(dep, branch) |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 349 | # Determine this dependency package's |
| 350 | # dependencies and add them before |
| 351 | # returning to add this package |
| 352 | dep_added = build_dep_tree(dep, |
| 353 | dep_pkgdir, |
| 354 | dep_added, |
| 355 | head, |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 356 | branch, |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 357 | new_child) |
| 358 | else: |
| 359 | # Dependency package known and added |
| 360 | if dep_added[dep]: |
Andrew Jeffery | 2cb0c7a | 2018-03-08 13:19:08 +1030 | [diff] [blame] | 361 | continue |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 362 | else: |
William A. Kennington III | c048cc0 | 2018-12-06 15:39:18 -0800 | [diff] [blame] | 363 | # Cyclic dependency failure |
Andrew Jeffery | ccf85d6 | 2020-03-13 10:25:42 +1030 | [diff] [blame] | 364 | raise Exception("Cyclic dependencies found in "+name) |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 365 | |
Andrew Jeffery | ccf85d6 | 2020-03-13 10:25:42 +1030 | [diff] [blame] | 366 | if not dep_added[name]: |
| 367 | dep_added[name] = True |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 368 | |
| 369 | return dep_added |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 370 | |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 371 | |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 372 | def run_cppcheck(): |
Andrew Jeffery | bf73510 | 2020-03-13 12:13:21 +1030 | [diff] [blame] | 373 | match_re = re.compile(r'((?!\.mako\.).)*\.[ch](?:pp)?$', re.I) |
Brad Bishop | 48424d4 | 2020-01-07 13:01:31 -0500 | [diff] [blame] | 374 | cppcheck_files = [] |
| 375 | stdout = subprocess.check_output(['git', 'ls-files']) |
Patrick Venture | ad4354e | 2018-10-12 16:59:54 -0700 | [diff] [blame] | 376 | |
Brad Bishop | 48424d4 | 2020-01-07 13:01:31 -0500 | [diff] [blame] | 377 | for f in stdout.decode('utf-8').split(): |
| 378 | if match_re.match(f): |
| 379 | cppcheck_files.append(f) |
| 380 | |
| 381 | if not cppcheck_files: |
| 382 | # skip cppcheck if there arent' any c or cpp sources. |
| 383 | print("no files") |
| 384 | return None |
| 385 | |
| 386 | # http://cppcheck.sourceforge.net/manual.pdf |
| 387 | params = ['cppcheck', '-j', str(multiprocessing.cpu_count()), |
Manojkiran Eda | 89a46cc | 2020-08-22 17:08:11 +0530 | [diff] [blame] | 388 | '--enable=all', '--library=googletest', '--file-list=-'] |
Brad Bishop | 48424d4 | 2020-01-07 13:01:31 -0500 | [diff] [blame] | 389 | |
| 390 | cppcheck_process = subprocess.Popen( |
| 391 | params, |
| 392 | stdout=subprocess.PIPE, |
| 393 | stderr=subprocess.PIPE, |
| 394 | stdin=subprocess.PIPE) |
| 395 | (stdout, stderr) = cppcheck_process.communicate( |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 396 | input='\n'.join(cppcheck_files).encode('utf-8')) |
Brad Bishop | 48424d4 | 2020-01-07 13:01:31 -0500 | [diff] [blame] | 397 | |
| 398 | if cppcheck_process.wait(): |
Patrick Venture | ad4354e | 2018-10-12 16:59:54 -0700 | [diff] [blame] | 399 | raise Exception('Cppcheck failed') |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 400 | print(stdout.decode('utf-8')) |
| 401 | print(stderr.decode('utf-8')) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 402 | |
Andrew Jeffery | e5fffa0 | 2020-03-13 11:51:57 +1030 | [diff] [blame] | 403 | |
William A. Kennington III | 37a89a2 | 2018-12-13 14:32:02 -0800 | [diff] [blame] | 404 | def is_valgrind_safe(): |
| 405 | """ |
| 406 | Returns whether it is safe to run valgrind on our platform |
| 407 | """ |
William A. Kennington III | 0326ded | 2019-02-07 00:33:28 -0800 | [diff] [blame] | 408 | src = 'unit-test-vg.c' |
| 409 | exe = './unit-test-vg' |
| 410 | with open(src, 'w') as h: |
William A. Kennington III | afb0f98 | 2019-04-26 17:30:28 -0700 | [diff] [blame] | 411 | h.write('#include <errno.h>\n') |
| 412 | h.write('#include <stdio.h>\n') |
William A. Kennington III | 0326ded | 2019-02-07 00:33:28 -0800 | [diff] [blame] | 413 | h.write('#include <stdlib.h>\n') |
| 414 | h.write('#include <string.h>\n') |
| 415 | h.write('int main() {\n') |
| 416 | h.write('char *heap_str = malloc(16);\n') |
| 417 | h.write('strcpy(heap_str, "RandString");\n') |
| 418 | h.write('int res = strcmp("RandString", heap_str);\n') |
| 419 | h.write('free(heap_str);\n') |
William A. Kennington III | afb0f98 | 2019-04-26 17:30:28 -0700 | [diff] [blame] | 420 | h.write('char errstr[64];\n') |
| 421 | h.write('strerror_r(EINVAL, errstr, sizeof(errstr));\n') |
| 422 | h.write('printf("%s\\n", errstr);\n') |
William A. Kennington III | 0326ded | 2019-02-07 00:33:28 -0800 | [diff] [blame] | 423 | h.write('return res;\n') |
| 424 | h.write('}\n') |
| 425 | try: |
| 426 | with open(os.devnull, 'w') as devnull: |
| 427 | check_call(['gcc', '-O2', '-o', exe, src], |
| 428 | stdout=devnull, stderr=devnull) |
| 429 | check_call(['valgrind', '--error-exitcode=99', exe], |
| 430 | stdout=devnull, stderr=devnull) |
| 431 | return True |
| 432 | except: |
| 433 | sys.stderr.write("###### Platform is not valgrind safe ######\n") |
| 434 | return False |
| 435 | finally: |
| 436 | os.remove(src) |
| 437 | os.remove(exe) |
William A. Kennington III | 37a89a2 | 2018-12-13 14:32:02 -0800 | [diff] [blame] | 438 | |
Andrew Jeffery | e5fffa0 | 2020-03-13 11:51:57 +1030 | [diff] [blame] | 439 | |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 440 | def is_sanitize_safe(): |
| 441 | """ |
| 442 | Returns whether it is safe to run sanitizers on our platform |
| 443 | """ |
William A. Kennington III | 0b7fb2b | 2019-02-07 00:33:42 -0800 | [diff] [blame] | 444 | src = 'unit-test-sanitize.c' |
| 445 | exe = './unit-test-sanitize' |
| 446 | with open(src, 'w') as h: |
| 447 | h.write('int main() { return 0; }\n') |
| 448 | try: |
| 449 | with open(os.devnull, 'w') as devnull: |
| 450 | check_call(['gcc', '-O2', '-fsanitize=address', |
| 451 | '-fsanitize=undefined', '-o', exe, src], |
| 452 | stdout=devnull, stderr=devnull) |
| 453 | check_call([exe], stdout=devnull, stderr=devnull) |
Andrew Geissler | cd9578b | 2021-05-04 10:18:01 -0500 | [diff] [blame] | 454 | |
| 455 | # TODO - Sanitizer not working on ppc64le |
| 456 | # https://github.com/openbmc/openbmc-build-scripts/issues/31 |
| 457 | if (platform.processor() == 'ppc64le'): |
| 458 | sys.stderr.write("###### ppc64le is not sanitize safe ######\n") |
| 459 | return False |
| 460 | else: |
| 461 | return True |
William A. Kennington III | 0b7fb2b | 2019-02-07 00:33:42 -0800 | [diff] [blame] | 462 | except: |
| 463 | sys.stderr.write("###### Platform is not sanitize safe ######\n") |
| 464 | return False |
| 465 | finally: |
| 466 | os.remove(src) |
| 467 | os.remove(exe) |
William A. Kennington III | 282e330 | 2019-02-04 16:55:05 -0800 | [diff] [blame] | 468 | |
William A. Kennington III | 49d4e59 | 2019-02-06 17:59:27 -0800 | [diff] [blame] | 469 | |
William A. Kennington III | eaff24a | 2019-02-06 16:57:42 -0800 | [diff] [blame] | 470 | def maybe_make_valgrind(): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 471 | """ |
| 472 | Potentially runs the unit tests through valgrind for the package |
| 473 | via `make check-valgrind`. If the package does not have valgrind testing |
| 474 | then it just skips over this. |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 475 | """ |
William A. Kennington III | 4e1d0a1 | 2018-07-16 12:04:03 -0700 | [diff] [blame] | 476 | # Valgrind testing is currently broken by an aggressive strcmp optimization |
| 477 | # that is inlined into optimized code for POWER by gcc 7+. Until we find |
| 478 | # a workaround, just don't run valgrind tests on POWER. |
| 479 | # https://github.com/openbmc/openbmc/issues/3315 |
William A. Kennington III | 37a89a2 | 2018-12-13 14:32:02 -0800 | [diff] [blame] | 480 | if not is_valgrind_safe(): |
William A. Kennington III | 7513019 | 2019-02-07 00:34:14 -0800 | [diff] [blame] | 481 | sys.stderr.write("###### Skipping valgrind ######\n") |
William A. Kennington III | 4e1d0a1 | 2018-07-16 12:04:03 -0700 | [diff] [blame] | 482 | return |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 483 | if not make_target_exists('check-valgrind'): |
| 484 | return |
| 485 | |
| 486 | try: |
Andrew Jeffery | 881041f | 2020-03-13 11:46:30 +1030 | [diff] [blame] | 487 | cmd = make_parallel + ['check-valgrind'] |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 488 | check_call_cmd(*cmd) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 489 | except CalledProcessError: |
William A. Kennington III | 90b106a | 2019-02-06 18:08:24 -0800 | [diff] [blame] | 490 | for root, _, files in os.walk(os.getcwd()): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 491 | for f in files: |
| 492 | if re.search('test-suite-[a-z]+.log', f) is None: |
| 493 | continue |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 494 | check_call_cmd('cat', os.path.join(root, f)) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 495 | raise Exception('Valgrind tests failed') |
| 496 | |
Andrew Jeffery | e5fffa0 | 2020-03-13 11:51:57 +1030 | [diff] [blame] | 497 | |
William A. Kennington III | eaff24a | 2019-02-06 16:57:42 -0800 | [diff] [blame] | 498 | def maybe_make_coverage(): |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 499 | """ |
| 500 | Potentially runs the unit tests through code coverage for the package |
| 501 | via `make check-code-coverage`. If the package does not have code coverage |
| 502 | testing then it just skips over this. |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 503 | """ |
| 504 | if not make_target_exists('check-code-coverage'): |
| 505 | return |
| 506 | |
| 507 | # Actually run code coverage |
| 508 | try: |
Andrew Jeffery | 881041f | 2020-03-13 11:46:30 +1030 | [diff] [blame] | 509 | cmd = make_parallel + ['check-code-coverage'] |
William A. Kennington III | 1fddb97 | 2019-02-06 18:03:53 -0800 | [diff] [blame] | 510 | check_call_cmd(*cmd) |
William A. Kennington III | 0f0a680 | 2018-07-16 11:52:33 -0700 | [diff] [blame] | 511 | except CalledProcessError: |
| 512 | raise Exception('Code coverage failed') |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 513 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 514 | |
| 515 | class BuildSystem(object): |
| 516 | """ |
| 517 | Build systems generally provide the means to configure, build, install and |
| 518 | test software. The BuildSystem class defines a set of interfaces on top of |
| 519 | which Autotools, Meson, CMake and possibly other build system drivers can |
| 520 | be implemented, separating out the phases to control whether a package |
| 521 | should merely be installed or also tested and analyzed. |
| 522 | """ |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 523 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 524 | def __init__(self, package, path): |
| 525 | """Initialise the driver with properties independent of the build system |
| 526 | |
| 527 | Keyword arguments: |
| 528 | package: The name of the package. Derived from the path if None |
| 529 | path: The path to the package. Set to the working directory if None |
| 530 | """ |
| 531 | self.path = "." if not path else path |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 532 | realpath = os.path.realpath(self.path) |
| 533 | self.package = package if package else os.path.basename(realpath) |
Andrew Jeffery | 3f8e5a7 | 2020-03-13 11:47:56 +1030 | [diff] [blame] | 534 | self.build_for_testing = False |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 535 | |
| 536 | def probe(self): |
| 537 | """Test if the build system driver can be applied to the package |
| 538 | |
| 539 | Return True if the driver can drive the package's build system, |
| 540 | otherwise False. |
| 541 | |
| 542 | Generally probe() is implemented by testing for the presence of the |
| 543 | build system's configuration file(s). |
| 544 | """ |
| 545 | raise NotImplemented |
| 546 | |
| 547 | def dependencies(self): |
| 548 | """Provide the package's dependencies |
| 549 | |
| 550 | Returns a list of dependencies. If no dependencies are required then an |
| 551 | empty list must be returned. |
| 552 | |
| 553 | Generally dependencies() is implemented by analysing and extracting the |
| 554 | data from the build system configuration. |
| 555 | """ |
| 556 | raise NotImplemented |
| 557 | |
| 558 | def configure(self, build_for_testing): |
| 559 | """Configure the source ready for building |
| 560 | |
| 561 | Should raise an exception if configuration failed. |
| 562 | |
| 563 | Keyword arguments: |
| 564 | build_for_testing: Mark the package as being built for testing rather |
| 565 | than for installation as a dependency for the |
| 566 | package under test. Setting to True generally |
| 567 | implies that the package will be configured to build |
| 568 | with debug information, at a low level of |
| 569 | optimisation and possibly with sanitizers enabled. |
| 570 | |
| 571 | Generally configure() is implemented by invoking the build system |
| 572 | tooling to generate Makefiles or equivalent. |
| 573 | """ |
| 574 | raise NotImplemented |
| 575 | |
| 576 | def build(self): |
| 577 | """Build the software ready for installation and/or testing |
| 578 | |
| 579 | Should raise an exception if the build fails |
| 580 | |
| 581 | Generally build() is implemented by invoking `make` or `ninja`. |
| 582 | """ |
| 583 | raise NotImplemented |
| 584 | |
| 585 | def install(self): |
| 586 | """Install the software ready for use |
| 587 | |
| 588 | Should raise an exception if installation fails |
| 589 | |
| 590 | Like build(), install() is generally implemented by invoking `make` or |
| 591 | `ninja`. |
| 592 | """ |
| 593 | raise NotImplemented |
| 594 | |
| 595 | def test(self): |
| 596 | """Build and run the test suite associated with the package |
| 597 | |
| 598 | Should raise an exception if the build or testing fails. |
| 599 | |
| 600 | Like install(), test() is generally implemented by invoking `make` or |
| 601 | `ninja`. |
| 602 | """ |
| 603 | raise NotImplemented |
| 604 | |
| 605 | def analyze(self): |
| 606 | """Run any supported analysis tools over the codebase |
| 607 | |
| 608 | Should raise an exception if analysis fails. |
| 609 | |
| 610 | Some analysis tools such as scan-build need injection into the build |
| 611 | system. analyze() provides the necessary hook to implement such |
| 612 | behaviour. Analyzers independent of the build system can also be |
| 613 | specified here but at the cost of possible duplication of code between |
| 614 | the build system driver implementations. |
| 615 | """ |
| 616 | raise NotImplemented |
| 617 | |
| 618 | |
| 619 | class Autotools(BuildSystem): |
| 620 | def __init__(self, package=None, path=None): |
| 621 | super(Autotools, self).__init__(package, path) |
| 622 | |
| 623 | def probe(self): |
| 624 | return os.path.isfile(os.path.join(self.path, 'configure.ac')) |
| 625 | |
| 626 | def dependencies(self): |
| 627 | configure_ac = os.path.join(self.path, 'configure.ac') |
| 628 | |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 629 | contents = '' |
Andrew Jeffery | 7d4a26f | 2020-03-13 11:42:34 +1030 | [diff] [blame] | 630 | # Prepend some special function overrides so we can parse out |
| 631 | # dependencies |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 632 | for macro in DEPENDENCIES.keys(): |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 633 | contents += ('m4_define([' + macro + '], [' + macro + '_START$' + |
| 634 | str(DEPENDENCIES_OFFSET[macro] + 1) + |
| 635 | macro + '_END])\n') |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 636 | with open(configure_ac, "rt") as f: |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 637 | contents += f.read() |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 638 | |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 639 | autoconf_cmdline = ['autoconf', '-Wno-undefined', '-'] |
| 640 | autoconf_process = subprocess.Popen(autoconf_cmdline, |
Andrew Jeffery | 7d4a26f | 2020-03-13 11:42:34 +1030 | [diff] [blame] | 641 | stdin=subprocess.PIPE, |
| 642 | stdout=subprocess.PIPE, |
| 643 | stderr=subprocess.PIPE) |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 644 | document = contents.encode('utf-8') |
| 645 | (stdout, stderr) = autoconf_process.communicate(input=document) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 646 | if not stdout: |
| 647 | print(stderr) |
| 648 | raise Exception("Failed to run autoconf for parsing dependencies") |
| 649 | |
| 650 | # Parse out all of the dependency text |
| 651 | matches = [] |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 652 | for macro in DEPENDENCIES.keys(): |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 653 | pattern = '(' + macro + ')_START(.*?)' + macro + '_END' |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 654 | for match in re.compile(pattern).finditer(stdout.decode('utf-8')): |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 655 | matches.append((match.group(1), match.group(2))) |
| 656 | |
| 657 | # Look up dependencies from the text |
| 658 | found_deps = [] |
| 659 | for macro, deptext in matches: |
| 660 | for potential_dep in deptext.split(' '): |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 661 | for known_dep in DEPENDENCIES[macro].keys(): |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 662 | if potential_dep.startswith(known_dep): |
| 663 | found_deps.append(DEPENDENCIES[macro][known_dep]) |
| 664 | |
| 665 | return found_deps |
| 666 | |
| 667 | def _configure_feature(self, flag, enabled): |
| 668 | """ |
| 669 | Returns an configure flag as a string |
| 670 | |
| 671 | Parameters: |
| 672 | flag The name of the flag |
| 673 | enabled Whether the flag is enabled or disabled |
| 674 | """ |
| 675 | return '--' + ('enable' if enabled else 'disable') + '-' + flag |
| 676 | |
| 677 | def configure(self, build_for_testing): |
| 678 | self.build_for_testing = build_for_testing |
| 679 | conf_flags = [ |
| 680 | self._configure_feature('silent-rules', False), |
| 681 | self._configure_feature('examples', build_for_testing), |
| 682 | self._configure_feature('tests', build_for_testing), |
Ramin Izadpanah | 298d56b | 2020-11-17 16:55:28 +0000 | [diff] [blame] | 683 | self._configure_feature('itests', INTEGRATION_TEST), |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 684 | ] |
Andrew Jeffery | d080969 | 2021-05-14 16:23:57 +0930 | [diff] [blame] | 685 | conf_flags.extend([ |
| 686 | self._configure_feature('code-coverage', build_for_testing), |
| 687 | self._configure_feature('valgrind', build_for_testing), |
| 688 | ]) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 689 | # Add any necessary configure flags for package |
| 690 | if CONFIGURE_FLAGS.get(self.package) is not None: |
| 691 | conf_flags.extend(CONFIGURE_FLAGS.get(self.package)) |
| 692 | for bootstrap in ['bootstrap.sh', 'bootstrap', 'autogen.sh']: |
| 693 | if os.path.exists(bootstrap): |
| 694 | check_call_cmd('./' + bootstrap) |
| 695 | break |
| 696 | check_call_cmd('./configure', *conf_flags) |
| 697 | |
| 698 | def build(self): |
| 699 | check_call_cmd(*make_parallel) |
| 700 | |
| 701 | def install(self): |
Andrew Jeffery | 881041f | 2020-03-13 11:46:30 +1030 | [diff] [blame] | 702 | check_call_cmd('sudo', '-n', '--', *(make_parallel + ['install'])) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 703 | |
| 704 | def test(self): |
| 705 | try: |
Andrew Jeffery | 881041f | 2020-03-13 11:46:30 +1030 | [diff] [blame] | 706 | cmd = make_parallel + ['check'] |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 707 | for i in range(0, args.repeat): |
| 708 | check_call_cmd(*cmd) |
Andrew Jeffery | d080969 | 2021-05-14 16:23:57 +0930 | [diff] [blame] | 709 | |
| 710 | maybe_make_valgrind() |
| 711 | maybe_make_coverage() |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 712 | except CalledProcessError: |
| 713 | for root, _, files in os.walk(os.getcwd()): |
| 714 | if 'test-suite.log' not in files: |
| 715 | continue |
| 716 | check_call_cmd('cat', os.path.join(root, 'test-suite.log')) |
| 717 | raise Exception('Unit tests failed') |
| 718 | |
| 719 | def analyze(self): |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 720 | run_cppcheck() |
| 721 | |
| 722 | |
| 723 | class CMake(BuildSystem): |
| 724 | def __init__(self, package=None, path=None): |
| 725 | super(CMake, self).__init__(package, path) |
| 726 | |
| 727 | def probe(self): |
| 728 | return os.path.isfile(os.path.join(self.path, 'CMakeLists.txt')) |
| 729 | |
| 730 | def dependencies(self): |
| 731 | return [] |
| 732 | |
| 733 | def configure(self, build_for_testing): |
| 734 | self.build_for_testing = build_for_testing |
Ramin Izadpanah | 298d56b | 2020-11-17 16:55:28 +0000 | [diff] [blame] | 735 | if INTEGRATION_TEST: |
| 736 | check_call_cmd('cmake', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', |
| 737 | '-DITESTS=ON', '.') |
| 738 | else: |
| 739 | check_call_cmd('cmake', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', '.') |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 740 | |
| 741 | def build(self): |
| 742 | check_call_cmd('cmake', '--build', '.', '--', '-j', |
| 743 | str(multiprocessing.cpu_count())) |
| 744 | |
| 745 | def install(self): |
| 746 | pass |
| 747 | |
| 748 | def test(self): |
| 749 | if make_target_exists('test'): |
| 750 | check_call_cmd('ctest', '.') |
| 751 | |
| 752 | def analyze(self): |
| 753 | if os.path.isfile('.clang-tidy'): |
Ed Tanous | bc5c1c4 | 2020-10-01 17:49:20 -0700 | [diff] [blame] | 754 | try: |
| 755 | os.mkdir("tidy-build") |
| 756 | except FileExistsError as e: |
| 757 | pass |
| 758 | # clang-tidy needs to run on a clang-specific build |
| 759 | check_call_cmd('cmake', '-DCMAKE_C_COMPILER=clang', |
| 760 | '-DCMAKE_CXX_COMPILER=clang++', |
| 761 | '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', |
| 762 | '-H.', |
| 763 | '-Btidy-build') |
| 764 | # we need to cd here because otherwise clang-tidy doesn't find the |
| 765 | # .clang-tidy file in the roots of repos. Its arguably a "bug" |
| 766 | # with run-clang-tidy at a minimum it's "weird" that it requires |
| 767 | # the .clang-tidy to be up a dir |
Ed Tanous | 662890f | 2020-09-28 17:35:20 -0700 | [diff] [blame] | 768 | os.chdir("tidy-build") |
| 769 | try: |
Patrick Williams | f89c850 | 2021-10-01 11:50:09 -0500 | [diff] [blame] | 770 | check_call_cmd('run-clang-tidy', "-header-filter=.*", '-p', |
Ed Tanous | bc5c1c4 | 2020-10-01 17:49:20 -0700 | [diff] [blame] | 771 | '.') |
Ed Tanous | 662890f | 2020-09-28 17:35:20 -0700 | [diff] [blame] | 772 | finally: |
| 773 | os.chdir("..") |
| 774 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 775 | maybe_make_valgrind() |
| 776 | maybe_make_coverage() |
| 777 | run_cppcheck() |
| 778 | |
| 779 | |
| 780 | class Meson(BuildSystem): |
| 781 | def __init__(self, package=None, path=None): |
| 782 | super(Meson, self).__init__(package, path) |
| 783 | |
| 784 | def probe(self): |
| 785 | return os.path.isfile(os.path.join(self.path, 'meson.build')) |
| 786 | |
| 787 | def dependencies(self): |
| 788 | meson_build = os.path.join(self.path, 'meson.build') |
| 789 | if not os.path.exists(meson_build): |
| 790 | return [] |
| 791 | |
| 792 | found_deps = [] |
| 793 | for root, dirs, files in os.walk(self.path): |
| 794 | if 'meson.build' not in files: |
| 795 | continue |
| 796 | with open(os.path.join(root, 'meson.build'), 'rt') as f: |
| 797 | build_contents = f.read() |
Nan Zhou | ef13d53 | 2020-07-07 09:52:02 -0700 | [diff] [blame] | 798 | pattern = r"dependency\('([^']*)'.*?\),?\n" |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 799 | for match in re.finditer(pattern, build_contents): |
| 800 | group = match.group(1) |
| 801 | maybe_dep = DEPENDENCIES['PKG_CHECK_MODULES'].get(group) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 802 | if maybe_dep is not None: |
| 803 | found_deps.append(maybe_dep) |
| 804 | |
| 805 | return found_deps |
| 806 | |
| 807 | def _parse_options(self, options_file): |
| 808 | """ |
| 809 | Returns a set of options defined in the provides meson_options.txt file |
| 810 | |
| 811 | Parameters: |
| 812 | options_file The file containing options |
| 813 | """ |
William A. Kennington III | 0ecd0bc | 2020-06-04 00:44:22 -0700 | [diff] [blame] | 814 | oi = optinterpreter.OptionInterpreter('') |
| 815 | oi.process(options_file) |
| 816 | return oi.options |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 817 | |
William A. Kennington III | fcd7077 | 2020-06-04 00:50:23 -0700 | [diff] [blame] | 818 | def _configure_boolean(self, val): |
| 819 | """ |
| 820 | Returns the meson flag which signifies the value |
| 821 | |
| 822 | True is true which requires the boolean. |
| 823 | False is false which disables the boolean. |
| 824 | |
| 825 | Parameters: |
| 826 | val The value being converted |
| 827 | """ |
| 828 | if val is True: |
| 829 | return 'true' |
| 830 | elif val is False: |
| 831 | return 'false' |
| 832 | else: |
| 833 | raise Exception("Bad meson boolean value") |
| 834 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 835 | def _configure_feature(self, val): |
| 836 | """ |
| 837 | Returns the meson flag which signifies the value |
| 838 | |
| 839 | True is enabled which requires the feature. |
| 840 | False is disabled which disables the feature. |
| 841 | None is auto which autodetects the feature. |
| 842 | |
| 843 | Parameters: |
| 844 | val The value being converted |
| 845 | """ |
| 846 | if val is True: |
| 847 | return "enabled" |
| 848 | elif val is False: |
| 849 | return "disabled" |
| 850 | elif val is None: |
| 851 | return "auto" |
| 852 | else: |
| 853 | raise Exception("Bad meson feature value") |
| 854 | |
William A. Kennington III | fcd7077 | 2020-06-04 00:50:23 -0700 | [diff] [blame] | 855 | def _configure_option(self, opts, key, val): |
| 856 | """ |
| 857 | Returns the meson flag which signifies the value |
| 858 | based on the type of the opt |
| 859 | |
| 860 | Parameters: |
| 861 | opt The meson option which we are setting |
| 862 | val The value being converted |
| 863 | """ |
| 864 | if isinstance(opts[key], coredata.UserBooleanOption): |
| 865 | str_val = self._configure_boolean(val) |
| 866 | elif isinstance(opts[key], coredata.UserFeatureOption): |
| 867 | str_val = self._configure_feature(val) |
| 868 | else: |
| 869 | raise Exception('Unknown meson option type') |
| 870 | return "-D{}={}".format(key, str_val) |
| 871 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 872 | def configure(self, build_for_testing): |
| 873 | self.build_for_testing = build_for_testing |
William A. Kennington III | 0ecd0bc | 2020-06-04 00:44:22 -0700 | [diff] [blame] | 874 | meson_options = {} |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 875 | if os.path.exists("meson_options.txt"): |
| 876 | meson_options = self._parse_options("meson_options.txt") |
| 877 | meson_flags = [ |
| 878 | '-Db_colorout=never', |
| 879 | '-Dwerror=true', |
| 880 | '-Dwarning_level=3', |
| 881 | ] |
| 882 | if build_for_testing: |
| 883 | meson_flags.append('--buildtype=debug') |
| 884 | else: |
| 885 | meson_flags.append('--buildtype=debugoptimized') |
Patrick Williams | e95626d | 2021-04-13 11:13:53 -0500 | [diff] [blame] | 886 | if OptionKey('tests') in meson_options: |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 887 | meson_flags.append(self._configure_option( |
| 888 | meson_options, OptionKey('tests'), build_for_testing)) |
Patrick Williams | e95626d | 2021-04-13 11:13:53 -0500 | [diff] [blame] | 889 | if OptionKey('examples') in meson_options: |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 890 | meson_flags.append(self._configure_option( |
| 891 | meson_options, OptionKey('examples'), build_for_testing)) |
Patrick Williams | e95626d | 2021-04-13 11:13:53 -0500 | [diff] [blame] | 892 | if OptionKey('itests') in meson_options: |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 893 | meson_flags.append(self._configure_option( |
| 894 | meson_options, OptionKey('itests'), INTEGRATION_TEST)) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 895 | if MESON_FLAGS.get(self.package) is not None: |
| 896 | meson_flags.extend(MESON_FLAGS.get(self.package)) |
| 897 | try: |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 898 | check_call_cmd('meson', 'setup', '--reconfigure', 'build', |
| 899 | *meson_flags) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 900 | except: |
| 901 | shutil.rmtree('build') |
| 902 | check_call_cmd('meson', 'setup', 'build', *meson_flags) |
| 903 | |
| 904 | def build(self): |
| 905 | check_call_cmd('ninja', '-C', 'build') |
| 906 | |
| 907 | def install(self): |
| 908 | check_call_cmd('sudo', '-n', '--', 'ninja', '-C', 'build', 'install') |
| 909 | |
| 910 | def test(self): |
Patrick Williams | 95095f1 | 2021-04-14 14:42:35 -0500 | [diff] [blame] | 911 | # It is useful to check various settings of the meson.build file |
| 912 | # for compatibility, such as meson_version checks. We shouldn't |
| 913 | # do this in the configure path though because it affects subprojects |
| 914 | # and dependencies as well, but we only want this applied to the |
| 915 | # project-under-test (otherwise an upstream dependency could fail |
| 916 | # this check without our control). |
| 917 | self._extra_meson_checks() |
| 918 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 919 | try: |
Brad Bishop | 7b8cef2 | 2020-08-26 15:58:09 -0400 | [diff] [blame] | 920 | test_args = ('--repeat', str(args.repeat), '-C', 'build') |
William A. Kennington III | d9062c4 | 2022-05-04 23:24:44 -0700 | [diff] [blame] | 921 | check_call_cmd('meson', 'test', '--print-errorlogs', *test_args) |
Brad Bishop | 7b8cef2 | 2020-08-26 15:58:09 -0400 | [diff] [blame] | 922 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 923 | except CalledProcessError: |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 924 | raise Exception('Unit tests failed') |
| 925 | |
| 926 | def _setup_exists(self, setup): |
| 927 | """ |
| 928 | Returns whether the meson build supports the named test setup. |
| 929 | |
| 930 | Parameter descriptions: |
| 931 | setup The setup target to check |
| 932 | """ |
| 933 | try: |
| 934 | with open(os.devnull, 'w') as devnull: |
| 935 | output = subprocess.check_output( |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 936 | ['meson', 'test', '-C', 'build', |
| 937 | '--setup', setup, '-t', '0'], |
| 938 | stderr=subprocess.STDOUT) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 939 | except CalledProcessError as e: |
| 940 | output = e.output |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 941 | output = output.decode('utf-8') |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 942 | return not re.search('Test setup .* not found from project', output) |
| 943 | |
| 944 | def _maybe_valgrind(self): |
| 945 | """ |
| 946 | Potentially runs the unit tests through valgrind for the package |
Andrew Jeffery | 47fbfa5 | 2020-03-13 12:05:09 +1030 | [diff] [blame] | 947 | via `meson test`. The package can specify custom valgrind |
| 948 | configurations by utilizing add_test_setup() in a meson.build |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 949 | """ |
| 950 | if not is_valgrind_safe(): |
| 951 | sys.stderr.write("###### Skipping valgrind ######\n") |
| 952 | return |
| 953 | try: |
| 954 | if self._setup_exists('valgrind'): |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 955 | check_call_cmd('meson', 'test', '-t', '10', '-C', 'build', |
William A. Kennington III | d9062c4 | 2022-05-04 23:24:44 -0700 | [diff] [blame] | 956 | '--print-errorlogs', '--setup', 'valgrind') |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 957 | else: |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 958 | check_call_cmd('meson', 'test', '-t', '10', '-C', 'build', |
William A. Kennington III | d9062c4 | 2022-05-04 23:24:44 -0700 | [diff] [blame] | 959 | '--print-errorlogs', '--wrapper', 'valgrind') |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 960 | except CalledProcessError: |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 961 | raise Exception('Valgrind tests failed') |
| 962 | |
| 963 | def analyze(self): |
| 964 | self._maybe_valgrind() |
| 965 | |
| 966 | # Run clang-tidy only if the project has a configuration |
| 967 | if os.path.isfile('.clang-tidy'): |
Manojkiran Eda | 1aa9199 | 2020-10-02 14:11:53 +0530 | [diff] [blame] | 968 | os.environ["CXX"] = "clang++" |
| 969 | check_call_cmd('meson', 'setup', 'build-clang') |
Ed Tanous | 88db4c2 | 2022-01-25 15:15:59 -0800 | [diff] [blame] | 970 | os.chdir("build-clang") |
| 971 | try: |
| 972 | check_call_cmd('run-clang-tidy', '-fix', '-format', '-p', '.') |
| 973 | except subprocess.CalledProcessError: |
| 974 | check_call_cmd("git", "-C", CODE_SCAN_DIR, |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 975 | "--no-pager", "diff") |
Ed Tanous | 88db4c2 | 2022-01-25 15:15:59 -0800 | [diff] [blame] | 976 | raise |
| 977 | finally: |
| 978 | os.chdir("..") |
| 979 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 980 | # Run the basic clang static analyzer otherwise |
| 981 | else: |
| 982 | check_call_cmd('ninja', '-C', 'build', |
| 983 | 'scan-build') |
| 984 | |
| 985 | # Run tests through sanitizers |
| 986 | # b_lundef is needed if clang++ is CXX since it resolves the |
| 987 | # asan symbols at runtime only. We don't want to set it earlier |
| 988 | # in the build process to ensure we don't have undefined |
| 989 | # runtime code. |
| 990 | if is_sanitize_safe(): |
| 991 | check_call_cmd('meson', 'configure', 'build', |
| 992 | '-Db_sanitize=address,undefined', |
| 993 | '-Db_lundef=false') |
William A. Kennington III | d9062c4 | 2022-05-04 23:24:44 -0700 | [diff] [blame] | 994 | check_call_cmd('meson', 'test', '-C', 'build', '--print-errorlogs', |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 995 | '--logbase', 'testlog-ubasan') |
| 996 | # TODO: Fix memory sanitizer |
Andrew Jeffery | 6015ca1 | 2020-03-13 11:50:10 +1030 | [diff] [blame] | 997 | # check_call_cmd('meson', 'configure', 'build', |
| 998 | # '-Db_sanitize=memory') |
| 999 | # check_call_cmd('meson', 'test', '-C', 'build' |
| 1000 | # '--logbase', 'testlog-msan') |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1001 | check_call_cmd('meson', 'configure', 'build', |
Lei YU | f684ad6 | 2020-09-21 17:02:26 +0800 | [diff] [blame] | 1002 | '-Db_sanitize=none') |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1003 | else: |
| 1004 | sys.stderr.write("###### Skipping sanitizers ######\n") |
| 1005 | |
| 1006 | # Run coverage checks |
| 1007 | check_call_cmd('meson', 'configure', 'build', |
| 1008 | '-Db_coverage=true') |
| 1009 | self.test() |
| 1010 | # Only build coverage HTML if coverage files were produced |
| 1011 | for root, dirs, files in os.walk('build'): |
| 1012 | if any([f.endswith('.gcda') for f in files]): |
| 1013 | check_call_cmd('ninja', '-C', 'build', |
| 1014 | 'coverage-html') |
| 1015 | break |
| 1016 | check_call_cmd('meson', 'configure', 'build', |
| 1017 | '-Db_coverage=false') |
| 1018 | run_cppcheck() |
| 1019 | |
Patrick Williams | 95095f1 | 2021-04-14 14:42:35 -0500 | [diff] [blame] | 1020 | def _extra_meson_checks(self): |
| 1021 | with open(os.path.join(self.path, 'meson.build'), 'rt') as f: |
| 1022 | build_contents = f.read() |
| 1023 | |
| 1024 | # Find project's specified meson_version. |
| 1025 | meson_version = None |
| 1026 | pattern = r"meson_version:[^']*'([^']*)'" |
| 1027 | for match in re.finditer(pattern, build_contents): |
| 1028 | group = match.group(1) |
| 1029 | meson_version = group |
| 1030 | |
| 1031 | # C++20 requires at least Meson 0.57 but Meson itself doesn't |
| 1032 | # identify this. Add to our unit-test checks so that we don't |
| 1033 | # get a meson.build missing this. |
| 1034 | pattern = r"'cpp_std=c\+\+20'" |
| 1035 | for match in re.finditer(pattern, build_contents): |
| 1036 | if not meson_version or \ |
| 1037 | not meson_version_compare(meson_version, ">=0.57"): |
| 1038 | raise Exception( |
| 1039 | "C++20 support requires specifying in meson.build: " |
| 1040 | + "meson_version: '>=0.57'" |
| 1041 | ) |
| 1042 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1043 | |
| 1044 | class Package(object): |
| 1045 | def __init__(self, name=None, path=None): |
Manojkiran Eda | 29d2825 | 2020-06-15 18:58:09 +0530 | [diff] [blame] | 1046 | self.supported = [Meson, Autotools, CMake] |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1047 | self.name = name |
| 1048 | self.path = path |
| 1049 | self.test_only = False |
| 1050 | |
| 1051 | def build_systems(self): |
Andrew Jeffery | 881041f | 2020-03-13 11:46:30 +1030 | [diff] [blame] | 1052 | instances = (system(self.name, self.path) for system in self.supported) |
| 1053 | return (instance for instance in instances if instance.probe()) |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1054 | |
| 1055 | def build_system(self, preferred=None): |
Andrew Geissler | 8cb74fc | 2020-03-19 14:48:05 -0500 | [diff] [blame] | 1056 | systems = list(self.build_systems()) |
| 1057 | |
| 1058 | if not systems: |
| 1059 | return None |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1060 | |
| 1061 | if preferred: |
Andrew Jeffery | 881041f | 2020-03-13 11:46:30 +1030 | [diff] [blame] | 1062 | return {type(system): system for system in systems}[preferred] |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1063 | |
| 1064 | return next(iter(systems)) |
| 1065 | |
| 1066 | def install(self, system=None): |
| 1067 | if not system: |
| 1068 | system = self.build_system() |
| 1069 | |
| 1070 | system.configure(False) |
| 1071 | system.build() |
| 1072 | system.install() |
| 1073 | |
Andrew Jeffery | 19d7567 | 2020-03-13 10:42:08 +1030 | [diff] [blame] | 1074 | def _test_one(self, system): |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1075 | system.configure(True) |
| 1076 | system.build() |
| 1077 | system.install() |
| 1078 | system.test() |
Andrew Jeffery | d080969 | 2021-05-14 16:23:57 +0930 | [diff] [blame] | 1079 | if not TEST_ONLY: |
| 1080 | system.analyze() |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1081 | |
Andrew Jeffery | 19d7567 | 2020-03-13 10:42:08 +1030 | [diff] [blame] | 1082 | def test(self): |
| 1083 | for system in self.build_systems(): |
| 1084 | self._test_one(system) |
| 1085 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1086 | |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1087 | def find_file(filename, basedir): |
| 1088 | """ |
Patrick Williams | 55448ad | 2020-12-14 14:28:28 -0600 | [diff] [blame] | 1089 | Finds all occurrences of a file (or list of files) in the base |
| 1090 | directory and passes them back with their relative paths. |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1091 | |
| 1092 | Parameter descriptions: |
Patrick Williams | 55448ad | 2020-12-14 14:28:28 -0600 | [diff] [blame] | 1093 | filename The name of the file (or list of files) to |
| 1094 | find |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1095 | basedir The base directory search in |
| 1096 | """ |
| 1097 | |
Patrick Williams | 55448ad | 2020-12-14 14:28:28 -0600 | [diff] [blame] | 1098 | if not isinstance(filename, list): |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 1099 | filename = [filename] |
Patrick Williams | 55448ad | 2020-12-14 14:28:28 -0600 | [diff] [blame] | 1100 | |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1101 | filepaths = [] |
| 1102 | for root, dirs, files in os.walk(basedir): |
Brad Bishop | eb66726 | 2021-08-06 13:54:58 -0400 | [diff] [blame] | 1103 | if os.path.split(root)[-1] == 'subprojects': |
| 1104 | for f in files: |
| 1105 | subproject = '.'.join(f.split('.')[0:-1]) |
| 1106 | if f.endswith('.wrap') and subproject in dirs: |
| 1107 | # don't find files in meson subprojects with wraps |
| 1108 | dirs.remove(subproject) |
Patrick Williams | 55448ad | 2020-12-14 14:28:28 -0600 | [diff] [blame] | 1109 | for f in filename: |
| 1110 | if f in files: |
| 1111 | filepaths.append(os.path.join(root, f)) |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1112 | return filepaths |
| 1113 | |
Andrew Jeffery | 9cc97d8 | 2020-03-13 11:57:44 +1030 | [diff] [blame] | 1114 | |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 1115 | if __name__ == '__main__': |
| 1116 | # CONFIGURE_FLAGS = [GIT REPO]:[CONFIGURE FLAGS] |
| 1117 | CONFIGURE_FLAGS = { |
Matthew Barth | 1d1c673 | 2017-03-24 10:00:28 -0500 | [diff] [blame] | 1118 | 'phosphor-logging': |
Matt Spinler | 0744bb8 | 2020-01-16 08:23:35 -0600 | [diff] [blame] | 1119 | ['--enable-metadata-processing', '--enable-openpower-pel-extension', |
Deepak Kodihalli | 3a4e1b4 | 2017-06-08 09:52:35 -0500 | [diff] [blame] | 1120 | 'YAML_DIR=/usr/local/share/phosphor-dbus-yaml/yaml'] |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 1121 | } |
| 1122 | |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 1123 | # MESON_FLAGS = [GIT REPO]:[MESON FLAGS] |
| 1124 | MESON_FLAGS = { |
Matt Spinler | 8448cfd | 2020-09-10 17:01:48 -0500 | [diff] [blame] | 1125 | 'phosphor-dbus-interfaces': |
Patrick Williams | 13af98a | 2021-04-21 15:17:00 -0500 | [diff] [blame] | 1126 | ['-Ddata_com_ibm=true', '-Ddata_org_open_power=true'], |
| 1127 | 'phosphor-logging': |
| 1128 | ['-Dopenpower-pel-extension=enabled'] |
William A. Kennington III | 3f1d120 | 2018-12-06 18:02:07 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 1131 | # DEPENDENCIES = [MACRO]:[library/header]:[GIT REPO] |
| 1132 | DEPENDENCIES = { |
| 1133 | 'AC_CHECK_LIB': {'mapper': 'phosphor-objmgr'}, |
Matthew Barth | 710f3f0 | 2017-01-18 15:20:19 -0600 | [diff] [blame] | 1134 | 'AC_CHECK_HEADER': { |
| 1135 | 'host-ipmid': 'phosphor-host-ipmid', |
Patrick Venture | b41a446 | 2018-10-03 17:27:38 -0700 | [diff] [blame] | 1136 | 'blobs-ipmid': 'phosphor-ipmi-blobs', |
Matthew Barth | 710f3f0 | 2017-01-18 15:20:19 -0600 | [diff] [blame] | 1137 | 'sdbusplus': 'sdbusplus', |
William A. Kennington III | b4f730a | 2018-09-12 11:21:20 -0700 | [diff] [blame] | 1138 | 'sdeventplus': 'sdeventplus', |
William A. Kennington III | 2370524 | 2019-01-15 18:17:25 -0800 | [diff] [blame] | 1139 | 'stdplus': 'stdplus', |
Patrick Venture | 2232996 | 2018-09-14 10:23:04 -0700 | [diff] [blame] | 1140 | 'gpioplus': 'gpioplus', |
Saqib Khan | 6614505 | 2017-02-14 12:02:07 -0600 | [diff] [blame] | 1141 | 'phosphor-logging/log.hpp': 'phosphor-logging', |
Patrick Williams | eab8a37 | 2017-01-30 11:21:32 -0600 | [diff] [blame] | 1142 | }, |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 1143 | 'AC_PATH_PROG': {'sdbus++': 'sdbusplus'}, |
Patrick Williams | eab8a37 | 2017-01-30 11:21:32 -0600 | [diff] [blame] | 1144 | 'PKG_CHECK_MODULES': { |
Matthew Barth | 19e261e | 2017-02-01 12:55:22 -0600 | [diff] [blame] | 1145 | 'phosphor-dbus-interfaces': 'phosphor-dbus-interfaces', |
William A. Kennington III | 993ab33 | 2019-02-07 02:12:31 -0800 | [diff] [blame] | 1146 | 'libipmid': 'phosphor-host-ipmid', |
| 1147 | 'libipmid-host': 'phosphor-host-ipmid', |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 1148 | 'sdbusplus': 'sdbusplus', |
William A. Kennington III | b4f730a | 2018-09-12 11:21:20 -0700 | [diff] [blame] | 1149 | 'sdeventplus': 'sdeventplus', |
William A. Kennington III | 2370524 | 2019-01-15 18:17:25 -0800 | [diff] [blame] | 1150 | 'stdplus': 'stdplus', |
Patrick Venture | 2232996 | 2018-09-14 10:23:04 -0700 | [diff] [blame] | 1151 | 'gpioplus': 'gpioplus', |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 1152 | 'phosphor-logging': 'phosphor-logging', |
Marri Devender Rao | a3eee8a | 2018-08-13 05:34:27 -0500 | [diff] [blame] | 1153 | 'phosphor-snmp': 'phosphor-snmp', |
Patrick Venture | e584c3b | 2019-03-07 09:44:16 -0800 | [diff] [blame] | 1154 | 'ipmiblob': 'ipmi-blob-tool', |
Ben Tyner | 300ed3c | 2020-05-14 15:30:37 -0500 | [diff] [blame] | 1155 | 'hei': 'openpower-libhei', |
Sui Chen | 40cf521 | 2020-11-24 12:12:23 -0800 | [diff] [blame] | 1156 | 'phosphor-ipmi-blobs': 'phosphor-ipmi-blobs', |
Willy Tu | 01534ec | 2021-10-14 10:16:45 -0700 | [diff] [blame] | 1157 | 'libcr51sign': 'google-misc', |
Brad Bishop | ebb4911 | 2017-02-13 11:07:26 -0500 | [diff] [blame] | 1158 | }, |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 1159 | } |
| 1160 | |
William A. Kennington III | e67f5fc | 2018-12-06 17:40:30 -0800 | [diff] [blame] | 1161 | # Offset into array of macro parameters MACRO(0, 1, ...N) |
| 1162 | DEPENDENCIES_OFFSET = { |
| 1163 | 'AC_CHECK_LIB': 0, |
| 1164 | 'AC_CHECK_HEADER': 0, |
| 1165 | 'AC_PATH_PROG': 1, |
| 1166 | 'PKG_CHECK_MODULES': 1, |
| 1167 | } |
| 1168 | |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 1169 | # DEPENDENCIES_REGEX = [GIT REPO]:[REGEX STRING] |
| 1170 | DEPENDENCIES_REGEX = { |
Patrick Venture | d37b805 | 2018-10-16 16:03:03 -0700 | [diff] [blame] | 1171 | 'phosphor-logging': r'\S+-dbus-interfaces$' |
Leonel Gonzalez | a62a1a1 | 2017-03-24 11:03:47 -0500 | [diff] [blame] | 1172 | } |
| 1173 | |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 1174 | # Set command line arguments |
| 1175 | parser = argparse.ArgumentParser() |
| 1176 | parser.add_argument("-w", "--workspace", dest="WORKSPACE", required=True, |
| 1177 | help="Workspace directory location(i.e. /home)") |
| 1178 | parser.add_argument("-p", "--package", dest="PACKAGE", required=True, |
| 1179 | help="OpenBMC package to be unit tested") |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 1180 | parser.add_argument("-t", "--test-only", dest="TEST_ONLY", |
| 1181 | action="store_true", required=False, default=False, |
| 1182 | help="Only run test cases, no other validation") |
Ramin Izadpanah | 298d56b | 2020-11-17 16:55:28 +0000 | [diff] [blame] | 1183 | arg_inttests = parser.add_mutually_exclusive_group() |
| 1184 | arg_inttests.add_argument("--integration-tests", dest="INTEGRATION_TEST", |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 1185 | action="store_true", required=False, default=True, |
| 1186 | help="Enable integration tests [default].") |
Ramin Izadpanah | 298d56b | 2020-11-17 16:55:28 +0000 | [diff] [blame] | 1187 | arg_inttests.add_argument("--no-integration-tests", dest="INTEGRATION_TEST", |
Lei YU | 08d2b92 | 2022-04-25 11:21:36 +0800 | [diff] [blame^] | 1188 | action="store_false", required=False, |
| 1189 | help="Disable integration tests.") |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 1190 | parser.add_argument("-v", "--verbose", action="store_true", |
| 1191 | help="Print additional package status messages") |
Andrew Jeffery | 468309d | 2018-03-08 13:46:33 +1030 | [diff] [blame] | 1192 | parser.add_argument("-r", "--repeat", help="Repeat tests N times", |
| 1193 | type=int, default=1) |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 1194 | parser.add_argument("-b", "--branch", dest="BRANCH", required=False, |
| 1195 | help="Branch to target for dependent repositories", |
| 1196 | default="master") |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 1197 | parser.add_argument("-n", "--noformat", dest="FORMAT", |
| 1198 | action="store_false", required=False, |
| 1199 | help="Whether or not to run format code") |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 1200 | args = parser.parse_args(sys.argv[1:]) |
| 1201 | WORKSPACE = args.WORKSPACE |
| 1202 | UNIT_TEST_PKG = args.PACKAGE |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 1203 | TEST_ONLY = args.TEST_ONLY |
Ramin Izadpanah | 298d56b | 2020-11-17 16:55:28 +0000 | [diff] [blame] | 1204 | INTEGRATION_TEST = args.INTEGRATION_TEST |
Andrew Geissler | a61acb5 | 2019-01-03 16:32:44 -0600 | [diff] [blame] | 1205 | BRANCH = args.BRANCH |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 1206 | FORMAT_CODE = args.FORMAT |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 1207 | if args.verbose: |
| 1208 | def printline(*line): |
| 1209 | for arg in line: |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 1210 | print(arg, end=' ') |
| 1211 | print() |
Matthew Barth | 33df879 | 2016-12-19 14:30:17 -0600 | [diff] [blame] | 1212 | else: |
Andrew Jeffery | fce557f | 2020-03-13 12:12:19 +1030 | [diff] [blame] | 1213 | def printline(*line): |
| 1214 | pass |
Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame] | 1215 | |
Patrick Williams | b653595 | 2020-12-15 06:40:10 -0600 | [diff] [blame] | 1216 | CODE_SCAN_DIR = os.path.join(WORKSPACE, UNIT_TEST_PKG) |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 1217 | |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 1218 | # First validate code formatting if repo has style formatting files. |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 1219 | # The format-code.sh checks for these files. |
Lei YU | 7ef9330 | 2019-11-06 13:53:21 +0800 | [diff] [blame] | 1220 | if FORMAT_CODE: |
Patrick Williams | 5b4a1e8 | 2020-12-15 06:46:31 -0600 | [diff] [blame] | 1221 | format_scripts = find_file(['format-code.sh', 'format-code'], |
| 1222 | CODE_SCAN_DIR) |
Andrew Geissler | 31502dd | 2021-01-07 14:04:02 -0600 | [diff] [blame] | 1223 | |
| 1224 | # use default format-code.sh if no other found |
| 1225 | if not format_scripts: |
| 1226 | format_scripts.append(os.path.join(WORKSPACE, "format-code.sh")) |
| 1227 | |
Patrick Williams | 5b4a1e8 | 2020-12-15 06:46:31 -0600 | [diff] [blame] | 1228 | for f in format_scripts: |
| 1229 | check_call_cmd(f, CODE_SCAN_DIR) |
Andrew Geissler | a28286d | 2018-01-10 11:00:00 -0800 | [diff] [blame] | 1230 | |
Ed Tanous | 32768b8 | 2022-01-05 14:14:06 -0800 | [diff] [blame] | 1231 | # Check to see if any files changed |
| 1232 | check_call_cmd("git", "-C", CODE_SCAN_DIR, |
| 1233 | "--no-pager", "diff", "--exit-code") |
| 1234 | |
Andrew Geissler | 8cb74fc | 2020-03-19 14:48:05 -0500 | [diff] [blame] | 1235 | # Check if this repo has a supported make infrastructure |
Patrick Williams | b653595 | 2020-12-15 06:40:10 -0600 | [diff] [blame] | 1236 | pkg = Package(UNIT_TEST_PKG, CODE_SCAN_DIR) |
Andrew Geissler | 8cb74fc | 2020-03-19 14:48:05 -0500 | [diff] [blame] | 1237 | if not pkg.build_system(): |
| 1238 | print("No valid build system, exit") |
| 1239 | sys.exit(0) |
| 1240 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1241 | prev_umask = os.umask(000) |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 1242 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1243 | # Determine dependencies and add them |
| 1244 | dep_added = dict() |
| 1245 | dep_added[UNIT_TEST_PKG] = False |
William A. Kennington III | 40d5c7c | 2018-12-13 14:37:59 -0800 | [diff] [blame] | 1246 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1247 | # Create dependency tree |
| 1248 | dep_tree = DepTree(UNIT_TEST_PKG) |
Patrick Williams | b653595 | 2020-12-15 06:40:10 -0600 | [diff] [blame] | 1249 | build_dep_tree(UNIT_TEST_PKG, CODE_SCAN_DIR, dep_added, dep_tree, BRANCH) |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 1250 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1251 | # Reorder Dependency Tree |
Andrew Jeffery | 89b64b6 | 2020-03-13 12:15:48 +1030 | [diff] [blame] | 1252 | for pkg_name, regex_str in DEPENDENCIES_REGEX.items(): |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1253 | dep_tree.ReorderDeps(pkg_name, regex_str) |
| 1254 | if args.verbose: |
| 1255 | dep_tree.PrintTree() |
William A. Kennington III | 65b37fa | 2019-01-31 15:15:17 -0800 | [diff] [blame] | 1256 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1257 | install_list = dep_tree.GetInstallList() |
Andrew Geissler | 9ced4ed | 2019-11-18 14:33:58 -0600 | [diff] [blame] | 1258 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1259 | # We don't want to treat our package as a dependency |
| 1260 | install_list.remove(UNIT_TEST_PKG) |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 1261 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1262 | # Install reordered dependencies |
| 1263 | for dep in install_list: |
| 1264 | build_and_install(dep, False) |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 1265 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1266 | # Run package unit tests |
| 1267 | build_and_install(UNIT_TEST_PKG, True) |
James Feist | 878df5c | 2018-07-26 14:54:28 -0700 | [diff] [blame] | 1268 | |
Andrew Jeffery | 15e423e | 2020-03-11 16:51:28 +1030 | [diff] [blame] | 1269 | os.umask(prev_umask) |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1270 | |
| 1271 | # Run any custom CI scripts the repo has, of which there can be |
| 1272 | # multiple of and anywhere in the repository. |
Patrick Williams | b653595 | 2020-12-15 06:40:10 -0600 | [diff] [blame] | 1273 | ci_scripts = find_file(['run-ci.sh', 'run-ci'], CODE_SCAN_DIR) |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1274 | if ci_scripts: |
Patrick Williams | b653595 | 2020-12-15 06:40:10 -0600 | [diff] [blame] | 1275 | os.chdir(CODE_SCAN_DIR) |
Matt Spinler | 9bfaaad | 2019-10-25 09:51:50 -0500 | [diff] [blame] | 1276 | for ci_script in ci_scripts: |
Patrick Williams | 55448ad | 2020-12-14 14:28:28 -0600 | [diff] [blame] | 1277 | check_call_cmd(ci_script) |