Skip to content

Commit

Permalink
Add preserve_ordering parameter into to_starlark
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreksh committed May 3, 2024
1 parent e2acc45 commit b28e531
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 18 additions & 11 deletions swiftpkg/internal/bzl_selects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,16 @@ _noop_kind_handler = _new_kind_handler(
transform = lambda v: v,
)

def _to_starlark(values, kind_handlers = {}):
def _to_starlark(values, kind_handlers = {}, preserve_ordering = False):
"""Converts the provied values into Starlark using the information in the \
kind handlers.
Args:
values: A `list` of values that are processed and added to the output.
kind_handlers: A `dict` of king handler `struct` values
(`bzl_selects.new_kind_handler`).
preserve_ordering: A `bool` for keeping the processing as lists instead of
sets that deduplicate entries.
Returns:
A `struct` as returned by `starlark_codegen.new_expr`.
Expand All @@ -152,40 +154,45 @@ def _to_starlark(values, kind_handlers = {}):
# dict whose keys are the conditions and the value is the value for the
# condition.
selects_by_kind = {}
no_condition_results = sets.make()
no_condition_results = []

def process_list(input):
if preserve_ordering:
return input
return sets.to_list(sets.make(input))

for v in values:
v_type = type(v)
if v_type != "struct":
if v_type == "list":
no_condition_results = sets.union(no_condition_results, sets.make(v))
no_condition_results.extend(v)
else:
sets.insert(no_condition_results, v)
no_condition_results.append(v)
continue

# We are assuming that the select will always result in a list.
# Hence, we wrap the transformed value in a list.
kind_handler = kind_handlers.get(v.kind, _noop_kind_handler)
tvs_set = sets.make(lists.flatten(kind_handler.transform(v.value)))
tvs_set = lists.flatten(kind_handler.transform(v.value))
if v.condition != None:
# Collect all of the values associted with a condition.
select_dict = selects_by_kind.get(v.kind, {})
condition_values = select_dict.get(v.condition, sets.make())
condition_values = sets.union(condition_values, tvs_set)
condition_values = sets.to_list(select_dict.get(v.condition, sets.make()))
condition_values = condition_values.extend(tvs_set)
select_dict[v.condition] = condition_values
selects_by_kind[v.kind] = select_dict
else:
no_condition_results = sets.union(no_condition_results, tvs_set)
no_condition_results.extend(tvs_set)

expr_members = []
if sets.length(no_condition_results) > 0:
expr_members.append(sets.to_list(no_condition_results))
if len(no_condition_results) > 0:
expr_members.append(process_list(no_condition_results))
for (kind, select_dict) in selects_by_kind.items():
if len(expr_members) > 0:
expr_members.append(scg.new_op("+"))
sorted_keys = sorted(select_dict.keys())
new_select_dict = {
k: sets.to_list(select_dict[k])
k: process_list(select_dict[k])
for k in sorted_keys
}
kind_handler = kind_handlers.get(kind, _noop_kind_handler)
Expand Down
2 changes: 1 addition & 1 deletion swiftpkg/internal/swiftpkg_build_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _swift_target_build_file(pkg_ctx, target):
if len(defines) > 0:
attrs["defines"] = bzl_selects.to_starlark(defines)
if len(copts) > 0:
attrs["copts"] = bzl_selects.to_starlark(copts)
attrs["copts"] = bzl_selects.to_starlark(copts, preserve_ordering = True)

res_build_file = _handle_target_resources(
pkg_ctx,
Expand Down

0 comments on commit b28e531

Please sign in to comment.