blob: f5d27ff3a988992425730efb1a63418781ccdbea [file] [log] [blame]
Yi Kongd5954a22022-01-26 17:36:26 +08001// Copyright 2022 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
15package cc
16
17import (
Liz Kammer8c8e8d52022-10-31 15:53:36 -040018 "strings"
Yi Kongd5954a22022-01-26 17:36:26 +080019 "testing"
20
21 "android/soong/android"
Liz Kammer8c8e8d52022-10-31 15:53:36 -040022
Yi Kongd5954a22022-01-26 17:36:26 +080023 "github.com/google/blueprint"
24)
25
Liz Kammer8c8e8d52022-10-31 15:53:36 -040026type visitDirectDepsInterface interface {
27 VisitDirectDeps(blueprint.Module, func(dep blueprint.Module))
28}
29
30func hasDirectDep(ctx visitDirectDepsInterface, m android.Module, wantDep android.Module) bool {
31 var found bool
32 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
33 if dep == wantDep {
34 found = true
35 }
36 })
37 return found
38}
39
Yi Kongd5954a22022-01-26 17:36:26 +080040func TestAfdoDeps(t *testing.T) {
41 bp := `
Liz Kammer8c8e8d52022-10-31 15:53:36 -040042 cc_library_shared {
Yi Kongd5954a22022-01-26 17:36:26 +080043 name: "libTest",
Liz Kammer8c8e8d52022-10-31 15:53:36 -040044 srcs: ["test.c"],
Yi Kongd5954a22022-01-26 17:36:26 +080045 static_libs: ["libFoo"],
46 afdo: true,
47 }
48
Liz Kammer8c8e8d52022-10-31 15:53:36 -040049 cc_library_static {
Yi Kongd5954a22022-01-26 17:36:26 +080050 name: "libFoo",
Liz Kammer8c8e8d52022-10-31 15:53:36 -040051 srcs: ["foo.c"],
Yi Kongd5954a22022-01-26 17:36:26 +080052 static_libs: ["libBar"],
53 }
54
Liz Kammer8c8e8d52022-10-31 15:53:36 -040055 cc_library_static {
Yi Kongd5954a22022-01-26 17:36:26 +080056 name: "libBar",
Liz Kammer8c8e8d52022-10-31 15:53:36 -040057 srcs: ["bar.c"],
Yi Kongd5954a22022-01-26 17:36:26 +080058 }
59 `
60 prepareForAfdoTest := android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libTest.afdo", "TEST")
61
62 result := android.GroupFixturePreparers(
63 prepareForCcTest,
64 prepareForAfdoTest,
65 ).RunTestWithBp(t, bp)
66
Liz Kammer8c8e8d52022-10-31 15:53:36 -040067 libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared")
68 libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest")
69 libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest")
Yi Kongd5954a22022-01-26 17:36:26 +080070
Liz Kammer8c8e8d52022-10-31 15:53:36 -040071 if !hasDirectDep(result, libTest.Module(), libFoo.Module()) {
Yi Kongd5954a22022-01-26 17:36:26 +080072 t.Errorf("libTest missing dependency on afdo variant of libFoo")
73 }
74
Liz Kammer8c8e8d52022-10-31 15:53:36 -040075 if !hasDirectDep(result, libFoo.Module(), libBar.Module()) {
Yi Kongd5954a22022-01-26 17:36:26 +080076 t.Errorf("libTest missing dependency on afdo variant of libBar")
77 }
Liz Kammer8c8e8d52022-10-31 15:53:36 -040078
79 cFlags := libTest.Rule("cc").Args["cFlags"]
80 if w := "-fprofile-sample-accurate"; !strings.Contains(cFlags, w) {
81 t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", w, cFlags)
82 }
83
84 cFlags = libFoo.Rule("cc").Args["cFlags"]
85 if w := "-fprofile-sample-accurate"; !strings.Contains(cFlags, w) {
86 t.Errorf("Expected 'libFoo' to enable afdo, but did not find %q in cflags %q", w, cFlags)
87 }
88
89 cFlags = libBar.Rule("cc").Args["cFlags"]
90 if w := "-fprofile-sample-accurate"; !strings.Contains(cFlags, w) {
91 t.Errorf("Expected 'libBar' to enable afdo, but did not find %q in cflags %q", w, cFlags)
92 }
93}
94
95func TestAfdoEnabledOnStaticDepNoAfdo(t *testing.T) {
96 bp := `
97 cc_library_shared {
98 name: "libTest",
99 srcs: ["foo.c"],
100 static_libs: ["libFoo"],
101 }
102
103 cc_library_static {
104 name: "libFoo",
105 srcs: ["foo.c"],
106 static_libs: ["libBar"],
107 afdo: true, // TODO(b/256670524): remove support for enabling afdo from static only libraries, this can only propagate from shared libraries/binaries
108 }
109
110 cc_library_static {
111 name: "libBar",
112 }
113 `
114 prepareForAfdoTest := android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libFoo.afdo", "TEST")
115
116 result := android.GroupFixturePreparers(
117 prepareForCcTest,
118 prepareForAfdoTest,
119 ).RunTestWithBp(t, bp)
120
121 libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared").Module()
122 libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static")
123 libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static").Module()
124
125 if !hasDirectDep(result, libTest, libFoo.Module()) {
126 t.Errorf("libTest missing dependency on afdo variant of libFoo")
127 }
128
129 if !hasDirectDep(result, libFoo.Module(), libBar) {
130 t.Errorf("libFoo missing dependency on afdo variant of libBar")
131 }
132
133 fooVariants := result.ModuleVariantsForTests("foo")
134 for _, v := range fooVariants {
135 if strings.Contains(v, "afdo-") {
136 t.Errorf("Expected no afdo variant of 'foo', got %q", v)
137 }
138 }
139
140 cFlags := libFoo.Rule("cc").Args["cFlags"]
141 if w := "-fprofile-sample-accurate"; strings.Contains(cFlags, w) {
142 t.Errorf("Expected 'foo' to not enable afdo, but found %q in cflags %q", w, cFlags)
143 }
144
145 barVariants := result.ModuleVariantsForTests("bar")
146 for _, v := range barVariants {
147 if strings.Contains(v, "afdo-") {
148 t.Errorf("Expected no afdo variant of 'bar', got %q", v)
149 }
150 }
151
Yi Kongd5954a22022-01-26 17:36:26 +0800152}
Vinh Tran9c6080c2022-12-05 14:55:38 -0500153
154func TestAfdoEnabledWithRuntimeDepNoAfdo(t *testing.T) {
155 bp := `
156 cc_library {
157 name: "libTest",
158 srcs: ["foo.c"],
159 runtime_libs: ["libFoo"],
160 afdo: true,
161 }
162
163 cc_library {
164 name: "libFoo",
165 }
166 `
167 prepareForAfdoTest := android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libTest.afdo", "TEST")
168
169 result := android.GroupFixturePreparers(
170 prepareForCcTest,
171 prepareForAfdoTest,
172 ).RunTestWithBp(t, bp)
173
174 libFooVariants := result.ModuleVariantsForTests("libFoo")
175 for _, v := range libFooVariants {
176 if strings.Contains(v, "afdo-") {
177 t.Errorf("Expected no afdo variant of 'foo', got %q", v)
178 }
179 }
180}