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