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 | ` |
| 61 | prepareForAfdoTest := android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libTest.afdo", "TEST") |
| 62 | |
| 63 | result := android.GroupFixturePreparers( |
| 64 | prepareForCcTest, |
| 65 | prepareForAfdoTest, |
| 66 | ).RunTestWithBp(t, bp) |
| 67 | |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 68 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared") |
| 69 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static_afdo-libTest") |
| 70 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static_afdo-libTest") |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 71 | |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 72 | if !hasDirectDep(result, libTest.Module(), libFoo.Module()) { |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 73 | t.Errorf("libTest missing dependency on afdo variant of libFoo") |
| 74 | } |
| 75 | |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 76 | if !hasDirectDep(result, libFoo.Module(), libBar.Module()) { |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 77 | t.Errorf("libTest missing dependency on afdo variant of libBar") |
| 78 | } |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 79 | |
| 80 | cFlags := libTest.Rule("cc").Args["cFlags"] |
| 81 | if w := "-fprofile-sample-accurate"; !strings.Contains(cFlags, w) { |
| 82 | t.Errorf("Expected 'libTest' to enable afdo, but did not find %q in cflags %q", w, cFlags) |
| 83 | } |
| 84 | |
| 85 | cFlags = libFoo.Rule("cc").Args["cFlags"] |
| 86 | if w := "-fprofile-sample-accurate"; !strings.Contains(cFlags, w) { |
| 87 | t.Errorf("Expected 'libFoo' to enable afdo, but did not find %q in cflags %q", w, cFlags) |
| 88 | } |
| 89 | |
| 90 | cFlags = libBar.Rule("cc").Args["cFlags"] |
| 91 | if w := "-fprofile-sample-accurate"; !strings.Contains(cFlags, w) { |
| 92 | t.Errorf("Expected 'libBar' to enable afdo, but did not find %q in cflags %q", w, cFlags) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestAfdoEnabledOnStaticDepNoAfdo(t *testing.T) { |
Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame^] | 97 | t.Parallel() |
Liz Kammer | 8c8e8d5 | 2022-10-31 15:53:36 -0400 | [diff] [blame] | 98 | bp := ` |
| 99 | cc_library_shared { |
| 100 | name: "libTest", |
| 101 | srcs: ["foo.c"], |
| 102 | static_libs: ["libFoo"], |
| 103 | } |
| 104 | |
| 105 | cc_library_static { |
| 106 | name: "libFoo", |
| 107 | srcs: ["foo.c"], |
| 108 | static_libs: ["libBar"], |
| 109 | afdo: true, // TODO(b/256670524): remove support for enabling afdo from static only libraries, this can only propagate from shared libraries/binaries |
| 110 | } |
| 111 | |
| 112 | cc_library_static { |
| 113 | name: "libBar", |
| 114 | } |
| 115 | ` |
| 116 | prepareForAfdoTest := android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/libFoo.afdo", "TEST") |
| 117 | |
| 118 | result := android.GroupFixturePreparers( |
| 119 | prepareForCcTest, |
| 120 | prepareForAfdoTest, |
| 121 | ).RunTestWithBp(t, bp) |
| 122 | |
| 123 | libTest := result.ModuleForTests("libTest", "android_arm64_armv8-a_shared").Module() |
| 124 | libFoo := result.ModuleForTests("libFoo", "android_arm64_armv8-a_static") |
| 125 | libBar := result.ModuleForTests("libBar", "android_arm64_armv8-a_static").Module() |
| 126 | |
| 127 | if !hasDirectDep(result, libTest, libFoo.Module()) { |
| 128 | t.Errorf("libTest missing dependency on afdo variant of libFoo") |
| 129 | } |
| 130 | |
| 131 | if !hasDirectDep(result, libFoo.Module(), libBar) { |
| 132 | t.Errorf("libFoo missing dependency on afdo variant of libBar") |
| 133 | } |
| 134 | |
| 135 | fooVariants := result.ModuleVariantsForTests("foo") |
| 136 | for _, v := range fooVariants { |
| 137 | if strings.Contains(v, "afdo-") { |
| 138 | t.Errorf("Expected no afdo variant of 'foo', got %q", v) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | cFlags := libFoo.Rule("cc").Args["cFlags"] |
| 143 | if w := "-fprofile-sample-accurate"; strings.Contains(cFlags, w) { |
| 144 | t.Errorf("Expected 'foo' to not enable afdo, but found %q in cflags %q", w, cFlags) |
| 145 | } |
| 146 | |
| 147 | barVariants := result.ModuleVariantsForTests("bar") |
| 148 | for _, v := range barVariants { |
| 149 | if strings.Contains(v, "afdo-") { |
| 150 | t.Errorf("Expected no afdo variant of 'bar', got %q", v) |
| 151 | } |
| 152 | } |
| 153 | |
Yi Kong | d5954a2 | 2022-01-26 17:36:26 +0800 | [diff] [blame] | 154 | } |