| 1 |
#!/usr/bin/python3 |
| 2 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 |
|
| 6 |
# Only necessary for flake8 to be happy... |
| 7 |
import argparse |
| 8 |
import errno |
| 9 |
import fnmatch |
| 10 |
import glob |
| 11 |
import json |
| 12 |
import os |
| 13 |
import os.path |
| 14 |
import platform |
| 15 |
import re |
| 16 |
import shutil |
| 17 |
import subprocess |
| 18 |
import sys |
| 19 |
import tarfile |
| 20 |
from contextlib import contextmanager |
| 21 |
from shutil import which |
| 22 |
|
| 23 |
import zstandard |
| 24 |
|
| 25 |
SUPPORTED_TARGETS = { |
| 26 |
"x86_64-unknown-linux-gnu": ("Linux", "x86_64"), |
| 27 |
"aarch64-unknown-linux-gnu": ("Linux", "aarch64"), |
| 28 |
"x86_64-pc-windows-msvc": ("Windows", "AMD64"), |
| 29 |
"aarch64-pc-windows-msvc": ("Windows", "ARM64"), |
| 30 |
"x86_64-apple-darwin": ("Darwin", "x86_64"), |
| 31 |
"aarch64-apple-darwin": ("Darwin", "arm64"), |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
def is_llvm_toolchain(cc, cxx): |
| 36 |
return "clang" in cc and "clang" in cxx |
| 37 |
|
| 38 |
|
| 39 |
def check_run(args): |
| 40 |
print(" ".join(args), file=sys.stderr, flush=True) |
| 41 |
if args[0] == "cmake": |
| 42 |
# CMake `message(STATUS)` messages, as appearing in failed source code |
| 43 |
# compiles, appear on stdout, so we only capture that. |
| 44 |
p = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 45 |
lines = [] |
| 46 |
for line in p.stdout: |
| 47 |
lines.append(line) |
| 48 |
sys.stdout.write(line.decode()) |
| 49 |
sys.stdout.flush() |
| 50 |
r = p.wait() |
| 51 |
if r != 0 and os.environ.get("UPLOAD_DIR"): |
| 52 |
cmake_output_re = re.compile(b'See also "(.*/CMakeOutput.log)"') |
| 53 |
cmake_error_re = re.compile(b'See also "(.*/CMakeError.log)"') |
| 54 |
|
| 55 |
def find_first_match(re): |
| 56 |
for l in lines: |
| 57 |
match = re.search(l) |
| 58 |
if match: |
| 59 |
return match |
| 60 |
|
| 61 |
output_match = find_first_match(cmake_output_re) |
| 62 |
error_match = find_first_match(cmake_error_re) |
| 63 |
|
| 64 |
upload_dir = os.environ["UPLOAD_DIR"].encode("utf-8") |
| 65 |
if output_match or error_match: |
| 66 |
mkdir_p(upload_dir) |
| 67 |
if output_match: |
| 68 |
shutil.copy2(output_match.group(1), upload_dir) |
| 69 |
if error_match: |
| 70 |
shutil.copy2(error_match.group(1), upload_dir) |
| 71 |
else: |
| 72 |
r = subprocess.call(args) |
| 73 |
assert r == 0 |
| 74 |
|
| 75 |
|
| 76 |
def run_in(path, args): |
| 77 |
with chdir(path): |
| 78 |
check_run(args) |
| 79 |
|
| 80 |
|
| 81 |
@contextmanager |
| 82 |
def chdir(path): |
| 83 |
d = os.getcwd() |
| 84 |
print('cd "%s"' % path, file=sys.stderr) |
| 85 |
os.chdir(path) |
| 86 |
try: |
| 87 |
yield |
| 88 |
finally: |
| 89 |
print('cd "%s"' % d, file=sys.stderr) |
| 90 |
os.chdir(d) |
| 91 |
|
| 92 |
|
| 93 |
def patch(patch, srcdir): |
| 94 |
patch = os.path.realpath(patch) |
| 95 |
check_run(["patch", "-d", srcdir, "-p1", "-i", patch, "--fuzz=0", "-s"]) |
| 96 |
|
| 97 |
|
| 98 |
def import_clang_tidy(source_dir, build_clang_tidy_alpha, build_clang_tidy_external): |
| 99 |
clang_plugin_path = os.path.join(os.path.dirname(sys.argv[0]), "..", "clang-plugin") |
| 100 |
clang_tidy_path = os.path.join(source_dir, "clang-tools-extra/clang-tidy") |
| 101 |
sys.path.append(clang_plugin_path) |
| 102 |
from import_mozilla_checks import do_import |
| 103 |
|
| 104 |
import_options = { |
| 105 |
"alpha": build_clang_tidy_alpha, |
| 106 |
"external": build_clang_tidy_external, |
| 107 |
} |
| 108 |
do_import(clang_plugin_path, clang_tidy_path, import_options) |
| 109 |
|
| 110 |
|
| 111 |
def build_package(package_build_dir, cmake_args): |
| 112 |
if not os.path.exists(package_build_dir): |
| 113 |
os.mkdir(package_build_dir) |
| 114 |
# If CMake has already been run, it may have been run with different |
| 115 |
# arguments, so we need to re-run it. Make sure the cached copy of the |
| 116 |
# previous CMake run is cleared before running it again. |
| 117 |
if os.path.exists(package_build_dir + "/CMakeCache.txt"): |
| 118 |
os.remove(package_build_dir + "/CMakeCache.txt") |
| 119 |
if os.path.exists(package_build_dir + "/CMakeFiles"): |
| 120 |
shutil.rmtree(package_build_dir + "/CMakeFiles") |
| 121 |
|
| 122 |
run_in(package_build_dir, ["cmake"] + cmake_args) |
| 123 |
run_in(package_build_dir, ["ninja", "install", "-v"]) |
| 124 |
|
| 125 |
|
| 126 |
@contextmanager |
| 127 |
def updated_env(env): |
| 128 |
old_env = os.environ.copy() |
| 129 |
os.environ.update(env) |
| 130 |
yield |
| 131 |
os.environ.clear() |
| 132 |
os.environ.update(old_env) |
| 133 |
|
| 134 |
|
| 135 |
def build_tar_package(name, base, directory): |
| 136 |
name = os.path.realpath(name) |
| 137 |
print(f"tarring {name} from {base}/{directory}", file=sys.stderr) |
| 138 |
assert name.endswith(".tar.zst") |
| 139 |
|
| 140 |
cctx = zstandard.ZstdCompressor() |
| 141 |
with open(name, "wb") as f, cctx.stream_writer(f) as z: |
| 142 |
with tarfile.open(mode="w|", fileobj=z) as tf: |
| 143 |
with chdir(base): |
| 144 |
tf.add(directory) |
| 145 |
|
| 146 |
|
| 147 |
def mkdir_p(path): |
| 148 |
try: |
| 149 |
os.makedirs(path) |
| 150 |
except OSError as e: |
| 151 |
if e.errno != errno.EEXIST or not os.path.isdir(path): |
| 152 |
raise |
| 153 |
|
| 154 |
|
| 155 |
def delete(path): |
| 156 |
if os.path.isdir(path): |
| 157 |
shutil.rmtree(path) |
| 158 |
else: |
| 159 |
try: |
| 160 |
os.unlink(path) |
| 161 |
except Exception: |
| 162 |
pass |
| 163 |
|
| 164 |
|
| 165 |
def install_import_library(build_dir, clang_dir): |
| 166 |
shutil.copy2( |
| 167 |
os.path.join(build_dir, "lib", "clang.lib"), os.path.join(clang_dir, "lib") |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
def is_darwin(target): |
| 172 |
return "-apple-darwin" in target |
| 173 |
|
| 174 |
|
| 175 |
def is_linux(target): |
| 176 |
return "-linux-gnu" in target |
| 177 |
|
| 178 |
|
| 179 |
def is_windows(target): |
| 180 |
return "-windows-msvc" in target |
| 181 |
|
| 182 |
|
| 183 |
def is_cross_compile(target): |
| 184 |
target_system, target_machine = SUPPORTED_TARGETS[target] |
| 185 |
system, machine = (platform.system(), platform.machine()) |
| 186 |
if system != target_system: |
| 187 |
return True |
| 188 |
# Don't consider x86 mac on arm64 mac a cross-compile so that we |
| 189 |
# can build x86 mac clang on arm64 mac via Rosetta, as if they |
| 190 |
# were building on x86. |
| 191 |
if system == "Darwin" and machine == "arm64": |
| 192 |
return False |
| 193 |
return machine != target_machine |
| 194 |
|
| 195 |
|
| 196 |
def build_one_stage( |
| 197 |
cc, |
| 198 |
cxx, |
| 199 |
asm, |
| 200 |
ar, |
| 201 |
ranlib, |
| 202 |
libtool, |
| 203 |
ldflags, |
| 204 |
src_dir, |
| 205 |
stage_dir, |
| 206 |
package_name, |
| 207 |
build_type, |
| 208 |
assertions, |
| 209 |
target, |
| 210 |
targets, |
| 211 |
is_final_stage=False, |
| 212 |
profile=None, |
| 213 |
bolt=False, |
| 214 |
): |
| 215 |
if not os.path.exists(stage_dir): |
| 216 |
os.mkdir(stage_dir) |
| 217 |
|
| 218 |
build_dir = stage_dir + "/build" |
| 219 |
inst_dir = stage_dir + "/" + package_name |
| 220 |
|
| 221 |
# cmake doesn't deal well with backslashes in paths. |
| 222 |
def slashify_path(path): |
| 223 |
return path.replace("\\", "/") |
| 224 |
|
| 225 |
def cmake_base_args(cc, cxx, asm, ar, ranlib, libtool, ldflags, inst_dir): |
| 226 |
if is_final_stage and targets: |
| 227 |
machine_targets = targets |
| 228 |
elif target.startswith("aarch64-"): |
| 229 |
machine_targets = "AArch64" |
| 230 |
else: |
| 231 |
machine_targets = "X86" |
| 232 |
|
| 233 |
# see llvm-project/clang/cmake/caches/BOLT.cmake |
| 234 |
if bolt: |
| 235 |
ldflags.append("-Wl,--emit-relocs,-znow") |
| 236 |
|
| 237 |
cmake_args = [ |
| 238 |
"-GNinja", |
| 239 |
"-DCMAKE_C_COMPILER=%s" % slashify_path(cc[0]), |
| 240 |
"-DCMAKE_CXX_COMPILER=%s" % slashify_path(cxx[0]), |
| 241 |
"-DCMAKE_ASM_COMPILER=%s" % slashify_path(asm[0]), |
| 242 |
"-DCMAKE_AR=%s" % slashify_path(ar), |
| 243 |
"-DCMAKE_C_FLAGS_INIT=%s" % " ".join(cc[1:]), |
| 244 |
"-DCMAKE_CXX_FLAGS_INIT=%s" % " ".join(cxx[1:]), |
| 245 |
"-DCMAKE_ASM_FLAGS_INIT=%s" % " ".join(asm[1:]), |
| 246 |
"-DCMAKE_EXE_LINKER_FLAGS_INIT=%s" % " ".join(ldflags), |
| 247 |
"-DCMAKE_SHARED_LINKER_FLAGS_INIT=%s" % " ".join(ldflags), |
| 248 |
"-DCMAKE_BUILD_TYPE=%s" % build_type, |
| 249 |
"-DCMAKE_INSTALL_PREFIX=%s" % inst_dir, |
| 250 |
"-DLLVM_TARGETS_TO_BUILD=%s" % machine_targets, |
| 251 |
"-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF", |
| 252 |
"-DLLVM_ENABLE_ASSERTIONS=%s" % ("ON" if assertions else "OFF"), |
| 253 |
"-DLLVM_ENABLE_BINDINGS=OFF", |
| 254 |
"-DLLVM_ENABLE_CURL=OFF", |
| 255 |
"-DLLVM_INCLUDE_TESTS=OFF", |
| 256 |
"-DLLVM_HOST_TRIPLE=%s" % target, |
| 257 |
"-DCMAKE_C_COMPILER_TARGET=%s" % target, |
| 258 |
"-DCMAKE_CXX_COMPILER_TARGET=%s" % target, |
| 259 |
"-DCMAKE_ASM_COMPILER_TARGET=%s" % target, |
| 260 |
] |
| 261 |
if is_cross_compile(target): |
| 262 |
cmake_args += [ |
| 263 |
"-DCMAKE_SYSTEM_NAME=%s" % SUPPORTED_TARGETS[target][0], |
| 264 |
] |
| 265 |
if is_llvm_toolchain(cc[0], cxx[0]): |
| 266 |
cmake_args += ["-DLLVM_ENABLE_LLD=ON"] |
| 267 |
elif is_windows(target) and is_cross_compile(target): |
| 268 |
raise Exception( |
| 269 |
"Cannot cross-compile for Windows with a compiler that is not clang" |
| 270 |
) |
| 271 |
|
| 272 |
if "TASK_ID" in os.environ: |
| 273 |
cmake_args += [ |
| 274 |
"-DCLANG_REPOSITORY_STRING=taskcluster-%s" % os.environ["TASK_ID"], |
| 275 |
] |
| 276 |
projects = ["clang", "lld"] |
| 277 |
if is_final_stage: |
| 278 |
projects.append("clang-tools-extra") |
| 279 |
else: |
| 280 |
cmake_args.append("-DLLVM_TOOL_LLI_BUILD=OFF") |
| 281 |
|
| 282 |
if bolt: |
| 283 |
projects.append("bolt") |
| 284 |
cmake_args.append("-DCLANG_BOLT=INSTRUMENT") |
| 285 |
cmake_args.append("-DCLANG_INCLUDE_TESTS=ON") |
| 286 |
|
| 287 |
cmake_args.append("-DLLVM_ENABLE_PROJECTS=%s" % ";".join(projects)) |
| 288 |
|
| 289 |
if is_final_stage: |
| 290 |
cmake_args += ["-DLLVM_ENABLE_LIBXML2=FORCE_ON"] |
| 291 |
if is_linux(target) and is_final_stage: |
| 292 |
sysroot = os.path.join(os.environ.get("MOZ_FETCHES_DIR", ""), "sysroot") |
| 293 |
if os.path.exists(sysroot): |
| 294 |
cmake_args += ["-DLLVM_BINUTILS_INCDIR=/usr/include"] |
| 295 |
cmake_args += ["-DCMAKE_SYSROOT=%s" % sysroot] |
| 296 |
# Work around the LLVM build system not building the i386 compiler-rt |
| 297 |
# because it doesn't allow to use a sysroot for that during the cmake |
| 298 |
# checks. |
| 299 |
cmake_args += ["-DCAN_TARGET_i386=1"] |
| 300 |
cmake_args += ["-DLLVM_ENABLE_TERMINFO=OFF"] |
| 301 |
libxml2 = os.path.join(os.environ.get("MOZ_FETCHES_DIR", ""), "libxml2") |
| 302 |
if os.path.exists(libxml2): |
| 303 |
cmake_args += [ |
| 304 |
"-DLIBXML2_DEFINITIONS=-DLIBXML_STATIC", |
| 305 |
f"-DLIBXML2_INCLUDE_DIR={libxml2}/include/libxml2", |
| 306 |
f"-DLIBXML2_LIBRARIES={libxml2}/lib/libxml2.a", |
| 307 |
] |
| 308 |
if is_windows(target): |
| 309 |
cmake_args.insert(-1, "-DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS=ON") |
| 310 |
cmake_args.insert(-1, "-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded") |
| 311 |
if is_cross_compile(target): |
| 312 |
cmake_args += [ |
| 313 |
f"-DCMAKE_TOOLCHAIN_FILE={src_dir}/cmake/platforms/WinMsvc.cmake", |
| 314 |
f"-DLLVM_NATIVE_TOOLCHAIN={os.path.dirname(os.path.dirname(cc[0]))}", |
| 315 |
f"-DHOST_ARCH={target[: -len('-pc-windows-msvc')]}", |
| 316 |
f"-DLLVM_WINSYSROOT={os.environ['VSINSTALLDIR']}", |
| 317 |
"-DLLVM_DISABLE_ASSEMBLY_FILES=ON", |
| 318 |
] |
| 319 |
if is_final_stage: |
| 320 |
fetches = os.environ["MOZ_FETCHES_DIR"] |
| 321 |
cmake_args += [ |
| 322 |
"-DLIBXML2_DEFINITIONS=-DLIBXML_STATIC", |
| 323 |
f"-DLIBXML2_INCLUDE_DIR={fetches}/libxml2/include/libxml2", |
| 324 |
f"-DLIBXML2_LIBRARIES={fetches}/libxml2/lib/libxml2s.lib", |
| 325 |
] |
| 326 |
else: |
| 327 |
# libllvm as a shared library is not supported on Windows |
| 328 |
cmake_args += ["-DLLVM_LINK_LLVM_DYLIB=ON"] |
| 329 |
if ranlib is not None: |
| 330 |
cmake_args += ["-DCMAKE_RANLIB=%s" % slashify_path(ranlib)] |
| 331 |
if libtool is not None: |
| 332 |
cmake_args += ["-DCMAKE_LIBTOOL=%s" % slashify_path(libtool)] |
| 333 |
if is_darwin(target): |
| 334 |
arch = "arm64" if target.startswith("aarch64") else "x86_64" |
| 335 |
cmake_args += [ |
| 336 |
"-DCMAKE_SYSTEM_VERSION=%s" % os.environ["MACOSX_DEPLOYMENT_TARGET"], |
| 337 |
"-DCMAKE_OSX_SYSROOT=%s" % slashify_path(os.getenv("OSX_SYSROOT")), |
| 338 |
"-DCMAKE_FIND_ROOT_PATH=%s" % slashify_path(os.getenv("OSX_SYSROOT")), |
| 339 |
"-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER", |
| 340 |
"-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY", |
| 341 |
"-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY", |
| 342 |
"-DCMAKE_MACOSX_RPATH=ON", |
| 343 |
"-DCMAKE_OSX_ARCHITECTURES=%s" % arch, |
| 344 |
"-DDARWIN_osx_ARCHS=%s" % arch, |
| 345 |
"-DDARWIN_osx_SYSROOT=%s" % slashify_path(os.getenv("OSX_SYSROOT")), |
| 346 |
] |
| 347 |
if arch == "arm64": |
| 348 |
cmake_args += [ |
| 349 |
"-DDARWIN_osx_BUILTIN_ARCHS=arm64", |
| 350 |
] |
| 351 |
# Starting in LLVM 11 (which requires SDK 10.12) the build tries to |
| 352 |
# detect the SDK version by calling xcrun. Cross-compiles don't have |
| 353 |
# an xcrun, so we have to set the version explicitly. |
| 354 |
cmake_args += [ |
| 355 |
"-DDARWIN_macosx_OVERRIDE_SDK_VERSION=%s" |
| 356 |
% os.environ["MACOSX_DEPLOYMENT_TARGET"], |
| 357 |
] |
| 358 |
|
| 359 |
if profile == "gen": |
| 360 |
# Per https://releases.llvm.org/10.0.0/docs/HowToBuildWithPGO.html |
| 361 |
cmake_args += [ |
| 362 |
"-DLLVM_BUILD_INSTRUMENTED=IR", |
| 363 |
"-DLLVM_BUILD_RUNTIME=No", |
| 364 |
] |
| 365 |
elif profile: |
| 366 |
cmake_args += [ |
| 367 |
"-DLLVM_PROFDATA_FILE=%s" % profile, |
| 368 |
] |
| 369 |
|
| 370 |
# Using LTO for both profile generation and usage to avoid most |
| 371 |
# "function control flow change detected (hash mismatch)" error. |
| 372 |
if profile and not is_windows(target): |
| 373 |
cmake_args.append("-DLLVM_ENABLE_LTO=Thin") |
| 374 |
return cmake_args |
| 375 |
|
| 376 |
cmake_args = [] |
| 377 |
cmake_args += cmake_base_args(cc, cxx, asm, ar, ranlib, libtool, ldflags, inst_dir) |
| 378 |
cmake_args += [src_dir] |
| 379 |
build_package(build_dir, cmake_args) |
| 380 |
|
| 381 |
# For some reasons the import library clang.lib of clang.exe is not |
| 382 |
# installed, so we copy it by ourselves. |
| 383 |
if is_windows(target) and is_final_stage: |
| 384 |
install_import_library(build_dir, inst_dir) |
| 385 |
|
| 386 |
|
| 387 |
# Return the absolute path of a build tool. We first look to see if the |
| 388 |
# variable is defined in the config file, and if so we make sure it's an |
| 389 |
# absolute path to an existing tool, otherwise we look for a program in |
| 390 |
# $PATH named "key". |
| 391 |
# |
| 392 |
# This expects the name of the key in the config file to match the name of |
| 393 |
# the tool in the default toolchain on the system (for example, "ld" on Unix |
| 394 |
# and "link" on Windows). |
| 395 |
def get_tool(config, key): |
| 396 |
f = None |
| 397 |
if key in config: |
| 398 |
f = config[key].format(**os.environ) |
| 399 |
if os.path.isabs(f): |
| 400 |
path, f = os.path.split(f) |
| 401 |
# Searches for .exes on windows too, even if the extension is |
| 402 |
# not given. which(absolute_path) doesn't do that until python 3.12. |
| 403 |
f = which(f, path=path) |
| 404 |
if not f: |
| 405 |
raise ValueError("%s must point to an existing path" % key) |
| 406 |
return f |
| 407 |
|
| 408 |
# Assume that we have the name of some program that should be on PATH. |
| 409 |
tool = which(f) if f else which(key) |
| 410 |
if not tool: |
| 411 |
raise ValueError("%s not found on PATH" % (f or key)) |
| 412 |
return tool |
| 413 |
|
| 414 |
|
| 415 |
# This function is intended to be called on the final build directory when |
| 416 |
# building clang-tidy. Also clang-format binaries are included that can be used |
| 417 |
# in conjunction with clang-tidy. |
| 418 |
# As a separate binary we also ship clangd for the language server protocol that |
| 419 |
# can be used as a plugin in `vscode`. |
| 420 |
# Its job is to remove all of the files which won't be used for clang-tidy or |
| 421 |
# clang-format to reduce the download size. Currently when this function |
| 422 |
# finishes its job, it will leave final_dir with a layout like this: |
| 423 |
# |
| 424 |
# clang/ |
| 425 |
# bin/ |
| 426 |
# clang-apply-replacements |
| 427 |
# clang-format |
| 428 |
# clang-tidy |
| 429 |
# clangd |
| 430 |
# run-clang-tidy |
| 431 |
# include/ |
| 432 |
# * (nothing will be deleted here) |
| 433 |
# lib/ |
| 434 |
# clang/ |
| 435 |
# 4.0.0/ |
| 436 |
# include/ |
| 437 |
# * (nothing will be deleted here) |
| 438 |
# share/ |
| 439 |
# clang/ |
| 440 |
# clang-format-diff.py |
| 441 |
# clang-tidy-diff.py |
| 442 |
# run-clang-tidy.py |
| 443 |
def prune_final_dir_for_clang_tidy(final_dir, target): |
| 444 |
# Make sure we only have what we expect. |
| 445 |
dirs = [ |
| 446 |
"bin", |
| 447 |
"include", |
| 448 |
"lib", |
| 449 |
"lib32", |
| 450 |
"libexec", |
| 451 |
"msbuild-bin", |
| 452 |
"share", |
| 453 |
"tools", |
| 454 |
] |
| 455 |
if is_linux(target): |
| 456 |
dirs.append("x86_64-unknown-linux-gnu") |
| 457 |
for f in glob.glob("%s/*" % final_dir): |
| 458 |
if os.path.basename(f) not in dirs: |
| 459 |
raise Exception("Found unknown file %s in the final directory" % f) |
| 460 |
if not os.path.isdir(f): |
| 461 |
raise Exception("Expected %s to be a directory" % f) |
| 462 |
|
| 463 |
kept_binaries = [ |
| 464 |
"clang-apply-replacements", |
| 465 |
"clang-format", |
| 466 |
"clang-tidy", |
| 467 |
"clangd", |
| 468 |
"clang-query", |
| 469 |
"run-clang-tidy", |
| 470 |
] |
| 471 |
re_clang_tidy = re.compile(r"^(" + "|".join(kept_binaries) + r")(\.exe)?$", re.I) |
| 472 |
for f in glob.glob("%s/bin/*" % final_dir): |
| 473 |
if re_clang_tidy.search(os.path.basename(f)) is None: |
| 474 |
delete(f) |
| 475 |
|
| 476 |
# Keep include/ intact. |
| 477 |
|
| 478 |
# Remove the target-specific files. |
| 479 |
if is_linux(target): |
| 480 |
if os.path.exists(os.path.join(final_dir, "x86_64-unknown-linux-gnu")): |
| 481 |
shutil.rmtree(os.path.join(final_dir, "x86_64-unknown-linux-gnu")) |
| 482 |
|
| 483 |
# In lib/, only keep lib/clang/N.M.O/include and the LLVM shared library. |
| 484 |
re_ver_num = re.compile(r"^\d+(?:\.\d+\.\d+)?$", re.I) |
| 485 |
for f in glob.glob("%s/lib/*" % final_dir): |
| 486 |
name = os.path.basename(f) |
| 487 |
if name == "clang": |
| 488 |
continue |
| 489 |
if is_darwin(target) and name in ["libLLVM.dylib", "libclang-cpp.dylib"]: |
| 490 |
continue |
| 491 |
if is_linux(target) and ( |
| 492 |
fnmatch.fnmatch(name, "libLLVM*.so*") |
| 493 |
or fnmatch.fnmatch(name, "libclang-cpp.so*") |
| 494 |
): |
| 495 |
continue |
| 496 |
delete(f) |
| 497 |
for f in glob.glob("%s/lib/clang/*" % final_dir): |
| 498 |
if re_ver_num.search(os.path.basename(f)) is None: |
| 499 |
delete(f) |
| 500 |
for f in glob.glob("%s/lib/clang/*/*" % final_dir): |
| 501 |
if os.path.basename(f) != "include": |
| 502 |
delete(f) |
| 503 |
|
| 504 |
# Completely remove libexec/, msbuild-bin and tools, if it exists. |
| 505 |
shutil.rmtree(os.path.join(final_dir, "libexec")) |
| 506 |
for d in ("msbuild-bin", "tools"): |
| 507 |
d = os.path.join(final_dir, d) |
| 508 |
if os.path.exists(d): |
| 509 |
shutil.rmtree(d) |
| 510 |
|
| 511 |
# In share/, only keep share/clang/*tidy* |
| 512 |
re_clang_tidy = re.compile(r"format|tidy", re.I) |
| 513 |
for f in glob.glob("%s/share/*" % final_dir): |
| 514 |
if os.path.basename(f) != "clang": |
| 515 |
delete(f) |
| 516 |
for f in glob.glob("%s/share/clang/*" % final_dir): |
| 517 |
if re_clang_tidy.search(os.path.basename(f)) is None: |
| 518 |
delete(f) |
| 519 |
|
| 520 |
|
| 521 |
def main(): |
| 522 |
parser = argparse.ArgumentParser() |
| 523 |
parser.add_argument( |
| 524 |
"-c", |
| 525 |
"--config", |
| 526 |
action="append", |
| 527 |
required=True, |
| 528 |
type=argparse.FileType("r"), |
| 529 |
help="Clang configuration file", |
| 530 |
) |
| 531 |
parser.add_argument( |
| 532 |
"--clean", required=False, action="store_true", help="Clean the build directory" |
| 533 |
) |
| 534 |
parser.add_argument( |
| 535 |
"--skip-tar", |
| 536 |
required=False, |
| 537 |
action="store_true", |
| 538 |
help="Skip tar packaging stage", |
| 539 |
) |
| 540 |
parser.add_argument( |
| 541 |
"--skip-patch", |
| 542 |
required=False, |
| 543 |
action="store_true", |
| 544 |
help="Do not patch source", |
| 545 |
) |
| 546 |
|
| 547 |
args = parser.parse_args() |
| 548 |
|
| 549 |
if not os.path.exists("llvm/README.txt"): |
| 550 |
raise Exception( |
| 551 |
"The script must be run from the root directory of the llvm-project tree" |
| 552 |
) |
| 553 |
source_dir = os.getcwd() |
| 554 |
build_dir = source_dir + "/build" |
| 555 |
|
| 556 |
if args.clean: |
| 557 |
shutil.rmtree(build_dir) |
| 558 |
os.sys.exit(0) |
| 559 |
|
| 560 |
llvm_source_dir = source_dir + "/llvm" |
| 561 |
|
| 562 |
config = {} |
| 563 |
# Merge all the configs we got from the command line. |
| 564 |
for c in args.config: |
| 565 |
this_config_dir = os.path.dirname(c.name) |
| 566 |
this_config = json.load(c) |
| 567 |
patches = this_config.get("patches") |
| 568 |
if patches: |
| 569 |
this_config["patches"] = [os.path.join(this_config_dir, p) for p in patches] |
| 570 |
for key, value in this_config.items(): |
| 571 |
old_value = config.get(key) |
| 572 |
if old_value is None: |
| 573 |
config[key] = value |
| 574 |
elif value is None: |
| 575 |
if key in config: |
| 576 |
del config[key] |
| 577 |
elif type(old_value) is not type(value): |
| 578 |
raise Exception( |
| 579 |
f"{c.name} is overriding `{key}` with a value of the wrong type" |
| 580 |
) |
| 581 |
elif isinstance(old_value, list): |
| 582 |
for v in value: |
| 583 |
if v not in old_value: |
| 584 |
old_value.append(v) |
| 585 |
elif isinstance(old_value, dict): |
| 586 |
raise Exception(f"{c.name} is setting `{key}` to a dict?") |
| 587 |
else: |
| 588 |
config[key] = value |
| 589 |
|
| 590 |
stages = 2 |
| 591 |
if "stages" in config: |
| 592 |
stages = int(config["stages"]) |
| 593 |
if stages not in (1, 2, 3, 4): |
| 594 |
raise ValueError("We only know how to build 1, 2, 3, or 4 stages.") |
| 595 |
skip_stages = 0 |
| 596 |
if "skip_stages" in config: |
| 597 |
# The assumption here is that the compiler given in `cc` and other configs |
| 598 |
# is the result of the last skip stage, built somewhere else. |
| 599 |
skip_stages = int(config["skip_stages"]) |
| 600 |
if skip_stages >= stages: |
| 601 |
raise ValueError("Cannot skip more stages than are built.") |
| 602 |
pgo = False |
| 603 |
if "pgo" in config: |
| 604 |
pgo = config["pgo"] |
| 605 |
if pgo not in (True, False): |
| 606 |
raise ValueError("Only boolean values are accepted for pgo.") |
| 607 |
bolt = config.get("bolt", False) |
| 608 |
if bolt not in (True, False): |
| 609 |
raise ValueError("Only boolean values are accepted for bolt.") |
| 610 |
build_type = "Release" |
| 611 |
if "build_type" in config: |
| 612 |
build_type = config["build_type"] |
| 613 |
if build_type not in ("Release", "Debug", "RelWithDebInfo", "MinSizeRel"): |
| 614 |
raise ValueError( |
| 615 |
"We only know how to do Release, Debug, RelWithDebInfo or " |
| 616 |
"MinSizeRel builds" |
| 617 |
) |
| 618 |
targets = config.get("targets") |
| 619 |
build_clang_tidy = False |
| 620 |
if "build_clang_tidy" in config: |
| 621 |
build_clang_tidy = config["build_clang_tidy"] |
| 622 |
if build_clang_tidy not in (True, False): |
| 623 |
raise ValueError("Only boolean values are accepted for build_clang_tidy.") |
| 624 |
build_clang_tidy_alpha = False |
| 625 |
# check for build_clang_tidy_alpha only if build_clang_tidy is true |
| 626 |
if build_clang_tidy and "build_clang_tidy_alpha" in config: |
| 627 |
build_clang_tidy_alpha = config["build_clang_tidy_alpha"] |
| 628 |
if build_clang_tidy_alpha not in (True, False): |
| 629 |
raise ValueError( |
| 630 |
"Only boolean values are accepted for build_clang_tidy_alpha." |
| 631 |
) |
| 632 |
build_clang_tidy_external = False |
| 633 |
# check for build_clang_tidy_external only if build_clang_tidy is true |
| 634 |
if build_clang_tidy and "build_clang_tidy_external" in config: |
| 635 |
build_clang_tidy_external = config["build_clang_tidy_external"] |
| 636 |
if build_clang_tidy_external not in (True, False): |
| 637 |
raise ValueError( |
| 638 |
"Only boolean values are accepted for build_clang_tidy_external." |
| 639 |
) |
| 640 |
assertions = False |
| 641 |
if "assertions" in config: |
| 642 |
assertions = config["assertions"] |
| 643 |
if assertions not in (True, False): |
| 644 |
raise ValueError("Only boolean values are accepted for assertions.") |
| 645 |
|
| 646 |
for t in SUPPORTED_TARGETS: |
| 647 |
if not is_cross_compile(t): |
| 648 |
host = t |
| 649 |
break |
| 650 |
else: |
| 651 |
raise Exception( |
| 652 |
f"Cannot use this script on {platform.system()} {platform.machine()}" |
| 653 |
) |
| 654 |
|
| 655 |
target = config.get("target", host) |
| 656 |
if target not in SUPPORTED_TARGETS: |
| 657 |
raise ValueError(f"{target} is not a supported target.") |
| 658 |
|
| 659 |
if is_cross_compile(target) and not is_linux(host): |
| 660 |
raise Exception("Cross-compilation is only supported on Linux") |
| 661 |
|
| 662 |
if is_darwin(target): |
| 663 |
os.environ["MACOSX_DEPLOYMENT_TARGET"] = ( |
| 664 |
"11.0" if target.startswith("aarch64") else "10.12" |
| 665 |
) |
| 666 |
|
| 667 |
if is_windows(target): |
| 668 |
exe_ext = ".exe" |
| 669 |
cc_name = "clang-cl" |
| 670 |
cxx_name = "clang-cl" |
| 671 |
|
| 672 |
# Used by llvm/lib/DebugInfo/PDB |
| 673 |
os.environ["VSCMD_ARG_TGT_ARCH"] = SUPPORTED_TARGETS[target][1].lower() |
| 674 |
else: |
| 675 |
exe_ext = "" |
| 676 |
cc_name = "clang" |
| 677 |
cxx_name = "clang++" |
| 678 |
|
| 679 |
cc = get_tool(config, "cc") |
| 680 |
cxx = get_tool(config, "cxx") |
| 681 |
asm = get_tool(config, "ml" if is_windows(target) else "as") |
| 682 |
# Not using lld here as default here because it's not in PATH. But clang |
| 683 |
# knows how to find it when they are installed alongside each others. |
| 684 |
ar = get_tool(config, "lib" if is_windows(target) else "ar") |
| 685 |
ranlib = None if is_windows(target) else get_tool(config, "ranlib") |
| 686 |
libtool = get_tool(config, "libtool") if is_darwin(target) else None |
| 687 |
|
| 688 |
if not os.path.exists(source_dir): |
| 689 |
os.makedirs(source_dir) |
| 690 |
|
| 691 |
if not args.skip_patch: |
| 692 |
for p in config.get("patches", []): |
| 693 |
patch(p, source_dir) |
| 694 |
|
| 695 |
package_name = "clang" |
| 696 |
if build_clang_tidy: |
| 697 |
package_name = "clang-tidy" |
| 698 |
if not args.skip_patch: |
| 699 |
import_clang_tidy( |
| 700 |
source_dir, build_clang_tidy_alpha, build_clang_tidy_external |
| 701 |
) |
| 702 |
|
| 703 |
if not os.path.exists(build_dir): |
| 704 |
os.makedirs(build_dir) |
| 705 |
|
| 706 |
stage1_dir = build_dir + "/stage1" |
| 707 |
stage1_inst_dir = stage1_dir + "/" + package_name |
| 708 |
|
| 709 |
final_stage_dir = stage1_dir |
| 710 |
|
| 711 |
if is_darwin(target): |
| 712 |
extra_cflags = [] |
| 713 |
extra_cxxflags = [] |
| 714 |
extra_cflags2 = [] |
| 715 |
extra_cxxflags2 = [] |
| 716 |
extra_asmflags = [] |
| 717 |
# It's unfortunately required to specify the linker used here because |
| 718 |
# the linker flags are used in LLVM's configure step before |
| 719 |
# -DLLVM_ENABLE_LLD is actually processed. |
| 720 |
extra_ldflags = [ |
| 721 |
"-fuse-ld=lld", |
| 722 |
"-Wl,-dead_strip", |
| 723 |
] |
| 724 |
elif is_linux(target): |
| 725 |
extra_cflags = [] |
| 726 |
extra_cxxflags = [] |
| 727 |
extra_cflags2 = ["-fPIC"] |
| 728 |
# Silence clang's warnings about arguments not being used in compilation. |
| 729 |
extra_cxxflags2 = [ |
| 730 |
"-fPIC", |
| 731 |
"-Qunused-arguments", |
| 732 |
] |
| 733 |
extra_asmflags = [] |
| 734 |
# Avoid libLLVM internal function calls going through the PLT. |
| 735 |
extra_ldflags = ["-Wl,-Bsymbolic-functions"] |
| 736 |
# For whatever reason, LLVM's build system will set things up to turn |
| 737 |
# on -ffunction-sections and -fdata-sections, but won't turn on the |
| 738 |
# corresponding option to strip unused sections. We do it explicitly |
| 739 |
# here. LLVM's build system is also picky about turning on ICF, so |
| 740 |
# we do that explicitly here, too. |
| 741 |
|
| 742 |
# It's unfortunately required to specify the linker used here because |
| 743 |
# the linker flags are used in LLVM's configure step before |
| 744 |
# -DLLVM_ENABLE_LLD is actually processed. |
| 745 |
if is_llvm_toolchain(cc, cxx): |
| 746 |
extra_ldflags += ["-fuse-ld=lld", "-Wl,--icf=safe"] |
| 747 |
extra_ldflags += ["-Wl,--gc-sections"] |
| 748 |
elif is_windows(target): |
| 749 |
extra_cflags = [] |
| 750 |
extra_cxxflags = [] |
| 751 |
# clang-cl would like to figure out what it's supposed to be emulating |
| 752 |
# by looking at an MSVC install, but we don't really have that here. |
| 753 |
# Force things on based on WinMsvc.cmake. |
| 754 |
# Ideally, we'd just use WinMsvc.cmake as a toolchain file, but it only |
| 755 |
# really works for cross-compiles, which this is not. |
| 756 |
with open(os.path.join(llvm_source_dir, "cmake/platforms/WinMsvc.cmake")) as f: |
| 757 |
compat = [ |
| 758 |
item |
| 759 |
for line in f |
| 760 |
for item in line.split() |
| 761 |
if "-fms-compatibility-version=" in item |
| 762 |
][0] |
| 763 |
extra_cflags2 = [compat] |
| 764 |
extra_cxxflags2 = [compat] |
| 765 |
extra_asmflags = [] |
| 766 |
extra_ldflags = [] |
| 767 |
|
| 768 |
upload_dir = os.getenv("UPLOAD_DIR") |
| 769 |
if assertions and upload_dir: |
| 770 |
extra_cflags2 += ["-fcrash-diagnostics-dir=%s" % upload_dir] |
| 771 |
extra_cxxflags2 += ["-fcrash-diagnostics-dir=%s" % upload_dir] |
| 772 |
|
| 773 |
if skip_stages < 1: |
| 774 |
build_one_stage( |
| 775 |
[cc] + extra_cflags, |
| 776 |
[cxx] + extra_cxxflags, |
| 777 |
[asm] + extra_asmflags, |
| 778 |
ar, |
| 779 |
ranlib, |
| 780 |
libtool, |
| 781 |
extra_ldflags, |
| 782 |
llvm_source_dir, |
| 783 |
stage1_dir, |
| 784 |
package_name, |
| 785 |
build_type, |
| 786 |
assertions, |
| 787 |
target, |
| 788 |
targets, |
| 789 |
is_final_stage=(stages == 1), |
| 790 |
) |
| 791 |
|
| 792 |
if stages >= 2 and skip_stages < 2: |
| 793 |
stage2_dir = build_dir + "/stage2" |
| 794 |
stage2_inst_dir = stage2_dir + "/" + package_name |
| 795 |
final_stage_dir = stage2_dir |
| 796 |
if skip_stages < 1: |
| 797 |
cc = stage1_inst_dir + "/bin/%s%s" % (cc_name, exe_ext) |
| 798 |
cxx = stage1_inst_dir + "/bin/%s%s" % (cxx_name, exe_ext) |
| 799 |
asm = stage1_inst_dir + "/bin/%s%s" % (cc_name, exe_ext) |
| 800 |
name_compression = [] |
| 801 |
if is_windows(target) and is_cross_compile(target) and pgo: |
| 802 |
# native llvm-profdata.exe on Windows can't read profile data |
| 803 |
# if name compression is enabled (which cross-compiling enables |
| 804 |
# by default) |
| 805 |
name_compression = ["-mllvm", "--enable-name-compression=false"] |
| 806 |
build_one_stage( |
| 807 |
[cc] + extra_cflags2 + name_compression, |
| 808 |
[cxx] + extra_cxxflags2 + name_compression, |
| 809 |
[asm] + extra_asmflags, |
| 810 |
ar, |
| 811 |
ranlib, |
| 812 |
libtool, |
| 813 |
extra_ldflags, |
| 814 |
llvm_source_dir, |
| 815 |
stage2_dir, |
| 816 |
package_name, |
| 817 |
build_type, |
| 818 |
assertions, |
| 819 |
target, |
| 820 |
targets, |
| 821 |
is_final_stage=(stages == 2 and not pgo), |
| 822 |
profile="gen" if pgo else None, |
| 823 |
) |
| 824 |
|
| 825 |
if stages >= 3 and skip_stages < 3: |
| 826 |
stage3_dir = build_dir + "/stage3" |
| 827 |
if pgo: |
| 828 |
profiles_dir = build_dir + "/profiles" |
| 829 |
mkdir_p(profiles_dir) |
| 830 |
os.environ["LLVM_PROFILE_FILE"] = profiles_dir + "/%m.profraw" |
| 831 |
stage3_inst_dir = stage3_dir + "/" + package_name |
| 832 |
final_stage_dir = stage3_dir |
| 833 |
if skip_stages < 2: |
| 834 |
cc = stage2_inst_dir + "/bin/%s%s" % (cc_name, exe_ext) |
| 835 |
cxx = stage2_inst_dir + "/bin/%s%s" % (cxx_name, exe_ext) |
| 836 |
asm = stage2_inst_dir + "/bin/%s%s" % (cc_name, exe_ext) |
| 837 |
build_one_stage( |
| 838 |
[cc] + extra_cflags2, |
| 839 |
[cxx] + extra_cxxflags2, |
| 840 |
[asm] + extra_asmflags, |
| 841 |
ar, |
| 842 |
ranlib, |
| 843 |
libtool, |
| 844 |
extra_ldflags, |
| 845 |
llvm_source_dir, |
| 846 |
stage3_dir, |
| 847 |
package_name, |
| 848 |
build_type, |
| 849 |
assertions, |
| 850 |
target, |
| 851 |
targets, |
| 852 |
is_final_stage=(stages == 3 and not pgo), |
| 853 |
) |
| 854 |
if pgo: |
| 855 |
del os.environ["LLVM_PROFILE_FILE"] |
| 856 |
if skip_stages < 1: |
| 857 |
llvm_profdata = stage1_inst_dir + "/bin/llvm-profdata%s" % exe_ext |
| 858 |
else: |
| 859 |
llvm_profdata = get_tool(config, "llvm-profdata") |
| 860 |
merge_cmd = [llvm_profdata, "merge", "-o", "merged.profdata"] |
| 861 |
profraw_files = glob.glob(os.path.join(profiles_dir, "*.profraw")) |
| 862 |
run_in(stage3_dir, merge_cmd + profraw_files) |
| 863 |
if stages == 3: |
| 864 |
mkdir_p(upload_dir) |
| 865 |
shutil.copy2(os.path.join(stage3_dir, "merged.profdata"), upload_dir) |
| 866 |
return |
| 867 |
|
| 868 |
if stages >= 4 and skip_stages < 4: |
| 869 |
stage4_dir = build_dir + "/stage4" |
| 870 |
final_stage_dir = stage4_dir |
| 871 |
profile = None |
| 872 |
if pgo: |
| 873 |
if skip_stages == 3: |
| 874 |
profile_dir = os.environ.get("MOZ_FETCHES_DIR", "") |
| 875 |
else: |
| 876 |
profile_dir = stage3_dir |
| 877 |
profile = os.path.join(profile_dir, "merged.profdata") |
| 878 |
if skip_stages < 3: |
| 879 |
cc = stage3_inst_dir + "/bin/%s%s" % (cc_name, exe_ext) |
| 880 |
cxx = stage3_inst_dir + "/bin/%s%s" % (cxx_name, exe_ext) |
| 881 |
asm = stage3_inst_dir + "/bin/%s%s" % (cc_name, exe_ext) |
| 882 |
build_one_stage( |
| 883 |
[cc] + extra_cflags2, |
| 884 |
[cxx] + extra_cxxflags2, |
| 885 |
[asm] + extra_asmflags, |
| 886 |
ar, |
| 887 |
ranlib, |
| 888 |
libtool, |
| 889 |
extra_ldflags, |
| 890 |
llvm_source_dir, |
| 891 |
stage4_dir, |
| 892 |
package_name, |
| 893 |
build_type, |
| 894 |
assertions, |
| 895 |
target, |
| 896 |
targets, |
| 897 |
is_final_stage=(stages == 4), |
| 898 |
profile=profile, |
| 899 |
bolt=bolt, |
| 900 |
) |
| 901 |
|
| 902 |
if build_clang_tidy: |
| 903 |
prune_final_dir_for_clang_tidy( |
| 904 |
os.path.join(final_stage_dir, package_name), target |
| 905 |
) |
| 906 |
|
| 907 |
if not args.skip_tar: |
| 908 |
build_tar_package("%s.tar.zst" % package_name, final_stage_dir, package_name) |
| 909 |
|
| 910 |
|
| 911 |
if __name__ == "__main__": |
| 912 |
main() |