Skip to content

Commit f72601a

Browse files
committed
[Bazel] Use LLVM_VERSION from llvm/CMakeLists.txt
* Generate `//:vars.bzl` from `llvm/CMakeLists.txt` `_extract_cmake_settings()` generates `//:vars.bzl` in `llvm_configure()`. It would be easier to use external commands like sed(1) and python. For portability, I think the parser should run on Starlark. `@llvm-project//:vars.bzl` may be loaded from both WORKSPACE and BUILD. At the moment, `vars.bzl` provides some values as string. - CMAKE_CXX_STANDARD = "17" - LLVM_VERSION_MAJOR = "16" - LLVM_VERSION_MINOR = "0" - LLVM_VERSION_PATCH = "0" - LLVM_VERSION = "16.0.0" - llvm_vars = (dict of these values) `CMAKE_CXX_STANDARD` may be used to configure toolchain. * Use `//vars.bzl` for each BUILD files It would be smarter if the BUILD phase could generate `llvm-config.h`. Since I am afraid of the discussion in D126581, I just remove LLVM_VERSION stuff out of the static `llvm-config.h`. * Eliminate Bazel stuff in 'bump-version.py' Current version of `bump-version.py` tries to substitute CLANG_VERSION. It is the reason why I modify bump-version in this change rather than incoming patch. Differential Revision: https://reviews.llvm.org/D136392
1 parent 3a58e11 commit f72601a

File tree

6 files changed

+121
-65
lines changed

6 files changed

+121
-65
lines changed

llvm/utils/release/bump-version.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -88,43 +88,6 @@ def process_line(self, line: str) -> str:
8888
return nline
8989

9090

91-
# Process the many bazel files.
92-
class BazelProcessor(Processor):
93-
def process_line(self, line: str) -> str:
94-
# This matches the CLANG_VERSION line of clang/Config/config.h
95-
nline = line
96-
if "CLANG_VERSION " in line:
97-
nline = re.sub(
98-
r"#define CLANG_VERSION (.*)'",
99-
f"#define CLANG_VERSION {self.version_str(include_suffix=False)}'",
100-
line,
101-
)
102-
# Match version strings of LLVM, Clang and LLD overlay headers
103-
elif "LLVM_VERSION_STRING" in line or "CLANG_VERSION_STRING" in line or "LLD_VERSION_STRING" in line:
104-
nline = re.sub(
105-
r"#define (LLVM|CLANG|LLD)_VERSION_STRING ([\\\"]+)[0-9\.rcgit-]+([\\\"]+)",
106-
rf"#define \g<1>_VERSION_STRING \g<2>{self.version_str()}\g<3>",
107-
line,
108-
)
109-
# Match the split out MAJOR/MINOR/PATCH versions of LLVM and Clang overlay headers
110-
# in LLVM the define is called _PATCH and in clang it's called _PATCHLEVEL
111-
elif "LLVM_VERSION_" in line or "CLANG_VERSION_" in line:
112-
for c, cver in (
113-
("(MAJOR)", self.major),
114-
("(MINOR)", self.minor),
115-
("(PATCH|PATCHLEVEL)", self.patch),
116-
):
117-
nline = re.sub(
118-
fr"(LLVM|CLANG)_VERSION_{c} \d+",
119-
rf"\g<1>_VERSION_\g<2> {cver}",
120-
line,
121-
)
122-
if nline != line:
123-
break
124-
125-
return nline
126-
127-
12891
# GN build system
12992
class GNIProcessor(Processor):
13093
def process_line(self, line: str) -> str:
@@ -214,23 +177,6 @@ def process_line(self, line: str) -> str:
214177
"llvm/utils/gn/secondary/llvm/version.gni",
215178
GNIProcessor(),
216179
),
217-
# Bazel build system
218-
(
219-
"utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/llvm-config.h",
220-
BazelProcessor(),
221-
),
222-
(
223-
"utils/bazel/llvm-project-overlay/clang/BUILD.bazel",
224-
BazelProcessor(),
225-
),
226-
(
227-
"utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h",
228-
BazelProcessor(),
229-
),
230-
(
231-
"utils/bazel/llvm-project-overlay/lld/BUILD.bazel",
232-
BazelProcessor(),
233-
),
234180
(
235181
"libcxx/include/__config",
236182
LibCXXProcessor(),

utils/bazel/configure.bzl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,89 @@ def _overlay_directories(repository_ctx):
7171
stderr = exec_result.stderr,
7272
))
7373

74+
def _extract_cmake_settings(repository_ctx, llvm_cmake):
75+
# The list to be written to vars.bzl
76+
# `CMAKE_CXX_STANDARD` may be used from WORKSPACE for the toolchain.
77+
c = {
78+
"CMAKE_CXX_STANDARD": None,
79+
"LLVM_VERSION_MAJOR": None,
80+
"LLVM_VERSION_MINOR": None,
81+
"LLVM_VERSION_PATCH": None,
82+
}
83+
84+
# It would be easier to use external commands like sed(1) and python.
85+
# For portability, the parser should run on Starlark.
86+
llvm_cmake_path = repository_ctx.path(Label("//:" + llvm_cmake))
87+
for line in repository_ctx.read(llvm_cmake_path).splitlines():
88+
# Extract "set ( FOO bar ... "
89+
setfoo = line.partition("(")
90+
if setfoo[1] != "(":
91+
continue
92+
if setfoo[0].strip().lower() != "set":
93+
continue
94+
# `kv` is assumed as \s*KEY\s+VAL\s*\).*
95+
# Typical case is like
96+
# LLVM_REQUIRED_CXX_STANDARD 17)
97+
# Possible case -- It should be ignored.
98+
# CMAKE_CXX_STANDARD ${...} CACHE STRING "...")
99+
kv = setfoo[2].strip()
100+
i = kv.find(" ")
101+
if i < 0:
102+
continue
103+
k = kv[:i]
104+
# Prefer LLVM_REQUIRED_CXX_STANDARD instead of CMAKE_CXX_STANDARD
105+
if k == "LLVM_REQUIRED_CXX_STANDARD":
106+
k = "CMAKE_CXX_STANDARD"
107+
c[k] = None
108+
if k not in c:
109+
continue
110+
# Skip if `CMAKE_CXX_STANDARD` is set with
111+
# `LLVM_REQUIRED_CXX_STANDARD`.
112+
# Then `v` will not be desired form, like "${...} CACHE"
113+
if c[k] != None:
114+
continue
115+
# Pick up 1st word as the value.
116+
# Note: It assumes unquoted word.
117+
v = kv[i:].strip().partition(")")[0].partition(" ")[0]
118+
c[k] = v
119+
120+
# Synthesize `LLVM_VERSION` for convenience.
121+
c["LLVM_VERSION"] = "{}.{}.{}".format(
122+
c["LLVM_VERSION_MAJOR"],
123+
c["LLVM_VERSION_MINOR"],
124+
c["LLVM_VERSION_PATCH"],
125+
)
126+
127+
return c
128+
129+
def _write_dict_to_file(repository_ctx, filepath, header, vars):
130+
# (fci + individual vars) + (fcd + dict items) + (fct)
131+
fci = header
132+
fcd = "\nllvm_vars={\n"
133+
fct = "}\n"
134+
135+
for k, v in vars.items():
136+
fci += '{} = "{}"\n'.format(k, v)
137+
fcd += ' "{}": "{}",\n'.format(k, v)
138+
139+
repository_ctx.file(filepath, content=fci + fcd + fct)
140+
74141
def _llvm_configure_impl(repository_ctx):
75142
_overlay_directories(repository_ctx)
76143

144+
llvm_cmake = "llvm/CMakeLists.txt"
145+
vars = _extract_cmake_settings(
146+
repository_ctx,
147+
llvm_cmake,
148+
)
149+
150+
_write_dict_to_file(
151+
repository_ctx,
152+
filepath="vars.bzl",
153+
header="# Generated from {}\n\n".format(llvm_cmake),
154+
vars=vars,
155+
)
156+
77157
# Create a starlark file with the requested LLVM targets.
78158
targets = repository_ctx.attr.targets
79159
repository_ctx.file(

utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ load("//llvm:tblgen.bzl", "gentbl")
77
load("//llvm:binary_alias.bzl", "binary_alias")
88
load("//llvm:cc_plugin_library.bzl", "cc_plugin_library")
99

10+
load(
11+
"//:vars.bzl",
12+
"LLVM_VERSION",
13+
"LLVM_VERSION_MAJOR",
14+
"LLVM_VERSION_MINOR",
15+
"LLVM_VERSION_PATCH",
16+
)
17+
1018
package(
1119
default_visibility = ["//visibility:public"],
1220
features = ["layering_check"],
@@ -360,12 +368,17 @@ genrule(
360368
name = "basic_version_gen",
361369
outs = ["include/clang/Basic/Version.inc"],
362370
cmd = (
363-
"echo '#define CLANG_VERSION 16.0.0' >> $@\n" +
364-
"echo '#define CLANG_VERSION_MAJOR 16' >> $@\n" +
365-
"echo '#define CLANG_VERSION_MAJOR_STRING \"16\"' >> $@\n" +
366-
"echo '#define CLANG_VERSION_MINOR 0' >> $@\n" +
367-
"echo '#define CLANG_VERSION_PATCHLEVEL 0' >> $@\n" +
368-
"echo '#define CLANG_VERSION_STRING \"16.0.0\"' >> $@\n"
371+
"echo '#define CLANG_VERSION {vers}' >> $@\n" +
372+
"echo '#define CLANG_VERSION_MAJOR {major}' >> $@\n" +
373+
"echo '#define CLANG_VERSION_MAJOR_STRING \"{major}\"' >> $@\n" +
374+
"echo '#define CLANG_VERSION_MINOR {minor}' >> $@\n" +
375+
"echo '#define CLANG_VERSION_PATCHLEVEL {patch}' >> $@\n" +
376+
"echo '#define CLANG_VERSION_STRING \"{vers}\"' >> $@\n"
377+
).format(
378+
vers=LLVM_VERSION,
379+
major=LLVM_VERSION_MAJOR,
380+
minor=LLVM_VERSION_MINOR,
381+
patch=LLVM_VERSION_PATCH,
369382
),
370383
)
371384

utils/bazel/llvm-project-overlay/lld/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
66
load("//llvm:tblgen.bzl", "gentbl")
77

8+
load(
9+
"//:vars.bzl",
10+
"LLVM_VERSION",
11+
)
12+
813
package(
914
default_visibility = ["//visibility:public"],
1015
features = ["layering_check"],
@@ -16,7 +21,7 @@ licenses(["notice"])
1621
genrule(
1722
name = "config_version_gen",
1823
outs = ["include/lld/Common/Version.inc"],
19-
cmd = "echo '#define LLD_VERSION_STRING \"16.0.0\"' > $@",
24+
cmd = "echo '#define LLD_VERSION_STRING \"{}\"' > $@".format(LLVM_VERSION),
2025
)
2126

2227
genrule(

utils/bazel/llvm-project-overlay/llvm/config.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
"""Defines variables that use selects to configure LLVM based on platform."""
66

7+
load(
8+
"//:vars.bzl",
9+
"LLVM_VERSION",
10+
"LLVM_VERSION_MAJOR",
11+
"LLVM_VERSION_MINOR",
12+
"LLVM_VERSION_PATCH",
13+
)
14+
715
def native_arch_defines(arch, triple):
816
return [
917
r'LLVM_NATIVE_ARCH=\"{}\"'.format(arch),
@@ -89,6 +97,10 @@ llvm_config_defines = os_defines + select({
8997
"@bazel_tools//src/conditions:linux_s390x": native_arch_defines("SystemZ", "systemz-unknown-linux_gnu"),
9098
"//conditions:default": native_arch_defines("X86", "x86_64-unknown-linux-gnu"),
9199
}) + [
100+
"LLVM_VERSION_MAJOR={}".format(LLVM_VERSION_MAJOR),
101+
"LLVM_VERSION_MINOR={}".format(LLVM_VERSION_MINOR),
102+
"LLVM_VERSION_PATCH={}".format(LLVM_VERSION_PATCH),
103+
r'LLVM_VERSION_STRING=\"{}git\"'.format(LLVM_VERSION),
92104
# These shouldn't be needed by the C++11 standard, but are for some
93105
# platforms (e.g. glibc < 2.18. See
94106
# https://sourceware.org/bugzilla/show_bug.cgi?id=15366). These are also

utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/llvm-config.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@
7474
#define LLVM_USE_PERF 0
7575

7676
/* Major version of the LLVM API */
77-
#define LLVM_VERSION_MAJOR 16
77+
/* #undef LLVM_VERSION_MAJOR */
7878

7979
/* Minor version of the LLVM API */
80-
#define LLVM_VERSION_MINOR 0
80+
/* #undef LLVM_VERSION_MINOR */
8181

8282
/* Patch version of the LLVM API */
83-
#define LLVM_VERSION_PATCH 0
83+
/* #undef LLVM_VERSION_PATCH */
8484

8585
/* LLVM version string */
86-
#define LLVM_VERSION_STRING "16.0.0git"
86+
/* #undef LLVM_VERSION_STRING */
8787

8888
/* Whether LLVM records statistics for use with GetStatistics(),
8989
* PrintStatistics() or PrintStatisticsJSON()

0 commit comments

Comments
 (0)