• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
14
15FEATURES_ATTR_KEY = "features"
16LTO_FEATURE = "android_thin_lto"
17CLI_FEATURES_KEY = "//command_line_option:features"
18
19# This propagates LTO enablement down the dependency tree for modules that
20# enable it explicitly
21# TODO(b/270418352): Move this logic to the incoming transition when incoming
22#                    transitions support select statements
23def lto_deps_transition_impl(settings, attr):
24    features = getattr(attr, FEATURES_ATTR_KEY)
25    new_cli_features = list(settings[CLI_FEATURES_KEY])
26    if LTO_FEATURE in features and LTO_FEATURE not in new_cli_features:
27        new_cli_features.append(LTO_FEATURE)
28
29    return {
30        CLI_FEATURES_KEY: new_cli_features,
31    }
32
33lto_deps_transition = transition(
34    implementation = lto_deps_transition_impl,
35    inputs = [
36        CLI_FEATURES_KEY,
37    ],
38    outputs = [
39        CLI_FEATURES_KEY,
40    ],
41)
42
43# This un-propagates LTO enablement for shared deps, as LTO should only
44# propagate down static deps. This approach avoids an error where we end up with
45# two config variants of the same dependency
46def apply_drop_lto(old_cli_features):
47    new_cli_features = list(old_cli_features)
48    if LTO_FEATURE in old_cli_features:
49        new_cli_features.remove(LTO_FEATURE)
50
51    return {
52        CLI_FEATURES_KEY: new_cli_features,
53    }
54
55def drop_lto_transition_impl(settings, _):
56    return apply_drop_lto(settings[CLI_FEATURES_KEY])
57
58drop_lto_transition = transition(
59    implementation = drop_lto_transition_impl,
60    inputs = [CLI_FEATURES_KEY],
61    outputs = [CLI_FEATURES_KEY],
62)
63