Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 1 | // Copyright 2023 Google Inc. All rights reserved. |
| 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 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame^] | 19 | "android/soong/bazel" |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame^] | 22 | "github.com/google/blueprint/proptools" |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | RegisterFdoProfileBuildComponents(android.InitRegistrationContext) |
| 27 | } |
| 28 | |
| 29 | func RegisterFdoProfileBuildComponents(ctx android.RegistrationContext) { |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame^] | 30 | ctx.RegisterModuleType("fdo_profile", FdoProfileFactory) |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | type fdoProfile struct { |
| 34 | android.ModuleBase |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame^] | 35 | android.BazelModuleBase |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 36 | |
| 37 | properties fdoProfileProperties |
| 38 | } |
| 39 | |
| 40 | type fdoProfileProperties struct { |
| 41 | Profile *string `android:"arch_variant"` |
| 42 | } |
| 43 | |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame^] | 44 | type bazelFdoProfileAttributes struct { |
| 45 | Profile bazel.StringAttribute |
| 46 | } |
| 47 | |
| 48 | func (fp *fdoProfile) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 49 | var profileAttr bazel.StringAttribute |
| 50 | |
| 51 | archVariantProps := fp.GetArchVariantProperties(ctx, &fdoProfileProperties{}) |
| 52 | for axis, configToProps := range archVariantProps { |
| 53 | for config, _props := range configToProps { |
| 54 | if archProps, ok := _props.(*fdoProfileProperties); ok { |
| 55 | if axis.String() == "arch" || axis.String() == "no_config" { |
| 56 | if archProps.Profile != nil { |
| 57 | profileAttr.SetSelectValue(axis, config, archProps.Profile) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Ideally, cc_library_shared's fdo_profile attr can be a select statement so that we |
| 65 | // don't lift the restriction here. However, in cc_library_shared macro, fdo_profile |
| 66 | // is used as a string, we need to temporarily lift the host restriction until we can |
| 67 | // pass use fdo_profile attr with select statement |
| 68 | // https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/cc/cc_library_shared.bzl;l=127;drc=cc01bdfd39857eddbab04ef69ab6db22dcb1858a |
| 69 | // TODO(b/276287371): Drop the restriction override after fdo_profile path is handled properly |
| 70 | var noRestriction bazel.BoolAttribute |
| 71 | noRestriction.SetSelectValue(bazel.NoConfigAxis, "", proptools.BoolPtr(true)) |
| 72 | |
| 73 | ctx.CreateBazelTargetModuleWithRestrictions( |
| 74 | bazel.BazelTargetModuleProperties{ |
| 75 | Rule_class: "fdo_profile", |
| 76 | }, |
| 77 | android.CommonAttributes{ |
| 78 | Name: fp.Name(), |
| 79 | }, |
| 80 | &bazelFdoProfileAttributes{ |
| 81 | Profile: profileAttr, |
| 82 | }, |
| 83 | noRestriction, |
| 84 | ) |
| 85 | } |
| 86 | |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 87 | // FdoProfileInfo is provided by FdoProfileProvider |
| 88 | type FdoProfileInfo struct { |
| 89 | Path android.Path |
| 90 | } |
| 91 | |
| 92 | // FdoProfileProvider is used to provide path to an fdo profile |
| 93 | var FdoProfileProvider = blueprint.NewMutatorProvider(FdoProfileInfo{}, "fdo_profile") |
| 94 | |
| 95 | // FdoProfileMutatorInterface is the interface implemented by fdo_profile module type |
| 96 | // module types that can depend on an fdo_profile module |
| 97 | type FdoProfileMutatorInterface interface { |
| 98 | // FdoProfileMutator eithers set or get FdoProfileProvider |
Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 99 | fdoProfileMutator(ctx android.BottomUpMutatorContext) |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | var _ FdoProfileMutatorInterface = (*fdoProfile)(nil) |
| 103 | |
| 104 | // GenerateAndroidBuildActions of fdo_profile does not have any build actions |
| 105 | func (fp *fdoProfile) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |
| 106 | |
| 107 | // FdoProfileMutator sets FdoProfileProvider to fdo_profile module |
| 108 | // or sets afdo.Properties.FdoProfilePath to path in FdoProfileProvider of the depended fdo_profile |
Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 109 | func (fp *fdoProfile) fdoProfileMutator(ctx android.BottomUpMutatorContext) { |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 110 | if fp.properties.Profile != nil { |
| 111 | path := android.PathForModuleSrc(ctx, *fp.properties.Profile) |
| 112 | ctx.SetProvider(FdoProfileProvider, FdoProfileInfo{ |
| 113 | Path: path, |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | |
Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 118 | // fdoProfileMutator calls the generic fdoProfileMutator function of fdoProfileMutator |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 119 | // which is implemented by cc and cc.FdoProfile |
| 120 | func fdoProfileMutator(ctx android.BottomUpMutatorContext) { |
| 121 | if f, ok := ctx.Module().(FdoProfileMutatorInterface); ok { |
Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 122 | f.fdoProfileMutator(ctx) |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame^] | 126 | func FdoProfileFactory() android.Module { |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 127 | m := &fdoProfile{} |
| 128 | m.AddProperties(&m.properties) |
| 129 | android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibBoth) |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame^] | 130 | android.InitBazelModule(m) |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 131 | return m |
| 132 | } |