1// Copyright (C) 2019 The Android Open Source Project
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	"android/soong/android"
19)
20
21func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
22	RegisterPrebuiltBuildComponents(ctx)
23	RegisterCCBuildComponents(ctx)
24	RegisterBinaryBuildComponents(ctx)
25	RegisterLibraryBuildComponents(ctx)
26	RegisterLibraryHeadersBuildComponents(ctx)
27
28	ctx.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
29	ctx.RegisterModuleType("llndk_library", LlndkLibraryFactory)
30	ctx.RegisterModuleType("cc_object", ObjectFactory)
31	ctx.RegisterModuleType("cc_genrule", genRuleFactory)
32	ctx.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
33	ctx.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
34	ctx.RegisterModuleType("ndk_library", NdkLibraryFactory)
35}
36
37func GatherRequiredDepsForTest(oses ...android.OsType) string {
38	ret := `
39		toolchain_library {
40			name: "libatomic",
41			defaults: ["linux_bionic_supported"],
42			vendor_available: true,
43			recovery_available: true,
44			native_bridge_supported: true,
45			src: "",
46		}
47
48		toolchain_library {
49			name: "libcompiler_rt-extras",
50			vendor_available: true,
51			recovery_available: true,
52			src: "",
53		}
54
55		toolchain_library {
56			name: "libclang_rt.builtins-arm-android",
57			vendor_available: true,
58			recovery_available: true,
59			native_bridge_supported: true,
60			src: "",
61		}
62
63		toolchain_library {
64			name: "libclang_rt.builtins-aarch64-android",
65			vendor_available: true,
66			recovery_available: true,
67			native_bridge_supported: true,
68			src: "",
69		}
70
71		cc_prebuilt_library_shared {
72			name: "libclang_rt.hwasan-aarch64-android",
73			nocrt: true,
74			vendor_available: true,
75			recovery_available: true,
76			system_shared_libs: [],
77			stl: "none",
78			srcs: [""],
79			check_elf_files: false,
80			sanitize: {
81				never: true,
82			},
83		}
84
85		toolchain_library {
86			name: "libclang_rt.builtins-i686-android",
87			vendor_available: true,
88			recovery_available: true,
89			native_bridge_supported: true,
90			src: "",
91		}
92
93		toolchain_library {
94			name: "libclang_rt.builtins-x86_64-android",
95			defaults: ["linux_bionic_supported"],
96			vendor_available: true,
97			recovery_available: true,
98			native_bridge_supported: true,
99			src: "",
100		}
101
102		toolchain_library {
103			name: "libclang_rt.fuzzer-arm-android",
104			vendor_available: true,
105			recovery_available: true,
106			src: "",
107		}
108
109		toolchain_library {
110			name: "libclang_rt.fuzzer-aarch64-android",
111			vendor_available: true,
112			recovery_available: true,
113			src: "",
114		}
115
116		toolchain_library {
117			name: "libclang_rt.fuzzer-i686-android",
118			vendor_available: true,
119			recovery_available: true,
120			src: "",
121		}
122
123		toolchain_library {
124			name: "libclang_rt.fuzzer-x86_64-android",
125			defaults: ["linux_bionic_supported"],
126			vendor_available: true,
127			recovery_available: true,
128			src: "",
129		}
130
131		toolchain_library {
132			name: "libclang_rt.fuzzer-x86_64",
133			vendor_available: true,
134			recovery_available: true,
135			src: "",
136		}
137
138		// Needed for sanitizer
139		cc_prebuilt_library_shared {
140			name: "libclang_rt.ubsan_standalone-aarch64-android",
141			vendor_available: true,
142			recovery_available: true,
143			system_shared_libs: [],
144			srcs: [""],
145		}
146
147		toolchain_library {
148			name: "libgcc",
149			defaults: ["linux_bionic_supported"],
150			vendor_available: true,
151			recovery_available: true,
152			src: "",
153		}
154
155		toolchain_library {
156			name: "libgcc_stripped",
157			defaults: ["linux_bionic_supported"],
158			vendor_available: true,
159			recovery_available: true,
160			sdk_version: "current",
161			src: "",
162		}
163
164		cc_library {
165			name: "libc",
166			defaults: ["linux_bionic_supported"],
167			no_libcrt: true,
168			nocrt: true,
169			stl: "none",
170			system_shared_libs: [],
171			recovery_available: true,
172			stubs: {
173				versions: ["27", "28", "29"],
174			},
175		}
176		llndk_library {
177			name: "libc",
178			symbol_file: "",
179			sdk_version: "current",
180		}
181		cc_library {
182			name: "libm",
183			defaults: ["linux_bionic_supported"],
184			no_libcrt: true,
185			nocrt: true,
186			stl: "none",
187			system_shared_libs: [],
188			recovery_available: true,
189			stubs: {
190				versions: ["27", "28", "29"],
191			},
192			apex_available: [
193				"//apex_available:platform",
194				"myapex"
195			],
196		}
197		llndk_library {
198			name: "libm",
199			symbol_file: "",
200			sdk_version: "current",
201		}
202
203		// Coverage libraries
204		cc_library {
205			name: "libprofile-extras",
206			vendor_available: true,
207			recovery_available: true,
208			native_coverage: false,
209			system_shared_libs: [],
210			stl: "none",
211			notice: "custom_notice",
212		}
213		cc_library {
214			name: "libprofile-clang-extras",
215			vendor_available: true,
216			recovery_available: true,
217			native_coverage: false,
218			system_shared_libs: [],
219			stl: "none",
220			notice: "custom_notice",
221		}
222		cc_library {
223			name: "libprofile-extras_ndk",
224			vendor_available: true,
225			native_coverage: false,
226			system_shared_libs: [],
227			stl: "none",
228			notice: "custom_notice",
229			sdk_version: "current",
230		}
231		cc_library {
232			name: "libprofile-clang-extras_ndk",
233			vendor_available: true,
234			native_coverage: false,
235			system_shared_libs: [],
236			stl: "none",
237			notice: "custom_notice",
238			sdk_version: "current",
239		}
240
241		cc_library {
242			name: "libdl",
243			defaults: ["linux_bionic_supported"],
244			no_libcrt: true,
245			nocrt: true,
246			stl: "none",
247			system_shared_libs: [],
248			recovery_available: true,
249			stubs: {
250				versions: ["27", "28", "29"],
251			},
252			apex_available: [
253				"//apex_available:platform",
254				"myapex"
255			],
256		}
257		llndk_library {
258			name: "libdl",
259			symbol_file: "",
260			sdk_version: "current",
261		}
262		cc_library {
263			name: "libft2",
264			no_libcrt: true,
265			nocrt: true,
266			system_shared_libs: [],
267			recovery_available: true,
268		}
269		llndk_library {
270			name: "libft2",
271			symbol_file: "",
272			vendor_available: false,
273			sdk_version: "current",
274		}
275		cc_library {
276			name: "libc++_static",
277			no_libcrt: true,
278			nocrt: true,
279			system_shared_libs: [],
280			stl: "none",
281			vendor_available: true,
282			recovery_available: true,
283			host_supported: true,
284			min_sdk_version: "29",
285			apex_available: [
286				"//apex_available:platform",
287				"//apex_available:anyapex",
288			],
289		}
290		cc_library {
291			name: "libc++",
292			no_libcrt: true,
293			nocrt: true,
294			system_shared_libs: [],
295			stl: "none",
296			vendor_available: true,
297			recovery_available: true,
298			host_supported: true,
299			min_sdk_version: "29",
300			vndk: {
301				enabled: true,
302				support_system_process: true,
303			},
304			apex_available: [
305				"//apex_available:platform",
306				"myapex"
307			],
308		}
309		cc_library {
310			name: "libc++demangle",
311			no_libcrt: true,
312			nocrt: true,
313			system_shared_libs: [],
314			stl: "none",
315			host_supported: false,
316			vendor_available: true,
317			recovery_available: true,
318			min_sdk_version: "29",
319			apex_available: [
320				"//apex_available:platform",
321				"//apex_available:anyapex",
322			],
323		}
324		cc_library {
325			name: "libunwind_llvm",
326			no_libcrt: true,
327			nocrt: true,
328			system_shared_libs: [],
329			stl: "none",
330			vendor_available: true,
331			recovery_available: true,
332		}
333
334		cc_defaults {
335			name: "crt_defaults",
336			defaults: ["linux_bionic_supported"],
337			recovery_available: true,
338			vendor_available: true,
339			native_bridge_supported: true,
340			stl: "none",
341			apex_available: [
342				"//apex_available:platform",
343				"//apex_available:anyapex",
344			],
345		}
346
347		cc_object {
348			name: "crtbegin_so",
349			defaults: ["crt_defaults"],
350			recovery_available: true,
351			vendor_available: true,
352			native_bridge_supported: true,
353			min_sdk_version: "29",
354			stl: "none",
355		}
356
357		cc_object {
358			name: "crtbegin_dynamic",
359			defaults: ["crt_defaults"],
360			recovery_available: true,
361			vendor_available: true,
362			native_bridge_supported: true,
363			stl: "none",
364		}
365
366		cc_object {
367			name: "crtbegin_static",
368			defaults: ["crt_defaults"],
369			recovery_available: true,
370			vendor_available: true,
371			native_bridge_supported: true,
372			stl: "none",
373		}
374
375		cc_object {
376			name: "crtend_so",
377			defaults: ["crt_defaults"],
378			recovery_available: true,
379			vendor_available: true,
380			native_bridge_supported: true,
381			min_sdk_version: "29",
382			stl: "none",
383		}
384
385		cc_object {
386			name: "crtend_android",
387			defaults: ["crt_defaults"],
388			recovery_available: true,
389			vendor_available: true,
390			native_bridge_supported: true,
391			stl: "none",
392		}
393
394		cc_library {
395			name: "libprotobuf-cpp-lite",
396		}
397
398		cc_library {
399			name: "ndk_libunwind",
400			sdk_version: "current",
401			stl: "none",
402			system_shared_libs: [],
403		}
404
405		ndk_library {
406			name: "libc",
407			first_version: "minimum",
408			symbol_file: "libc.map.txt",
409		}
410
411		ndk_library {
412			name: "libm",
413			first_version: "minimum",
414			symbol_file: "libm.map.txt",
415		}
416
417		ndk_library {
418			name: "libdl",
419			first_version: "minimum",
420			symbol_file: "libdl.map.txt",
421		}
422
423		ndk_prebuilt_object {
424			name: "ndk_crtbegin_so.27",
425			sdk_version: "27",
426		}
427
428		ndk_prebuilt_object {
429			name: "ndk_crtend_so.27",
430			sdk_version: "27",
431		}
432
433		ndk_prebuilt_object {
434			name: "ndk_crtbegin_dynamic.27",
435			sdk_version: "27",
436		}
437
438		ndk_prebuilt_object {
439			name: "ndk_crtend_android.27",
440			sdk_version: "27",
441		}
442
443		ndk_prebuilt_shared_stl {
444			name: "ndk_libc++_shared",
445		}
446	`
447
448	supportLinuxBionic := false
449	for _, os := range oses {
450		if os == android.Fuchsia {
451			ret += `
452		cc_library {
453			name: "libbioniccompat",
454			stl: "none",
455		}
456		cc_library {
457			name: "libcompiler_rt",
458			stl: "none",
459		}
460		`
461		}
462		if os == android.Windows {
463			ret += `
464		toolchain_library {
465			name: "libwinpthread",
466			host_supported: true,
467			enabled: false,
468			target: {
469				windows: {
470					enabled: true,
471				},
472			},
473			src: "",
474		}
475		`
476		}
477		if os == android.LinuxBionic {
478			supportLinuxBionic = true
479			ret += `
480				cc_binary {
481					name: "linker",
482					defaults: ["linux_bionic_supported"],
483					recovery_available: true,
484					stl: "none",
485					nocrt: true,
486					static_executable: true,
487					native_coverage: false,
488					system_shared_libs: [],
489				}
490
491				cc_genrule {
492					name: "host_bionic_linker_flags",
493					host_supported: true,
494					device_supported: false,
495					target: {
496						host: {
497							enabled: false,
498						},
499						linux_bionic: {
500							enabled: true,
501						},
502					},
503					out: ["linker.flags"],
504				}
505
506				cc_defaults {
507					name: "linux_bionic_supported",
508					host_supported: true,
509					target: {
510						host: {
511							enabled: false,
512						},
513						linux_bionic: {
514							enabled: true,
515						},
516					},
517				}
518			`
519		}
520	}
521
522	if !supportLinuxBionic {
523		ret += `
524			cc_defaults {
525				name: "linux_bionic_supported",
526			}
527		`
528	}
529
530	return ret
531}
532
533func GatherRequiredFilesForTest(fs map[string][]byte) {
534}
535
536func TestConfig(buildDir string, os android.OsType, env map[string]string,
537	bp string, fs map[string][]byte) android.Config {
538
539	// add some modules that are required by the compiler and/or linker
540	bp = bp + GatherRequiredDepsForTest(os)
541
542	mockFS := map[string][]byte{}
543
544	GatherRequiredFilesForTest(mockFS)
545
546	for k, v := range fs {
547		mockFS[k] = v
548	}
549
550	var config android.Config
551	if os == android.Fuchsia {
552		config = android.TestArchConfigFuchsia(buildDir, env, bp, mockFS)
553	} else {
554		config = android.TestArchConfig(buildDir, env, bp, mockFS)
555	}
556
557	return config
558}
559
560func CreateTestContext() *android.TestContext {
561	ctx := android.NewTestArchContext()
562	ctx.RegisterModuleType("cc_fuzz", FuzzFactory)
563	ctx.RegisterModuleType("cc_test", TestFactory)
564	ctx.RegisterModuleType("cc_test_library", TestLibraryFactory)
565	ctx.RegisterModuleType("llndk_headers", llndkHeadersFactory)
566	ctx.RegisterModuleType("vendor_public_library", vendorPublicLibraryFactory)
567	ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
568	ctx.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
569	ctx.RegisterModuleType("vndk_libraries_txt", VndkLibrariesTxtFactory)
570	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
571	android.RegisterPrebuiltMutators(ctx)
572	RegisterRequiredBuildComponentsForTest(ctx)
573	ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
574	ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
575
576	return ctx
577}
578