afdo.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. package rust
  15. import (
  16. "fmt"
  17. "android/soong/android"
  18. "android/soong/cc"
  19. "github.com/google/blueprint"
  20. )
  21. const afdoFlagFormat = "-Zprofile-sample-use=%s"
  22. type afdo struct {
  23. Properties cc.AfdoProperties
  24. }
  25. func (afdo *afdo) props() []interface{} {
  26. return []interface{}{&afdo.Properties}
  27. }
  28. func (afdo *afdo) addDep(ctx BaseModuleContext, actx android.BottomUpMutatorContext) {
  29. // afdo is not supported outside of Android
  30. if ctx.Host() {
  31. return
  32. }
  33. if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
  34. fdoProfileName, err := actx.DeviceConfig().AfdoProfile(actx.ModuleName())
  35. if err != nil {
  36. ctx.ModuleErrorf("%s", err.Error())
  37. }
  38. if fdoProfileName != nil {
  39. actx.AddFarVariationDependencies(
  40. []blueprint.Variation{
  41. {Mutator: "arch", Variation: actx.Target().ArchVariation()},
  42. {Mutator: "os", Variation: "android"},
  43. },
  44. cc.FdoProfileTag,
  45. []string{*fdoProfileName}...,
  46. )
  47. }
  48. }
  49. }
  50. func (afdo *afdo) flags(ctx android.ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
  51. if ctx.Host() {
  52. return flags, deps
  53. }
  54. if !afdo.Properties.Afdo {
  55. return flags, deps
  56. }
  57. ctx.VisitDirectDepsWithTag(cc.FdoProfileTag, func(m android.Module) {
  58. if ctx.OtherModuleHasProvider(m, cc.FdoProfileProvider) {
  59. info := ctx.OtherModuleProvider(m, cc.FdoProfileProvider).(cc.FdoProfileInfo)
  60. path := info.Path
  61. profileUseFlag := fmt.Sprintf(afdoFlagFormat, path.String())
  62. flags.RustFlags = append(flags.RustFlags, profileUseFlag)
  63. deps.AfdoProfiles = append(deps.AfdoProfiles, path)
  64. }
  65. })
  66. return flags, deps
  67. }