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 15""" 16Configuration transitions for APEX rules. 17 18Transitions are a Bazel mechanism to analyze/build dependencies in a different 19configuration (i.e. options and flags). The APEX transition is applied from a 20top level APEX rule to its dependencies via an outgoing edge, so that the 21dependencies can be built specially for APEXes (vs the platform). 22 23e.g. if an apex A depends on some target T, building T directly as a top level target 24will use a different configuration from building T indirectly as a dependency of A. The 25latter will contain APEX specific configuration settings that its rule or an aspect can 26use to create different actions or providers for APEXes specifically.. 27 28The outgoing transitions are similar to ApexInfo propagation in Soong's 29top-down ApexInfoMutator: 30https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=948-962;drc=539d41b686758eeb86236c0e0dcf75478acb77f3 31""" 32 33load("@bazel_skylib//lib:dicts.bzl", "dicts") 34load("//build/bazel/rules/apex:sdk_versions.bzl", "maybe_override_min_sdk_version") 35 36def _create_apex_configuration(settings, attr, additional = {}): 37 min_sdk_version = maybe_override_min_sdk_version( 38 attr.min_sdk_version, 39 settings["//build/bazel/rules/apex:apex_global_min_sdk_version_override"], 40 ) 41 42 return dicts.add({ 43 "//build/bazel/rules/apex:apex_name": attr.name, # Name of the APEX 44 "//build/bazel/rules/apex:base_apex_name": attr.base_apex_name, # Name of the base APEX, if exists 45 "//build/bazel/rules/apex:min_sdk_version": min_sdk_version, 46 "//build/bazel/rules/apex:within_apex": True, # Building a APEX 47 }, additional) 48 49def _impl(settings, attr): 50 # Perform a transition to apply APEX specific build settings on the 51 # destination target (i.e. an APEX dependency). 52 53 # At this point, the configurable attributes native_shared_libs_32 and 54 # native_shared_libs_64 are already resolved according to the lunch target 55 direct_deps = [str(dep) for dep in attr.native_shared_libs_32] 56 direct_deps += [str(dep) for dep in attr.native_shared_libs_64] 57 direct_deps += [str(dep) for dep in attr.binaries] 58 59 return _create_apex_configuration(settings, attr, { 60 "//build/bazel/rules/apex:apex_direct_deps": direct_deps, 61 }) 62 63_TRANSITION_INPUTS = [ 64 "//build/bazel/rules/apex:apex_global_min_sdk_version_override", 65] 66 67_TRANSITION_OUTPUTS = [ 68 "//build/bazel/rules/apex:apex_name", 69 "//build/bazel/rules/apex:base_apex_name", 70 "//build/bazel/rules/apex:within_apex", 71 "//build/bazel/rules/apex:min_sdk_version", 72 "//build/bazel/rules/apex:apex_direct_deps", 73] 74 75apex_transition = transition( 76 implementation = _impl, 77 inputs = _TRANSITION_INPUTS, 78 outputs = _TRANSITION_OUTPUTS, 79) 80 81# The following table describes how target platform of shared_lib_transition_32 and shared_lib_transition_64 82# look like when building APEXes for different primary/secondary architecture. 83# 84# |---------------------------+----------------------------------------------------+----------------------------------------------------| 85# | Primary arch | Platform for | Platform for | 86# | / Secondary arch | 32b libs transition | 64b libs transition | 87# |---------------------------+----------------------------------------------------+----------------------------------------------------| 88# | 32bit / N/A | android_target | android_target | 89# | (android_target is 32bit) | | (wrong target platform indicates the transition | 90# | | | is not needed, and the 64bit libs are not included | 91# | | | in APEXes for 32bit devices, see | 92# | | | _create_file_mapping() in apex.bzl) | 93# |---------------------------+----------------------------------------------------+----------------------------------------------------| 94# | 64bit / 32bit | android_target_secondary | android_target | 95# | (android_target is 64bit) | | | 96# |---------------------------+----------------------------------------------------+----------------------------------------------------| 97# | 64bit / N/A | android_target | android_target | 98# | (android_target is 64bit) | (wrong target platform indicates the transition | | 99# | | is not needed, and the 32bit libs are not included | | 100# | | in APEXes for 64bit ONLY devices, see | | 101# | | _create_file_mapping() in apex.bzl) | | 102# |---------------------------+----------------------------------------------------+----------------------------------------------------| 103 104def _impl_shared_lib_transition_32(settings, attr): 105 # Perform a transition to apply APEX specific build settings on the 106 # destination target (i.e. an APEX dependency). 107 108 direct_deps = [str(dep) for dep in attr.native_shared_libs_32] 109 direct_deps += [str(dep) for dep in attr.binaries] 110 111 old_platform = str(settings["//command_line_option:platforms"][0]) 112 113 return _create_apex_configuration(settings, attr, { 114 "//build/bazel/rules/apex:apex_direct_deps": direct_deps, 115 "//command_line_option:platforms": old_platform + "_secondary", 116 }) 117 118shared_lib_transition_32 = transition( 119 implementation = _impl_shared_lib_transition_32, 120 inputs = _TRANSITION_INPUTS + ["//command_line_option:platforms"], 121 outputs = _TRANSITION_OUTPUTS + ["//command_line_option:platforms"], 122) 123 124def _impl_shared_lib_transition_64(settings, attr): 125 # Perform a transition to apply APEX specific build settings on the 126 # destination target (i.e. an APEX dependency). 127 128 direct_deps = [str(dep) for dep in attr.native_shared_libs_64] 129 direct_deps += [str(dep) for dep in attr.binaries] 130 131 # For the 64 bit transition, we don't actually change the arch, because 132 # we only read the value of native_shared_libs_64 when the target 133 # is 64-bit already 134 return _create_apex_configuration(settings, attr, { 135 "//build/bazel/rules/apex:apex_direct_deps": direct_deps, 136 }) 137 138shared_lib_transition_64 = transition( 139 implementation = _impl_shared_lib_transition_64, 140 inputs = _TRANSITION_INPUTS, 141 outputs = _TRANSITION_OUTPUTS, 142) 143