blob: 5268ea1fe2eb9ebfd3f48686ac04cff785b18b41 [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
56def llvm_loader_repository_dependencies():
Matthew Riley0a256652022-06-15 00:55:43 -070057 # This *declares* the dependency, but it won't actually be *downloaded* unless it's used.
Luca Versari99fddff2022-05-25 10:22:32 -070058 new_git_repository(
Matthew Riley03a01e32022-05-24 10:16:04 -070059 name = "llvm-raw",
60 build_file_content = "# empty",
Jing Lud905a5f2023-05-31 09:23:01 -070061 commit = "afb73f7a913ec8e7e8704afe18784571f320ebf6",
Lukasz Anforowicz88328ed2023-04-13 06:50:41 -070062 # `shallow_since` is not required but it improves the performance of CI
63 # builds.
Jing Lud905a5f2023-05-31 09:23:01 -070064 shallow_since = "1685492206 -0700",
Luca Versari99fddff2022-05-25 10:22:32 -070065 remote = "https://github.com/llvm/llvm-project.git",
Matthew Riley03a01e32022-05-24 10:16:04 -070066 )
67
68llvm_loader_repository = repository_rule(
69 implementation = _llvm_loader_repository,
70 attrs = {
71 # We need a file from the root in order to get the workspace path
72 "file_at_root": attr.label(default = "//:BUILD"),
73 },
74 environ = [
Lukasz Anforowicz4c772642022-05-30 11:11:02 -070075 "LLVM_INSTALL_PATH",
Matthew Riley03a01e32022-05-24 10:16:04 -070076 ],
77)