|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +from utils import compute_patch_hash |
| 5 | +from utils import log |
| 6 | + |
| 7 | + |
| 8 | +def is_configured_build(build_dir): |
| 9 | + return os.path.exists(os.path.join(build_dir, "build.ninja")) or os.path.exists( |
| 10 | + os.path.join(build_dir, "CMakeCache.txt") |
| 11 | + ) |
| 12 | + |
| 13 | + |
| 14 | +def configure_llvm_build(build_dir): |
| 15 | + print(f"[patch-coverage] Configuring LLVM build in {build_dir}...") |
| 16 | + |
| 17 | + subprocess.check_call( |
| 18 | + [ |
| 19 | + "cmake", |
| 20 | + "-G", |
| 21 | + "Ninja", |
| 22 | + "-S", |
| 23 | + "llvm", |
| 24 | + "-B", |
| 25 | + build_dir, |
| 26 | + "-DCMAKE_BUILD_TYPE=Release", |
| 27 | + "-DLLVM_ENABLE_ASSERTIONS=ON", |
| 28 | + "-DLLVM_INCLUDE_TESTS=ON", |
| 29 | + "-DLLVM_BUILD_TESTS=ON", |
| 30 | + ] |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def ensure_llvm_tools(build_dir): |
| 35 | + if not is_configured_build(build_dir): |
| 36 | + configure_llvm_build(build_dir) |
| 37 | + |
| 38 | + required_tools = [ |
| 39 | + "bin/llvm-lit", |
| 40 | + "bin/FileCheck", |
| 41 | + "bin/count", |
| 42 | + "bin/not", |
| 43 | + "bin/llvm-config", |
| 44 | + ] |
| 45 | + |
| 46 | + missing = [ |
| 47 | + tool |
| 48 | + for tool in required_tools |
| 49 | + if not os.path.exists(os.path.join(build_dir, tool)) |
| 50 | + ] |
| 51 | + |
| 52 | + if not missing: |
| 53 | + return |
| 54 | + |
| 55 | + subprocess.check_call( |
| 56 | + [ |
| 57 | + "ninja", |
| 58 | + "-C", |
| 59 | + build_dir, |
| 60 | + "FileCheck", |
| 61 | + "count", |
| 62 | + "not", |
| 63 | + "llvm-config", |
| 64 | + ] |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def should_rebuild(build_dir, patch_path): |
| 69 | + new_hash = compute_patch_hash(patch_path) |
| 70 | + hash_file = os.path.join(build_dir, ".last_patch_hash") |
| 71 | + |
| 72 | + if os.path.exists(hash_file): |
| 73 | + old_hash = open(hash_file).read().strip() |
| 74 | + if old_hash == new_hash: |
| 75 | + return False |
| 76 | + |
| 77 | + with open(hash_file, "w") as f: |
| 78 | + f.write(new_hash) |
| 79 | + |
| 80 | + return True |
| 81 | + |
| 82 | + |
| 83 | +def build_llvm(build_dir, binary): |
| 84 | + try: |
| 85 | + |
| 86 | + cwd = os.getcwd() |
| 87 | + |
| 88 | + os.chdir(build_dir) |
| 89 | + |
| 90 | + command = 'find . -type f -name "*.profraw" -delete' |
| 91 | + |
| 92 | + try: |
| 93 | + subprocess.run(command, shell=True, check=True) |
| 94 | + log( |
| 95 | + "Files in build directory with '.profraw' extension deleted successfully." |
| 96 | + ) |
| 97 | + except subprocess.CalledProcessError as e: |
| 98 | + log(f"Error: {e}") |
| 99 | + log("") |
| 100 | + |
| 101 | + needs_cmake = True |
| 102 | + cache_path = os.path.join(build_dir, "CMakeCache.txt") |
| 103 | + if os.path.exists(cache_path): |
| 104 | + with open(cache_path, "r") as f: |
| 105 | + if "LLVM_BUILD_INSTRUMENTED_COVERAGE:BOOL=ON" in f.read(): |
| 106 | + needs_cmake = False |
| 107 | + |
| 108 | + if needs_cmake: |
| 109 | + cmake_command = [ |
| 110 | + "cmake", |
| 111 | + "-DLLVM_BUILD_INSTRUMENTED_COVERAGE=ON", |
| 112 | + "-DLLVM_INDIVIDUAL_TEST_COVERAGE=ON", |
| 113 | + f"-DCMAKE_C_FLAGS=-fprofile-list={os.path.abspath('fun.list')}", |
| 114 | + f"-DCMAKE_CXX_FLAGS=-fprofile-list={os.path.abspath('fun.list')}", |
| 115 | + ".", |
| 116 | + ] |
| 117 | + |
| 118 | + subprocess.check_call(cmake_command) |
| 119 | + |
| 120 | + target_name = os.path.basename(binary) |
| 121 | + try: |
| 122 | + print() |
| 123 | + subprocess.check_call( |
| 124 | + [ |
| 125 | + "ninja", |
| 126 | + "FileCheck", |
| 127 | + "count", |
| 128 | + "not", |
| 129 | + "llvm-config", |
| 130 | + "llvm-cov", |
| 131 | + "llvm-profdata", |
| 132 | + "llvm-dwarfdump", |
| 133 | + "llvm-readobj", |
| 134 | + target_name, |
| 135 | + ] |
| 136 | + ) |
| 137 | + |
| 138 | + except subprocess.CalledProcessError as ninja_error: |
| 139 | + log(f"Error during Ninja build: {ninja_error}") |
| 140 | + log("Attempting to build with 'make' using the available processors.") |
| 141 | + num_processors = os.cpu_count() or 1 |
| 142 | + make_command = ["make", f"-j{num_processors}"] |
| 143 | + subprocess.check_call( |
| 144 | + make_command, |
| 145 | + "FileCheck", |
| 146 | + "count", |
| 147 | + "not", |
| 148 | + "llvm-config", |
| 149 | + "llvm-cov", |
| 150 | + "llvm-profdata", |
| 151 | + "llvm-dwarfdump", |
| 152 | + "llvm-readobj", |
| 153 | + target_name, |
| 154 | + ) |
| 155 | + |
| 156 | + os.chdir(cwd) |
| 157 | + |
| 158 | + log("LLVM build completed successfully.") |
| 159 | + log("") |
| 160 | + |
| 161 | + except subprocess.CalledProcessError as e: |
| 162 | + log("Error during LLVM build:", e) |
| 163 | + sys.exit(1) |
0 commit comments