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