blob: 65dc032d419a384e5ea69bdb47a432babadfd612 [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) {
Liz Kammer7c5d1592022-10-31 16:27:38 -040041 t.Parallel()
Yi Kongd5954a22022-01-26 17:36:26 +080042 bp := `
Liz Kammer8c8e8d52022-10-31 15:53:36 -040043 cc_library_shared {
Yi Kongd5954a22022-01-26 17:36:26 +080044 name: "libTest",
Liz Kammer8c8e8d52022-10-31 15:53:36 -040045 srcs: ["test.c"],
Yi Kongd5954a22022-01-26 17:36:26 +080046 static_libs: ["libFoo"],
47 afdo: true,
Colin Crossda4c89f2024-02-07 15:03:01 -080048 lto: {
49 thin: true,
50 },
Yi Kongd5954a22022-01-26 17:36:26 +080051 }
52
Liz Kammer8c8e8d52022-10-31 15:53:36 -040053 cc_library_static {
Yi Kongd5954a22022-01-26 17:36:26 +080054 name: "libFoo",
Liz Kammer8c8e8d52022-10-31 15:53:36 -040055 srcs: ["foo.c"],
Yi Kongd5954a22022-01-26 17:36:26 +080056 static_libs: ["libBar"],
57 }
58
Liz Kammer8c8e8d52022-10-31 15:53:36 -040059 cc_library_static {
Yi Kongd5954a22022-01-26 17:36:26 +080060 name: "libBar",
Liz Kammer8c8e8d52022-10-31 15:53:36 -040061 srcs: ["bar.c"],
Yi Kongd5954a22022-01-26 17:36:26 +080062 }
63 `
Yi Kongd5954a22022-01-26 17:36:26 +080064
65 result := android.GroupFixturePreparers(
Vinh Trancde10162023-03-09 22:07:19 -050066 PrepareForTestWithFdoProfile,
Yi Kongd5954a22022-01-26 17:36:26 +080067 prepareForCcTest,
Vinh Tran44cb78c2023-03-09 22:07:19 -050068 android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""),
69 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
70 variables.AfdoProfiles = []string{
71 "libTest://afdo_profiles_package:libTest_afdo",
72 }
73 }),
74 android.MockFS{
75 "afdo_profiles_package/Android.bp": []byte(`
76 fdo_profile {
77 name: "libTest_afdo",
Colin Crossda4c89f2024-02-07 15:03:01 -080078 arch: {
79 arm64: {
80 profile: "libTest.afdo",
81 },
82 },
Vinh Tran44cb78c2023-03-09 22:07:19 -050083 }
84 `),
85 }.AddToFixture(),
Yi Kongd5954a22022-01-26 17:36:26 +080086 ).RunTestWithBp(t, bp)
87
Colin Crossda4c89f2024-02-07 15:03:01 -080088 profileSampleCFlag := "-fprofile-sample-use=afdo_profiles_package/libTest.afdo"
89 uniqueInternalLinkageNamesCFlag := "-funique-internal-linkage-names"
90 afdoLtoLdFlag := "-Wl,-plugin-opt,-import-instr-limit=40"
91 noAfdoLtoLdFlag := "-Wl,-plugin-opt,-import-instr-limit=5"
Yi Kongd5954a22022-01-26 17:36:26 +080092
Vinh Tran44cb78c2023-03-09 22:07:19 -050093 libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared")
94 libFooAfdoVariant := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest")
95 libBarAfdoVariant := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest")
96
97 // Check cFlags of afdo-enabled module and the afdo-variant of its static deps
98 cFlags := libTest.Rule("cc").Args["cFlags"]
Colin Crossda4c89f2024-02-07 15:03:01 -080099 if !strings.Contains(cFlags, profileSampleCFlag) {
100 t.Errorf("Expected 'libTest' to enable afdo profile, but did not find %q in cflags %q", profileSampleCFlag, cFlags)
101 }
102 if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
103 t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", profileSampleCFlag, cFlags)
104 }
105
106 ldFlags := libTest.Rule("ld").Args["ldFlags"]
107 if !strings.Contains(ldFlags, afdoLtoLdFlag) {
108 t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in ldflags %q", afdoLtoLdFlag, ldFlags)
Vinh Tran44cb78c2023-03-09 22:07:19 -0500109 }
110
111 cFlags = libFooAfdoVariant.Rule("cc").Args["cFlags"]
Colin Crossda4c89f2024-02-07 15:03:01 -0800112 if !strings.Contains(cFlags, profileSampleCFlag) {
113 t.Errorf("Expected 'libFooAfdoVariant' to enable afdo profile, but did not find %q in cflags %q", profileSampleCFlag, cFlags)
114 }
115 if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
116 t.Errorf("Expected 'libFooAfdoVariant' to enable afdo, but did not find %q in cflags %q", profileSampleCFlag, cFlags)
Vinh Tran44cb78c2023-03-09 22:07:19 -0500117 }
118
119 cFlags = libBarAfdoVariant.Rule("cc").Args["cFlags"]
Colin Crossda4c89f2024-02-07 15:03:01 -0800120 if !strings.Contains(cFlags, profileSampleCFlag) {
121 t.Errorf("Expected 'libBarAfdoVariant' to enable afdo profile, but did not find %q in cflags %q", profileSampleCFlag, cFlags)
122 }
123 if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
124 t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", profileSampleCFlag, cFlags)
Vinh Tran44cb78c2023-03-09 22:07:19 -0500125 }
126
127 // Check dependency edge from afdo-enabled module to static deps
128 if !hasDirectDep(result, libTest.Module(), libFooAfdoVariant.Module()) {
Yi Kongd5954a22022-01-26 17:36:26 +0800129 t.Errorf("libTest missing dependency on afdo variant of libFoo")
130 }
131
Vinh Tran44cb78c2023-03-09 22:07:19 -0500132 if !hasDirectDep(result, libFooAfdoVariant.Module(), libBarAfdoVariant.Module()) {
Yi Kongd5954a22022-01-26 17:36:26 +0800133 t.Errorf("libTest missing dependency on afdo variant of libBar")
134 }
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400135
Vinh Tran44cb78c2023-03-09 22:07:19 -0500136 // Verify non-afdo variant exists and doesn't contain afdo
137 libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static")
138 libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static")
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400139
140 cFlags = libFoo.Rule("cc").Args["cFlags"]
Colin Crossda4c89f2024-02-07 15:03:01 -0800141 if strings.Contains(cFlags, profileSampleCFlag) {
142 t.Errorf("Expected 'libFoo' to not enable afdo profile, but found %q in cflags %q", profileSampleCFlag, cFlags)
143 }
144 if strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
145 t.Errorf("Expected 'libFoo' to not enable afdo, but found %q in cflags %q", profileSampleCFlag, cFlags)
Vinh Tran44cb78c2023-03-09 22:07:19 -0500146 }
147 cFlags = libBar.Rule("cc").Args["cFlags"]
Colin Crossda4c89f2024-02-07 15:03:01 -0800148 if strings.Contains(cFlags, profileSampleCFlag) {
149 t.Errorf("Expected 'libBar' to not enable afdo profile, but found %q in cflags %q", profileSampleCFlag, cFlags)
150 }
151 if strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
152 t.Errorf("Expected 'libBar' to not enable afdo, but found %q in cflags %q", profileSampleCFlag, cFlags)
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400153 }
154
Vinh Tran44cb78c2023-03-09 22:07:19 -0500155 // Check dependency edges of static deps
156 if hasDirectDep(result, libTest.Module(), libFoo.Module()) {
157 t.Errorf("libTest should not depend on non-afdo variant of libFoo")
158 }
159
160 if !hasDirectDep(result, libFoo.Module(), libBar.Module()) {
161 t.Errorf("libFoo missing dependency on non-afdo variant of libBar")
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400162 }
Colin Crossda4c89f2024-02-07 15:03:01 -0800163
164 // Verify that the arm variant does not have FDO since the fdo_profile module only has a profile for arm64
165 libTest32 := result.ModuleForTests("libTest", "android_arm_armv7-a-neon_shared")
166 libFooAfdoVariant32 := result.ModuleForTests("libFoo", "android_arm_armv7-a-neon_static_afdo-libTest_lto-thin")
167 libBarAfdoVariant32 := result.ModuleForTests("libBar", "android_arm_armv7-a-neon_static_afdo-libTest_lto-thin")
168
169 cFlags = libTest32.Rule("cc").Args["cFlags"]
170 if strings.Contains(cFlags, profileSampleCFlag) {
171 t.Errorf("Expected arm32 'libTest' not to enable afdo, but found %q in cflags %q", profileSampleCFlag, cFlags)
172 }
173
174 // TODO(b/324141705): when the fdo_profile module doesn't provide a source file the dependencies don't get
175 // -funique-internal-linkage-names but the module does.
176 if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
177 t.Errorf("Expected arm32 'libTest' to enable -funique-internal-linkage-names but did not find %q in cflags %q",
178 uniqueInternalLinkageNamesCFlag, cFlags)
179 }
180
181 ldFlags = libTest32.Rule("ld").Args["ldFlags"]
182 if !strings.Contains(ldFlags, noAfdoLtoLdFlag) {
183 t.Errorf("Expected arm32 'libTest' to not enable afdo, but did not find %q in ldflags %q", noAfdoLtoLdFlag, ldFlags)
184 }
185 if strings.Contains(ldFlags, afdoLtoLdFlag) {
186 t.Errorf("Expected arm32 'libTest' to not enable afdo, but found %q in ldflags %q", afdoLtoLdFlag, ldFlags)
187 }
188
189 // Check dependency edge from afdo-enabled module to static deps
190 if !hasDirectDep(result, libTest32.Module(), libFooAfdoVariant32.Module()) {
191 t.Errorf("arm32 libTest missing dependency on afdo variant of libFoo")
192 }
193
194 if !hasDirectDep(result, libFooAfdoVariant32.Module(), libBarAfdoVariant32.Module()) {
195 t.Errorf("arm32 libTest missing dependency on afdo variant of libBar")
196 }
197
198 cFlags = libFooAfdoVariant32.Rule("cc").Args["cFlags"]
199 if strings.Contains(cFlags, profileSampleCFlag) {
200 t.Errorf("Expected arm32 'libFoo' to not enable afdo profile, but found %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags)
201 }
202 if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
203 t.Errorf("Expected arm32 'libFoo' to enable afdo, but did not find %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags)
204 }
205 cFlags = libBarAfdoVariant32.Rule("cc").Args["cFlags"]
206 if strings.Contains(cFlags, profileSampleCFlag) {
207 t.Errorf("Expected arm32 'libBar' to not enable afdo profile, but found %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags)
208 }
209 if !strings.Contains(cFlags, uniqueInternalLinkageNamesCFlag) {
210 t.Errorf("Expected arm32 'libBar' to enable afdo, but did not find %q in cflags %q", uniqueInternalLinkageNamesCFlag, cFlags)
211 }
212
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400213}
214
215func TestAfdoEnabledOnStaticDepNoAfdo(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400216 t.Parallel()
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400217 bp := `
218 cc_library_shared {
219 name: "libTest",
220 srcs: ["foo.c"],
221 static_libs: ["libFoo"],
222 }
223
224 cc_library_static {
225 name: "libFoo",
226 srcs: ["foo.c"],
227 static_libs: ["libBar"],
228 afdo: true, // TODO(b/256670524): remove support for enabling afdo from static only libraries, this can only propagate from shared libraries/binaries
229 }
230
231 cc_library_static {
232 name: "libBar",
233 }
234 `
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400235
236 result := android.GroupFixturePreparers(
237 prepareForCcTest,
Vinh Trancde10162023-03-09 22:07:19 -0500238 PrepareForTestWithFdoProfile,
Vinh Tran44cb78c2023-03-09 22:07:19 -0500239 android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libFoo.afdo", ""),
240 android.MockFS{
241 "afdo_profiles_package/Android.bp": []byte(`
242 soong_namespace {
243 }
244 fdo_profile {
245 name: "libFoo_afdo",
246 profile: "libFoo.afdo",
247 }
248 `),
249 }.AddToFixture(),
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400250 ).RunTestWithBp(t, bp)
251
252 libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared").Module()
253 libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static")
254 libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static").Module()
255
256 if !hasDirectDep(result, libTest, libFoo.Module()) {
Colin Crossda4c89f2024-02-07 15:03:01 -0800257 t.Errorf("libTest missing dependency on non-afdo variant of libFoo")
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400258 }
259
260 if !hasDirectDep(result, libFoo.Module(), libBar) {
Colin Crossda4c89f2024-02-07 15:03:01 -0800261 t.Errorf("libFoo missing dependency on non-afdo variant of libBar")
Liz Kammer8c8e8d52022-10-31 15:53:36 -0400262 }
263
264 fooVariants := result.ModuleVariantsForTests("foo")
265 for _, v := range fooVariants {
266 if strings.Contains(v, "afdo-") {
267 t.Errorf("Expected no afdo variant of 'foo', got %q", v)
268 }
269 }
270
271 cFlags := libFoo.Rule("cc").Args["cFlags"]
272 if w := "-fprofile-sample-accurate"; strings.Contains(cFlags, w) {
273 t.Errorf("Expected 'foo' to not enable afdo, but found %q in cflags %q", w, cFlags)
274 }
275
276 barVariants := result.ModuleVariantsForTests("bar")
277 for _, v := range barVariants {
278 if strings.Contains(v, "afdo-") {
279 t.Errorf("Expected no afdo variant of 'bar', got %q", v)
280 }
281 }
Yi Kongd5954a22022-01-26 17:36:26 +0800282}
Vinh Tran9c6080c2022-12-05 14:55:38 -0500283
284func TestAfdoEnabledWithRuntimeDepNoAfdo(t *testing.T) {
285 bp := `
286 cc_library {
287 name: "libTest",
288 srcs: ["foo.c"],
289 runtime_libs: ["libFoo"],
290 afdo: true,
291 }
292
293 cc_library {
294 name: "libFoo",
295 }
296 `
Vinh Tran9c6080c2022-12-05 14:55:38 -0500297
298 result := android.GroupFixturePreparers(
299 prepareForCcTest,
Vinh Trancde10162023-03-09 22:07:19 -0500300 PrepareForTestWithFdoProfile,
Vinh Tran44cb78c2023-03-09 22:07:19 -0500301 android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""),
302 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
303 variables.AfdoProfiles = []string{
304 "libTest://afdo_profiles_package:libTest_afdo",
305 }
306 }),
307 android.MockFS{
308 "afdo_profiles_package/Android.bp": []byte(`
309 fdo_profile {
310 name: "libTest_afdo",
311 profile: "libTest.afdo",
312 }
313 `),
314 }.AddToFixture(),
Vinh Tran9c6080c2022-12-05 14:55:38 -0500315 ).RunTestWithBp(t, bp)
316
317 libFooVariants := result.ModuleVariantsForTests("libFoo")
318 for _, v := range libFooVariants {
319 if strings.Contains(v, "afdo-") {
320 t.Errorf("Expected no afdo variant of 'foo', got %q", v)
321 }
322 }
323}
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400324
325func TestAfdoEnabledWithMultiArchs(t *testing.T) {
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400326 bp := `
327 cc_library_shared {
328 name: "foo",
329 srcs: ["test.c"],
330 afdo: true,
331 compile_multilib: "both",
332 }
333`
334 result := android.GroupFixturePreparers(
Vinh Trancde10162023-03-09 22:07:19 -0500335 PrepareForTestWithFdoProfile,
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400336 prepareForCcTest,
Vinh Tran44cb78c2023-03-09 22:07:19 -0500337 android.FixtureAddTextFile("afdo_profiles_package/foo_arm.afdo", ""),
338 android.FixtureAddTextFile("afdo_profiles_package/foo_arm64.afdo", ""),
339 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
340 variables.AfdoProfiles = []string{
341 "foo://afdo_profiles_package:foo_afdo",
342 }
343 }),
344 android.MockFS{
345 "afdo_profiles_package/Android.bp": []byte(`
346 soong_namespace {
347 }
348 fdo_profile {
349 name: "foo_afdo",
350 arch: {
351 arm: {
352 profile: "foo_arm.afdo",
353 },
354 arm64: {
355 profile: "foo_arm64.afdo",
356 }
357 }
358 }
359 `),
360 }.AddToFixture(),
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400361 ).RunTestWithBp(t, bp)
362
363 fooArm := result.ModuleForTests("foo", "android_arm_armv7-a-neon_shared")
364 fooArmCFlags := fooArm.Rule("cc").Args["cFlags"]
Vinh Tran44cb78c2023-03-09 22:07:19 -0500365 if w := "-fprofile-sample-use=afdo_profiles_package/foo_arm.afdo"; !strings.Contains(fooArmCFlags, w) {
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400366 t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", w, fooArmCFlags)
367 }
368
369 fooArm64 := result.ModuleForTests("foo", "android_arm64_armv8-a_shared")
370 fooArm64CFlags := fooArm64.Rule("cc").Args["cFlags"]
Vinh Tran44cb78c2023-03-09 22:07:19 -0500371 if w := "-fprofile-sample-use=afdo_profiles_package/foo_arm64.afdo"; !strings.Contains(fooArm64CFlags, w) {
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400372 t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", w, fooArm64CFlags)
373 }
374}
375
376func TestMultipleAfdoRDeps(t *testing.T) {
377 t.Parallel()
378 bp := `
379 cc_library_shared {
380 name: "libTest",
381 srcs: ["test.c"],
382 static_libs: ["libFoo"],
383 afdo: true,
384 }
385
386 cc_library_shared {
387 name: "libBar",
388 srcs: ["bar.c"],
389 static_libs: ["libFoo"],
390 afdo: true,
391 }
392
393 cc_library_static {
394 name: "libFoo",
395 srcs: ["foo.c"],
396 }
397 `
398
399 result := android.GroupFixturePreparers(
Vinh Trancde10162023-03-09 22:07:19 -0500400 PrepareForTestWithFdoProfile,
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400401 prepareForCcTest,
Vinh Tran44cb78c2023-03-09 22:07:19 -0500402 android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""),
403 android.FixtureAddTextFile("afdo_profiles_package/libBar.afdo", ""),
404 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
405 variables.AfdoProfiles = []string{
406 "libTest://afdo_profiles_package:libTest_afdo",
407 "libBar://afdo_profiles_package:libBar_afdo",
408 }
409 }),
410 android.MockFS{
411 "afdo_profiles_package/Android.bp": []byte(`
412 fdo_profile {
413 name: "libTest_afdo",
414 profile: "libTest.afdo",
415 }
416 fdo_profile {
417 name: "libBar_afdo",
418 profile: "libBar.afdo",
419 }
420 `),
421 }.AddToFixture(),
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400422 ).RunTestWithBp(t, bp)
423
Vinh Tran44cb78c2023-03-09 22:07:19 -0500424 expectedCFlagLibTest := "-fprofile-sample-use=afdo_profiles_package/libTest.afdo"
425 expectedCFlagLibBar := "-fprofile-sample-use=afdo_profiles_package/libBar.afdo"
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400426
427 libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared")
Vinh Tran44cb78c2023-03-09 22:07:19 -0500428 libFooAfdoVariantWithLibTest := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest")
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400429
430 libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_shared")
Vinh Tran44cb78c2023-03-09 22:07:19 -0500431 libFooAfdoVariantWithLibBar := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libBar")
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400432
Vinh Tran44cb78c2023-03-09 22:07:19 -0500433 // Check cFlags of afdo-enabled module and the afdo-variant of its static deps
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400434 cFlags := libTest.Rule("cc").Args["cFlags"]
435 if !strings.Contains(cFlags, expectedCFlagLibTest) {
436 t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibTest, cFlags)
437 }
438 cFlags = libBar.Rule("cc").Args["cFlags"]
439 if !strings.Contains(cFlags, expectedCFlagLibBar) {
Vinh Tran44cb78c2023-03-09 22:07:19 -0500440 t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibBar, cFlags)
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400441 }
442
Vinh Tran44cb78c2023-03-09 22:07:19 -0500443 cFlags = libFooAfdoVariantWithLibTest.Rule("cc").Args["cFlags"]
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400444 if !strings.Contains(cFlags, expectedCFlagLibTest) {
Vinh Tran44cb78c2023-03-09 22:07:19 -0500445 t.Errorf("Expected 'libFooAfdoVariantWithLibTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibTest, cFlags)
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400446 }
447
Vinh Tran44cb78c2023-03-09 22:07:19 -0500448 cFlags = libFooAfdoVariantWithLibBar.Rule("cc").Args["cFlags"]
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400449 if !strings.Contains(cFlags, expectedCFlagLibBar) {
Vinh Tran44cb78c2023-03-09 22:07:19 -0500450 t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibBar, cFlags)
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400451 }
452
453 // Check dependency edges of static deps
Vinh Tran44cb78c2023-03-09 22:07:19 -0500454 if !hasDirectDep(result, libTest.Module(), libFooAfdoVariantWithLibTest.Module()) {
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400455 t.Errorf("libTest missing dependency on afdo variant of libFoo")
456 }
457
Vinh Tran44cb78c2023-03-09 22:07:19 -0500458 if !hasDirectDep(result, libBar.Module(), libFooAfdoVariantWithLibBar.Module()) {
459 t.Errorf("libFoo missing dependency on non-afdo variant of libBar")
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400460 }
461}
Yabin Cui01c44562023-04-20 14:07:29 -0700462
463func TestAfdoDepsWithoutProfile(t *testing.T) {
464 t.Parallel()
465 bp := `
466 cc_library_shared {
467 name: "libTest",
468 srcs: ["test.c"],
469 static_libs: ["libFoo"],
470 afdo: true,
471 }
472
473 cc_library_static {
474 name: "libFoo",
475 srcs: ["foo.c"],
476 static_libs: ["libBar"],
477 }
478
479 cc_library_static {
480 name: "libBar",
481 srcs: ["bar.c"],
482 }
483 `
484
485 result := android.GroupFixturePreparers(
486 PrepareForTestWithFdoProfile,
487 prepareForCcTest,
488 ).RunTestWithBp(t, bp)
489
490 // Even without a profile path, the afdo enabled libraries should be built with
491 // -funique-internal-linkage-names.
492 expectedCFlag := "-funique-internal-linkage-names"
493
494 libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared")
495 libFooAfdoVariant := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest")
496 libBarAfdoVariant := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest")
497
498 // Check cFlags of afdo-enabled module and the afdo-variant of its static deps
499 cFlags := libTest.Rule("cc").Args["cFlags"]
500 if !strings.Contains(cFlags, expectedCFlag) {
501 t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags)
502 }
503
504 cFlags = libFooAfdoVariant.Rule("cc").Args["cFlags"]
505 if !strings.Contains(cFlags, expectedCFlag) {
506 t.Errorf("Expected 'libFooAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags)
507 }
508
509 cFlags = libBarAfdoVariant.Rule("cc").Args["cFlags"]
510 if !strings.Contains(cFlags, expectedCFlag) {
511 t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags)
512 }
513 // Check dependency edge from afdo-enabled module to static deps
514 if !hasDirectDep(result, libTest.Module(), libFooAfdoVariant.Module()) {
515 t.Errorf("libTest missing dependency on afdo variant of libFoo")
516 }
517
518 if !hasDirectDep(result, libFooAfdoVariant.Module(), libBarAfdoVariant.Module()) {
519 t.Errorf("libTest missing dependency on afdo variant of libBar")
520 }
521
522 // Verify non-afdo variant exists and doesn't contain afdo
523 libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static")
524 libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static")
525
526 cFlags = libFoo.Rule("cc").Args["cFlags"]
527 if strings.Contains(cFlags, expectedCFlag) {
528 t.Errorf("Expected 'libFoo' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags)
529 }
530 cFlags = libBar.Rule("cc").Args["cFlags"]
531 if strings.Contains(cFlags, expectedCFlag) {
532 t.Errorf("Expected 'libBar' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags)
533 }
534
535 // Check dependency edges of static deps
536 if hasDirectDep(result, libTest.Module(), libFoo.Module()) {
537 t.Errorf("libTest should not depend on non-afdo variant of libFoo")
538 }
539
540 if !hasDirectDep(result, libFoo.Module(), libBar.Module()) {
541 t.Errorf("libFoo missing dependency on non-afdo variant of libBar")
542 }
543}