-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.bzl
executable file
·220 lines (177 loc) · 7.45 KB
/
debug.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
This file was created by Konstantin Erman (konste _at_ ermank _dot_ com) in Spring 2020 for the purpose of debugging complex Bazel build custom rules.
"""
types_to_show_in_one_line = ["string", "int", "bool", "Label", "File", "Target"]
# obj_repr_empty_keep_scanning = [
# 'runfiles', 'FilesToRunProvider', 'OutputGroupInfo', 'CcInfo', 'CompilationContext', 'LinkingContext', 'LinkerInput'
# ]
structs_not_to_parse = ["rule"]
how_many_list_items_to_show = 50
attribute_names_to_skip = [
"tree_relative_path", # To avoid error "tree_relative_path not allowed for files that are not tree artifact files."
"rule", # To avoid error "'rule' is only available in aspect implementations."
"build_setting_value", # To avoid error "attempting to access 'build_setting_value' of non-build setting."
"aspect_ids", # To avoid error "'aspect_ids' is only available in aspect implementations."
"actions", # To avoid reporting useless actions property of Target.
"swift",
"py",
"proto",
"objc",
"java",
"j2objc",
"cpp",
"apple",
"android",
]
# buildifier: disable=function-docstring
def query_provider(provider_name, provider, stack, obj, name):
if provider in obj:
stack.append(struct(obj = obj[provider], name = (name or "") + "[" + provider_name + "]", prefix = ""))
def query_known_providers(stack, obj, name):
# Full list of documented providers is here: https://docs.bazel.build/versions/master/skylark/lib/skylark-provider.html
known_providers = {
# ListBinaryInfo, # Commented out to make this file self-contained.
# ListDependenciesInfo, # Not interested in it at this time
"CcInfo": CcInfo,
# PlatformInfo, # https://docs.bazel.build/versions/master/skylark/lib/PlatformInfo.html
"PyInfo": PyInfo,
"PyRuntimeInfo": PyRuntimeInfo,
# CcStarlarkApiProvider, # unknown
"CcToolchainConfigInfo": CcToolchainConfigInfo,
"cc_common.CcToolchainInfo": cc_common.CcToolchainInfo,
# CompilationContext, # unknown
# ConstraintCollection, # unknown
# ConstraintSettingInfo, # unknown
# ConstraintValueInfo, # unknown
# InstrumentedFilesInfo, # unknown
# TemplateVariableInfo, # unknown
# ToolchainInfo, # unknown
# ToolchainTypeInfo, # unknown
}
for provider_name, provider in known_providers.items():
query_provider(provider_name, provider, stack, obj, name)
def trace(msg):
yellow = "\033[1;33m"
no_color = "\033[0m"
# buildifier: disable=print
print("%sTrace:%s %s" % (yellow, no_color, msg))
# buildifier: disable=function-docstring
def describe_object(obj, name = None, prefix = ""):
# if not obj:
# return ('', False)
if name == "licenses":
return ("", False)
object_type = type(obj)
# print("object_type = %s" % object_type)
if object_type == "function":
return ("", False)
keep_scanning = True
obj_repr = "" # <empty>
if object_type in types_to_show_in_one_line:
obj_repr = str(obj)
keep_scanning = False
elif object_type == "list":
obj_repr = "list of %s elements" % len(obj)
elif object_type == "depset":
obj_repr = "depset of %s elements" % len(obj.to_list())
elif object_type == "dict":
obj_repr = "dict of %s elements" % len(obj.items())
elif object_type == "struct" and name in structs_not_to_parse:
obj_repr = obj.location
keep_scanning = False
elif object_type == "struct":
obj_repr = ""
# elif object_type in obj_repr_empty_keep_scanning:
# pass # obj_repr empty, keep scanning.
else:
max_str_len = 70
obj_repr = str(obj)[0:max_str_len]
if len(obj_repr) == max_str_len:
obj_repr += "..."
# print('\nobject_type = %s, obj_repr = %s' % (object_type, obj_repr))
if name:
named_prefix = "\n%s%s(%s): " % (prefix, name, object_type)
else:
named_prefix = "\n%s(%s): " % (prefix, object_type)
new_text = named_prefix + obj_repr
return (new_text, keep_scanning)
def describe_to_string(obj, name = None):
"""Print the properties of the given struct obj.
Args:
obj: the object to introspect
name: the name of the object to introspect
"""
msg = ""
state = struct(obj = obj, name = name, prefix = "")
stack = [state] # list has methods append and pop
for _ in range(2147483647):
state = stack.pop()
(new_text, keep_scanning) = describe_object(state.obj, state.name, state.prefix)
msg += new_text
depth = len(stack)
# print('depth = %s' % depth)
if depth == 0:
# First level expansion must happen even for the types from types_to_show_in_one_line list.
keep_scanning = True
if keep_scanning:
# print('\nscanning "%s" of type %s' % (state.name, type(state.obj)))
obj = state.obj
if type(obj) == "depset":
obj = obj.to_list()
if type(obj) == "list" and len(obj) > 0:
index = min(how_many_list_items_to_show - 1, len(obj) - 1)
for item in reversed(obj):
attribute_name = "item[%s]" % index
new_state = struct(obj = item, name = attribute_name, prefix = state.prefix + " ")
stack.append(new_state)
index = index - 1
if index < 0:
break
if type(obj) == "dict" and len(obj.items()) > 0:
for key, value in reversed(obj.items()):
new_state = struct(obj = value, name = key, prefix = state.prefix + " ")
stack.append(new_state)
else:
if type(obj) == "Target":
query_known_providers(stack, obj, name)
# Search for the attribute which crashes Bazel when used in getattr().
# if 'android' in dir(obj):
# print(dir(obj))
# continue
for attribute_name in reversed(dir(obj)):
if attribute_name in attribute_names_to_skip:
continue
# print("attribute_name = %s" % attribute_name)
attribute_value = getattr(obj, attribute_name)
# print('\n found attribute "%s" of type %s and value %s' % (attribute_name, type(attribute_value), attribute_value))
if attribute_value and type(attribute_value) == "builtin_function_or_method":
continue
new_state = struct(obj = attribute_value, name = attribute_name, prefix = state.prefix + " ")
stack.append(new_state)
if len(stack) == 0:
break
return msg
def describe(obj, name = None):
"""Print the properties of the given struct obj.
Args:
obj: the struct to introspect
name: the name of the struct we are introspecting.
"""
msg = describe_to_string(obj, name)
trace(msg)
# buildifier: disable=function-docstring
def _dump_impl(ctx):
target = ctx.attr.src
name = ctx.label.name
msg = describe_to_string(target, name)
trace(msg)
return [
DefaultInfo(),
CcInfo(), # This is so that cc_ rules can depend on it.
]
dump = rule(
implementation = _dump_impl,
attrs = {
"src": attr.label(), # allow_single_file=True
},
)