Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 1 | # Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | # Exceptions. See /LICENSE for license information. |
| 3 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 5 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 6 | |
| 7 | # Create a loader/trampoline repository that we can call into to load LLVM. |
| 8 | # |
| 9 | # Our real goal is to choose between two different sources for LLVM binaries: |
Lukasz Anforowicz | 4c77264 | 2022-05-30 11:11:02 -0700 | [diff] [blame] | 10 | # - if `LLVM_INSTALL_PATH` is in the environment, we treat it as the root of |
| 11 | # an LLVM installation and try to use headers and libraries from there |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 12 | # - otherwise, we build LLVM from source |
| 13 | # |
| 14 | # We *could* implement this choice directly as an if/else between `http_archive` |
| 15 | # or `new_local_repository`. However, all Bazel `load`s are unconditional, so we |
| 16 | # would always end up cloning the (very large) LLVM project repository to load |
| 17 | # its Bazel configuration even if we aren't going to use it. |
| 18 | # |
| 19 | # To avoid that, we add the extra indirection of the "loader" repository. We |
| 20 | # populate the loader repository with one of two templated .bzl files depending |
| 21 | # on whether we want "local" or "remote" LLVM. Then our caller activates that |
| 22 | # .bzl file and gets the desired effect. |
| 23 | def _llvm_loader_repository(repository_ctx): |
| 24 | # The final repository is required to have a `BUILD` file at the root. |
| 25 | repository_ctx.file("BUILD") |
| 26 | |
| 27 | # Create `llvm.bzl` from one of `llvm_{remote|local}.bzl.tmpl`. |
Lukasz Anforowicz | 4c77264 | 2022-05-30 11:11:02 -0700 | [diff] [blame] | 28 | if "LLVM_INSTALL_PATH" in repository_ctx.os.environ: |
| 29 | # Use LLVM install |
| 30 | path = repository_ctx.os.environ["LLVM_INSTALL_PATH"] |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 31 | |
| 32 | # If needed, resolve relative to root of *calling* repository |
| 33 | if not path.startswith("/"): |
| 34 | root_path = repository_ctx.path( |
| 35 | repository_ctx.attr.file_at_root, |
| 36 | ).dirname |
| 37 | path = repository_ctx.path(str(root_path) + "/" + path) |
| 38 | |
| 39 | repository_ctx.template( |
| 40 | "llvm.bzl", |
| 41 | Label("//bazel:llvm_local.bzl.tmpl"), |
| 42 | substitutions = { |
Lukasz Anforowicz | 4c77264 | 2022-05-30 11:11:02 -0700 | [diff] [blame] | 43 | "${LLVM_INSTALL_PATH}": str(path), |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 44 | }, |
| 45 | executable = False, |
| 46 | ) |
| 47 | else: |
| 48 | # Use downloaded LLVM built with Bazel |
| 49 | repository_ctx.template( |
| 50 | "llvm.bzl", |
| 51 | Label("//bazel:llvm_remote.bzl.tmpl"), |
| 52 | substitutions = {}, |
| 53 | executable = False, |
| 54 | ) |
| 55 | |
Devin Jeanpierre | 2cc7467 | 2024-02-02 01:18:13 -0800 | [diff] [blame] | 56 | LLVM_COMMIT_SHA = "33b463ad9976fa7a27c1a22419297fcccd79f99f" |
Jing Lu | c856134 | 2023-11-28 02:37:46 -0800 | [diff] [blame] | 57 | |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 58 | def llvm_loader_repository_dependencies(): |
Matthew Riley | 0a25665 | 2022-06-15 00:55:43 -0700 | [diff] [blame] | 59 | # This *declares* the dependency, but it won't actually be *downloaded* unless it's used. |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 60 | new_git_repository( |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 61 | name = "llvm-raw", |
| 62 | build_file_content = "# empty", |
Jing Lu | c856134 | 2023-11-28 02:37:46 -0800 | [diff] [blame] | 63 | commit = LLVM_COMMIT_SHA, |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 64 | remote = "https://github.com/llvm/llvm-project.git", |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 65 | ) |
| 66 | |
| 67 | llvm_loader_repository = repository_rule( |
| 68 | implementation = _llvm_loader_repository, |
| 69 | attrs = { |
| 70 | # We need a file from the root in order to get the workspace path |
| 71 | "file_at_root": attr.label(default = "//:BUILD"), |
| 72 | }, |
| 73 | environ = [ |
Lukasz Anforowicz | 4c77264 | 2022-05-30 11:11:02 -0700 | [diff] [blame] | 74 | "LLVM_INSTALL_PATH", |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 75 | ], |
| 76 | ) |