2
0
Fork 0
mirror of https://github.com/bazelbuild/bazel-skylib synced 2024-11-30 10:41:18 +00:00
bazel-skylib/rules/maprule_util.bzl
László Csomor 1b28145983
maprule: use ctx.resolve_tools (#117)
In this PR:

- In the _resolve_locations function: use the
  Bash-less ctx.resolve_tools function to resolve
  the runfiles manifests and inputs of tools,
  instead of using ctx.resolve_command for
  the same purpose.

- In the _custom_envmap function: no longer
  resolve $(location) references when
  creating the envvars from custom_env, because
  those references were already resolved in
  _resolve_locations.

The ctx.resolve_tools() method was added in this
PR: https://github.com/bazelbuild/bazel/pull/7139
See design doc there.
2019-03-04 11:04:01 +01:00

69 lines
2.7 KiB
Python

# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for maprule."""
def resolve_locations(ctx, strategy, d):
"""Resolve $(location) references in the values of a dictionary.
Args:
ctx: the 'ctx' argument of the rule implementation function
strategy: a struct with an 'as_path(string) -> string' function
d: {string: string} dictionary; values may contain $(location) references
for labels declared in the rule's 'srcs' and 'tools' attributes
Returns:
{string: string} dict, same as 'd' except "$(location)" references are
resolved.
"""
location_expressions = []
parts = {}
was_anything_to_resolve = False
for k, v in d.items():
# Look for "$(location ...)" or "$(locations ...)", resolve if found.
# _validate_attributes already ensured that there's at most one $(location/s ...) in "v".
if "$(location" in v:
tokens = v.split("$(location")
was_anything_to_resolve = True
closing_paren = tokens[1].find(")")
location_expressions.append("$(location" + tokens[1][:closing_paren + 1])
parts[k] = (tokens[0], tokens[1][closing_paren + 1:])
else:
location_expressions.append("")
resolved = {}
if was_anything_to_resolve:
# Resolve all $(location) expressions in one go. Should be faster than resolving them
# one-by-one.
all_location_expressions = "<split_here>".join(location_expressions)
all_resolved_locations = ctx.expand_location(all_location_expressions)
resolved_locations = strategy.as_path(all_resolved_locations).split("<split_here>")
i = 0
# Starlark dictionaries have a deterministic order of iteration, so the element order in
# "resolved_locations" matches the order in "location_expressions", i.e. the previous
# iteration order of "d".
for k, v in d.items():
if location_expressions[i]:
head, tail = parts[k]
resolved[k] = head + resolved_locations[i] + tail
else:
resolved[k] = v
i += 1
else:
resolved = d
return resolved