aboutsummaryrefslogtreecommitdiff
path: root/cc/library_stub_test.go
blob: 528577a21640b0874be178a40ad01702ca620ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cc

import (
	_ "fmt"
	_ "sort"

	"testing"

	"android/soong/android"

	"github.com/google/blueprint"
)

func hasDirectDependency(t *testing.T, ctx *android.TestResult, from android.Module, to android.Module) bool {
	t.Helper()
	var found bool
	ctx.VisitDirectDeps(from, func(dep blueprint.Module) {
		if dep == to {
			found = true
		}
	})
	return found
}

func TestApiLibraryReplacesExistingModule(t *testing.T) {
	bp := `
		cc_library {
			name: "libfoo",
			shared_libs: ["libbar"],
			vendor_available: true,
		}

		cc_library {
			name: "libbar",
		}

		cc_api_library {
			name: "libbar",
			vendor_available: true,
			src: "libbar.so",
		}

		api_imports {
			name: "api_imports",
			shared_libs: [
				"libbar",
			],
		}
	`

	ctx := prepareForCcTest.RunTestWithBp(t, bp)

	libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
	libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
	libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()

	android.AssertBoolEquals(t, "original library should be linked with non-stub variant", true, hasDirectDependency(t, ctx, libfoo, libbar))
	android.AssertBoolEquals(t, "Stub library from API surface should be not linked with non-stub variant", false, hasDirectDependency(t, ctx, libfoo, libbarApiImport))

	libfooVendor := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_shared").Module()
	libbarApiImportVendor := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()

	android.AssertBoolEquals(t, "original library should not be linked", false, hasDirectDependency(t, ctx, libfooVendor, libbar))
	android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, libfooVendor, libbarApiImportVendor))
}

func TestApiLibraryDoNotRequireOriginalModule(t *testing.T) {
	bp := `
		cc_library {
			name: "libfoo",
			shared_libs: ["libbar"],
			vendor: true,
		}

		cc_api_library {
			name: "libbar",
			src: "libbar.so",
			vendor_available: true,
		}

		api_imports {
			name: "api_imports",
			shared_libs: [
				"libbar",
			],
		}
	`

	ctx := prepareForCcTest.RunTestWithBp(t, bp)

	libfoo := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_shared").Module()
	libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()

	android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
}

func TestApiLibraryShouldNotReplaceWithoutApiImport(t *testing.T) {
	bp := `
		cc_library {
			name: "libfoo",
			shared_libs: ["libbar"],
			vendor_available: true,
		}

		cc_library {
			name: "libbar",
			vendor_available: true,
		}

		cc_api_library {
			name: "libbar",
			src: "libbar.so",
			vendor_available: true,
		}

		api_imports {
			name: "api_imports",
			shared_libs: [],
		}
	`

	ctx := prepareForCcTest.RunTestWithBp(t, bp)

	libfoo := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_shared").Module()
	libbar := ctx.ModuleForTests("libbar", "android_vendor.29_arm64_armv8-a_shared").Module()
	libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()

	android.AssertBoolEquals(t, "original library should be linked", true, hasDirectDependency(t, ctx, libfoo, libbar))
	android.AssertBoolEquals(t, "Stub library from API surface should not be linked", false, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
}

func TestExportDirFromStubLibrary(t *testing.T) {
	bp := `
		cc_library {
			name: "libfoo",
			export_include_dirs: ["source_include_dir"],
			export_system_include_dirs: ["source_system_include_dir"],
			vendor_available: true,
		}
		cc_api_library {
			name: "libfoo",
			export_include_dirs: ["stub_include_dir"],
			export_system_include_dirs: ["stub_system_include_dir"],
			vendor_available: true,
			src: "libfoo.so",
		}
		api_imports {
			name: "api_imports",
			shared_libs: [
				"libfoo",
			],
			header_libs: [],
		}
		// vendor binary
		cc_binary {
			name: "vendorbin",
			vendor: true,
			srcs: ["vendor.cc"],
			shared_libs: ["libfoo"],
		}
	`
	ctx := prepareForCcTest.RunTestWithBp(t, bp)
	vendorCFlags := ctx.ModuleForTests("vendorbin", "android_vendor.29_arm64_armv8-a").Rule("cc").Args["cFlags"]
	android.AssertStringDoesContain(t, "Vendor binary should compile using headers provided by stub", vendorCFlags, "-Istub_include_dir")
	android.AssertStringDoesNotContain(t, "Vendor binary should not compile using headers of source", vendorCFlags, "-Isource_include_dir")
	android.AssertStringDoesContain(t, "Vendor binary should compile using system headers provided by stub", vendorCFlags, "-isystem stub_system_include_dir")
	android.AssertStringDoesNotContain(t, "Vendor binary should not compile using system headers of source", vendorCFlags, "-isystem source_system_include_dir")

	vendorImplicits := ctx.ModuleForTests("vendorbin", "android_vendor.29_arm64_armv8-a").Rule("cc").OrderOnly.Strings()
	// Building the stub.so file first assembles its .h files in multi-tree out.
	// These header files are required for compiling the other API domain (vendor in this case)
	android.AssertStringListContains(t, "Vendor binary compilation should have an implicit dep on the stub .so file", vendorImplicits, "libfoo.so")
}

func TestApiLibraryWithLlndkVariant(t *testing.T) {
	bp := `
		cc_binary {
			name: "binfoo",
			vendor: true,
			srcs: ["binfoo.cc"],
			shared_libs: ["libbar"],
		}

		cc_api_library {
			name: "libbar",
			// TODO(b/244244438) Remove src property once all variants are implemented.
			src: "libbar.so",
			vendor_available: true,
			variants: [
				"llndk",
			],
		}

		cc_api_variant {
			name: "libbar",
			variant: "llndk",
			src: "libbar_llndk.so",
			export_include_dirs: ["libbar_llndk_include"]
		}

		api_imports {
			name: "api_imports",
			shared_libs: [
				"libbar",
			],
			header_libs: [],
		}
	`

	ctx := prepareForCcTest.RunTestWithBp(t, bp)

	binfoo := ctx.ModuleForTests("binfoo", "android_vendor.29_arm64_armv8-a").Module()
	libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()
	libbarApiVariant := ctx.ModuleForTests("libbar.llndk.apiimport", "android_vendor.29_arm64_armv8-a").Module()

	android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, binfoo, libbarApiImport))
	android.AssertBoolEquals(t, "Stub library variant from API surface should be linked", true, hasDirectDependency(t, ctx, libbarApiImport, libbarApiVariant))

	binFooLibFlags := ctx.ModuleForTests("binfoo", "android_vendor.29_arm64_armv8-a").Rule("ld").Args["libFlags"]
	android.AssertStringDoesContain(t, "Vendor binary should be linked with LLNDK variant source", binFooLibFlags, "libbar.llndk.apiimport.so")

	binFooCFlags := ctx.ModuleForTests("binfoo", "android_vendor.29_arm64_armv8-a").Rule("cc").Args["cFlags"]
	android.AssertStringDoesContain(t, "Vendor binary should include headers from the LLNDK variant source", binFooCFlags, "-Ilibbar_llndk_include")
}

func TestApiLibraryWithNdkVariant(t *testing.T) {
	bp := `
		cc_binary {
			name: "binfoo",
			sdk_version: "29",
			srcs: ["binfoo.cc"],
			shared_libs: ["libbar"],
			stl: "c++_shared",
		}

		cc_binary {
			name: "binbaz",
			sdk_version: "30",
			srcs: ["binbaz.cc"],
			shared_libs: ["libbar"],
			stl: "c++_shared",
		}

		cc_binary {
			name: "binqux",
			srcs: ["binfoo.cc"],
			shared_libs: ["libbar"],
		}

		cc_library {
			name: "libbar",
			srcs: ["libbar.cc"],
		}

		cc_api_library {
			name: "libbar",
			// TODO(b/244244438) Remove src property once all variants are implemented.
			src: "libbar.so",
			variants: [
				"ndk.29",
				"ndk.30",
				"ndk.current",
			],
		}

		cc_api_variant {
			name: "libbar",
			variant: "ndk",
			version: "29",
			src: "libbar_ndk_29.so",
			export_include_dirs: ["libbar_ndk_29_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "ndk",
			version: "30",
			src: "libbar_ndk_30.so",
			export_include_dirs: ["libbar_ndk_30_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "ndk",
			version: "current",
			src: "libbar_ndk_current.so",
			export_include_dirs: ["libbar_ndk_current_include"]
		}

		api_imports {
			name: "api_imports",
			shared_libs: [
				"libbar",
			],
			header_libs: [],
		}
	`

	ctx := prepareForCcTest.RunTestWithBp(t, bp)

	binfoo := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Module()
	libbarApiImportv29 := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_sdk_shared_29").Module()
	libbarApiVariantv29 := ctx.ModuleForTests("libbar.ndk.29.apiimport", "android_arm64_armv8-a_sdk").Module()
	libbarApiImportv30 := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_sdk_shared_30").Module()
	libbarApiVariantv30 := ctx.ModuleForTests("libbar.ndk.30.apiimport", "android_arm64_armv8-a_sdk").Module()

	android.AssertBoolEquals(t, "Stub library from API surface should be linked with target version", true, hasDirectDependency(t, ctx, binfoo, libbarApiImportv29))
	android.AssertBoolEquals(t, "Stub library variant from API surface should be linked with target version", true, hasDirectDependency(t, ctx, libbarApiImportv29, libbarApiVariantv29))
	android.AssertBoolEquals(t, "Stub library from API surface should not be linked with different version", false, hasDirectDependency(t, ctx, binfoo, libbarApiImportv30))
	android.AssertBoolEquals(t, "Stub library variant from API surface should not be linked with different version", false, hasDirectDependency(t, ctx, libbarApiImportv29, libbarApiVariantv30))

	binbaz := ctx.ModuleForTests("binbaz", "android_arm64_armv8-a_sdk").Module()

	android.AssertBoolEquals(t, "Stub library from API surface should be linked with target version", true, hasDirectDependency(t, ctx, binbaz, libbarApiImportv30))
	android.AssertBoolEquals(t, "Stub library from API surface should not be linked with different version", false, hasDirectDependency(t, ctx, binbaz, libbarApiImportv29))

	binFooLibFlags := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Rule("ld").Args["libFlags"]
	android.AssertStringDoesContain(t, "Binary using sdk should be linked with NDK variant source", binFooLibFlags, "libbar.ndk.29.apiimport.so")

	binFooCFlags := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Rule("cc").Args["cFlags"]
	android.AssertStringDoesContain(t, "Binary using sdk should include headers from the NDK variant source", binFooCFlags, "-Ilibbar_ndk_29_include")

	binQux := ctx.ModuleForTests("binqux", "android_arm64_armv8-a").Module()
	android.AssertBoolEquals(t, "NDK Stub library from API surface should not be linked with nonSdk binary", false,
		(hasDirectDependency(t, ctx, binQux, libbarApiImportv30) || hasDirectDependency(t, ctx, binQux, libbarApiImportv29)))
}

func TestApiLibraryWithMultipleVariants(t *testing.T) {
	bp := `
		cc_binary {
			name: "binfoo",
			sdk_version: "29",
			srcs: ["binfoo.cc"],
			shared_libs: ["libbar"],
			stl: "c++_shared",
		}

		cc_binary {
			name: "binbaz",
			vendor: true,
			srcs: ["binbaz.cc"],
			shared_libs: ["libbar"],
		}

		cc_library {
			name: "libbar",
			srcs: ["libbar.cc"],
		}

		cc_api_library {
			name: "libbar",
			// TODO(b/244244438) Remove src property once all variants are implemented.
			src: "libbar.so",
			vendor_available: true,
			variants: [
				"llndk",
				"ndk.29",
				"ndk.30",
				"ndk.current",
				"apex.29",
				"apex.30",
				"apex.current",
			],
		}

		cc_api_variant {
			name: "libbar",
			variant: "ndk",
			version: "29",
			src: "libbar_ndk_29.so",
			export_include_dirs: ["libbar_ndk_29_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "ndk",
			version: "30",
			src: "libbar_ndk_30.so",
			export_include_dirs: ["libbar_ndk_30_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "ndk",
			version: "current",
			src: "libbar_ndk_current.so",
			export_include_dirs: ["libbar_ndk_current_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "apex",
			version: "29",
			src: "libbar_apex_29.so",
			export_include_dirs: ["libbar_apex_29_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "apex",
			version: "30",
			src: "libbar_apex_30.so",
			export_include_dirs: ["libbar_apex_30_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "apex",
			version: "current",
			src: "libbar_apex_current.so",
			export_include_dirs: ["libbar_apex_current_include"]
		}

		cc_api_variant {
			name: "libbar",
			variant: "llndk",
			src: "libbar_llndk.so",
			export_include_dirs: ["libbar_llndk_include"]
		}

		api_imports {
			name: "api_imports",
			shared_libs: [
				"libbar",
			],
			apex_shared_libs: [
				"libbar",
			],
		}
	`
	ctx := prepareForCcTest.RunTestWithBp(t, bp)

	binfoo := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Module()
	libbarApiImportv29 := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_sdk_shared_29").Module()
	libbarApiImportLlndk := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()

	android.AssertBoolEquals(t, "Binary using SDK should be linked with API library from NDK variant", true, hasDirectDependency(t, ctx, binfoo, libbarApiImportv29))
	android.AssertBoolEquals(t, "Binary using SDK should not be linked with API library from LLNDK variant", false, hasDirectDependency(t, ctx, binfoo, libbarApiImportLlndk))

	binbaz := ctx.ModuleForTests("binbaz", "android_vendor.29_arm64_armv8-a").Module()

	android.AssertBoolEquals(t, "Vendor binary should be linked with API library from LLNDK variant", true, hasDirectDependency(t, ctx, binbaz, libbarApiImportLlndk))
	android.AssertBoolEquals(t, "Vendor binary should not be linked with API library from NDK variant", false, hasDirectDependency(t, ctx, binbaz, libbarApiImportv29))

}