blob: 3534ee6e4186d43b7c4310efea83f5243c81b8c2 [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
42 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
43 fdoProfileName, err := actx.DeviceConfig().AfdoProfile(actx.ModuleName())
44 if err != nil {
45 ctx.ModuleErrorf("%s", err.Error())
46 }
47 if fdoProfileName != nil {
48 actx.AddFarVariationDependencies(
49 []blueprint.Variation{
50 {Mutator: "arch", Variation: actx.Target().ArchVariation()},
51 {Mutator: "os", Variation: "android"},
52 },
53 cc.FdoProfileTag,
54 []string{*fdoProfileName}...,
55 )
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) {
70 if ctx.OtherModuleHasProvider(m, cc.FdoProfileProvider) {
71 info := ctx.OtherModuleProvider(m, cc.FdoProfileProvider).(cc.FdoProfileInfo)
72 path := info.Path
73 profileUseFlag := fmt.Sprintf(afdoFlagFormat, path.String())
Yi Kong46c6e592022-01-20 22:55:00 +080074 flags.RustFlags = append(flags.RustFlags, profileUseFlag)
75
Vinh Tran611f0362023-03-09 22:07:19 -050076 deps.AfdoProfiles = append(deps.AfdoProfiles, path)
Yi Kong46c6e592022-01-20 22:55:00 +080077 }
Vinh Tran611f0362023-03-09 22:07:19 -050078 })
79
Yi Kong46c6e592022-01-20 22:55:00 +080080 return flags, deps
81}