Skip to content

Commit 95a2773

Browse files
committed
Get hello world building with musl
Fully buildable hello world with the repo entirely generated. Super rough, but can be iterated on.
1 parent a424c51 commit 95a2773

File tree

4 files changed

+161
-31
lines changed

4 files changed

+161
-31
lines changed

repo_gen/generate_repo.py

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ def create_version_configs(rows, dir):
134134
value=f"{name}-{version}",
135135
config=f"use_{dir}",
136136
)
137+
if "configurations" in row:
138+
for config, info in row["configurations"].items():
139+
versions = f" \":{name}-{config}-latest\",\n"
140+
for version, _ in row["versions"].items():
141+
versions += f" \":{name}-{config}-{version}\",\n"
142+
143+
settings += config_settings_group_tpl.format(
144+
name=f"{name}-{config}",
145+
versions=versions.strip()
146+
)
137147

138148
name_to_configs[name] = settings.strip()
139149
return name_to_configs
@@ -232,6 +242,56 @@ def create_module_archives(rows, archives):
232242
)
233243
archives[key] += http_archive
234244

245+
link_arg_tpl = """
246+
cc_args(
247+
name = "{link_action}",
248+
actions = [
249+
"@rules_cc//cc/toolchains/actions:{link_action}",
250+
],
251+
args = select({{
252+
{link_args}
253+
}}),
254+
data = [
255+
":lib",
256+
],
257+
format = {{
258+
"lib": ":lib",
259+
}},
260+
)
261+
"""
262+
263+
def create_link_args(rows):
264+
name_to_link_args = {}
265+
for row in rows:
266+
name = row["name"]
267+
link_args = ""
268+
for config, info in row["configurations"].items():
269+
link_args += f" \":{name}-{config}\": [\n"
270+
for arg in info["link_actions"]:
271+
link_args += f" \"{arg}\",\n"
272+
link_args += " ],\n"
273+
274+
name_to_link_args[name] = link_arg_tpl.format(
275+
link_action="link_actions",
276+
link_args=link_args.strip()
277+
)
278+
279+
for row in rows:
280+
name = row["name"]
281+
link_args = ""
282+
for config, info in row["configurations"].items():
283+
link_args += f" \":{name}-{config}\": [\n"
284+
for arg in info["link_executable_actions"]:
285+
link_args += f" \"{arg}\",\n"
286+
link_args += " ],\n"
287+
288+
name_to_link_args[name] += link_arg_tpl.format(
289+
link_action="link_executable_actions",
290+
link_args=link_args.strip()
291+
)
292+
293+
return name_to_link_args
294+
235295
def generate_module():
236296
with open('repo_gen/MODULE.bazel.tpl', 'r') as file:
237297
module_tpl = file.read()
@@ -252,6 +312,7 @@ def generate_build_files(dir, actions):
252312
name_to_configs = create_version_configs(configurations, dir)
253313
name_to_aliases = create_version_aliases(configurations, dir, actions)
254314
name_to_group = create_config_settings_group(configurations)
315+
name_to_link_args = create_link_args(configurations)
255316

256317
for row in configurations:
257318
name = row["name"]
@@ -260,7 +321,8 @@ def generate_build_files(dir, actions):
260321
name = name,
261322
version_aliases=name_to_aliases[name],
262323
config_setting_group=name_to_group[name],
263-
version_configs=name_to_configs[name]
324+
version_configs=name_to_configs[name],
325+
link_args=name_to_link_args[name]
264326
)
265327

266328
with open(f"{dir}/{name}/BUILD", 'w') as file:
@@ -281,13 +343,13 @@ def generate_build_files(dir, actions):
281343
if __name__ == "__main__":
282344
generate_module()
283345
generate_build_files('runtimes', ["include", "lib"])
284-
generate_build_files('toolchain', [
285-
"ar_actions",
286-
"assembly_actions",
287-
"c_compile",
288-
"cpp_compile_actions",
289-
"link_actions",
290-
"link_data",
291-
"objcopy_embed_data",
292-
"strip",
293-
])
346+
# generate_build_files('toolchain', [
347+
# "ar_actions",
348+
# "assembly_actions",
349+
# "c_compile",
350+
# "cpp_compile_actions",
351+
# "link_actions",
352+
# "link_data",
353+
# "objcopy_embed_data",
354+
# "strip",
355+
# ])

repo_gen/runtimes.json

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,28 @@
1515
"configurations": {
1616
"shared": {
1717
"is-default": true,
18-
"linux": {
19-
"link-general": [
20-
"-lc"
21-
],
22-
"link-executable": [
23-
"{lib}/crt1.o",
24-
"{lib}/crti.o",
25-
"{lib}/crtn.o"
26-
]
27-
}
18+
"link_actions": [
19+
"-L{lib}",
20+
"-fuse-ld=lld",
21+
"-lc"
22+
],
23+
"link_executable_actions": [
24+
"{lib}/crt1.o",
25+
"{lib}/crti.o",
26+
"{lib}/crtn.o"
27+
]
2828
},
2929
"static": {
3030
"is-default": false,
31-
"linux": {
32-
"link-general": [
33-
"{lib}/libc.a"
34-
],
35-
"link-executable": [
36-
"{lib}/crt1.o",
37-
"{lib}/crti.o",
38-
"{lib}/crtn.o"
39-
]
40-
}
31+
"link_actions": [
32+
"-fuse-ld=lld",
33+
"{lib}/libc.a"
34+
],
35+
"link_executable_actions": [
36+
"{lib}/crt1.o",
37+
"{lib}/crti.o",
38+
"{lib}/crtn.o"
39+
]
4140
}
4241
}
4342
}

repo_gen/runtimes/BUILD.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cc_args_list(
99
":{name}": [
1010
":arg-include",
1111
":arg-lib",
12+
":link_actions",
13+
":link_executable_actions",
1214
],
1315
"//conditions:default": [],
1416
}}),
@@ -48,6 +50,7 @@ cc_args(
4850
"lib": ":lib",
4951
}},
5052
)
53+
{link_args}
5154
{version_aliases}
5255
{config_setting_group}
5356
{version_configs}

runtimes/musl/BUILD

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cc_args_list(
99
":musl": [
1010
":arg-include",
1111
":arg-lib",
12+
":link_actions",
13+
":link_executable_actions",
1214
],
1315
"//conditions:default": [],
1416
}),
@@ -49,6 +51,56 @@ cc_args(
4951
},
5052
)
5153

54+
cc_args(
55+
name = "link_actions",
56+
actions = [
57+
"@rules_cc//cc/toolchains/actions:link_actions",
58+
],
59+
args = select({
60+
":musl-shared": [
61+
"-L{lib}",
62+
"-fuse-ld=lld",
63+
"-lc",
64+
],
65+
":musl-static": [
66+
"-fuse-ld=lld",
67+
"{lib}/libc.a",
68+
],
69+
}),
70+
data = [
71+
":lib",
72+
],
73+
format = {
74+
"lib": ":lib",
75+
},
76+
)
77+
78+
cc_args(
79+
name = "link_executable_actions",
80+
actions = [
81+
"@rules_cc//cc/toolchains/actions:link_executable_actions",
82+
],
83+
args = select({
84+
":musl-shared": [
85+
"{lib}/crt1.o",
86+
"{lib}/crti.o",
87+
"{lib}/crtn.o",
88+
],
89+
":musl-static": [
90+
"{lib}/crt1.o",
91+
"{lib}/crti.o",
92+
"{lib}/crtn.o",
93+
],
94+
}),
95+
data = [
96+
":lib",
97+
],
98+
format = {
99+
"lib": ":lib",
100+
},
101+
)
102+
103+
52104
alias(
53105
name = "include",
54106
actual = select({
@@ -111,3 +163,17 @@ selects.config_setting_group(
111163
":musl-static-1.2.5",
112164
],
113165
)
166+
selects.config_setting_group(
167+
name = "musl-shared",
168+
match_any = [
169+
":musl-shared-latest",
170+
":musl-shared-1.2.5",
171+
],
172+
)
173+
selects.config_setting_group(
174+
name = "musl-static",
175+
match_any = [
176+
":musl-static-latest",
177+
":musl-static-1.2.5",
178+
],
179+
)

0 commit comments

Comments
 (0)