[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / utils / bazel / configure.bzl
blob6fca2060491fc718cd2290defd9b3091eed0a571
1 # This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 """Helper macros to configure the LLVM overlay project."""
7 load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
8 load(":zlib.bzl", "llvm_zlib_disable", "llvm_zlib_system")
9 load(":terminfo.bzl", "llvm_terminfo_disable", "llvm_terminfo_system")
11 # Directory of overlay files relative to WORKSPACE
12 DEFAULT_OVERLAY_PATH = "llvm-project-overlay"
14 DEFAULT_TARGETS = [
15     "AArch64",
16     "AMDGPU",
17     "ARM",
18     "AVR",
19     "BPF",
20     "Hexagon",
21     "Lanai",
22     "Mips",
23     "MSP430",
24     "NVPTX",
25     "PowerPC",
26     "RISCV",
27     "Sparc",
28     "SystemZ",
29     "VE",
30     "WebAssembly",
31     "X86",
32     "XCore",
35 def _overlay_directories(repository_ctx):
36     src_path = repository_ctx.path(Label("//:WORKSPACE")).dirname
37     bazel_path = src_path.get_child("utils").get_child("bazel")
38     overlay_path = bazel_path.get_child("llvm-project-overlay")
39     script_path = bazel_path.get_child("overlay_directories.py")
41     python_bin = repository_ctx.which("python3")
42     if not python_bin:
43         # Windows typically just defines "python" as python3. The script itself
44         # contains a check to ensure python3.
45         python_bin = repository_ctx.which("python")
47     if not python_bin:
48         fail("Failed to find python3 binary")
50     cmd = [
51         python_bin,
52         script_path,
53         "--src",
54         src_path,
55         "--overlay",
56         overlay_path,
57         "--target",
58         ".",
59     ]
60     exec_result = repository_ctx.execute(cmd, timeout = 20)
62     if exec_result.return_code != 0:
63         fail(("Failed to execute overlay script: '{cmd}'\n" +
64               "Exited with code {return_code}\n" +
65               "stdout:\n{stdout}\n" +
66               "stderr:\n{stderr}\n").format(
67             cmd = " ".join([str(arg) for arg in cmd]),
68             return_code = exec_result.return_code,
69             stdout = exec_result.stdout,
70             stderr = exec_result.stderr,
71         ))
73 def _llvm_configure_impl(repository_ctx):
74     _overlay_directories(repository_ctx)
76     # Create a starlark file with the requested LLVM targets.
77     targets = repository_ctx.attr.targets
78     repository_ctx.file(
79         "llvm/targets.bzl",
80         content = "llvm_targets = " + str(targets),
81         executable = False,
82     )
84 llvm_configure = repository_rule(
85     implementation = _llvm_configure_impl,
86     local = True,
87     configure = True,
88     attrs = {
89         "targets": attr.string_list(default = DEFAULT_TARGETS),
90     },
93 def llvm_disable_optional_support_deps():
94     maybe(
95         llvm_zlib_disable,
96         name = "llvm_zlib",
97     )
99     maybe(
100         llvm_terminfo_disable,
101         name = "llvm_terminfo",
102     )
104 def llvm_use_system_support_deps():
105     maybe(
106         llvm_zlib_system,
107         name = "llvm_zlib",
108     )
110     maybe(
111         llvm_terminfo_system,
112         name = "llvm_terminfo",
113     )