| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package cc | 
|  | 16 |  | 
|  | 17 | import ( | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 18 | "strings" | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 19 | "testing" | 
|  | 20 |  | 
|  | 21 | "android/soong/android" | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 22 |  | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 23 | "github.com/google/blueprint" | 
|  | 24 | ) | 
|  | 25 |  | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 26 | type visitDirectDepsInterface interface { | 
|  | 27 | VisitDirectDeps(blueprint.Module, func(dep blueprint.Module)) | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | func 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 Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 40 | func TestAfdoDeps(t *testing.T) { | 
| Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame] | 41 | t.Parallel() | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 42 | bp := ` | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 43 | cc_library_shared { | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 44 | name: "libTest", | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 45 | srcs: ["test.c"], | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 46 | static_libs: ["libFoo"], | 
|  | 47 | afdo: true, | 
|  | 48 | } | 
|  | 49 |  | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 50 | cc_library_static { | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 51 | name: "libFoo", | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 52 | srcs: ["foo.c"], | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 53 | static_libs: ["libBar"], | 
|  | 54 | } | 
|  | 55 |  | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 56 | cc_library_static { | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 57 | name: "libBar", | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 58 | srcs: ["bar.c"], | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 59 | } | 
|  | 60 | ` | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 61 |  | 
|  | 62 | result := android.GroupFixturePreparers( | 
| Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 63 | PrepareForTestWithFdoProfile, | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 64 | prepareForCcTest, | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 65 | android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""), | 
|  | 66 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { | 
|  | 67 | variables.AfdoProfiles = []string{ | 
|  | 68 | "libTest://afdo_profiles_package:libTest_afdo", | 
|  | 69 | } | 
|  | 70 | }), | 
|  | 71 | android.MockFS{ | 
|  | 72 | "afdo_profiles_package/Android.bp": []byte(` | 
|  | 73 | fdo_profile { | 
|  | 74 | name: "libTest_afdo", | 
|  | 75 | profile: "libTest.afdo", | 
|  | 76 | } | 
|  | 77 | `), | 
|  | 78 | }.AddToFixture(), | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 79 | ).RunTestWithBp(t, bp) | 
|  | 80 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 81 | expectedCFlag := "-fprofile-sample-use=afdo_profiles_package/libTest.afdo" | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 82 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 83 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared") | 
|  | 84 | libFooAfdoVariant := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest") | 
|  | 85 | libBarAfdoVariant := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest") | 
|  | 86 |  | 
|  | 87 | // Check cFlags of afdo-enabled module and the afdo-variant of its static deps | 
|  | 88 | cFlags := libTest.Rule("cc").Args["cFlags"] | 
|  | 89 | if !strings.Contains(cFlags, expectedCFlag) { | 
|  | 90 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | cFlags = libFooAfdoVariant.Rule("cc").Args["cFlags"] | 
|  | 94 | if !strings.Contains(cFlags, expectedCFlag) { | 
|  | 95 | t.Errorf("Expected 'libFooAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | cFlags = libBarAfdoVariant.Rule("cc").Args["cFlags"] | 
|  | 99 | if !strings.Contains(cFlags, expectedCFlag) { | 
|  | 100 | t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | // Check dependency edge from afdo-enabled module to static deps | 
|  | 104 | if !hasDirectDep(result, libTest.Module(), libFooAfdoVariant.Module()) { | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 105 | t.Errorf("libTest missing dependency on afdo variant of libFoo") | 
|  | 106 | } | 
|  | 107 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 108 | if !hasDirectDep(result, libFooAfdoVariant.Module(), libBarAfdoVariant.Module()) { | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 109 | t.Errorf("libTest missing dependency on afdo variant of libBar") | 
|  | 110 | } | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 111 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 112 | // Verify non-afdo variant exists and doesn't contain afdo | 
|  | 113 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static") | 
|  | 114 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static") | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 115 |  | 
|  | 116 | cFlags = libFoo.Rule("cc").Args["cFlags"] | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 117 | if strings.Contains(cFlags, expectedCFlag) { | 
|  | 118 | t.Errorf("Expected 'libFoo' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags) | 
|  | 119 | } | 
|  | 120 | cFlags = libBar.Rule("cc").Args["cFlags"] | 
|  | 121 | if strings.Contains(cFlags, expectedCFlag) { | 
|  | 122 | t.Errorf("Expected 'libBar' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags) | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 125 | // Check dependency edges of static deps | 
|  | 126 | if hasDirectDep(result, libTest.Module(), libFoo.Module()) { | 
|  | 127 | t.Errorf("libTest should not depend on non-afdo variant of libFoo") | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | if !hasDirectDep(result, libFoo.Module(), libBar.Module()) { | 
|  | 131 | t.Errorf("libFoo missing dependency on non-afdo variant of libBar") | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 132 | } | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | func TestAfdoEnabledOnStaticDepNoAfdo(t *testing.T) { | 
| Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame] | 136 | t.Parallel() | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 137 | bp := ` | 
|  | 138 | cc_library_shared { | 
|  | 139 | name: "libTest", | 
|  | 140 | srcs: ["foo.c"], | 
|  | 141 | static_libs: ["libFoo"], | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | cc_library_static { | 
|  | 145 | name: "libFoo", | 
|  | 146 | srcs: ["foo.c"], | 
|  | 147 | static_libs: ["libBar"], | 
|  | 148 | afdo: true, // TODO(b/256670524): remove support for enabling afdo from static only libraries, this can only propagate from shared libraries/binaries | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | cc_library_static { | 
|  | 152 | name: "libBar", | 
|  | 153 | } | 
|  | 154 | ` | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 155 |  | 
|  | 156 | result := android.GroupFixturePreparers( | 
|  | 157 | prepareForCcTest, | 
| Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 158 | PrepareForTestWithFdoProfile, | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 159 | android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libFoo.afdo", ""), | 
|  | 160 | android.MockFS{ | 
|  | 161 | "afdo_profiles_package/Android.bp": []byte(` | 
|  | 162 | soong_namespace { | 
|  | 163 | } | 
|  | 164 | fdo_profile { | 
|  | 165 | name: "libFoo_afdo", | 
|  | 166 | profile: "libFoo.afdo", | 
|  | 167 | } | 
|  | 168 | `), | 
|  | 169 | }.AddToFixture(), | 
| Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 170 | ).RunTestWithBp(t, bp) | 
|  | 171 |  | 
|  | 172 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared").Module() | 
|  | 173 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static") | 
|  | 174 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static").Module() | 
|  | 175 |  | 
|  | 176 | if !hasDirectDep(result, libTest, libFoo.Module()) { | 
|  | 177 | t.Errorf("libTest missing dependency on afdo variant of libFoo") | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | if !hasDirectDep(result, libFoo.Module(), libBar) { | 
|  | 181 | t.Errorf("libFoo missing dependency on afdo variant of libBar") | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | fooVariants := result.ModuleVariantsForTests("foo") | 
|  | 185 | for _, v := range fooVariants { | 
|  | 186 | if strings.Contains(v, "afdo-") { | 
|  | 187 | t.Errorf("Expected no afdo variant of 'foo', got %q", v) | 
|  | 188 | } | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | cFlags := libFoo.Rule("cc").Args["cFlags"] | 
|  | 192 | if w := "-fprofile-sample-accurate"; strings.Contains(cFlags, w) { | 
|  | 193 | t.Errorf("Expected 'foo' to not enable afdo, but found %q in cflags %q", w, cFlags) | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | barVariants := result.ModuleVariantsForTests("bar") | 
|  | 197 | for _, v := range barVariants { | 
|  | 198 | if strings.Contains(v, "afdo-") { | 
|  | 199 | t.Errorf("Expected no afdo variant of 'bar', got %q", v) | 
|  | 200 | } | 
|  | 201 | } | 
| Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 202 | } | 
| Vinh Tran | 9c6080c | 2022-12-05 14:55:38 -0500 | [diff] [blame] | 203 |  | 
|  | 204 | func TestAfdoEnabledWithRuntimeDepNoAfdo(t *testing.T) { | 
|  | 205 | bp := ` | 
|  | 206 | cc_library { | 
|  | 207 | name: "libTest", | 
|  | 208 | srcs: ["foo.c"], | 
|  | 209 | runtime_libs: ["libFoo"], | 
|  | 210 | afdo: true, | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | cc_library { | 
|  | 214 | name: "libFoo", | 
|  | 215 | } | 
|  | 216 | ` | 
| Vinh Tran | 9c6080c | 2022-12-05 14:55:38 -0500 | [diff] [blame] | 217 |  | 
|  | 218 | result := android.GroupFixturePreparers( | 
|  | 219 | prepareForCcTest, | 
| Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 220 | PrepareForTestWithFdoProfile, | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 221 | android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""), | 
|  | 222 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { | 
|  | 223 | variables.AfdoProfiles = []string{ | 
|  | 224 | "libTest://afdo_profiles_package:libTest_afdo", | 
|  | 225 | } | 
|  | 226 | }), | 
|  | 227 | android.MockFS{ | 
|  | 228 | "afdo_profiles_package/Android.bp": []byte(` | 
|  | 229 | fdo_profile { | 
|  | 230 | name: "libTest_afdo", | 
|  | 231 | profile: "libTest.afdo", | 
|  | 232 | } | 
|  | 233 | `), | 
|  | 234 | }.AddToFixture(), | 
| Vinh Tran | 9c6080c | 2022-12-05 14:55:38 -0500 | [diff] [blame] | 235 | ).RunTestWithBp(t, bp) | 
|  | 236 |  | 
|  | 237 | libFooVariants := result.ModuleVariantsForTests("libFoo") | 
|  | 238 | for _, v := range libFooVariants { | 
|  | 239 | if strings.Contains(v, "afdo-") { | 
|  | 240 | t.Errorf("Expected no afdo variant of 'foo', got %q", v) | 
|  | 241 | } | 
|  | 242 | } | 
|  | 243 | } | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 244 |  | 
|  | 245 | func TestAfdoEnabledWithMultiArchs(t *testing.T) { | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 246 | bp := ` | 
|  | 247 | cc_library_shared { | 
|  | 248 | name: "foo", | 
|  | 249 | srcs: ["test.c"], | 
|  | 250 | afdo: true, | 
|  | 251 | compile_multilib: "both", | 
|  | 252 | } | 
|  | 253 | ` | 
|  | 254 | result := android.GroupFixturePreparers( | 
| Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 255 | PrepareForTestWithFdoProfile, | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 256 | prepareForCcTest, | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 257 | android.FixtureAddTextFile("afdo_profiles_package/foo_arm.afdo", ""), | 
|  | 258 | android.FixtureAddTextFile("afdo_profiles_package/foo_arm64.afdo", ""), | 
|  | 259 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { | 
|  | 260 | variables.AfdoProfiles = []string{ | 
|  | 261 | "foo://afdo_profiles_package:foo_afdo", | 
|  | 262 | } | 
|  | 263 | }), | 
|  | 264 | android.MockFS{ | 
|  | 265 | "afdo_profiles_package/Android.bp": []byte(` | 
|  | 266 | soong_namespace { | 
|  | 267 | } | 
|  | 268 | fdo_profile { | 
|  | 269 | name: "foo_afdo", | 
|  | 270 | arch: { | 
|  | 271 | arm: { | 
|  | 272 | profile: "foo_arm.afdo", | 
|  | 273 | }, | 
|  | 274 | arm64: { | 
|  | 275 | profile: "foo_arm64.afdo", | 
|  | 276 | } | 
|  | 277 | } | 
|  | 278 | } | 
|  | 279 | `), | 
|  | 280 | }.AddToFixture(), | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 281 | ).RunTestWithBp(t, bp) | 
|  | 282 |  | 
|  | 283 | fooArm := result.ModuleForTests("foo", "android_arm_armv7-a-neon_shared") | 
|  | 284 | fooArmCFlags := fooArm.Rule("cc").Args["cFlags"] | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 285 | if w := "-fprofile-sample-use=afdo_profiles_package/foo_arm.afdo"; !strings.Contains(fooArmCFlags, w) { | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 286 | t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", w, fooArmCFlags) | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | fooArm64 := result.ModuleForTests("foo", "android_arm64_armv8-a_shared") | 
|  | 290 | fooArm64CFlags := fooArm64.Rule("cc").Args["cFlags"] | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 291 | if w := "-fprofile-sample-use=afdo_profiles_package/foo_arm64.afdo"; !strings.Contains(fooArm64CFlags, w) { | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 292 | t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", w, fooArm64CFlags) | 
|  | 293 | } | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | func TestMultipleAfdoRDeps(t *testing.T) { | 
|  | 297 | t.Parallel() | 
|  | 298 | bp := ` | 
|  | 299 | cc_library_shared { | 
|  | 300 | name: "libTest", | 
|  | 301 | srcs: ["test.c"], | 
|  | 302 | static_libs: ["libFoo"], | 
|  | 303 | afdo: true, | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | cc_library_shared { | 
|  | 307 | name: "libBar", | 
|  | 308 | srcs: ["bar.c"], | 
|  | 309 | static_libs: ["libFoo"], | 
|  | 310 | afdo: true, | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | cc_library_static { | 
|  | 314 | name: "libFoo", | 
|  | 315 | srcs: ["foo.c"], | 
|  | 316 | } | 
|  | 317 | ` | 
|  | 318 |  | 
|  | 319 | result := android.GroupFixturePreparers( | 
| Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 320 | PrepareForTestWithFdoProfile, | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 321 | prepareForCcTest, | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 322 | android.FixtureAddTextFile("afdo_profiles_package/libTest.afdo", ""), | 
|  | 323 | android.FixtureAddTextFile("afdo_profiles_package/libBar.afdo", ""), | 
|  | 324 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { | 
|  | 325 | variables.AfdoProfiles = []string{ | 
|  | 326 | "libTest://afdo_profiles_package:libTest_afdo", | 
|  | 327 | "libBar://afdo_profiles_package:libBar_afdo", | 
|  | 328 | } | 
|  | 329 | }), | 
|  | 330 | android.MockFS{ | 
|  | 331 | "afdo_profiles_package/Android.bp": []byte(` | 
|  | 332 | fdo_profile { | 
|  | 333 | name: "libTest_afdo", | 
|  | 334 | profile: "libTest.afdo", | 
|  | 335 | } | 
|  | 336 | fdo_profile { | 
|  | 337 | name: "libBar_afdo", | 
|  | 338 | profile: "libBar.afdo", | 
|  | 339 | } | 
|  | 340 | `), | 
|  | 341 | }.AddToFixture(), | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 342 | ).RunTestWithBp(t, bp) | 
|  | 343 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 344 | expectedCFlagLibTest := "-fprofile-sample-use=afdo_profiles_package/libTest.afdo" | 
|  | 345 | expectedCFlagLibBar := "-fprofile-sample-use=afdo_profiles_package/libBar.afdo" | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 346 |  | 
|  | 347 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared") | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 348 | libFooAfdoVariantWithLibTest := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest") | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 349 |  | 
|  | 350 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_shared") | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 351 | libFooAfdoVariantWithLibBar := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libBar") | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 352 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 353 | // Check cFlags of afdo-enabled module and the afdo-variant of its static deps | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 354 | cFlags := libTest.Rule("cc").Args["cFlags"] | 
|  | 355 | if !strings.Contains(cFlags, expectedCFlagLibTest) { | 
|  | 356 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibTest, cFlags) | 
|  | 357 | } | 
|  | 358 | cFlags = libBar.Rule("cc").Args["cFlags"] | 
|  | 359 | if !strings.Contains(cFlags, expectedCFlagLibBar) { | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 360 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibBar, cFlags) | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 363 | cFlags = libFooAfdoVariantWithLibTest.Rule("cc").Args["cFlags"] | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 364 | if !strings.Contains(cFlags, expectedCFlagLibTest) { | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 365 | t.Errorf("Expected 'libFooAfdoVariantWithLibTest' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibTest, cFlags) | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 368 | cFlags = libFooAfdoVariantWithLibBar.Rule("cc").Args["cFlags"] | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 369 | if !strings.Contains(cFlags, expectedCFlagLibBar) { | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 370 | t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlagLibBar, cFlags) | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 371 | } | 
|  | 372 |  | 
|  | 373 | // Check dependency edges of static deps | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 374 | if !hasDirectDep(result, libTest.Module(), libFooAfdoVariantWithLibTest.Module()) { | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 375 | t.Errorf("libTest missing dependency on afdo variant of libFoo") | 
|  | 376 | } | 
|  | 377 |  | 
| Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 378 | if !hasDirectDep(result, libBar.Module(), libFooAfdoVariantWithLibBar.Module()) { | 
|  | 379 | t.Errorf("libFoo missing dependency on non-afdo variant of libBar") | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 380 | } | 
|  | 381 | } | 
| Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 382 |  | 
|  | 383 | func TestAfdoDepsWithoutProfile(t *testing.T) { | 
|  | 384 | t.Parallel() | 
|  | 385 | bp := ` | 
|  | 386 | cc_library_shared { | 
|  | 387 | name: "libTest", | 
|  | 388 | srcs: ["test.c"], | 
|  | 389 | static_libs: ["libFoo"], | 
|  | 390 | afdo: true, | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | cc_library_static { | 
|  | 394 | name: "libFoo", | 
|  | 395 | srcs: ["foo.c"], | 
|  | 396 | static_libs: ["libBar"], | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | cc_library_static { | 
|  | 400 | name: "libBar", | 
|  | 401 | srcs: ["bar.c"], | 
|  | 402 | } | 
|  | 403 | ` | 
|  | 404 |  | 
|  | 405 | result := android.GroupFixturePreparers( | 
|  | 406 | PrepareForTestWithFdoProfile, | 
|  | 407 | prepareForCcTest, | 
|  | 408 | ).RunTestWithBp(t, bp) | 
|  | 409 |  | 
|  | 410 | // Even without a profile path, the afdo enabled libraries should be built with | 
|  | 411 | // -funique-internal-linkage-names. | 
|  | 412 | expectedCFlag := "-funique-internal-linkage-names" | 
|  | 413 |  | 
|  | 414 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared") | 
|  | 415 | libFooAfdoVariant := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest") | 
|  | 416 | libBarAfdoVariant := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest") | 
|  | 417 |  | 
|  | 418 | // Check cFlags of afdo-enabled module and the afdo-variant of its static deps | 
|  | 419 | cFlags := libTest.Rule("cc").Args["cFlags"] | 
|  | 420 | if !strings.Contains(cFlags, expectedCFlag) { | 
|  | 421 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | cFlags = libFooAfdoVariant.Rule("cc").Args["cFlags"] | 
|  | 425 | if !strings.Contains(cFlags, expectedCFlag) { | 
|  | 426 | t.Errorf("Expected 'libFooAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) | 
|  | 427 | } | 
|  | 428 |  | 
|  | 429 | cFlags = libBarAfdoVariant.Rule("cc").Args["cFlags"] | 
|  | 430 | if !strings.Contains(cFlags, expectedCFlag) { | 
|  | 431 | t.Errorf("Expected 'libBarAfdoVariant' to enable afdo, but did not find %q in cflags %q", expectedCFlag, cFlags) | 
|  | 432 | } | 
|  | 433 | // Check dependency edge from afdo-enabled module to static deps | 
|  | 434 | if !hasDirectDep(result, libTest.Module(), libFooAfdoVariant.Module()) { | 
|  | 435 | t.Errorf("libTest missing dependency on afdo variant of libFoo") | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | if !hasDirectDep(result, libFooAfdoVariant.Module(), libBarAfdoVariant.Module()) { | 
|  | 439 | t.Errorf("libTest missing dependency on afdo variant of libBar") | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | // Verify non-afdo variant exists and doesn't contain afdo | 
|  | 443 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static") | 
|  | 444 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static") | 
|  | 445 |  | 
|  | 446 | cFlags = libFoo.Rule("cc").Args["cFlags"] | 
|  | 447 | if strings.Contains(cFlags, expectedCFlag) { | 
|  | 448 | t.Errorf("Expected 'libFoo' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags) | 
|  | 449 | } | 
|  | 450 | cFlags = libBar.Rule("cc").Args["cFlags"] | 
|  | 451 | if strings.Contains(cFlags, expectedCFlag) { | 
|  | 452 | t.Errorf("Expected 'libBar' to not enable afdo, but found %q in cflags %q", expectedCFlag, cFlags) | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | // Check dependency edges of static deps | 
|  | 456 | if hasDirectDep(result, libTest.Module(), libFoo.Module()) { | 
|  | 457 | t.Errorf("libTest should not depend on non-afdo variant of libFoo") | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | if !hasDirectDep(result, libFoo.Module(), libBar.Module()) { | 
|  | 461 | t.Errorf("libFoo missing dependency on non-afdo variant of libBar") | 
|  | 462 | } | 
|  | 463 | } |