blob: 6bd4bae1a97fd5b7d02406312865257854b14468 [file] [log] [blame]
Yi Kong46c6e592022-01-20 22:55:00 +08001// 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
15package rust
16
17import (
18 "fmt"
19
Vinh Tran611f0362023-03-09 22:07:19 -050020 "android/soong/android"
Yi Kong46c6e592022-01-20 22:55:00 +080021 "android/soong/cc"
Vinh Tran611f0362023-03-09 22:07:19 -050022
23 "github.com/google/blueprint"
Yi Kong46c6e592022-01-20 22:55:00 +080024)
25
26const afdoFlagFormat = "-Zprofile-sample-use=%s"
27
28type afdo struct {
29 Properties cc.AfdoProperties
30}
31
32func (afdo *afdo) props() []interface{} {
33 return []interface{}{&afdo.Properties}
34}
35
Vinh Tran611f0362023-03-09 22:07:19 -050036func (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 Fausta963b942024-04-11 17:43:00 -070042 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Vinh Tran611f0362023-03-09 22:07:19 -050043 fdoProfileName, err := actx.DeviceConfig().AfdoProfile(actx.ModuleName())
44 if err != nil {
45 ctx.ModuleErrorf("%s", err.Error())
46 }
Colin Crossd38feb02024-01-23 16:38:06 -080047 if fdoProfileName != "" {
Vinh Tran611f0362023-03-09 22:07:19 -050048 actx.AddFarVariationDependencies(
49 []blueprint.Variation{
50 {Mutator: "arch", Variation: actx.Target().ArchVariation()},
51 {Mutator: "os", Variation: "android"},
52 },
53 cc.FdoProfileTag,
Colin Crossd38feb02024-01-23 16:38:06 -080054 []string{fdoProfileName}...,
Vinh Tran611f0362023-03-09 22:07:19 -050055 )
56 }
57 }
58}
59
60func (afdo *afdo) flags(ctx android.ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Yi Kong46c6e592022-01-20 22:55:00 +080061 if ctx.Host() {
62 return flags, deps
63 }
64
Vinh Tran611f0362023-03-09 22:07:19 -050065 if !afdo.Properties.Afdo {
66 return flags, deps
67 }
68
69 ctx.VisitDirectDepsWithTag(cc.FdoProfileTag, func(m android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -080070 if info, ok := android.OtherModuleProvider(ctx, m, cc.FdoProfileProvider); ok {
Vinh Tran611f0362023-03-09 22:07:19 -050071 path := info.Path
72 profileUseFlag := fmt.Sprintf(afdoFlagFormat, path.String())
Yi Kong46c6e592022-01-20 22:55:00 +080073 flags.RustFlags = append(flags.RustFlags, profileUseFlag)
74
Vinh Tran611f0362023-03-09 22:07:19 -050075 deps.AfdoProfiles = append(deps.AfdoProfiles, path)
Yi Kong46c6e592022-01-20 22:55:00 +080076 }
Vinh Tran611f0362023-03-09 22:07:19 -050077 })
78
Yi Kong46c6e592022-01-20 22:55:00 +080079 return flags, deps
80}