| Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 1 | // Copyright 2022 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 | package rust | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "fmt" | 
|  | 19 |  | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 20 | "android/soong/android" | 
| Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 21 | "android/soong/cc" | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 22 |  | 
|  | 23 | "github.com/google/blueprint" | 
| Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 24 | ) | 
|  | 25 |  | 
|  | 26 | const afdoFlagFormat = "-Zprofile-sample-use=%s" | 
|  | 27 |  | 
|  | 28 | type afdo struct { | 
|  | 29 | Properties cc.AfdoProperties | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | func (afdo *afdo) props() []interface{} { | 
|  | 33 | return []interface{}{&afdo.Properties} | 
|  | 34 | } | 
|  | 35 |  | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 36 | func (afdo *afdo) addDep(ctx BaseModuleContext, actx android.BottomUpMutatorContext) { | 
|  | 37 | // afdo is not supported outside of Android | 
|  | 38 | if ctx.Host() { | 
|  | 39 | return | 
|  | 40 | } | 
|  | 41 |  | 
| Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 42 | if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) { | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 43 | fdoProfileName, err := actx.DeviceConfig().AfdoProfile(actx.ModuleName()) | 
|  | 44 | if err != nil { | 
|  | 45 | ctx.ModuleErrorf("%s", err.Error()) | 
|  | 46 | } | 
| Colin Cross | d38feb0 | 2024-01-23 16:38:06 -0800 | [diff] [blame] | 47 | if fdoProfileName != "" { | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 48 | actx.AddFarVariationDependencies( | 
|  | 49 | []blueprint.Variation{ | 
|  | 50 | {Mutator: "arch", Variation: actx.Target().ArchVariation()}, | 
|  | 51 | {Mutator: "os", Variation: "android"}, | 
|  | 52 | }, | 
|  | 53 | cc.FdoProfileTag, | 
| Colin Cross | d38feb0 | 2024-01-23 16:38:06 -0800 | [diff] [blame] | 54 | []string{fdoProfileName}..., | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 55 | ) | 
|  | 56 | } | 
|  | 57 | } | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | func (afdo *afdo) flags(ctx android.ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { | 
| Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 61 | if ctx.Host() { | 
|  | 62 | return flags, deps | 
|  | 63 | } | 
|  | 64 |  | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 65 | if !afdo.Properties.Afdo { | 
|  | 66 | return flags, deps | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | ctx.VisitDirectDepsWithTag(cc.FdoProfileTag, func(m android.Module) { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 70 | if info, ok := android.OtherModuleProvider(ctx, m, cc.FdoProfileProvider); ok { | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 71 | path := info.Path | 
|  | 72 | profileUseFlag := fmt.Sprintf(afdoFlagFormat, path.String()) | 
| Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 73 | flags.RustFlags = append(flags.RustFlags, profileUseFlag) | 
|  | 74 |  | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 75 | deps.AfdoProfiles = append(deps.AfdoProfiles, path) | 
| Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 76 | } | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 77 | }) | 
|  | 78 |  | 
| Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 79 | return flags, deps | 
|  | 80 | } |