Skip to content

Commit c4c33f5

Browse files
committed
Use escaped canonical string from of labels as canonical names
1 parent d9c839b commit c4c33f5

File tree

7 files changed

+43
-4
lines changed

7 files changed

+43
-4
lines changed

zig/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bzl_library(
1616
deps = [
1717
"//zig/private/common:bazel_builtin",
1818
"//zig/private/common:data",
19+
"//zig/private/common:escape_label",
1920
"//zig/private/common:filetypes",
2021
"//zig/private/providers:zig_module_info",
2122
],

zig/private/common/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ bzl_library(
2525
":cdeps",
2626
":csrcs",
2727
":data",
28+
":escape_label",
2829
":filetypes",
2930
":linker_script",
3031
":location_expansion",
@@ -50,6 +51,7 @@ bzl_library(
5051
":bazel_builtin",
5152
":cdeps",
5253
":csrcs",
54+
":escape_label",
5355
":location_expansion",
5456
":zig_cache",
5557
":zig_lib_dir",
@@ -67,6 +69,12 @@ bzl_library(
6769
deps = ["@bazel_skylib//lib:paths"],
6870
)
6971

72+
bzl_library(
73+
name = "escape_label",
74+
srcs = ["escape_label.bzl"],
75+
visibility = ["//zig:__subpackages__"],
76+
)
77+
7078
bzl_library(
7179
name = "location_expansion",
7280
srcs = ["location_expansion.bzl"],
@@ -86,6 +94,7 @@ bzl_library(
8694
srcs = ["translate_c.bzl"],
8795
visibility = ["//zig:__subpackages__"],
8896
deps = [
97+
":escape_label",
8998
"//zig/private:cc_helper",
9099
"//zig/private/providers:zig_module_info",
91100
"@rules_cc//cc:action_names.bzl",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Utility function for escaping Bazel labels into valid path components."""
2+
3+
# Taken from https://github.com/bazelbuild/rules_cc/blob/109e43da9b210aa1806ac27bb640d2233c9703ce/cc/private/cc_common.bzl#L681-L701
4+
5+
_PATH_ESCAPE_REPLACEMENTS = {
6+
"_": "_U",
7+
"/": "_S",
8+
"\\": "_B",
9+
":": "_C",
10+
"@": "_A",
11+
}
12+
13+
# buildifier: disable=function-docstring
14+
def escape_label(*, label):
15+
path = label.package + ":" + label.name
16+
if label.repo_name:
17+
path = label.repo_name + "@" + path
18+
result = []
19+
for idx in range(len(path)):
20+
c = path[idx]
21+
result.append(_PATH_ESCAPE_REPLACEMENTS.get(
22+
c,
23+
c, # no escaping by default
24+
))
25+
return "".join(result)

zig/private/common/translate_c.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
44
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
55
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
6+
load("//zig/private/common:escape_label.bzl", "escape_label")
67
load("//zig/private/providers:zig_module_info.bzl", "zig_module_info")
78

89
def zig_translate_c(*, ctx, name, zigtoolchaininfo, global_args, cc_infos, output_prefix = ""):
@@ -84,7 +85,7 @@ def zig_translate_c(*, ctx, name, zigtoolchaininfo, global_args, cc_infos, outpu
8485

8586
return zig_module_info(
8687
name = name,
87-
canonical_name = "{}/{}".format(str(ctx.label), name),
88+
canonical_name = "{}_U{}".format(escape_label(label = ctx.label), name),
8889
main = zig_out,
8990
cdeps = [cc_info],
9091
)

zig/private/common/zig_build.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ load(
1414
load("//zig/private/common:cdeps.bzl", "zig_cdeps_copts", "zig_cdeps_linker_inputs")
1515
load("//zig/private/common:csrcs.bzl", "zig_csrcs")
1616
load("//zig/private/common:data.bzl", "zig_collect_data", "zig_create_runfiles")
17+
load("//zig/private/common:escape_label.bzl", "escape_label")
1718
load(
1819
"//zig/private/common:filetypes.bzl",
1920
"ZIG_C_SOURCE_EXTENSIONS",
@@ -336,7 +337,7 @@ def zig_build_impl(ctx, *, kind):
336337
else:
337338
root_module = zig_module_info(
338339
name = ctx.attr.name,
339-
canonical_name = str(ctx.label),
340+
canonical_name = escape_label(label = ctx.label),
340341
main = ctx.file.main,
341342
srcs = ctx.files.srcs,
342343
extra_srcs = ctx.files.extra_srcs,

zig/private/common/zig_docs.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ load(
77
"bazel_builtin_module",
88
)
99
load("//zig/private/common:csrcs.bzl", "zig_csrcs")
10+
load("//zig/private/common:escape_label.bzl", "escape_label")
1011
load("//zig/private/common:location_expansion.bzl", "location_expansion")
1112
load("//zig/private/common:translate_c.bzl", "zig_translate_c")
1213
load("//zig/private/common:zig_cache.bzl", "zig_cache_output")
@@ -120,7 +121,7 @@ def zig_docs_impl(ctx, *, kind):
120121
else:
121122
root_module = zig_module_info(
122123
name = ctx.attr.name,
123-
canonical_name = str(ctx.label),
124+
canonical_name = escape_label(label = ctx.label),
124125
main = ctx.file.main,
125126
srcs = ctx.files.srcs,
126127
extra_srcs = ctx.files.extra_srcs,

zig/private/zig_library.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ load(
77
BAZEL_BUILTIN_ATTRS = "ATTRS",
88
)
99
load("//zig/private/common:data.bzl", "zig_collect_data", "zig_create_runfiles")
10+
load("//zig/private/common:escape_label.bzl", "escape_label")
1011
load("//zig/private/common:filetypes.bzl", "ZIG_SOURCE_EXTENSIONS")
1112
load(
1213
"//zig/private/providers:zig_module_info.bzl",
@@ -112,7 +113,7 @@ def _zig_library_impl(ctx):
112113

113114
module = zig_module_info(
114115
name = ctx.attr.import_name or ctx.label.name,
115-
canonical_name = str(ctx.label),
116+
canonical_name = escape_label(label = ctx.label),
116117
main = ctx.file.main,
117118
srcs = ctx.files.srcs,
118119
extra_srcs = ctx.files.extra_srcs,

0 commit comments

Comments
 (0)