1# Copyright (C) 2021 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
15FDO_PROFILE_ATTR_KEY = "fdo_profile"
16CLI_FDO_KEY = "//command_line_option:fdo_profile"
17CLI_CODECOV_KEY = "//command_line_option:collect_code_coverage"
18
19# https://github.com/bazelbuild/bazel/blob/8a53b0e51506d825d276ea7c9480190bd2287009/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoHelper.java#L170
20# Coverage mode is not compatible with FDO optimization in Bazel cc rules
21# If both collect_code_coverage is set, disable fdo optimization
22def apply_fdo_profile(codecov_setting, fdo_profile_attr):
23    if codecov_setting:
24        return {
25            CLI_FDO_KEY: None,
26        }
27    else:
28        return {
29            CLI_FDO_KEY: fdo_profile_attr,
30        }
31
32def fdo_profile_transition_impl(setting, attr):
33    return apply_fdo_profile(
34        setting[CLI_CODECOV_KEY],
35        getattr(attr, FDO_PROFILE_ATTR_KEY),
36    )
37
38# This transition reads the fdo_profile attribute of a rule and set the value
39# to //command_line_option:fdo_profile"
40fdo_profile_transition = transition(
41    implementation = fdo_profile_transition_impl,
42    inputs = [
43        CLI_CODECOV_KEY,
44    ],
45    outputs = [
46        CLI_FDO_KEY,
47    ],
48)
49
50def apply_drop_fdo_profile():
51    return {
52        CLI_FDO_KEY: None,
53    }
54