blob: e4b5a3a01a6694af1dd260807adbfa5436012a6b [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
Colin Cross6ac83a82024-01-23 11:23:10 -0800149 libFooNoLto := result.ModuleForTests("foo", "android_arm64_armv8-a_static_lto-none")
150 if !hasDep(result, libRootLtoNever, libFooNoLto.Module()) {
151 t.Errorf("'root_no_lto' missing dependency on the lto_none variant of 'foo'")
Liz Kammer81d09502022-10-31 14:44:46 -0400152 }
153
154 libFooCFlags := libFoo.Rule("cc").Args["cFlags"]
155 if w := "-flto=thin -fsplit-lto-unit"; !strings.Contains(libFooCFlags, w) {
156 t.Errorf("'foo' expected to have flags %q, but got %q", w, libFooCFlags)
157 }
158
Yi Kong577a73a2023-10-05 05:03:42 +0000159 libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static")
160 if !hasDep(result, libFoo.Module(), libBaz.Module()) {
161 t.Errorf("'foo' missing dependency on the default variant of transitive dep 'baz'")
Liz Kammer81d09502022-10-31 14:44:46 -0400162 }
163
164 libBazCFlags := libFoo.Rule("cc").Args["cFlags"]
165 if w := "-flto=thin -fsplit-lto-unit"; !strings.Contains(libBazCFlags, w) {
166 t.Errorf("'baz' expected to have flags %q, but got %q", w, libFooCFlags)
167 }
168}
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000169
170func TestLtoDisabledButEnabledForArch(t *testing.T) {
171 t.Parallel()
172 bp := `
173 cc_library {
174 name: "libfoo",
175 srcs: ["foo.c"],
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000176 lto: {
177 never: true,
178 },
179 target: {
Trevor Radcliffee22f3842023-02-09 19:01:31 +0000180 android_arm: {
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000181 lto: {
182 never: false,
183 thin: true,
184 },
185 },
186 },
187 }`
Yi Kong577a73a2023-10-05 05:03:42 +0000188 result := LTOPreparer.RunTestWithBp(t, bp)
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000189
Trevor Radcliffee22f3842023-02-09 19:01:31 +0000190 libFooWithLto := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
191 libFooWithoutLto := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld")
Trevor Radcliffe07cf4ed2023-02-07 19:46:51 +0000192
193 android.AssertStringDoesContain(t, "missing flag for LTO in variant that expects it",
194 libFooWithLto.Args["ldFlags"], "-flto=thin")
195 android.AssertStringDoesNotContain(t, "got flag for LTO in variant that doesn't expect it",
196 libFooWithoutLto.Args["ldFlags"], "-flto=thin")
197}
Trevor Radcliffecb7c4482023-02-10 20:29:20 +0000198
199func TestLtoDoesNotPropagateToRuntimeLibs(t *testing.T) {
200 t.Parallel()
201 bp := `
202 cc_library {
203 name: "runtime_libbar",
204 srcs: ["bar.c"],
205 }
206
207 cc_library {
208 name: "libfoo",
209 srcs: ["foo.c"],
210 runtime_libs: ["runtime_libbar"],
211 lto: {
212 thin: true,
213 },
214 }`
215
Yi Kong577a73a2023-10-05 05:03:42 +0000216 result := LTOPreparer.RunTestWithBp(t, bp)
Trevor Radcliffecb7c4482023-02-10 20:29:20 +0000217
218 libFoo := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
219 libBar := result.ModuleForTests("runtime_libbar", "android_arm_armv7-a-neon_shared").Rule("ld")
220
221 android.AssertStringDoesContain(t, "missing flag for LTO in LTO enabled library",
222 libFoo.Args["ldFlags"], "-flto=thin")
223 android.AssertStringDoesNotContain(t, "got flag for LTO in runtime_lib",
224 libBar.Args["ldFlags"], "-flto=thin")
225}