| # Part of the Crubit project, under the Apache License v2.0 with LLVM | |
| # Exceptions. See /LICENSE for license information. | |
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| """Repository rules for fetching or stubbing Crubit's LLVM source tree.""" | |
| _GCS_LLVM_URL = "https://storage.googleapis.com/crubit-ci-bazel/llvm-project/v2/{commit}.tar.zst" | |
| _GITHUB_LLVM_URL = "https://github.com/llvm/llvm-project/archive/{commit}.tar.gz" | |
| _OVERLAY = "utils/bazel/llvm-project-overlay/" | |
| def _patch_overlay(repository_ctx): | |
| """Brings an older overlay up to what Crubit's build expects. | |
| Each edit leaves a current overlay untouched. | |
| """ | |
| # llvm-project 0c8394de moved these selects from the `llvm` module, a dev | |
| # dependency that Crubit cannot see, to rules_cc. | |
| config = _OVERLAY + "llvm/config.bzl" | |
| content = repository_ctx.read(config) | |
| patched = content.replace( | |
| '"@llvm//platforms/config:musl"', | |
| '"@rules_cc//cc/libc:musl"', | |
| ).replace( | |
| '"@llvm//platforms/config:gnu"', | |
| '"@rules_cc//cc/libc:glibc"', | |
| ) | |
| if '"@llvm//' in patched or "'@llvm//" in patched: | |
| fail("{} names @llvm in a way Crubit does not rewrite".format(config)) | |
| if patched != content: | |
| repository_ctx.file(config, patched) | |
| # llvm-project 65cb738f split USR generation out of clangIndex. | |
| clang = _OVERLAY + "clang/BUILD.bazel" | |
| content = repository_ctx.read(clang) | |
| if 'name = "unified_symbol_resolution"' not in content: | |
| repository_ctx.file(clang, content + """ | |
| alias( | |
| name = "unified_symbol_resolution", | |
| actual = ":index", | |
| ) | |
| """) | |
| def _disable_llvm_optional_third_party_deps(repository_ctx): | |
| """Disable optional third-party deps in LLVM. | |
| We know rust_api_from_cpp does not need these LLVM deps, so we remove them to speed up builds | |
| and reduce how much llvm we have to cache. rust_api_from_cpp builds againt it's own LLVM, so | |
| this won't impact a user's LLVM toolchain.""" | |
| build_path = _OVERLAY + "third-party/BUILD.bazel" | |
| content = repository_ctx.read(build_path) | |
| required_replacements = [ | |
| ( | |
| ' name = "llvm_enable_zstd",\n build_setting_default = True,', | |
| ' name = "llvm_enable_zstd",\n build_setting_default = False,', | |
| ), | |
| ( | |
| ' name = "llvm_enable_zlib",\n build_setting_default = True,', | |
| ' name = "llvm_enable_zlib",\n build_setting_default = False,', | |
| ), | |
| ] | |
| for before, after in required_replacements: | |
| if after in content: | |
| continue | |
| if before not in content: | |
| fail( | |
| "Failed to patch %s: expected snippet not found:\n%s" % ( | |
| build_path, | |
| before, | |
| ), | |
| ) | |
| content = content.replace(before, after, 1) | |
| # `llvm_enable_lzma` and `llvm_enable_libxml2` were added after the LLVM | |
| # floor in `bazel/llvm_floor.txt`, so older overlays don't define them. | |
| optional_replacements = [ | |
| ( | |
| 'name = "llvm_enable_lzma"', | |
| ' name = "llvm_enable_lzma",\n build_setting_default = True,', | |
| ' name = "llvm_enable_lzma",\n build_setting_default = False,', | |
| ), | |
| ( | |
| 'name = "llvm_enable_libxml2"', | |
| ' name = "llvm_enable_libxml2",\n build_setting_default = "auto",', | |
| ' name = "llvm_enable_libxml2",\n build_setting_default = "disable",', | |
| ), | |
| ] | |
| for marker, before, after in optional_replacements: | |
| if marker not in content or after in content: | |
| continue | |
| if before not in content: | |
| fail( | |
| "Failed to patch %s: expected snippet not found:\n%s" % ( | |
| build_path, | |
| before, | |
| ), | |
| ) | |
| content = content.replace(before, after, 1) | |
| repository_ctx.file(build_path, content, executable = False) | |
| def _llvm_source_fetch_impl(repository_ctx): | |
| commit = repository_ctx.os.environ.get("CRUBIT_LLVM_COMMIT") or repository_ctx.attr.commit | |
| gcs_archive = "llvm-project.tar.zst" | |
| gcs_res = repository_ctx.download( | |
| url = _GCS_LLVM_URL.format(commit = commit), | |
| output = gcs_archive, | |
| allow_fail = True, | |
| ) | |
| if gcs_res.success: | |
| repository_ctx.extract( | |
| archive = gcs_archive, | |
| stripPrefix = "llvm-project-" + commit, | |
| ) | |
| repository_ctx.delete(gcs_archive) | |
| else: | |
| repository_ctx.download_and_extract( | |
| url = _GITHUB_LLVM_URL.format(commit = commit), | |
| stripPrefix = "llvm-project-" + commit, | |
| ) | |
| _patch_overlay(repository_ctx) | |
| _disable_llvm_optional_third_party_deps(repository_ctx) | |
| # Extract the LLVM commit date (YYYYmmDD UTC) from the archive's mtime (or Gitiles fallback). | |
| python = repository_ctx.which("python3") | |
| if not python: | |
| python = repository_ctx.which("python") | |
| if not python: | |
| fail("Neither python3 nor python found in PATH") | |
| script = repository_ctx.path(repository_ctx.attr._get_llvm_commit_date) | |
| res = repository_ctx.execute([python, str(script), commit]) | |
| if res.return_code != 0: | |
| fail("Failed to get LLVM commit date for commit %s: %s" % (commit, res.stderr)) | |
| dev_date = res.stdout.strip() | |
| # Create a `BUILD` to expose the date of the LLVM/Clang commit. | |
| repository_ctx.file("BUILD.bazel", """load("@rules_cc//cc:cc_library.bzl", "cc_library") | |
| cc_library( | |
| name = "llvm_dev_date", | |
| defines = ["CRUBIT_LLVM_DEV_DATE={dev_date}"], | |
| visibility = ["//visibility:public"], | |
| ) | |
| """.format(dev_date = dev_date)) | |
| llvm_source_fetch = repository_rule( | |
| implementation = _llvm_source_fetch_impl, | |
| attrs = { | |
| "commit": attr.string(mandatory = True), | |
| "_get_llvm_commit_date": attr.label( | |
| default = Label("//cargo/build:get_llvm_commit_date.py"), | |
| allow_single_file = True, | |
| ), | |
| }, | |
| environ = [ | |
| "CRUBIT_LLVM_COMMIT", | |
| "CRUBIT_LLVM_DEV_DATE", | |
| ], | |
| ) | |
| def _stub_llvm_raw_impl(repository_ctx): | |
| repository_ctx.file("BUILD.bazel") | |
| repository_ctx.file("utils/bazel/BUILD.bazel") | |
| repository_ctx.file("utils/bazel/configure.bzl", """ | |
| def _stub_llvm_configure_impl(repository_ctx): | |
| repository_ctx.file("BUILD.bazel") | |
| llvm_configure = repository_rule( | |
| implementation = _stub_llvm_configure_impl, | |
| attrs = {"targets": attr.string_list(default = [])}, | |
| ) | |
| """) | |
| stub_llvm_raw = repository_rule(implementation = _stub_llvm_raw_impl) |