Skip to content

Commit ab1dce6

Browse files
committed
Start commenting and code cleanup of the generator script
1 parent 95a2773 commit ab1dce6

File tree

1 file changed

+80
-48
lines changed

1 file changed

+80
-48
lines changed

repo_gen/generate_repo.py

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,6 @@ def get_configurations(dir):
77

88
return rows
99

10-
def is_newer(version, latest):
11-
version = version.split('.')
12-
latest = latest.split('.')
13-
for v, l in zip(version, latest):
14-
if int(v) > int(l):
15-
return True
16-
elif int(v) < int(l):
17-
return False
18-
return False
19-
20-
def create_version_lookup(rows):
21-
versions_lookup = {}
22-
for row in rows:
23-
name = row["name"]
24-
version = row["version"]
25-
target_os = row["os"]
26-
arch = row["arch"]
27-
28-
if name not in versions_lookup:
29-
versions_lookup[name] = {
30-
"versions": set(),
31-
"latest": "0.0.0",
32-
"version_to_platforms": {}
33-
}
34-
if version not in versions_lookup[name]["version_to_platforms"]:
35-
versions_lookup[name]["version_to_platforms"][version] = []
36-
37-
versions_lookup[name]["versions"].add(version)
38-
versions_lookup[name]["version_to_platforms"][version].append({
39-
"os": target_os,
40-
"arch": arch
41-
})
42-
43-
if is_newer(version, versions_lookup[name]["latest"]):
44-
versions_lookup[name]["latest"] = version
45-
46-
return versions_lookup
47-
4810
config_setting_tpl = """
4911
config_setting(
5012
name = "{name}",
@@ -58,11 +20,12 @@ def create_version_lookup(rows):
5820
selects.config_setting_group(
5921
name = "{name}",
6022
match_any = [
61-
{versions}
23+
{targets}
6224
],
6325
)
6426
""".lstrip()
6527

28+
# this does way too much and should probably be broken up
6629
def create_version_configs(rows, dir):
6730
name_to_configs = {}
6831
for row in rows:
@@ -92,7 +55,7 @@ def create_version_configs(rows, dir):
9255

9356
settings += config_settings_group_tpl.format(
9457
name=f"{name}-latest",
95-
versions=version_tags.strip()
58+
targets=version_tags.strip()
9659
)
9760
else:
9861
settings += config_setting_tpl.format(
@@ -126,7 +89,7 @@ def create_version_configs(rows, dir):
12689

12790
settings += config_settings_group_tpl.format(
12891
name=f"{name}-{version}",
129-
versions=version_tags.strip()
92+
targets=version_tags.strip()
13093
)
13194
else:
13295
settings += config_setting_tpl.format(
@@ -142,13 +105,32 @@ def create_version_configs(rows, dir):
142105

143106
settings += config_settings_group_tpl.format(
144107
name=f"{name}-{config}",
145-
versions=versions.strip()
108+
targets=versions.strip()
146109
)
147110

148111
name_to_configs[name] = settings.strip()
149112
return name_to_configs
150113

151-
def create_config_settings_group(rows):
114+
# Used in //toolchain/<toolchain>/BUILD and //runtimes/<runtime>/BUILD files
115+
# This creates the top-level `selects.config_setting_group` that identifies whether or not the toolchain/runtime is being used
116+
# example outputs:
117+
# from //toolchain/llvm/BUILD
118+
# selects.config_setting_group(
119+
# name = "llvm",
120+
# match_any = [
121+
# ":llvm-latest",
122+
# ":llvm-19.1.7",
123+
# ],
124+
# )
125+
# from //runtime/musl/BUILD
126+
# selects.config_setting_group(
127+
# name = "musl",
128+
# match_any = [
129+
# ":musl-latest",
130+
# ":musl-1.2.5",
131+
# ],
132+
# )
133+
def create_top_level_config_settings_group(rows):
152134
name_to_group = {}
153135
for row in rows:
154136
name = row["name"]
@@ -158,7 +140,7 @@ def create_config_settings_group(rows):
158140

159141
name_to_group[name] = config_settings_group_tpl.format(
160142
name=name,
161-
versions=version_tags.strip()
143+
targets=version_tags.strip()
162144
)
163145
return name_to_group
164146

@@ -171,6 +153,25 @@ def create_config_settings_group(rows):
171153
)
172154
"""
173155

156+
# Used in //toolchain/<toolchain>/BUILD and //runtimes/<runtime>/BUILD files
157+
# Creates the aliases that switch on the version of the toolchain/runtime
158+
# example outputs:
159+
# from //toolchain/llvm/BUILD:
160+
# alias(
161+
# name = "c_compile",
162+
# actual = select({
163+
# ":llvm-latest": "//toolchain/llvm/19.1.7:c_compile",
164+
# ":llvm-19.1.7": "//toolchain/llvm/19.1.7:c_compile",
165+
# }),
166+
# )
167+
# from //runtimes/musl/BUILD:
168+
# alias(
169+
# name = "include",
170+
# actual = select({
171+
# ":musl-latest": "//runtimes/musl/1.2.5:include",
172+
# ":musl-1.2.5": "//runtimes/musl/1.2.5:include",
173+
# }),
174+
# )
174175
def create_version_aliases(rows, dir, actions):
175176
name_to_aliases = {}
176177
for action in actions:
@@ -193,6 +194,23 @@ def create_version_aliases(rows, dir, actions):
193194

194195
return name_to_aliases
195196

197+
# Used in //toolchain/<toolchain>/<version>/BUILD and //runtimes/<runtime>/<version>/BUILD files
198+
# Creates the aliases that switch on the os and arch of the toolchain/runtime
199+
# example outputs:
200+
# from //toolchain/llvm/19.1.7/BUILD:
201+
# alias(
202+
# name = "c_compile",
203+
# actual = select({
204+
# "//constraint:linux_x86_64": "@llvm-19.1.7-linux-x86_64//:c_compile",
205+
# }),
206+
# )
207+
# from //runtimes/musl/1.2.5/BUILD:
208+
# alias(
209+
# name = "include",
210+
# actual = select({
211+
# "//constraint:linux_x86_64": "@musl-1.2.5-linux-x86_64//:include",
212+
# }),
213+
# )
196214
def create_platform_aliases(name, version, os_to_arch, actions):
197215
aliases = "package(default_visibility = [\"//:__subpackages__\"])\n"
198216
for action in actions:
@@ -218,7 +236,21 @@ def create_platform_aliases(name, version, os_to_arch, actions):
218236
)
219237
""".lstrip()
220238

221-
def create_module_archives(rows, archives):
239+
# Used in //MODULE.bazel
240+
# This creates the http_archive rules for each toolchain/runtime
241+
# example outputs:
242+
# http_archive(
243+
# name = "llvm-19.1.7-linux-x86_64",
244+
# url = "https://github.com/reutermj/toolchains_cc/releases/download/binaries/llvm-19.1.7-linux-x86_64.tar.xz",
245+
# sha256 = "ac027eb9f1cde6364d063fe91bd299937eb03b8d906f7ddde639cf65b4872cb3",
246+
# )
247+
# ...
248+
# http_archive(
249+
# name = "musl-1.2.5-linux-x86_64",
250+
# url = "https://github.com/reutermj/toolchains_cc/releases/download/binaries/musl-1.2.5-r10-linux-x86_64.tar.xz",
251+
# sha256 = "5c2ba292f20013f34f6553000171f488c38bcd497472fd0586d2374c447423ff",
252+
# )
253+
def create_http_archives(rows, archives):
222254
for row in rows:
223255
for version, os_to_arch in row["versions"].items():
224256
for target_os, archs in os_to_arch.items():
@@ -297,8 +329,8 @@ def generate_module():
297329
module_tpl = file.read()
298330

299331
archives = {}
300-
create_module_archives(get_configurations('toolchain'), archives)
301-
create_module_archives(get_configurations('runtimes'), archives)
332+
create_http_archives(get_configurations('toolchain'), archives)
333+
create_http_archives(get_configurations('runtimes'), archives)
302334

303335
module = module_tpl.format(**archives)
304336
with open('MODULE.bazel', 'w') as file:
@@ -311,7 +343,7 @@ def generate_build_files(dir, actions):
311343
configurations = get_configurations(dir)
312344
name_to_configs = create_version_configs(configurations, dir)
313345
name_to_aliases = create_version_aliases(configurations, dir, actions)
314-
name_to_group = create_config_settings_group(configurations)
346+
name_to_group = create_top_level_config_settings_group(configurations)
315347
name_to_link_args = create_link_args(configurations)
316348

317349
for row in configurations:

0 commit comments

Comments
 (0)