blob: 7b7fe8c8a768c3f1149149b172d457b557d2c640 [file] [log] [blame]
Liz Kammer3b0f36c2022-09-16 12:39:27 -04001// Copyright 2021 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 Kammer3b0f36c2022-09-16 12:39:27 -040018 "strings"
19 "testing"
20
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +000021 "android/soong/android"
22
Liz Kammer3b0f36c2022-09-16 12:39:27 -040023 "github.com/google/blueprint"
24)
25
Yi Kong577a73a2023-10-05 05:03:42 +000026var LTOPreparer = android.GroupFixturePreparers(
Yi Kongf1d0ba42023-06-12 14:50:26 +080027 prepareForCcTest,
Yi Kong577a73a2023-10-05 05:03:42 +000028)
29
30func hasDep(result *android.TestResult, m android.Module, wantDep android.Module) bool {
31 var found bool
32 result.VisitDirectDeps(m, func(dep blueprint.Module) {
33 if dep == wantDep {
34 found = true
35 }
36 })
37 return found
38}
Yi Kongf1d0ba42023-06-12 14:50:26 +080039
Liz Kammer3b0f36c2022-09-16 12:39:27 -040040func TestThinLtoDeps(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -040041 t.Parallel()
Liz Kammer3b0f36c2022-09-16 12:39:27 -040042 bp := `
Liz Kammer81d09502022-10-31 14:44:46 -040043 cc_library_shared {
Liz Kammer3b0f36c2022-09-16 12:39:27 -040044 name: "lto_enabled",
45 srcs: ["src.c"],
Liz Kammer81d09502022-10-31 14:44:46 -040046 static_libs: ["foo", "lib_never_lto"],
Liz Kammer3b0f36c2022-09-16 12:39:27 -040047 shared_libs: ["bar"],
Liz Kammer3b0f36c2022-09-16 12:39:27 -040048 }
Liz Kammer81d09502022-10-31 14:44:46 -040049 cc_library_static {
Liz Kammer3b0f36c2022-09-16 12:39:27 -040050 name: "foo",
51 static_libs: ["baz"],
52 }
Liz Kammer81d09502022-10-31 14:44:46 -040053 cc_library_shared {
Liz Kammer3b0f36c2022-09-16 12:39:27 -040054 name: "bar",
55 static_libs: ["qux"],
56 }
Liz Kammer81d09502022-10-31 14:44:46 -040057 cc_library_static {
Liz Kammer3b0f36c2022-09-16 12:39:27 -040058 name: "baz",
59 }
Liz Kammer81d09502022-10-31 14:44:46 -040060 cc_library_static {
Liz Kammer3b0f36c2022-09-16 12:39:27 -040061 name: "qux",
62 }
Liz Kammer81d09502022-10-31 14:44:46 -040063 cc_library_static {
64 name: "lib_never_lto",
65 lto: {
66 never: true,
67 },
68 }
Liz Kammer3b0f36c2022-09-16 12:39:27 -040069`
70
Yi Kong577a73a2023-10-05 05:03:42 +000071 result := LTOPreparer.RunTestWithBp(t, bp)
Liz Kammer3b0f36c2022-09-16 12:39:27 -040072
73 libLto := result.ModuleForTests("lto_enabled", "android_arm64_armv8-a_shared").Module()
Liz Kammer3b0f36c2022-09-16 12:39:27 -040074
Yi Kong577a73a2023-10-05 05:03:42 +000075 libFoo := result.ModuleForTests("foo", "android_arm64_armv8-a_static").Module()
76 if !hasDep(result, libLto, libFoo) {
77 t.Errorf("'lto_enabled' missing dependency on the default variant of 'foo'")
Liz Kammer3b0f36c2022-09-16 12:39:27 -040078 }
79
Yi Kong577a73a2023-10-05 05:03:42 +000080 libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static").Module()
81 if !hasDep(result, libFoo, libBaz) {
82 t.Errorf("'foo' missing dependency on the default variant of transitive dep 'baz'")
Liz Kammer3b0f36c2022-09-16 12:39:27 -040083 }
84
Yi Kong577a73a2023-10-05 05:03:42 +000085 libNeverLto := result.ModuleForTests("lib_never_lto", "android_arm64_armv8-a_static").Module()
86 if !hasDep(result, libLto, libNeverLto) {
87 t.Errorf("'lto_enabled' missing dependency on the default variant of 'lib_never_lto'")
Liz Kammer81d09502022-10-31 14:44:46 -040088 }
89
90 libBar := result.ModuleForTests("bar", "android_arm64_armv8-a_shared").Module()
Yi Kong577a73a2023-10-05 05:03:42 +000091 if !hasDep(result, libLto, libBar) {
92 t.Errorf("'lto_enabled' missing dependency on the default variant of 'bar'")
Liz Kammer3b0f36c2022-09-16 12:39:27 -040093 }
94
95 barVariants := result.ModuleVariantsForTests("bar")
96 for _, v := range barVariants {
Yi Kong577a73a2023-10-05 05:03:42 +000097 if strings.Contains(v, "lto-none") {
98 t.Errorf("Expected variants for 'bar' to not contain 'lto-none', but found %q", v)
Liz Kammer3b0f36c2022-09-16 12:39:27 -040099 }
100 }
101 quxVariants := result.ModuleVariantsForTests("qux")
102 for _, v := range quxVariants {
Yi Kong577a73a2023-10-05 05:03:42 +0000103 if strings.Contains(v, "lto-none") {
104 t.Errorf("Expected variants for 'qux' to not contain 'lto-none', but found %q", v)
Liz Kammer3b0f36c2022-09-16 12:39:27 -0400105 }
106 }
107}
Liz Kammer81d09502022-10-31 14:44:46 -0400108
109func TestThinLtoOnlyOnStaticDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400110 t.Parallel()
Liz Kammer81d09502022-10-31 14:44:46 -0400111 bp := `
112 cc_library_shared {
113 name: "root",
114 srcs: ["src.c"],
115 static_libs: ["foo"],
116 }
117 cc_library_shared {
118 name: "root_no_lto",
119 srcs: ["src.c"],
120 static_libs: ["foo"],
121 lto: {
122 never: true,
123 }
124 }
125 cc_library_static {
126 name: "foo",
127 srcs: ["foo.c"],
128 static_libs: ["baz"],
129 lto: {
130 thin: true,
131 }
132 }
133 cc_library_static {
134 name: "baz",
135 srcs: ["baz.c"],
136 }
137`
138
Yi Kong577a73a2023-10-05 05:03:42 +0000139 result := LTOPreparer.RunTestWithBp(t, bp)
Liz Kammer81d09502022-10-31 14:44:46 -0400140
141 libRoot := result.ModuleForTests("root", "android_arm64_armv8-a_shared").Module()
142 libRootLtoNever := result.ModuleForTests("root_no_lto", "android_arm64_armv8-a_shared").Module()
143
Liz Kammer81d09502022-10-31 14:44:46 -0400144 libFoo := result.ModuleForTests("foo", "android_arm64_armv8-a_static")
Yi Kong577a73a2023-10-05 05:03:42 +0000145 if !hasDep(result, libRoot, libFoo.Module()) {
146 t.Errorf("'root' missing dependency on the default variant of 'foo'")
Liz Kammer81d09502022-10-31 14:44:46 -0400147 }
148
Yi Kong577a73a2023-10-05 05:03:42 +0000149 if !hasDep(result, libRootLtoNever, libFoo.Module()) {
150 t.Errorf("'root_no_lto' missing dependency on the default variant of 'foo'")
Liz Kammer81d09502022-10-31 14:44:46 -0400151 }
152
153 libFooCFlags := libFoo.Rule("cc").Args["cFlags"]
154 if w := "-flto=thin -fsplit-lto-unit"; !strings.Contains(libFooCFlags, w) {
155 t.Errorf("'foo' expected to have flags %q, but got %q", w, libFooCFlags)
156 }
157
Yi Kong577a73a2023-10-05 05:03:42 +0000158 libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static")
159 if !hasDep(result, libFoo.Module(), libBaz.Module()) {
160 t.Errorf("'foo' missing dependency on the default variant of transitive dep 'baz'")
Liz Kammer81d09502022-10-31 14:44:46 -0400161 }
162
163 libBazCFlags := libFoo.Rule("cc").Args["cFlags"]
164 if w := "-flto=thin -fsplit-lto-unit"; !strings.Contains(libBazCFlags, w) {
165 t.Errorf("'baz' expected to have flags %q, but got %q", w, libFooCFlags)
166 }
167}
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000168
169func TestLtoDisabledButEnabledForArch(t *testing.T) {
170 t.Parallel()
171 bp := `
172 cc_library {
173 name: "libfoo",
174 srcs: ["foo.c"],
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000175 lto: {
176 never: true,
177 },
178 target: {
Trevor Radcliffee22f3842023-02-09 19:01:31 +0000179 android_arm: {
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000180 lto: {
181 never: false,
182 thin: true,
183 },
184 },
185 },
186 }`
Yi Kong577a73a2023-10-05 05:03:42 +0000187 result := LTOPreparer.RunTestWithBp(t, bp)
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000188
Trevor Radcliffee22f3842023-02-09 19:01:31 +0000189 libFooWithLto := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
190 libFooWithoutLto := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld")
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000191
192 android.AssertStringDoesContain(t, "missing flag for LTO in variant that expects it",
193 libFooWithLto.Args["ldFlags"], "-flto=thin")
194 android.AssertStringDoesNotContain(t, "got flag for LTO in variant that doesn't expect it",
195 libFooWithoutLto.Args["ldFlags"], "-flto=thin")
196}
Trevor Radcliffecb7c4482023-02-10 20:29:20 +0000197
198func TestLtoDoesNotPropagateToRuntimeLibs(t *testing.T) {
199 t.Parallel()
200 bp := `
201 cc_library {
202 name: "runtime_libbar",
203 srcs: ["bar.c"],
204 }
205
206 cc_library {
207 name: "libfoo",
208 srcs: ["foo.c"],
209 runtime_libs: ["runtime_libbar"],
210 lto: {
211 thin: true,
212 },
213 }`
214
Yi Kong577a73a2023-10-05 05:03:42 +0000215 result := LTOPreparer.RunTestWithBp(t, bp)
Trevor Radcliffecb7c4482023-02-10 20:29:20 +0000216
217 libFoo := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
218 libBar := result.ModuleForTests("runtime_libbar", "android_arm_armv7-a-neon_shared").Rule("ld")
219
220 android.AssertStringDoesContain(t, "missing flag for LTO in LTO enabled library",
221 libFoo.Args["ldFlags"], "-flto=thin")
222 android.AssertStringDoesNotContain(t, "got flag for LTO in runtime_lib",
223 libBar.Args["ldFlags"], "-flto=thin")
224}