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 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 48 | func (fp *fdoProfile) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame] | 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{ |
Vinh Tran | 0bc8c95 | 2023-09-26 11:17:31 -0400 | [diff] [blame] | 75 | Bzl_load_location: "//build/bazel/rules/fdo:fdo_profile.bzl", |
| 76 | Rule_class: "fdo_profile", |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame] | 77 | }, |
| 78 | android.CommonAttributes{ |
| 79 | Name: fp.Name(), |
| 80 | }, |
| 81 | &bazelFdoProfileAttributes{ |
| 82 | Profile: profileAttr, |
| 83 | }, |
| 84 | noRestriction, |
| 85 | ) |
| 86 | } |
| 87 | |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 88 | // FdoProfileInfo is provided by FdoProfileProvider |
| 89 | type FdoProfileInfo struct { |
| 90 | Path android.Path |
| 91 | } |
| 92 | |
| 93 | // FdoProfileProvider is used to provide path to an fdo profile |
| 94 | var FdoProfileProvider = blueprint.NewMutatorProvider(FdoProfileInfo{}, "fdo_profile") |
| 95 | |
| 96 | // FdoProfileMutatorInterface is the interface implemented by fdo_profile module type |
| 97 | // module types that can depend on an fdo_profile module |
| 98 | type FdoProfileMutatorInterface interface { |
| 99 | // FdoProfileMutator eithers set or get FdoProfileProvider |
Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 100 | fdoProfileMutator(ctx android.BottomUpMutatorContext) |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | var _ FdoProfileMutatorInterface = (*fdoProfile)(nil) |
| 104 | |
| 105 | // GenerateAndroidBuildActions of fdo_profile does not have any build actions |
| 106 | func (fp *fdoProfile) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |
| 107 | |
| 108 | // FdoProfileMutator sets FdoProfileProvider to fdo_profile module |
| 109 | // 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] | 110 | func (fp *fdoProfile) fdoProfileMutator(ctx android.BottomUpMutatorContext) { |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 111 | if fp.properties.Profile != nil { |
| 112 | path := android.PathForModuleSrc(ctx, *fp.properties.Profile) |
| 113 | ctx.SetProvider(FdoProfileProvider, FdoProfileInfo{ |
| 114 | Path: path, |
| 115 | }) |
| 116 | } |
| 117 | } |
| 118 | |
Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 119 | // fdoProfileMutator calls the generic fdoProfileMutator function of fdoProfileMutator |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 120 | // which is implemented by cc and cc.FdoProfile |
| 121 | func fdoProfileMutator(ctx android.BottomUpMutatorContext) { |
| 122 | if f, ok := ctx.Module().(FdoProfileMutatorInterface); ok { |
Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 123 | f.fdoProfileMutator(ctx) |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame] | 127 | func FdoProfileFactory() android.Module { |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 128 | m := &fdoProfile{} |
| 129 | m.AddProperties(&m.properties) |
| 130 | android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibBoth) |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame] | 131 | android.InitBazelModule(m) |
Vinh Tran | 0ccc232 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 132 | return m |
| 133 | } |