blob: 6d790e09d522f2d3616f455d0e8f051492aee2d9 [file] [log] [blame]
Matthew Riley03a01e32022-05-24 10:16:04 -07001# 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 Versari99fddff2022-05-25 10:22:32 -07005load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
Matthew Riley03a01e32022-05-24 10:16:04 -07006
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 Anforowicz4c772642022-05-30 11:11:02 -070010# - 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 Riley03a01e32022-05-24 10:16:04 -070012# - 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.
23def _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 Anforowicz4c772642022-05-30 11:11:02 -070028 if "LLVM_INSTALL_PATH" in repository_ctx.os.environ:
29 # Use LLVM install
30 path = repository_ctx.os.environ["LLVM_INSTALL_PATH"]
Matthew Riley03a01e32022-05-24 10:16:04 -070031
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 Anforowicz4c772642022-05-30 11:11:02 -070043 "${LLVM_INSTALL_PATH}": str(path),
Matthew Riley03a01e32022-05-24 10:16:04 -070044 },
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 Jeanpierre2cc74672024-02-02 01:18:13 -080056LLVM_COMMIT_SHA = "33b463ad9976fa7a27c1a22419297fcccd79f99f"
Jing Luc8561342023-11-28 02:37:46 -080057
Matthew Riley03a01e32022-05-24 10:16:04 -070058def llvm_loader_repository_dependencies():
Matthew Riley0a256652022-06-15 00:55:43 -070059 # This *declares* the dependency, but it won't actually be *downloaded* unless it's used.
Luca Versari99fddff2022-05-25 10:22:32 -070060 new_git_repository(
Matthew Riley03a01e32022-05-24 10:16:04 -070061 name = "llvm-raw",
62 build_file_content = "# empty",
Jing Luc8561342023-11-28 02:37:46 -080063 commit = LLVM_COMMIT_SHA,
Luca Versari99fddff2022-05-25 10:22:32 -070064 remote = "https://github.com/llvm/llvm-project.git",
Matthew Riley03a01e32022-05-24 10:16:04 -070065 )
66
67llvm_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 Anforowicz4c772642022-05-30 11:11:02 -070074 "LLVM_INSTALL_PATH",
Matthew Riley03a01e32022-05-24 10:16:04 -070075 ],
76)