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")
9 # Directory of overlay files relative to WORKSPACE
10 DEFAULT_OVERLAY_PATH = "llvm-project-overlay"
34 def _overlay_directories(repository_ctx):
35 src_path = repository_ctx.path(Label("@llvm-raw//:WORKSPACE")).dirname
36 bazel_path = src_path.get_child("utils").get_child("bazel")
37 overlay_path = bazel_path.get_child("llvm-project-overlay")
38 script_path = bazel_path.get_child("overlay_directories.py")
40 python_bin = repository_ctx.which("python3")
42 # Windows typically just defines "python" as python3. The script itself
43 # contains a check to ensure python3.
44 python_bin = repository_ctx.which("python")
47 fail("Failed to find python3 binary")
59 exec_result = repository_ctx.execute(cmd, timeout = 20)
61 if exec_result.return_code != 0:
62 fail(("Failed to execute overlay script: '{cmd}'\n" +
63 "Exited with code {return_code}\n" +
64 "stdout:\n{stdout}\n" +
65 "stderr:\n{stderr}\n").format(
66 cmd = " ".join([str(arg) for arg in cmd]),
67 return_code = exec_result.return_code,
68 stdout = exec_result.stdout,
69 stderr = exec_result.stderr,
72 def _extract_cmake_settings(repository_ctx, llvm_cmake):
73 # The list to be written to vars.bzl
74 # `CMAKE_CXX_STANDARD` may be used from WORKSPACE for the toolchain.
76 "CMAKE_CXX_STANDARD": None,
77 "LLVM_VERSION_MAJOR": None,
78 "LLVM_VERSION_MINOR": None,
79 "LLVM_VERSION_PATCH": None,
82 # It would be easier to use external commands like sed(1) and python.
83 # For portability, the parser should run on Starlark.
84 llvm_cmake_path = repository_ctx.path(Label("//:" + llvm_cmake))
85 for line in repository_ctx.read(llvm_cmake_path).splitlines():
86 # Extract "set ( FOO bar ... "
87 setfoo = line.partition("(")
90 if setfoo[0].strip().lower() != "set":
93 # `kv` is assumed as \s*KEY\s+VAL\s*\).*
94 # Typical case is like
95 # LLVM_REQUIRED_CXX_STANDARD 17)
96 # Possible case -- It should be ignored.
97 # CMAKE_CXX_STANDARD ${...} CACHE STRING "...")
98 kv = setfoo[2].strip()
104 # Prefer LLVM_REQUIRED_CXX_STANDARD instead of CMAKE_CXX_STANDARD
105 if k == "LLVM_REQUIRED_CXX_STANDARD":
106 k = "CMAKE_CXX_STANDARD"
111 # Skip if `CMAKE_CXX_STANDARD` is set with
112 # `LLVM_REQUIRED_CXX_STANDARD`.
113 # Then `v` will not be desired form, like "${...} CACHE"
117 # Pick up 1st word as the value.
118 # Note: It assumes unquoted word.
119 v = kv[i:].strip().partition(")")[0].partition(" ")[0]
122 # Synthesize `LLVM_VERSION` for convenience.
123 c["LLVM_VERSION"] = "{}.{}.{}".format(
124 c["LLVM_VERSION_MAJOR"],
125 c["LLVM_VERSION_MINOR"],
126 c["LLVM_VERSION_PATCH"],
131 def _write_dict_to_file(repository_ctx, filepath, header, vars):
132 # (fci + individual vars) + (fcd + dict items) + (fct)
134 fcd = "\nllvm_vars={\n"
137 for k, v in vars.items():
138 fci += '{} = "{}"\n'.format(k, v)
139 fcd += ' "{}": "{}",\n'.format(k, v)
141 repository_ctx.file(filepath, content = fci + fcd + fct)
143 def _llvm_configure_impl(repository_ctx):
144 _overlay_directories(repository_ctx)
146 llvm_cmake = "llvm/CMakeLists.txt"
147 vars = _extract_cmake_settings(
154 filepath = "vars.bzl",
155 header = "# Generated from {}\n\n".format(llvm_cmake),
159 # Create a starlark file with the requested LLVM targets.
160 targets = repository_ctx.attr.targets
163 content = "llvm_targets = " + str(targets),
167 llvm_configure = repository_rule(
168 implementation = _llvm_configure_impl,
172 "targets": attr.string_list(default = DEFAULT_TARGETS),