|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * llvm-lit wrapper |
| 5 | + * |
| 6 | + * To use, set unit.engine in .arcconfig, or use --engine flag |
| 7 | + * with arc unit. |
| 8 | + * |
| 9 | + * This file was authored by Clemens Hammacher <[email protected]> |
| 10 | + * and initially used in the Sambamba project (www.sambamba.org). |
| 11 | + * |
| 12 | + * @group unitrun |
| 13 | + */ |
| 14 | +final class LitTestEngine extends ArcanistUnitTestEngine { |
| 15 | + |
| 16 | + protected function supportsRunAllTests() { |
| 17 | + return true; |
| 18 | + } |
| 19 | + |
| 20 | + private function progress($results, $numTests) { |
| 21 | + static $colors = array( |
| 22 | + ArcanistUnitTestResult::RESULT_PASS => 'green', |
| 23 | + ArcanistUnitTestResult::RESULT_FAIL => 'red', |
| 24 | + ArcanistUnitTestResult::RESULT_SKIP => 'yellow', |
| 25 | + ArcanistUnitTestResult::RESULT_BROKEN => 'red', |
| 26 | + ArcanistUnitTestResult::RESULT_UNSOUND => 'yellow', |
| 27 | + ArcanistUnitTestResult::RESULT_POSTPONED => 'yellow' |
| 28 | + ); |
| 29 | + |
| 30 | + $s = "["; |
| 31 | + $lastColor = ""; |
| 32 | + foreach ($results as $result) { |
| 33 | + $color = $colors[$result->getResult()]; |
| 34 | + if (!$color && $lastColor) |
| 35 | + $s .= "</bg>"; |
| 36 | + elseif (!$lastColor && $color) |
| 37 | + $s .= "<bg:$color>"; |
| 38 | + elseif ($lastColor !== $color) |
| 39 | + $s .= "</bg><bg:$color>"; |
| 40 | + $lastColor = $color; |
| 41 | + $s .= "."; |
| 42 | + } |
| 43 | + if ($lastColor) |
| 44 | + $s .= "</bg>"; |
| 45 | + $c = count($results); |
| 46 | + if ($numTests) |
| 47 | + $s .= str_repeat(" ", $numTests - $c) . " $c/$numTests]"; |
| 48 | + else |
| 49 | + $s .= " $c]"; |
| 50 | + return phutil_console_format($s); |
| 51 | + } |
| 52 | + |
| 53 | + public function run() { |
| 54 | + |
| 55 | + $projectRoot = $this->getWorkingCopy()->getProjectRoot(); |
| 56 | + $cwd = getcwd(); |
| 57 | + $buildDir = $this->findBuildDirectory($projectRoot, $cwd); |
| 58 | + print "Using build directory '$buildDir'\n"; |
| 59 | + $makeVars = $this->getMakeVars($buildDir); |
| 60 | + $lit = $this->findLitExecutable($makeVars); |
| 61 | + print "Using lit executable '$lit'\n"; |
| 62 | + |
| 63 | + // We have to modify the format string, because llvm-lit does not like a '' argument |
| 64 | + $cmd = '%s ' . ($this->getEnableAsyncTests() ? '' : '-j1 ') .'%s 2>&1'; |
| 65 | + $litFuture = new ExecFuture($cmd, $lit, $buildDir."/test"); |
| 66 | + $out = ""; |
| 67 | + $results = array(); |
| 68 | + $lastTime = microtime(true); |
| 69 | + $ready = false; |
| 70 | + $dots = ""; |
| 71 | + $numTests = 0; |
| 72 | + while (!$ready) { |
| 73 | + $ready = $litFuture->isReady(); |
| 74 | + $newout = $litFuture->readStdout(); |
| 75 | + if (strlen($newout) == 0) { |
| 76 | + usleep(100); |
| 77 | + continue; |
| 78 | + } |
| 79 | + $out .= $newout; |
| 80 | + if ($ready && strlen($out) > 0 && substr($out, -1) != "\n") |
| 81 | + $out .= "\n"; |
| 82 | + |
| 83 | + while (($nlPos = strpos($out, "\n")) !== FALSE) { |
| 84 | + $line = substr($out, 0, $nlPos+1); |
| 85 | + $out = substr($out, $nlPos+1); |
| 86 | + |
| 87 | + $res = ArcanistUnitTestResult::RESULT_UNSOUND; |
| 88 | + if (substr($line, 0, 6) == "PASS: ") { |
| 89 | + $res = ArcanistUnitTestResult::RESULT_PASS; |
| 90 | + } elseif (substr($line, 0, 6) == "FAIL: ") { |
| 91 | + $res = ArcanistUnitTestResult::RESULT_FAIL; |
| 92 | + } elseif (substr($line, 0, 7) == "XPASS: ") { |
| 93 | + $res = ArcanistUnitTestResult::RESULT_FAIL; |
| 94 | + } elseif (substr($line, 0, 7) == "XFAIL: ") { |
| 95 | + $res = ArcanistUnitTestResult::RESULT_PASS; |
| 96 | + } elseif (substr($line, 0, 13) == "UNSUPPORTED: ") { |
| 97 | + $res = ArcanistUnitTestResult::RESULT_SKIP; |
| 98 | + } elseif (!$numTests && preg_match('/Testing: ([0-9]+) tests/', $line, $matches)) { |
| 99 | + $numTests = (int)$matches[1]; |
| 100 | + } |
| 101 | + if ($res == ArcanistUnitTestResult::RESULT_FAIL) |
| 102 | + print "\033[1A"; |
| 103 | + if ($res != ArcanistUnitTestResult::RESULT_SKIP && $res != ArcanistUnitTestResult::RESULT_PASS) |
| 104 | + print "\r\033[K\033[1A".$line.self::progress($results, $numTests); |
| 105 | + if ($res == ArcanistUnitTestResult::RESULT_UNSOUND) |
| 106 | + continue; |
| 107 | + $result = new ArcanistUnitTestResult(); |
| 108 | + $result->setName(trim(substr($line, strpos($line, ':') + 1))); |
| 109 | + $result->setResult($res); |
| 110 | + $newTime = microtime(true); |
| 111 | + $result->setDuration($newTime - $lastTime); |
| 112 | + $lastTime = $newTime; |
| 113 | + $results[] = $result; |
| 114 | + $dots .= "."; |
| 115 | + print "\r\033[K\033[1A".self::progress($results, $numTests); |
| 116 | + } |
| 117 | + } |
| 118 | + list ($out1,$out2) = $litFuture->read(); |
| 119 | + print $out1; |
| 120 | + if ($out2) { |
| 121 | + throw new Exception('There was error output, even though it should have been redirected to stdout.'); |
| 122 | + } |
| 123 | + print "\n"; |
| 124 | + |
| 125 | + return $results; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Try to find the build directory of the project, starting from the |
| 130 | + * project root, and the current working directory. |
| 131 | + * Also, the environment variable POLLY_BIN_DIR is read to determine the |
| 132 | + * correct location. |
| 133 | + * |
| 134 | + * Search locations are: |
| 135 | + * $POLLY_BIN_DIR (environment variable) |
| 136 | + * <root>/build |
| 137 | + * <root>.build |
| 138 | + * <root>-build |
| 139 | + * <root:s/src/build> |
| 140 | + * <cwd> |
| 141 | + * |
| 142 | + * This list might be extended in the future according to other common |
| 143 | + * setup. |
| 144 | + * |
| 145 | + * @param projectRoot Directory of the project root |
| 146 | + * @param cwd Current working directory |
| 147 | + * @return string Presumable build directory |
| 148 | + */ |
| 149 | + public static function findBuildDirectory($projectRoot, $cwd) { |
| 150 | + |
| 151 | + $projectRoot = rtrim($projectRoot, DIRECTORY_SEPARATOR); |
| 152 | + $cwd = rtrim($cwd, DIRECTORY_SEPARATOR); |
| 153 | + |
| 154 | + $tries = array(); |
| 155 | + |
| 156 | + $smbbin_env = getenv("POLLY_BIN_DIR"); |
| 157 | + if ($smbbin_env) |
| 158 | + $tries[] = $smbbin_env; |
| 159 | + |
| 160 | + $tries[] = $projectRoot.DIRECTORY_SEPARATOR."build"; |
| 161 | + $tries[] = $projectRoot.".build"; |
| 162 | + $tries[] = $projectRoot."-build"; |
| 163 | + |
| 164 | + // Try to replace each occurence of "src" by "build" (also within path components, like llvm-src) |
| 165 | + $srcPos = 0; |
| 166 | + while (($srcPos = strpos($projectRoot, "src", $srcPos)) !== FALSE) { |
| 167 | + $tries[] = substr($projectRoot, 0, $srcPos)."build".substr($projectRoot, $srcPos+3); |
| 168 | + $srcPos += 3; |
| 169 | + } |
| 170 | + |
| 171 | + $tries[] = $cwd; |
| 172 | + |
| 173 | + foreach ($tries as $try) { |
| 174 | + if (is_dir($try) && |
| 175 | + file_exists($try.DIRECTORY_SEPARATOR."Makefile") && |
| 176 | + (file_exists($try.DIRECTORY_SEPARATOR."test".DIRECTORY_SEPARATOR."lit.site.cfg") || |
| 177 | + file_exists($try.DIRECTORY_SEPARATOR."test".DIRECTORY_SEPARATOR."lit.cfg"))) |
| 178 | + return Filesystem::resolvePath($try); |
| 179 | + } |
| 180 | + |
| 181 | + throw new Exception("Did not find a build directory for project '$projectRoot', cwd '$cwd'.\n" . |
| 182 | + "Make sure to have a 'test' directory inside build, which contains lit.cfg or lit.site.cfg.\n" . |
| 183 | + "You might have to run 'make test' once in the build directory."); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Try to find the llvm build directory, based on the make variables |
| 188 | + * as determined by getMakeVars(). |
| 189 | + * |
| 190 | + * @param makeVars The determined make variables |
| 191 | + * @return string Presumable llvm build directory |
| 192 | + */ |
| 193 | + public static function getLLVMObjDir($makeVars) { |
| 194 | + if (!array_key_exists('LLVM_OBJ_ROOT', $makeVars)) |
| 195 | + throw new Exception("Make variables (determined by 'make printvars') does not contain LLVM_OBJ_ROOT."); |
| 196 | + $llvmObjDir = $makeVars['LLVM_OBJ_ROOT']; |
| 197 | + if (!is_dir($llvmObjDir)) |
| 198 | + throw new Exception("LLVM_OBJ_ROOT ('$llvmObjDir') is no directory."); |
| 199 | + return $llvmObjDir; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Determine the make variables, by calling 'make printvars' in the |
| 204 | + * build directory. |
| 205 | + * |
| 206 | + * @param buildDir The determined build directory |
| 207 | + * @return map<str,str> Map of printed make variables to values |
| 208 | + */ |
| 209 | + public static function getMakeVars($buildDir) { |
| 210 | + $litFuture = new ExecFuture('make -C %s printvars', $buildDir); |
| 211 | + list($stdout, $stderr) = $litFuture->resolvex(10); |
| 212 | + print $stderr; |
| 213 | + $makeVars = array(); |
| 214 | + |
| 215 | + foreach (explode("\n", $stdout) as $line) { |
| 216 | + $components = explode(':', $line); |
| 217 | + if (count($components) == 3) |
| 218 | + $makeVars[trim($components[1])] = trim($components[2]); |
| 219 | + } |
| 220 | + |
| 221 | + return $makeVars; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Return full path to the llvm-lit executable. |
| 226 | + * |
| 227 | + * @param llvmObjDir The determined llvm build directory |
| 228 | + * @return string Full path to the llvm-lit executable |
| 229 | + */ |
| 230 | + public static function findLitExecutable($makeVars) { |
| 231 | + $llvmObjDir = self::getLLVMObjDir($makeVars); |
| 232 | + $buildMode = array_key_exists('BuildMode', $makeVars) ? $makeVars['BuildMode'] : ''; |
| 233 | + |
| 234 | + if (!$buildMode) |
| 235 | + throw new Exception("Make variables (determined by 'make printvars') does not contain BuildMode."); |
| 236 | + |
| 237 | + $lit = $llvmObjDir.DIRECTORY_SEPARATOR.$buildMode.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'llvm-lit'; |
| 238 | + if (!is_executable($lit)) |
| 239 | + throw new Exception("File does not exists or is not executable: $lit"); |
| 240 | + return $lit; |
| 241 | + } |
| 242 | +} |
0 commit comments