blob: dbe2be8994a8a32797913b8ab5d62d93a62014dd [file] [log] [blame]
Colin Crossad59e752017-11-16 14:29:11 -08001// Copyright 2017 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 (
18 "reflect"
19 "testing"
Jooyung Hanaed150d2020-04-02 01:41:41 +090020
21 "android/soong/android"
Chris Parsons94a0bba2021-06-04 15:03:47 -040022 "android/soong/bazel/cquery"
Colin Crossad59e752017-11-16 14:29:11 -080023)
24
25func TestLibraryReuse(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -040026 t.Parallel()
Colin Crossad59e752017-11-16 14:29:11 -080027 t.Run("simple", func(t *testing.T) {
28 ctx := testCc(t, `
29 cc_library {
30 name: "libfoo",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +010031 srcs: ["foo.c", "baz.o"],
Colin Crossad59e752017-11-16 14:29:11 -080032 }`)
33
Colin Cross7113d202019-11-20 16:39:12 -080034 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
35 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
Colin Crossad59e752017-11-16 14:29:11 -080036
Pete Bentleyfcf55bf2019-08-16 20:14:32 +010037 if len(libfooShared.Inputs) != 2 {
Colin Crossad59e752017-11-16 14:29:11 -080038 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
39 }
40
Pete Bentleyfcf55bf2019-08-16 20:14:32 +010041 if len(libfooStatic.Inputs) != 2 {
Colin Crossad59e752017-11-16 14:29:11 -080042 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
43 }
44
45 if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
46 t.Errorf("static object not reused for shared library")
47 }
Pete Bentleyfcf55bf2019-08-16 20:14:32 +010048 if libfooShared.Inputs[1] != libfooStatic.Inputs[1] {
49 t.Errorf("static object not reused for shared library")
50 }
Colin Crossad59e752017-11-16 14:29:11 -080051 })
52
53 t.Run("extra static source", func(t *testing.T) {
54 ctx := testCc(t, `
55 cc_library {
56 name: "libfoo",
57 srcs: ["foo.c"],
58 static: {
59 srcs: ["bar.c"]
60 },
61 }`)
62
Colin Cross7113d202019-11-20 16:39:12 -080063 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
64 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
Colin Crossad59e752017-11-16 14:29:11 -080065
66 if len(libfooShared.Inputs) != 1 {
67 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
68 }
69
70 if len(libfooStatic.Inputs) != 2 {
71 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
72 }
73
74 if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
75 t.Errorf("static object not reused for shared library")
76 }
77 })
78
79 t.Run("extra shared source", func(t *testing.T) {
80 ctx := testCc(t, `
81 cc_library {
82 name: "libfoo",
83 srcs: ["foo.c"],
84 shared: {
85 srcs: ["bar.c"]
86 },
87 }`)
88
Colin Cross7113d202019-11-20 16:39:12 -080089 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
90 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
Colin Crossad59e752017-11-16 14:29:11 -080091
92 if len(libfooShared.Inputs) != 2 {
93 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
94 }
95
96 if len(libfooStatic.Inputs) != 1 {
97 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
98 }
99
100 if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
101 t.Errorf("static object not reused for shared library")
102 }
103 })
104
105 t.Run("extra static cflags", func(t *testing.T) {
106 ctx := testCc(t, `
107 cc_library {
108 name: "libfoo",
109 srcs: ["foo.c"],
110 static: {
111 cflags: ["-DFOO"],
112 },
113 }`)
114
Colin Cross7113d202019-11-20 16:39:12 -0800115 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
116 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
Colin Crossad59e752017-11-16 14:29:11 -0800117
118 if len(libfooShared.Inputs) != 1 {
119 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
120 }
121
122 if len(libfooStatic.Inputs) != 1 {
123 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
124 }
125
126 if libfooShared.Inputs[0] == libfooStatic.Inputs[0] {
127 t.Errorf("static object reused for shared library when it shouldn't be")
128 }
129 })
130
131 t.Run("extra shared cflags", func(t *testing.T) {
132 ctx := testCc(t, `
133 cc_library {
134 name: "libfoo",
135 srcs: ["foo.c"],
136 shared: {
137 cflags: ["-DFOO"],
138 },
139 }`)
140
Colin Cross7113d202019-11-20 16:39:12 -0800141 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
142 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
Colin Crossad59e752017-11-16 14:29:11 -0800143
144 if len(libfooShared.Inputs) != 1 {
145 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
146 }
147
148 if len(libfooStatic.Inputs) != 1 {
149 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
150 }
151
152 if libfooShared.Inputs[0] == libfooStatic.Inputs[0] {
153 t.Errorf("static object reused for shared library when it shouldn't be")
154 }
155 })
156
157 t.Run("global cflags for reused generated sources", func(t *testing.T) {
158 ctx := testCc(t, `
159 cc_library {
160 name: "libfoo",
161 srcs: [
162 "foo.c",
163 "a.proto",
164 ],
165 shared: {
166 srcs: [
167 "bar.c",
168 ],
169 },
170 }`)
171
Colin Cross7113d202019-11-20 16:39:12 -0800172 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
173 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
Colin Crossad59e752017-11-16 14:29:11 -0800174
175 if len(libfooShared.Inputs) != 3 {
176 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
177 }
178
179 if len(libfooStatic.Inputs) != 2 {
180 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
181 }
182
183 if !reflect.DeepEqual(libfooShared.Inputs[0:2].Strings(), libfooStatic.Inputs.Strings()) {
184 t.Errorf("static objects not reused for shared library")
185 }
186
Colin Cross7113d202019-11-20 16:39:12 -0800187 libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Module().(*Module)
Colin Cross4af21ed2019-11-04 09:37:55 -0800188 if !inList("-DGOOGLE_PROTOBUF_NO_RTTI", libfoo.flags.Local.CFlags) {
Colin Crossad59e752017-11-16 14:29:11 -0800189 t.Errorf("missing protobuf cflags")
190 }
191 })
192}
Jooyung Hanaed150d2020-04-02 01:41:41 +0900193
194func TestStubsVersions(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400195 t.Parallel()
Jooyung Hanaed150d2020-04-02 01:41:41 +0900196 bp := `
197 cc_library {
198 name: "libfoo",
199 srcs: ["foo.c"],
200 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -0700201 versions: ["29", "R", "current"],
Jooyung Hanaed150d2020-04-02 01:41:41 +0900202 },
203 }
204 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000205 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Hanaed150d2020-04-02 01:41:41 +0900206 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
207 ctx := testCcWithConfig(t, config)
208
209 variants := ctx.ModuleVariantsForTests("libfoo")
Dan Albertc8060532020-07-22 22:32:17 -0700210 for _, expectedVer := range []string{"29", "R", "current"} {
Jooyung Hanaed150d2020-04-02 01:41:41 +0900211 expectedVariant := "android_arm_armv7-a-neon_shared_" + expectedVer
212 if !inList(expectedVariant, variants) {
213 t.Errorf("missing expected variant: %q", expectedVariant)
214 }
215 }
216}
217
218func TestStubsVersions_NotSorted(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400219 t.Parallel()
Jooyung Hanaed150d2020-04-02 01:41:41 +0900220 bp := `
221 cc_library {
222 name: "libfoo",
223 srcs: ["foo.c"],
224 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -0700225 versions: ["29", "current", "R"],
Jooyung Hanaed150d2020-04-02 01:41:41 +0900226 },
227 }
228 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000229 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Hanaed150d2020-04-02 01:41:41 +0900230 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
231 testCcErrorWithConfig(t, `"libfoo" .*: versions: not sorted`, config)
232}
233
234func TestStubsVersions_ParseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400235 t.Parallel()
Jooyung Hanaed150d2020-04-02 01:41:41 +0900236 bp := `
237 cc_library {
238 name: "libfoo",
239 srcs: ["foo.c"],
240 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -0700241 versions: ["29", "current", "X"],
Jooyung Hanaed150d2020-04-02 01:41:41 +0900242 },
243 }
244 `
245
Dan Albertc8060532020-07-22 22:32:17 -0700246 testCcError(t, `"libfoo" .*: versions: "X" could not be parsed as an integer and is not a recognized codename`, bp)
Jooyung Hanaed150d2020-04-02 01:41:41 +0900247}
Chris Parsons94a0bba2021-06-04 15:03:47 -0400248
249func TestCcLibraryWithBazel(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400250 t.Parallel()
Chris Parsons94a0bba2021-06-04 15:03:47 -0400251 bp := `
252cc_library {
253 name: "foo",
254 srcs: ["foo.cc"],
255 bazel_module: { label: "//foo/bar:bar" },
256}`
257 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
258 config.BazelContext = android.MockBazelContext{
259 OutputBaseDir: "outputbase",
260 LabelToCcInfo: map[string]cquery.CcInfo{
261 "//foo/bar:bar": cquery.CcInfo{
262 CcObjectFiles: []string{"foo.o"},
263 Includes: []string{"include"},
264 SystemIncludes: []string{"system_include"},
Liz Kammereb2d6d12021-12-06 14:56:25 -0500265 Headers: []string{"foo.h"},
Chris Parsons94a0bba2021-06-04 15:03:47 -0400266 RootDynamicLibraries: []string{"foo.so"},
Sasha Smundakedd16662022-10-07 14:44:50 -0700267 UnstrippedOutput: "foo_unstripped.so",
Chris Parsons94a0bba2021-06-04 15:03:47 -0400268 },
Chris Parsons77acf2e2021-12-03 17:27:16 -0500269 "//foo/bar:bar_bp2build_cc_library_static": cquery.CcInfo{
270 CcObjectFiles: []string{"foo.o"},
271 Includes: []string{"include"},
272 SystemIncludes: []string{"system_include"},
Liz Kammereb2d6d12021-12-06 14:56:25 -0500273 Headers: []string{"foo.h"},
Chris Parsons77acf2e2021-12-03 17:27:16 -0500274 RootStaticArchives: []string{"foo.a"},
275 },
Chris Parsons94a0bba2021-06-04 15:03:47 -0400276 },
277 }
278 ctx := testCcWithConfig(t, config)
279
280 staticFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_static").Module()
281 outputFiles, err := staticFoo.(android.OutputFileProducer).OutputFiles("")
282 if err != nil {
283 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
284 }
285
286 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.a"}
287 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
288
Liz Kammereb2d6d12021-12-06 14:56:25 -0500289 flagExporter := ctx.ModuleProvider(staticFoo, FlagExporterInfoProvider).(FlagExporterInfo)
290 android.AssertPathsRelativeToTopEquals(t, "exported include dirs", []string{"outputbase/execroot/__main__/include"}, flagExporter.IncludeDirs)
291 android.AssertPathsRelativeToTopEquals(t, "exported system include dirs", []string{"outputbase/execroot/__main__/system_include"}, flagExporter.SystemIncludeDirs)
292 android.AssertPathsRelativeToTopEquals(t, "exported headers", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.GeneratedHeaders)
293 android.AssertPathsRelativeToTopEquals(t, "deps", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.Deps)
294
Chris Parsons94a0bba2021-06-04 15:03:47 -0400295 sharedFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
296 outputFiles, err = sharedFoo.(android.OutputFileProducer).OutputFiles("")
297 if err != nil {
Liz Kammereb2d6d12021-12-06 14:56:25 -0500298 t.Errorf("Unexpected error getting cc_library outputfiles %s", err)
Chris Parsons94a0bba2021-06-04 15:03:47 -0400299 }
300 expectedOutputFiles = []string{"outputbase/execroot/__main__/foo.so"}
301 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
302
Sasha Smundakedd16662022-10-07 14:44:50 -0700303 android.AssertStringEquals(t, "unstripped shared library", "outputbase/execroot/__main__/foo_unstripped.so", sharedFoo.(*Module).linker.unstrippedOutputFilePath().String())
Liz Kammereb2d6d12021-12-06 14:56:25 -0500304 flagExporter = ctx.ModuleProvider(sharedFoo, FlagExporterInfoProvider).(FlagExporterInfo)
305 android.AssertPathsRelativeToTopEquals(t, "exported include dirs", []string{"outputbase/execroot/__main__/include"}, flagExporter.IncludeDirs)
306 android.AssertPathsRelativeToTopEquals(t, "exported system include dirs", []string{"outputbase/execroot/__main__/system_include"}, flagExporter.SystemIncludeDirs)
307 android.AssertPathsRelativeToTopEquals(t, "exported headers", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.GeneratedHeaders)
308 android.AssertPathsRelativeToTopEquals(t, "deps", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.Deps)
Chris Parsons94a0bba2021-06-04 15:03:47 -0400309}
Colin Cross59422382021-07-22 16:35:24 -0700310
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500311func TestCcLibraryWithBazelValidations(t *testing.T) {
312 t.Parallel()
313 bp := `
314cc_library {
315 name: "foo",
316 srcs: ["foo.cc"],
317 bazel_module: { label: "//foo/bar:bar" },
318 tidy: true,
319}`
320 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
321 config.BazelContext = android.MockBazelContext{
322 OutputBaseDir: "outputbase",
323 LabelToCcInfo: map[string]cquery.CcInfo{
324 "//foo/bar:bar": cquery.CcInfo{
325 CcObjectFiles: []string{"foo.o"},
326 Includes: []string{"include"},
327 SystemIncludes: []string{"system_include"},
328 Headers: []string{"foo.h"},
329 RootDynamicLibraries: []string{"foo.so"},
330 UnstrippedOutput: "foo_unstripped.so",
331 },
332 "//foo/bar:bar_bp2build_cc_library_static": cquery.CcInfo{
333 CcObjectFiles: []string{"foo.o"},
334 Includes: []string{"include"},
335 SystemIncludes: []string{"system_include"},
336 Headers: []string{"foo.h"},
337 RootStaticArchives: []string{"foo.a"},
338 TidyFiles: []string{"foo.c.tidy"},
339 },
340 },
341 }
342 ctx := android.GroupFixturePreparers(
343 prepareForCcTest,
344 android.FixtureMergeEnv(map[string]string{
345 "ALLOW_LOCAL_TIDY_TRUE": "1",
346 }),
347 ).RunTestWithConfig(t, config).TestContext
348
349 staticFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_static").Module()
350 outputFiles, err := staticFoo.(android.OutputFileProducer).OutputFiles("")
351 if err != nil {
352 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
353 }
354
355 expectedOutputFiles := []string{"out/soong/.intermediates/foo/android_arm_armv7-a-neon_static/validated/foo.a"}
356 android.AssertPathsRelativeToTopEquals(t, "output files", expectedOutputFiles, outputFiles)
357
358 flagExporter := ctx.ModuleProvider(staticFoo, FlagExporterInfoProvider).(FlagExporterInfo)
359 android.AssertPathsRelativeToTopEquals(t, "exported include dirs", []string{"outputbase/execroot/__main__/include"}, flagExporter.IncludeDirs)
360 android.AssertPathsRelativeToTopEquals(t, "exported system include dirs", []string{"outputbase/execroot/__main__/system_include"}, flagExporter.SystemIncludeDirs)
361 android.AssertPathsRelativeToTopEquals(t, "exported headers", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.GeneratedHeaders)
362 android.AssertPathsRelativeToTopEquals(t, "deps", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.Deps)
363
364 sharedFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
365 outputFiles, err = sharedFoo.(android.OutputFileProducer).OutputFiles("")
366 if err != nil {
367 t.Errorf("Unexpected error getting cc_library outputfiles %s", err)
368 }
369 expectedOutputFiles = []string{"outputbase/execroot/__main__/foo.so"}
370 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
371
372 android.AssertStringEquals(t, "unstripped shared library", "outputbase/execroot/__main__/foo_unstripped.so", sharedFoo.(*Module).linker.unstrippedOutputFilePath().String())
373 flagExporter = ctx.ModuleProvider(sharedFoo, FlagExporterInfoProvider).(FlagExporterInfo)
374 android.AssertPathsRelativeToTopEquals(t, "exported include dirs", []string{"outputbase/execroot/__main__/include"}, flagExporter.IncludeDirs)
375 android.AssertPathsRelativeToTopEquals(t, "exported system include dirs", []string{"outputbase/execroot/__main__/system_include"}, flagExporter.SystemIncludeDirs)
376 android.AssertPathsRelativeToTopEquals(t, "exported headers", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.GeneratedHeaders)
377 android.AssertPathsRelativeToTopEquals(t, "deps", []string{"outputbase/execroot/__main__/foo.h"}, flagExporter.Deps)
378}
379
Colin Cross59422382021-07-22 16:35:24 -0700380func TestLibraryVersionScript(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400381 t.Parallel()
Colin Cross59422382021-07-22 16:35:24 -0700382 result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
383 cc_library {
384 name: "libfoo",
385 srcs: ["foo.c"],
386 version_script: "foo.map.txt",
387 }`)
388
389 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld")
390
391 android.AssertStringListContains(t, "missing dependency on version_script",
392 libfoo.Implicits.Strings(), "foo.map.txt")
393 android.AssertStringDoesContain(t, "missing flag for version_script",
394 libfoo.Args["ldFlags"], "-Wl,--version-script,foo.map.txt")
395
396}
397
398func TestLibraryDynamicList(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400399 t.Parallel()
Colin Cross59422382021-07-22 16:35:24 -0700400 result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
401 cc_library {
402 name: "libfoo",
403 srcs: ["foo.c"],
404 dynamic_list: "foo.dynamic.txt",
405 }`)
406
407 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld")
408
409 android.AssertStringListContains(t, "missing dependency on dynamic_list",
410 libfoo.Implicits.Strings(), "foo.dynamic.txt")
411 android.AssertStringDoesContain(t, "missing flag for dynamic_list",
412 libfoo.Args["ldFlags"], "-Wl,--dynamic-list,foo.dynamic.txt")
413
414}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxbc4e7342021-09-14 20:58:25 +0000415
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500416func TestCcLibrarySharedWithBazelValidations(t *testing.T) {
417 t.Parallel()
418 bp := `
419cc_library_shared {
420 name: "foo",
421 srcs: ["foo.cc"],
422 bazel_module: { label: "//foo/bar:bar" },
423 tidy: true,
424}`
425 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
426 config.BazelContext = android.MockBazelContext{
427 OutputBaseDir: "outputbase",
428 LabelToCcInfo: map[string]cquery.CcInfo{
429 "//foo/bar:bar": cquery.CcInfo{
430 CcObjectFiles: []string{"foo.o"},
431 Includes: []string{"include"},
432 SystemIncludes: []string{"system_include"},
433 RootDynamicLibraries: []string{"foo.so"},
434 TocFile: "foo.so.toc",
435 TidyFiles: []string{"foo.c.tidy"},
436 },
437 },
438 }
439 ctx := android.GroupFixturePreparers(
440 prepareForCcTest,
441 android.FixtureMergeEnv(map[string]string{
442 "ALLOW_LOCAL_TIDY_TRUE": "1",
443 }),
444 ).RunTestWithConfig(t, config).TestContext
445
446 sharedFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
447 producer := sharedFoo.(android.OutputFileProducer)
448 outputFiles, err := producer.OutputFiles("")
449 if err != nil {
450 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
451 }
452 expectedOutputFiles := []string{"out/soong/.intermediates/foo/android_arm_armv7-a-neon_shared/validated/foo.so"}
453 android.AssertPathsRelativeToTopEquals(t, "output files", expectedOutputFiles, outputFiles)
454
455 tocFilePath := sharedFoo.(*Module).Toc()
456 if !tocFilePath.Valid() {
457 t.Errorf("Invalid tocFilePath: %s", tocFilePath)
458 }
459 tocFile := tocFilePath.Path()
460 expectedToc := "outputbase/execroot/__main__/foo.so.toc"
461 android.AssertStringEquals(t, "toc file", expectedToc, tocFile.String())
462
463 entries := android.AndroidMkEntriesForTest(t, ctx, sharedFoo)[0]
464 expectedFlags := []string{"-Ioutputbase/execroot/__main__/include", "-isystem outputbase/execroot/__main__/system_include"}
465 gotFlags := entries.EntryMap["LOCAL_EXPORT_CFLAGS"]
466 android.AssertDeepEquals(t, "androidmk exported cflags", expectedFlags, gotFlags)
Wei Li598f92d2023-01-04 17:12:24 -0800467 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "cc_library_shared", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500468}
469
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxbc4e7342021-09-14 20:58:25 +0000470func TestCcLibrarySharedWithBazel(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400471 t.Parallel()
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxbc4e7342021-09-14 20:58:25 +0000472 bp := `
473cc_library_shared {
474 name: "foo",
475 srcs: ["foo.cc"],
476 bazel_module: { label: "//foo/bar:bar" },
477}`
478 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
479 config.BazelContext = android.MockBazelContext{
480 OutputBaseDir: "outputbase",
481 LabelToCcInfo: map[string]cquery.CcInfo{
482 "//foo/bar:bar": cquery.CcInfo{
483 CcObjectFiles: []string{"foo.o"},
484 Includes: []string{"include"},
485 SystemIncludes: []string{"system_include"},
486 RootDynamicLibraries: []string{"foo.so"},
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux62585e82021-09-14 21:19:31 +0000487 TocFile: "foo.so.toc",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxbc4e7342021-09-14 20:58:25 +0000488 },
489 },
490 }
491 ctx := testCcWithConfig(t, config)
492
493 sharedFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux62585e82021-09-14 21:19:31 +0000494 producer := sharedFoo.(android.OutputFileProducer)
495 outputFiles, err := producer.OutputFiles("")
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxbc4e7342021-09-14 20:58:25 +0000496 if err != nil {
497 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
498 }
499 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
500 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
501
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux62585e82021-09-14 21:19:31 +0000502 tocFilePath := sharedFoo.(*Module).Toc()
503 if !tocFilePath.Valid() {
504 t.Errorf("Invalid tocFilePath: %s", tocFilePath)
505 }
506 tocFile := tocFilePath.Path()
507 expectedToc := "outputbase/execroot/__main__/foo.so.toc"
508 android.AssertStringEquals(t, "toc file", expectedToc, tocFile.String())
509
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxbc4e7342021-09-14 20:58:25 +0000510 entries := android.AndroidMkEntriesForTest(t, ctx, sharedFoo)[0]
511 expectedFlags := []string{"-Ioutputbase/execroot/__main__/include", "-isystem outputbase/execroot/__main__/system_include"}
512 gotFlags := entries.EntryMap["LOCAL_EXPORT_CFLAGS"]
513 android.AssertDeepEquals(t, "androidmk exported cflags", expectedFlags, gotFlags)
Wei Li598f92d2023-01-04 17:12:24 -0800514 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "cc_library_shared", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxbc4e7342021-09-14 20:58:25 +0000515}
Colin Crossa2bcf2c2022-02-11 13:11:55 -0800516
517func TestWholeStaticLibPrebuilts(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400518 t.Parallel()
Colin Crossa2bcf2c2022-02-11 13:11:55 -0800519 result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
520 cc_prebuilt_library_static {
521 name: "libprebuilt",
522 srcs: ["foo.a"],
523 }
524
525 cc_library_static {
526 name: "libdirect",
527 whole_static_libs: ["libprebuilt"],
528 }
529
530 cc_library_static {
531 name: "libtransitive",
532 whole_static_libs: ["libdirect"],
533 }
534
535 cc_library_static {
536 name: "libdirect_with_srcs",
537 srcs: ["bar.c"],
538 whole_static_libs: ["libprebuilt"],
539 }
540
541 cc_library_static {
542 name: "libtransitive_with_srcs",
543 srcs: ["baz.c"],
544 whole_static_libs: ["libdirect_with_srcs"],
545 }
546 `)
547
548 libdirect := result.ModuleForTests("libdirect", "android_arm64_armv8-a_static").Rule("arWithLibs")
549 libtransitive := result.ModuleForTests("libtransitive", "android_arm64_armv8-a_static").Rule("arWithLibs")
550
551 libdirectWithSrcs := result.ModuleForTests("libdirect_with_srcs", "android_arm64_armv8-a_static").Rule("arWithLibs")
552 libtransitiveWithSrcs := result.ModuleForTests("libtransitive_with_srcs", "android_arm64_armv8-a_static").Rule("arWithLibs")
553
554 barObj := result.ModuleForTests("libdirect_with_srcs", "android_arm64_armv8-a_static").Rule("cc")
555 bazObj := result.ModuleForTests("libtransitive_with_srcs", "android_arm64_armv8-a_static").Rule("cc")
556
557 android.AssertStringListContains(t, "missing dependency on foo.a",
558 libdirect.Inputs.Strings(), "foo.a")
559 android.AssertStringDoesContain(t, "missing flag for foo.a",
560 libdirect.Args["arLibs"], "foo.a")
561
562 android.AssertStringListContains(t, "missing dependency on foo.a",
563 libtransitive.Inputs.Strings(), "foo.a")
564 android.AssertStringDoesContain(t, "missing flag for foo.a",
565 libtransitive.Args["arLibs"], "foo.a")
566
567 android.AssertStringListContains(t, "missing dependency on foo.a",
568 libdirectWithSrcs.Inputs.Strings(), "foo.a")
569 android.AssertStringDoesContain(t, "missing flag for foo.a",
570 libdirectWithSrcs.Args["arLibs"], "foo.a")
571 android.AssertStringListContains(t, "missing dependency on bar.o",
572 libdirectWithSrcs.Inputs.Strings(), barObj.Output.String())
573 android.AssertStringDoesContain(t, "missing flag for bar.o",
574 libdirectWithSrcs.Args["arObjs"], barObj.Output.String())
575
576 android.AssertStringListContains(t, "missing dependency on foo.a",
577 libtransitiveWithSrcs.Inputs.Strings(), "foo.a")
578 android.AssertStringDoesContain(t, "missing flag for foo.a",
579 libtransitiveWithSrcs.Args["arLibs"], "foo.a")
580
581 android.AssertStringListContains(t, "missing dependency on bar.o",
582 libtransitiveWithSrcs.Inputs.Strings(), barObj.Output.String())
583 android.AssertStringDoesContain(t, "missing flag for bar.o",
584 libtransitiveWithSrcs.Args["arObjs"], barObj.Output.String())
585
586 android.AssertStringListContains(t, "missing dependency on baz.o",
587 libtransitiveWithSrcs.Inputs.Strings(), bazObj.Output.String())
588 android.AssertStringDoesContain(t, "missing flag for baz.o",
589 libtransitiveWithSrcs.Args["arObjs"], bazObj.Output.String())
590}