diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 9b44f02..60ff217 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -204,30 +204,41 @@ def filter_out_features_re(dropped_features): def drop_foreign_dependencies(lines): dropped_lines = 0 - dropped_features = set() + dropped_optional_deps = set() good_lines = [] - value = True + keep = True + feature = None for line in lines: if m := TARGET_DEPENDENCY_LINE.match(line): expr = m.group("cfg") expr = ast.literal_eval(expr) try: - value = cfg.parse_and_evaluate(expr) + keep = cfg.parse_and_evaluate(expr) except (ValueError, cfg.ParseException): log.warn(f"Could not evaluate {expr!r}, treating as true.") - value = True + keep = True - if not value: + if not keep: feature = m.group("feature") log.info(f"Dropping target-specific dependency on {feature!r}.") - dropped_features.add(feature) + + elif line == "optional = true\n" and feature: + if not keep: + # dropped feature was optional: + # remove occurrences from feature dependencies + dropped_optional_deps.add(feature) + else: + # optional dependency occurs in multiple targets: + # do not drop from feature dependencies + if feature in dropped_optional_deps: + dropped_optional_deps.remove(feature) elif line.startswith("["): # previous section ended, let's keep printing lines again - value = True + keep = True - if value: + if keep: good_lines += [line] else: dropped_lines += 1 @@ -238,7 +249,7 @@ def drop_foreign_dependencies(lines): good_lines2 = [] in_features = False - filt = filter_out_features_re(dropped_features) + filt = filter_out_features_re(dropped_optional_deps) for line in good_lines: if line.rstrip() == "[features]": in_features = True