Skip to content

Commit 42f3259

Browse files
Merge pull request #180 from silvergasp:patch-2
PiperOrigin-RevId: 510383031 Change-Id: Id0796b560d646fab81dbda1ee98e6adaf68bfa8e
2 parents ad18309 + 507f496 commit 42f3259

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed

cc/cc_toolchain_config_lib.bzl

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,31 @@
1414

1515
""" A library of functions creating structs for CcToolchainConfigInfo."""
1616

17+
def _check_is_none(obj, parameter_name, method_name):
18+
if obj != None:
19+
fail("{} parameter of {} should be None, found {}."
20+
.format(parameter_name, method_name, type(obj)))
21+
1722
def _check_is_none_or_right_type(obj, obj_of_right_type, parameter_name, method_name):
1823
if obj != None:
19-
_check_right_type(obj, obj_of_right_type, parameter_name, method_name)
24+
_check_same_type(obj, obj_of_right_type, parameter_name, method_name)
2025

21-
def _check_right_type(obj, obj_of_right_type, parameter_name, method_name):
22-
if type(obj) != type(obj_of_right_type):
26+
def _check_right_type(obj, expected_type, parameter_name, method_name):
27+
if type(obj) != expected_type:
2328
fail("{} parameter of {} should be a {}, found {}."
24-
.format(parameter_name, method_name, type(obj_of_right_type), type(obj)))
29+
.format(parameter_name, method_name, expected_type, type(obj)))
30+
31+
def _check_same_type(obj, obj_of_right_type, parameter_name, method_name):
32+
_check_right_type(obj, type(obj_of_right_type), parameter_name, method_name)
2533

2634
def _check_is_nonempty_string(obj, parameter_name, method_name):
27-
_check_right_type(obj, "", parameter_name, method_name)
35+
_check_same_type(obj, "", parameter_name, method_name)
2836
if obj == "":
2937
fail("{} parameter of {} must be a nonempty string."
3038
.format(parameter_name, method_name))
3139

3240
def _check_is_nonempty_list(obj, parameter_name, method_name):
33-
_check_right_type(obj, [], parameter_name, method_name)
41+
_check_same_type(obj, [], parameter_name, method_name)
3442
if len(obj) == 0:
3543
fail("{} parameter of {} must be a nonempty list."
3644
.format(parameter_name, method_name))
@@ -119,7 +127,7 @@ def feature_set(features = []):
119127
Returns:
120128
A FeatureSetInfo provider.
121129
"""
122-
_check_right_type(features, [], "features", "feature_set")
130+
_check_same_type(features, [], "features", "feature_set")
123131
return FeatureSetInfo(features = features, type_name = "feature_set")
124132

125133
WithFeatureSetInfo = provider(
@@ -140,8 +148,8 @@ def with_feature_set(features = [], not_features = []):
140148
Returns:
141149
A WithFeatureSetInfo provider.
142150
"""
143-
_check_right_type(features, [], "features", "with_feature_set")
144-
_check_right_type(not_features, [], "not_features", "with_feature_set")
151+
_check_same_type(features, [], "features", "with_feature_set")
152+
_check_same_type(not_features, [], "not_features", "with_feature_set")
145153
return WithFeatureSetInfo(
146154
features = features,
147155
not_features = not_features,
@@ -175,8 +183,8 @@ def env_set(actions, env_entries = [], with_features = []):
175183
An EnvSetInfo provider.
176184
"""
177185
_check_is_nonempty_list(actions, "actions", "env_set")
178-
_check_right_type(env_entries, [], "env_entries", "env_set")
179-
_check_right_type(with_features, [], "with_features", "env_set")
186+
_check_same_type(env_entries, [], "env_entries", "env_set")
187+
_check_same_type(with_features, [], "with_features", "env_set")
180188
return EnvSetInfo(
181189
actions = actions,
182190
env_entries = env_entries,
@@ -277,8 +285,8 @@ def flag_group(
277285
A FlagGroupInfo provider.
278286
"""
279287

280-
_check_right_type(flags, [], "flags", "flag_group")
281-
_check_right_type(flag_groups, [], "flag_groups", "flag_group")
288+
_check_same_type(flags, [], "flags", "flag_group")
289+
_check_same_type(flag_groups, [], "flag_groups", "flag_group")
282290
if len(flags) > 0 and len(flag_groups) > 0:
283291
fail("flag_group must not contain both a flag and another flag_group.")
284292
if len(flags) == 0 and len(flag_groups) == 0:
@@ -336,9 +344,9 @@ def flag_set(
336344
Returns:
337345
A FlagSetInfo provider.
338346
"""
339-
_check_right_type(actions, [], "actions", "flag_set")
340-
_check_right_type(with_features, [], "with_features", "flag_set")
341-
_check_right_type(flag_groups, [], "flag_groups", "flag_set")
347+
_check_same_type(actions, [], "actions", "flag_set")
348+
_check_same_type(with_features, [], "with_features", "flag_set")
349+
_check_same_type(flag_groups, [], "flag_groups", "flag_set")
342350
return FlagSetInfo(
343351
actions = actions,
344352
with_features = with_features,
@@ -405,12 +413,12 @@ def feature(
405413
Returns:
406414
A FeatureInfo provider.
407415
"""
408-
_check_right_type(enabled, True, "enabled", "feature")
409-
_check_right_type(flag_sets, [], "flag_sets", "feature")
410-
_check_right_type(env_sets, [], "env_sets", "feature")
411-
_check_right_type(requires, [], "requires", "feature")
412-
_check_right_type(provides, [], "provides", "feature")
413-
_check_right_type(implies, [], "implies", "feature")
416+
_check_same_type(enabled, True, "enabled", "feature")
417+
_check_same_type(flag_sets, [], "flag_sets", "feature")
418+
_check_same_type(env_sets, [], "env_sets", "feature")
419+
_check_same_type(requires, [], "requires", "feature")
420+
_check_same_type(provides, [], "provides", "feature")
421+
_check_same_type(implies, [], "implies", "feature")
414422
return FeatureInfo(
415423
name = name,
416424
enabled = enabled,
@@ -447,16 +455,26 @@ def tool_path(name, path):
447455
return ToolPathInfo(name = name, path = path, type_name = "tool_path")
448456

449457
ToolInfo = provider(
450-
"Describes a tool associated with a crosstool action config.",
451-
fields = ["path", "with_features", "execution_requirements", "type_name"],
458+
doc = "Tool information. This differs from ToolPathInfo as it is intended to be used\
459+
in action_configs and can accept labels.",
460+
fields = [
461+
"path",
462+
"tool",
463+
"with_features",
464+
"execution_requirements",
465+
"type_name",
466+
],
452467
)
453468

454-
def tool(path, with_features = [], execution_requirements = []):
469+
def tool(path = None, with_features = [], execution_requirements = [], tool = None):
455470
""" Describes a tool associated with a crosstool action config.
456471
457472
Args:
458473
path: Location of the tool; Can be absolute path (in case of non hermetic
459-
toolchain), or path relative to the cc_toolchain's package.
474+
toolchain), or path relative to the cc_toolchain's package. If this
475+
parameter is set, tool must not be set.
476+
tool: The built-artifact that should be used as this tool. If this is
477+
set, path must not be set.
460478
with_features: A list of feature sets defining when this tool is
461479
applicable. The tool will used when any one of the feature sets
462480
evaluate to true. (That is, when when every 'feature' is enabled,
@@ -471,11 +489,21 @@ def tool(path, with_features = [], execution_requirements = []):
471489
Returns:
472490
A ToolInfo provider.
473491
"""
474-
_check_is_nonempty_string(path, "path", "tool")
475-
_check_right_type(with_features, [], "with_features", "tool")
476-
_check_right_type(execution_requirements, [], "execution_requirements", "tool")
492+
if path == None and tool == None:
493+
fail("Parameter path or parameter tool of tool should not be None.")
494+
495+
if path != None:
496+
_check_is_nonempty_string(path, "path", "tool")
497+
_check_is_none(tool, "tool", "tool")
498+
if tool != None:
499+
_check_is_none(path, "path", "tool")
500+
_check_right_type(tool, "File", "tool", "tool")
501+
502+
_check_same_type(with_features, [], "with_features", "tool")
503+
_check_same_type(execution_requirements, [], "execution_requirements", "tool")
477504
return ToolInfo(
478505
path = path,
506+
tool = tool,
479507
with_features = with_features,
480508
execution_requirements = execution_requirements,
481509
type_name = "tool",
@@ -529,10 +557,10 @@ def action_config(
529557
An ActionConfigInfo provider.
530558
"""
531559
_check_is_nonempty_string(action_name, "name", "action_config")
532-
_check_right_type(enabled, True, "enabled", "action_config")
533-
_check_right_type(tools, [], "tools", "action_config")
534-
_check_right_type(flag_sets, [], "flag_sets", "action_config")
535-
_check_right_type(implies, [], "implies", "action_config")
560+
_check_same_type(enabled, True, "enabled", "action_config")
561+
_check_same_type(tools, [], "tools", "action_config")
562+
_check_same_type(flag_sets, [], "flag_sets", "action_config")
563+
_check_same_type(implies, [], "implies", "action_config")
536564
return ActionConfigInfo(
537565
action_name = action_name,
538566
enabled = enabled,

0 commit comments

Comments
 (0)