xref: /aosp_15_r20/build/bazel/rules/cc/cfi_transitions.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project
2*7594170eSAndroid Build Coastguard Worker#
3*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*7594170eSAndroid Build Coastguard Worker#
7*7594170eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*7594170eSAndroid Build Coastguard Worker#
9*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*7594170eSAndroid Build Coastguard Worker# limitations under the License.
14*7594170eSAndroid Build Coastguard Worker
15*7594170eSAndroid Build Coastguard Workerload("//build/bazel/product_config:android_product.bzl", "host_platforms")
16*7594170eSAndroid Build Coastguard Workerload(":cc_library_common.bzl", "path_in_list")
17*7594170eSAndroid Build Coastguard Worker
18*7594170eSAndroid Build Coastguard WorkerCFI_FEATURE = "android_cfi"
19*7594170eSAndroid Build Coastguard WorkerCFI_ASSEMBLY_FEATURE = "android_cfi_assembly_support"
20*7594170eSAndroid Build Coastguard WorkerDISABLE_CFI_FEATURE = "-" + CFI_FEATURE
21*7594170eSAndroid Build Coastguard Worker
22*7594170eSAndroid Build Coastguard Worker# This propagates CFI enablement and disablement down the dependency graph
23*7594170eSAndroid Build Coastguard Workerdef apply_cfi_deps(
24*7594170eSAndroid Build Coastguard Worker        features,
25*7594170eSAndroid Build Coastguard Worker        old_cli_features,
26*7594170eSAndroid Build Coastguard Worker        path,
27*7594170eSAndroid Build Coastguard Worker        cfi_include_paths,
28*7594170eSAndroid Build Coastguard Worker        cfi_exclude_paths,
29*7594170eSAndroid Build Coastguard Worker        enable_cfi,
30*7594170eSAndroid Build Coastguard Worker        platform):
31*7594170eSAndroid Build Coastguard Worker    new_cli_features = list(old_cli_features)
32*7594170eSAndroid Build Coastguard Worker    disabled_by_product_vars = (
33*7594170eSAndroid Build Coastguard Worker        not enable_cfi or
34*7594170eSAndroid Build Coastguard Worker        path_in_list(path, cfi_exclude_paths)
35*7594170eSAndroid Build Coastguard Worker    )
36*7594170eSAndroid Build Coastguard Worker    enabled_by_product_vars = (
37*7594170eSAndroid Build Coastguard Worker        path_in_list(path, cfi_include_paths) and _os_is_android(platform[0].name)
38*7594170eSAndroid Build Coastguard Worker    )
39*7594170eSAndroid Build Coastguard Worker
40*7594170eSAndroid Build Coastguard Worker    # Counterintuitive though it may be, we propagate not only the enablement
41*7594170eSAndroid Build Coastguard Worker    # of CFI down the dependency graph, but also its disablement
42*7594170eSAndroid Build Coastguard Worker    if (
43*7594170eSAndroid Build Coastguard Worker        CFI_FEATURE in features or
44*7594170eSAndroid Build Coastguard Worker        enabled_by_product_vars
45*7594170eSAndroid Build Coastguard Worker    ) and not disabled_by_product_vars:
46*7594170eSAndroid Build Coastguard Worker        if CFI_FEATURE not in new_cli_features:
47*7594170eSAndroid Build Coastguard Worker            new_cli_features.append(CFI_FEATURE)
48*7594170eSAndroid Build Coastguard Worker        if DISABLE_CFI_FEATURE in new_cli_features:
49*7594170eSAndroid Build Coastguard Worker            new_cli_features.remove(DISABLE_CFI_FEATURE)
50*7594170eSAndroid Build Coastguard Worker    else:
51*7594170eSAndroid Build Coastguard Worker        if DISABLE_CFI_FEATURE not in new_cli_features:
52*7594170eSAndroid Build Coastguard Worker            new_cli_features.append(DISABLE_CFI_FEATURE)
53*7594170eSAndroid Build Coastguard Worker        if CFI_FEATURE in new_cli_features:
54*7594170eSAndroid Build Coastguard Worker            new_cli_features.remove(CFI_FEATURE)
55*7594170eSAndroid Build Coastguard Worker
56*7594170eSAndroid Build Coastguard Worker    return new_cli_features
57*7594170eSAndroid Build Coastguard Worker
58*7594170eSAndroid Build Coastguard Worker# Since CFI is only propagated down static deps, we use this transition to
59*7594170eSAndroid Build Coastguard Worker# remove it from shared deps that it's added to. It is also used to prevent
60*7594170eSAndroid Build Coastguard Worker# stub libraries from having two configurations for the same dependency.
61*7594170eSAndroid Build Coastguard Workerdef apply_drop_cfi(old_cli_features):
62*7594170eSAndroid Build Coastguard Worker    new_cli_features = list(old_cli_features)
63*7594170eSAndroid Build Coastguard Worker    if CFI_FEATURE in old_cli_features:
64*7594170eSAndroid Build Coastguard Worker        new_cli_features.remove(CFI_FEATURE)
65*7594170eSAndroid Build Coastguard Worker    if DISABLE_CFI_FEATURE not in old_cli_features:
66*7594170eSAndroid Build Coastguard Worker        new_cli_features.append(DISABLE_CFI_FEATURE)
67*7594170eSAndroid Build Coastguard Worker    return new_cli_features
68*7594170eSAndroid Build Coastguard Worker
69*7594170eSAndroid Build Coastguard Workerdef _os_is_android(platform):
70*7594170eSAndroid Build Coastguard Worker    if type(platform) != "string":
71*7594170eSAndroid Build Coastguard Worker        fail("platform argument should be a string!")
72*7594170eSAndroid Build Coastguard Worker    for os_suffix in host_platforms:
73*7594170eSAndroid Build Coastguard Worker        if platform.endswith(os_suffix):
74*7594170eSAndroid Build Coastguard Worker            return False
75*7594170eSAndroid Build Coastguard Worker    return True
76