1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14load(":cc_constants.bzl", "transition_constants")
15
16LTO_FEATURE = "android_thin_lto"
17
18# This propagates LTO enablement down the dependency tree for modules that
19# enable it explicitly
20# TODO(b/270418352): Move this logic to the incoming transition when incoming
21#                    transitions support select statements
22def apply_lto_deps(features, old_cli_features):
23    new_cli_features = list(old_cli_features)
24    if LTO_FEATURE in features and LTO_FEATURE not in new_cli_features:
25        new_cli_features.append(LTO_FEATURE)
26
27    return new_cli_features
28
29def _lto_deps_transition_impl(settings, attr):
30    return {
31        transition_constants.cli_features_key: apply_lto_deps(
32            getattr(attr, transition_constants.features_attr_key),
33            settings[transition_constants.cli_features_key],
34        ),
35    }
36
37lto_deps_transition = transition(
38    implementation = _lto_deps_transition_impl,
39    inputs = [
40        transition_constants.cli_features_key,
41    ],
42    outputs = [
43        transition_constants.cli_features_key,
44    ],
45)
46
47# This un-propagates LTO enablement for shared deps, as LTO should only
48# propagate down static deps. This approach avoids an error where we end up with
49# two config variants of the same dependency
50def apply_drop_lto(old_cli_features):
51    new_cli_features = list(old_cli_features)
52    if LTO_FEATURE in old_cli_features:
53        new_cli_features.remove(LTO_FEATURE)
54
55    return new_cli_features
56