• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 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 apex
16
17import (
18	"fmt"
19	"path"
20	"path/filepath"
21	"regexp"
22	"sort"
23	"strings"
24	"sync"
25
26	"github.com/google/blueprint"
27	"github.com/google/blueprint/bootstrap"
28	"github.com/google/blueprint/proptools"
29
30	"android/soong/android"
31	"android/soong/cc"
32	prebuilt_etc "android/soong/etc"
33	"android/soong/java"
34	"android/soong/python"
35	"android/soong/sh"
36)
37
38const (
39	imageApexSuffix = ".apex"
40	zipApexSuffix   = ".zipapex"
41	flattenedSuffix = ".flattened"
42
43	imageApexType     = "image"
44	zipApexType       = "zip"
45	flattenedApexType = "flattened"
46)
47
48type dependencyTag struct {
49	blueprint.BaseDependencyTag
50	name string
51
52	// determines if the dependent will be part of the APEX payload
53	payload bool
54}
55
56var (
57	sharedLibTag   = dependencyTag{name: "sharedLib", payload: true}
58	executableTag  = dependencyTag{name: "executable", payload: true}
59	javaLibTag     = dependencyTag{name: "javaLib", payload: true}
60	prebuiltTag    = dependencyTag{name: "prebuilt", payload: true}
61	testTag        = dependencyTag{name: "test", payload: true}
62	keyTag         = dependencyTag{name: "key"}
63	certificateTag = dependencyTag{name: "certificate"}
64	usesTag        = dependencyTag{name: "uses"}
65	androidAppTag  = dependencyTag{name: "androidApp", payload: true}
66
67	apexAvailBaseline = makeApexAvailableBaseline()
68
69	inverseApexAvailBaseline = invertApexBaseline(apexAvailBaseline)
70)
71
72// Transform the map of apex -> modules to module -> apexes.
73func invertApexBaseline(m map[string][]string) map[string][]string {
74	r := make(map[string][]string)
75	for apex, modules := range m {
76		for _, module := range modules {
77			r[module] = append(r[module], apex)
78		}
79	}
80	return r
81}
82
83// Retrieve the baseline of apexes to which the supplied module belongs.
84func BaselineApexAvailable(moduleName string) []string {
85	return inverseApexAvailBaseline[normalizeModuleName(moduleName)]
86}
87
88// This is a map from apex to modules, which overrides the
89// apex_available setting for that particular module to make
90// it available for the apex regardless of its setting.
91// TODO(b/147364041): remove this
92func makeApexAvailableBaseline() map[string][]string {
93	// The "Module separator"s below are employed to minimize merge conflicts.
94	m := make(map[string][]string)
95	//
96	// Module separator
97	//
98	m["com.android.appsearch"] = []string{
99		"icing-java-proto-lite",
100		"libprotobuf-java-lite",
101	}
102	//
103	// Module separator
104	//
105	m["com.android.bluetooth.updatable"] = []string{
106		"android.hardware.audio.common@5.0",
107		"android.hardware.bluetooth.a2dp@1.0",
108		"android.hardware.bluetooth.audio@2.0",
109		"android.hardware.bluetooth@1.0",
110		"android.hardware.bluetooth@1.1",
111		"android.hardware.graphics.bufferqueue@1.0",
112		"android.hardware.graphics.bufferqueue@2.0",
113		"android.hardware.graphics.common@1.0",
114		"android.hardware.graphics.common@1.1",
115		"android.hardware.graphics.common@1.2",
116		"android.hardware.media@1.0",
117		"android.hidl.safe_union@1.0",
118		"android.hidl.token@1.0",
119		"android.hidl.token@1.0-utils",
120		"avrcp-target-service",
121		"avrcp_headers",
122		"bluetooth-protos-lite",
123		"bluetooth.mapsapi",
124		"com.android.vcard",
125		"dnsresolver_aidl_interface-V2-java",
126		"ipmemorystore-aidl-interfaces-V5-java",
127		"ipmemorystore-aidl-interfaces-java",
128		"internal_include_headers",
129		"lib-bt-packets",
130		"lib-bt-packets-avrcp",
131		"lib-bt-packets-base",
132		"libFraunhoferAAC",
133		"libaudio-a2dp-hw-utils",
134		"libaudio-hearing-aid-hw-utils",
135		"libbinder_headers",
136		"libbluetooth",
137		"libbluetooth-types",
138		"libbluetooth-types-header",
139		"libbluetooth_gd",
140		"libbluetooth_headers",
141		"libbluetooth_jni",
142		"libbt-audio-hal-interface",
143		"libbt-bta",
144		"libbt-common",
145		"libbt-hci",
146		"libbt-platform-protos-lite",
147		"libbt-protos-lite",
148		"libbt-sbc-decoder",
149		"libbt-sbc-encoder",
150		"libbt-stack",
151		"libbt-utils",
152		"libbtcore",
153		"libbtdevice",
154		"libbte",
155		"libbtif",
156		"libchrome",
157		"libevent",
158		"libfmq",
159		"libg722codec",
160		"libgui_headers",
161		"libmedia_headers",
162		"libmodpb64",
163		"libosi",
164		"libstagefright_foundation_headers",
165		"libstagefright_headers",
166		"libstatslog",
167		"libstatssocket",
168		"libtinyxml2",
169		"libudrv-uipc",
170		"libz",
171		"media_plugin_headers",
172		"net-utils-services-common",
173		"netd_aidl_interface-unstable-java",
174		"netd_event_listener_interface-java",
175		"netlink-client",
176		"networkstack-client",
177		"sap-api-java-static",
178		"services.net",
179	}
180	//
181	// Module separator
182	//
183	m["com.android.cellbroadcast"] = []string{"CellBroadcastApp", "CellBroadcastServiceModule"}
184	//
185	// Module separator
186	//
187	m["com.android.conscrypt"] = []string{
188		"libnativehelper_header_only",
189	}
190	//
191	// Module separator
192	//
193	m["com.android.extservices"] = []string{
194		"error_prone_annotations",
195		"ExtServices-core",
196		"ExtServices",
197		"libtextclassifier-java",
198		"libz_current",
199		"textclassifier-statsd",
200		"TextClassifierNotificationLibNoManifest",
201		"TextClassifierServiceLibNoManifest",
202	}
203	//
204	// Module separator
205	//
206	m["com.android.neuralnetworks"] = []string{
207		"android.hardware.neuralnetworks@1.0",
208		"android.hardware.neuralnetworks@1.1",
209		"android.hardware.neuralnetworks@1.2",
210		"android.hardware.neuralnetworks@1.3",
211		"android.hidl.allocator@1.0",
212		"android.hidl.memory.token@1.0",
213		"android.hidl.memory@1.0",
214		"android.hidl.safe_union@1.0",
215		"libarect",
216		"libbuildversion",
217		"libmath",
218		"libprocpartition",
219		"libsync",
220	}
221	//
222	// Module separator
223	//
224	m["com.android.media"] = []string{
225		"android.frameworks.bufferhub@1.0",
226		"android.hardware.cas.native@1.0",
227		"android.hardware.cas@1.0",
228		"android.hardware.configstore-utils",
229		"android.hardware.configstore@1.0",
230		"android.hardware.configstore@1.1",
231		"android.hardware.graphics.allocator@2.0",
232		"android.hardware.graphics.allocator@3.0",
233		"android.hardware.graphics.bufferqueue@1.0",
234		"android.hardware.graphics.bufferqueue@2.0",
235		"android.hardware.graphics.common@1.0",
236		"android.hardware.graphics.common@1.1",
237		"android.hardware.graphics.common@1.2",
238		"android.hardware.graphics.mapper@2.0",
239		"android.hardware.graphics.mapper@2.1",
240		"android.hardware.graphics.mapper@3.0",
241		"android.hardware.media.omx@1.0",
242		"android.hardware.media@1.0",
243		"android.hidl.allocator@1.0",
244		"android.hidl.memory.token@1.0",
245		"android.hidl.memory@1.0",
246		"android.hidl.token@1.0",
247		"android.hidl.token@1.0-utils",
248		"bionic_libc_platform_headers",
249		"exoplayer2-extractor",
250		"exoplayer2-extractor-annotation-stubs",
251		"gl_headers",
252		"jsr305",
253		"libEGL",
254		"libEGL_blobCache",
255		"libEGL_getProcAddress",
256		"libFLAC",
257		"libFLAC-config",
258		"libFLAC-headers",
259		"libGLESv2",
260		"libaacextractor",
261		"libamrextractor",
262		"libarect",
263		"libaudio_system_headers",
264		"libaudioclient",
265		"libaudioclient_headers",
266		"libaudiofoundation",
267		"libaudiofoundation_headers",
268		"libaudiomanager",
269		"libaudiopolicy",
270		"libaudioutils",
271		"libaudioutils_fixedfft",
272		"libbinder_headers",
273		"libbluetooth-types-header",
274		"libbufferhub",
275		"libbufferhub_headers",
276		"libbufferhubqueue",
277		"libc_malloc_debug_backtrace",
278		"libcamera_client",
279		"libcamera_metadata",
280		"libdexfile_external_headers",
281		"libdexfile_support",
282		"libdvr_headers",
283		"libexpat",
284		"libfifo",
285		"libflacextractor",
286		"libgrallocusage",
287		"libgraphicsenv",
288		"libgui",
289		"libgui_headers",
290		"libhardware_headers",
291		"libinput",
292		"liblzma",
293		"libmath",
294		"libmedia",
295		"libmedia_codeclist",
296		"libmedia_headers",
297		"libmedia_helper",
298		"libmedia_helper_headers",
299		"libmedia_midiiowrapper",
300		"libmedia_omx",
301		"libmediautils",
302		"libmidiextractor",
303		"libmkvextractor",
304		"libmp3extractor",
305		"libmp4extractor",
306		"libmpeg2extractor",
307		"libnativebase_headers",
308		"libnativebridge-headers",
309		"libnativebridge_lazy",
310		"libnativeloader-headers",
311		"libnativeloader_lazy",
312		"libnativewindow_headers",
313		"libnblog",
314		"liboggextractor",
315		"libpackagelistparser",
316		"libpdx",
317		"libpdx_default_transport",
318		"libpdx_headers",
319		"libpdx_uds",
320		"libprocinfo",
321		"libspeexresampler",
322		"libspeexresampler",
323		"libstagefright_esds",
324		"libstagefright_flacdec",
325		"libstagefright_flacdec",
326		"libstagefright_foundation",
327		"libstagefright_foundation_headers",
328		"libstagefright_foundation_without_imemory",
329		"libstagefright_headers",
330		"libstagefright_id3",
331		"libstagefright_metadatautils",
332		"libstagefright_mpeg2extractor",
333		"libstagefright_mpeg2support",
334		"libsync",
335		"libui",
336		"libui_headers",
337		"libunwindstack",
338		"libvibrator",
339		"libvorbisidec",
340		"libwavextractor",
341		"libwebm",
342		"media_ndk_headers",
343		"media_plugin_headers",
344		"updatable-media",
345	}
346	//
347	// Module separator
348	//
349	m["com.android.media.swcodec"] = []string{
350		"android.frameworks.bufferhub@1.0",
351		"android.hardware.common-ndk_platform",
352		"android.hardware.configstore-utils",
353		"android.hardware.configstore@1.0",
354		"android.hardware.configstore@1.1",
355		"android.hardware.graphics.allocator@2.0",
356		"android.hardware.graphics.allocator@3.0",
357		"android.hardware.graphics.allocator@4.0",
358		"android.hardware.graphics.bufferqueue@1.0",
359		"android.hardware.graphics.bufferqueue@2.0",
360		"android.hardware.graphics.common-ndk_platform",
361		"android.hardware.graphics.common@1.0",
362		"android.hardware.graphics.common@1.1",
363		"android.hardware.graphics.common@1.2",
364		"android.hardware.graphics.mapper@2.0",
365		"android.hardware.graphics.mapper@2.1",
366		"android.hardware.graphics.mapper@3.0",
367		"android.hardware.graphics.mapper@4.0",
368		"android.hardware.media.bufferpool@2.0",
369		"android.hardware.media.c2@1.0",
370		"android.hardware.media.c2@1.1",
371		"android.hardware.media.omx@1.0",
372		"android.hardware.media@1.0",
373		"android.hardware.media@1.0",
374		"android.hidl.memory.token@1.0",
375		"android.hidl.memory@1.0",
376		"android.hidl.safe_union@1.0",
377		"android.hidl.token@1.0",
378		"android.hidl.token@1.0-utils",
379		"libEGL",
380		"libFLAC",
381		"libFLAC-config",
382		"libFLAC-headers",
383		"libFraunhoferAAC",
384		"libLibGuiProperties",
385		"libarect",
386		"libaudio_system_headers",
387		"libaudioutils",
388		"libaudioutils",
389		"libaudioutils_fixedfft",
390		"libavcdec",
391		"libavcenc",
392		"libavservices_minijail",
393		"libavservices_minijail",
394		"libbinder_headers",
395		"libbinderthreadstateutils",
396		"libbluetooth-types-header",
397		"libbufferhub_headers",
398		"libc_scudo",
399		"libcodec2",
400		"libcodec2_headers",
401		"libcodec2_hidl@1.0",
402		"libcodec2_hidl@1.1",
403		"libcodec2_internal",
404		"libcodec2_soft_aacdec",
405		"libcodec2_soft_aacenc",
406		"libcodec2_soft_amrnbdec",
407		"libcodec2_soft_amrnbenc",
408		"libcodec2_soft_amrwbdec",
409		"libcodec2_soft_amrwbenc",
410		"libcodec2_soft_av1dec_gav1",
411		"libcodec2_soft_avcdec",
412		"libcodec2_soft_avcenc",
413		"libcodec2_soft_common",
414		"libcodec2_soft_flacdec",
415		"libcodec2_soft_flacenc",
416		"libcodec2_soft_g711alawdec",
417		"libcodec2_soft_g711mlawdec",
418		"libcodec2_soft_gsmdec",
419		"libcodec2_soft_h263dec",
420		"libcodec2_soft_h263enc",
421		"libcodec2_soft_hevcdec",
422		"libcodec2_soft_hevcenc",
423		"libcodec2_soft_mp3dec",
424		"libcodec2_soft_mpeg2dec",
425		"libcodec2_soft_mpeg4dec",
426		"libcodec2_soft_mpeg4enc",
427		"libcodec2_soft_opusdec",
428		"libcodec2_soft_opusenc",
429		"libcodec2_soft_rawdec",
430		"libcodec2_soft_vorbisdec",
431		"libcodec2_soft_vp8dec",
432		"libcodec2_soft_vp8enc",
433		"libcodec2_soft_vp9dec",
434		"libcodec2_soft_vp9enc",
435		"libcodec2_vndk",
436		"libdexfile_support",
437		"libdvr_headers",
438		"libfmq",
439		"libfmq",
440		"libgav1",
441		"libgralloctypes",
442		"libgrallocusage",
443		"libgraphicsenv",
444		"libgsm",
445		"libgui_bufferqueue_static",
446		"libgui_headers",
447		"libhardware",
448		"libhardware_headers",
449		"libhevcdec",
450		"libhevcenc",
451		"libion",
452		"libjpeg",
453		"liblzma",
454		"libmath",
455		"libmedia_codecserviceregistrant",
456		"libmedia_headers",
457		"libmpeg2dec",
458		"libnativebase_headers",
459		"libnativebridge_lazy",
460		"libnativeloader_lazy",
461		"libnativewindow_headers",
462		"libpdx_headers",
463		"libscudo_wrapper",
464		"libsfplugin_ccodec_utils",
465		"libspeexresampler",
466		"libstagefright_amrnb_common",
467		"libstagefright_amrnbdec",
468		"libstagefright_amrnbenc",
469		"libstagefright_amrwbdec",
470		"libstagefright_amrwbenc",
471		"libstagefright_bufferpool@2.0.1",
472		"libstagefright_bufferqueue_helper",
473		"libstagefright_enc_common",
474		"libstagefright_flacdec",
475		"libstagefright_foundation",
476		"libstagefright_foundation_headers",
477		"libstagefright_headers",
478		"libstagefright_m4vh263dec",
479		"libstagefright_m4vh263enc",
480		"libstagefright_mp3dec",
481		"libsync",
482		"libui",
483		"libui_headers",
484		"libunwindstack",
485		"libvorbisidec",
486		"libvpx",
487		"libyuv",
488		"libyuv_static",
489		"media_ndk_headers",
490		"media_plugin_headers",
491		"mediaswcodec",
492	}
493	//
494	// Module separator
495	//
496	m["com.android.mediaprovider"] = []string{
497		"MediaProvider",
498		"MediaProviderGoogle",
499		"fmtlib_ndk",
500		"libbase_ndk",
501		"libfuse",
502		"libfuse_jni",
503		"libnativehelper_header_only",
504	}
505	//
506	// Module separator
507	//
508	m["com.android.permission"] = []string{
509		"car-ui-lib",
510		"iconloader",
511		"kotlin-annotations",
512		"kotlin-stdlib",
513		"kotlin-stdlib-jdk7",
514		"kotlin-stdlib-jdk8",
515		"kotlinx-coroutines-android",
516		"kotlinx-coroutines-android-nodeps",
517		"kotlinx-coroutines-core",
518		"kotlinx-coroutines-core-nodeps",
519		"permissioncontroller-statsd",
520		"GooglePermissionController",
521		"PermissionController",
522		"SettingsLibActionBarShadow",
523		"SettingsLibAppPreference",
524		"SettingsLibBarChartPreference",
525		"SettingsLibLayoutPreference",
526		"SettingsLibProgressBar",
527		"SettingsLibSearchWidget",
528		"SettingsLibSettingsTheme",
529		"SettingsLibRestrictedLockUtils",
530		"SettingsLibHelpUtils",
531	}
532	//
533	// Module separator
534	//
535	m["com.android.runtime"] = []string{
536		"bionic_libc_platform_headers",
537		"libarm-optimized-routines-math",
538		"libc_aeabi",
539		"libc_bionic",
540		"libc_bionic_ndk",
541		"libc_bootstrap",
542		"libc_common",
543		"libc_common_shared",
544		"libc_common_static",
545		"libc_dns",
546		"libc_dynamic_dispatch",
547		"libc_fortify",
548		"libc_freebsd",
549		"libc_freebsd_large_stack",
550		"libc_gdtoa",
551		"libc_init_dynamic",
552		"libc_init_static",
553		"libc_jemalloc_wrapper",
554		"libc_netbsd",
555		"libc_nomalloc",
556		"libc_nopthread",
557		"libc_openbsd",
558		"libc_openbsd_large_stack",
559		"libc_openbsd_ndk",
560		"libc_pthread",
561		"libc_static_dispatch",
562		"libc_syscalls",
563		"libc_tzcode",
564		"libc_unwind_static",
565		"libdebuggerd",
566		"libdebuggerd_common_headers",
567		"libdebuggerd_handler_core",
568		"libdebuggerd_handler_fallback",
569		"libdexfile_external_headers",
570		"libdexfile_support",
571		"libdexfile_support_static",
572		"libdl_static",
573		"libjemalloc5",
574		"liblinker_main",
575		"liblinker_malloc",
576		"liblz4",
577		"liblzma",
578		"libprocinfo",
579		"libpropertyinfoparser",
580		"libscudo",
581		"libstdc++",
582		"libsystemproperties",
583		"libtombstoned_client_static",
584		"libunwindstack",
585		"libz",
586		"libziparchive",
587	}
588	//
589	// Module separator
590	//
591	m["com.android.tethering"] = []string{
592		"android.hardware.tetheroffload.config-V1.0-java",
593		"android.hardware.tetheroffload.control-V1.0-java",
594		"android.hidl.base-V1.0-java",
595		"ipmemorystore-aidl-interfaces-java",
596		"libcgrouprc",
597		"libcgrouprc_format",
598		"libnativehelper_compat_libc++",
599		"libtetherutilsjni",
600		"libvndksupport",
601		"net-utils-framework-common",
602		"netd_aidl_interface-V3-java",
603		"netlink-client",
604		"networkstack-aidl-interfaces-java",
605		"tethering-aidl-interfaces-java",
606		"TetheringApiCurrentLib",
607	}
608	//
609	// Module separator
610	//
611	m["com.android.wifi"] = []string{
612		"PlatformProperties",
613		"android.hardware.wifi-V1.0-java",
614		"android.hardware.wifi-V1.0-java-constants",
615		"android.hardware.wifi-V1.1-java",
616		"android.hardware.wifi-V1.2-java",
617		"android.hardware.wifi-V1.3-java",
618		"android.hardware.wifi-V1.4-java",
619		"android.hardware.wifi.hostapd-V1.0-java",
620		"android.hardware.wifi.hostapd-V1.1-java",
621		"android.hardware.wifi.hostapd-V1.2-java",
622		"android.hardware.wifi.supplicant-V1.0-java",
623		"android.hardware.wifi.supplicant-V1.1-java",
624		"android.hardware.wifi.supplicant-V1.2-java",
625		"android.hardware.wifi.supplicant-V1.3-java",
626		"android.hidl.base-V1.0-java",
627		"android.hidl.manager-V1.0-java",
628		"android.hidl.manager-V1.1-java",
629		"android.hidl.manager-V1.2-java",
630		"bouncycastle-unbundled",
631		"dnsresolver_aidl_interface-V2-java",
632		"error_prone_annotations",
633		"framework-wifi-pre-jarjar",
634		"framework-wifi-util-lib",
635		"ipmemorystore-aidl-interfaces-V3-java",
636		"ipmemorystore-aidl-interfaces-java",
637		"ksoap2",
638		"libnanohttpd",
639		"libwifi-jni",
640		"net-utils-services-common",
641		"netd_aidl_interface-V2-java",
642		"netd_aidl_interface-unstable-java",
643		"netd_event_listener_interface-java",
644		"netlink-client",
645		"networkstack-client",
646		"services.net",
647		"wifi-lite-protos",
648		"wifi-nano-protos",
649		"wifi-service-pre-jarjar",
650		"wifi-service-resources",
651	}
652	//
653	// Module separator
654	//
655	m["com.android.sdkext"] = []string{
656		"fmtlib_ndk",
657		"libbase_ndk",
658		"libprotobuf-cpp-lite-ndk",
659	}
660	//
661	// Module separator
662	//
663	m["com.android.os.statsd"] = []string{
664		"libstatssocket",
665	}
666	//
667	// Module separator
668	//
669	m[android.AvailableToAnyApex] = []string{
670		// TODO(b/156996905) Set apex_available/min_sdk_version for androidx/extras support libraries
671		"androidx",
672		"androidx-constraintlayout_constraintlayout",
673		"androidx-constraintlayout_constraintlayout-nodeps",
674		"androidx-constraintlayout_constraintlayout-solver",
675		"androidx-constraintlayout_constraintlayout-solver-nodeps",
676		"com.google.android.material_material",
677		"com.google.android.material_material-nodeps",
678
679		"libatomic",
680		"libclang_rt",
681		"libgcc_stripped",
682		"libprofile-clang-extras",
683		"libprofile-clang-extras_ndk",
684		"libprofile-extras",
685		"libprofile-extras_ndk",
686		"libunwind_llvm",
687	}
688	return m
689}
690
691// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
692// Adding code to the bootclasspath in new packages will cause issues on module update.
693func qModulesPackages() map[string][]string {
694	return map[string][]string{
695		"com.android.conscrypt": []string{
696			"android.net.ssl",
697			"com.android.org.conscrypt",
698		},
699		"com.android.media": []string{
700			"android.media",
701		},
702	}
703}
704
705// DO NOT EDIT! These are the package prefixes that are exempted from being AOT'ed by ART.
706// Adding code to the bootclasspath in new packages will cause issues on module update.
707func rModulesPackages() map[string][]string {
708	return map[string][]string{
709		"com.android.mediaprovider": []string{
710			"android.provider",
711		},
712		"com.android.permission": []string{
713			"android.permission",
714			"android.app.role",
715			"com.android.permission",
716			"com.android.role",
717		},
718		"com.android.sdkext": []string{
719			"android.os.ext",
720		},
721		"com.android.os.statsd": []string{
722			"android.app",
723			"android.os",
724			"android.util",
725			"com.android.internal.statsd",
726			"com.android.server.stats",
727		},
728		"com.android.wifi": []string{
729			"com.android.server.wifi",
730			"com.android.wifi.x",
731			"android.hardware.wifi",
732			"android.net.wifi",
733		},
734		"com.android.tethering": []string{
735			"android.net",
736		},
737	}
738}
739
740func init() {
741	android.RegisterModuleType("apex", BundleFactory)
742	android.RegisterModuleType("apex_test", testApexBundleFactory)
743	android.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
744	android.RegisterModuleType("apex_defaults", defaultsFactory)
745	android.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
746	android.RegisterModuleType("override_apex", overrideApexFactory)
747	android.RegisterModuleType("apex_set", apexSetFactory)
748
749	android.PreDepsMutators(RegisterPreDepsMutators)
750	android.PostDepsMutators(RegisterPostDepsMutators)
751
752	android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
753		apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
754		sort.Strings(*apexFileContextsInfos)
755		ctx.Strict("APEX_FILE_CONTEXTS_INFOS", strings.Join(*apexFileContextsInfos, " "))
756	})
757
758	android.AddNeverAllowRules(createApexPermittedPackagesRules(qModulesPackages())...)
759	android.AddNeverAllowRules(createApexPermittedPackagesRules(rModulesPackages())...)
760}
761
762func createApexPermittedPackagesRules(modules_packages map[string][]string) []android.Rule {
763	rules := make([]android.Rule, 0, len(modules_packages))
764	for module_name, module_packages := range modules_packages {
765		permitted_packages_rule := android.NeverAllow().
766			BootclasspathJar().
767			With("apex_available", module_name).
768			WithMatcher("permitted_packages", android.NotInList(module_packages)).
769			Because("jars that are part of the " + module_name +
770				" module may only allow these packages: " + strings.Join(module_packages, ",") +
771				". Please jarjar or move code around.")
772		rules = append(rules, permitted_packages_rule)
773	}
774	return rules
775}
776
777func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
778	ctx.TopDown("apex_vndk", apexVndkMutator).Parallel()
779	ctx.BottomUp("apex_vndk_deps", apexVndkDepsMutator).Parallel()
780}
781
782func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
783	ctx.TopDown("apex_deps", apexDepsMutator)
784	ctx.BottomUp("apex", apexMutator).Parallel()
785	ctx.BottomUp("apex_flattened", apexFlattenedMutator).Parallel()
786	ctx.BottomUp("apex_uses", apexUsesMutator).Parallel()
787	ctx.BottomUp("mark_platform_availability", markPlatformAvailability).Parallel()
788}
789
790// Mark the direct and transitive dependencies of apex bundles so that they
791// can be built for the apex bundles.
792func apexDepsMutator(mctx android.TopDownMutatorContext) {
793	if !mctx.Module().Enabled() {
794		return
795	}
796	var apexBundles []android.ApexInfo
797	var directDep bool
798	if a, ok := mctx.Module().(*apexBundle); ok && !a.vndkApex {
799		apexBundles = []android.ApexInfo{{
800			ApexName:      mctx.ModuleName(),
801			MinSdkVersion: a.minSdkVersion(mctx),
802			Updatable:     a.Updatable(),
803		}}
804		directDep = true
805	} else if am, ok := mctx.Module().(android.ApexModule); ok {
806		apexBundles = am.ApexVariations()
807		directDep = false
808	}
809
810	if len(apexBundles) == 0 {
811		return
812	}
813
814	cur := mctx.Module().(android.DepIsInSameApex)
815
816	mctx.VisitDirectDeps(func(child android.Module) {
817		depName := mctx.OtherModuleName(child)
818		if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() &&
819			(cur.DepIsInSameApex(mctx, child) || inAnySdk(child)) {
820			android.UpdateApexDependency(apexBundles, depName, directDep)
821			am.BuildForApexes(apexBundles)
822		}
823	})
824}
825
826// mark if a module cannot be available to platform. A module cannot be available
827// to platform if 1) it is explicitly marked as not available (i.e. "//apex_available:platform"
828// is absent) or 2) it depends on another module that isn't (or can't be) available to platform
829func markPlatformAvailability(mctx android.BottomUpMutatorContext) {
830	// Host and recovery are not considered as platform
831	if mctx.Host() || mctx.Module().InstallInRecovery() {
832		return
833	}
834
835	if am, ok := mctx.Module().(android.ApexModule); ok {
836		availableToPlatform := am.AvailableFor(android.AvailableToPlatform)
837
838		// In a rare case when a lib is marked as available only to an apex
839		// but the apex doesn't exist. This can happen in a partial manifest branch
840		// like master-art. Currently, libstatssocket in the stats APEX is causing
841		// this problem.
842		// Include the lib in platform because the module SDK that ought to provide
843		// it doesn't exist, so it would otherwise be left out completely.
844		// TODO(b/154888298) remove this by adding those libraries in module SDKS and skipping
845		// this check for libraries provided by SDKs.
846		if !availableToPlatform && !android.InAnyApex(am.Name()) {
847			availableToPlatform = true
848		}
849
850		// If any of the dep is not available to platform, this module is also considered
851		// as being not available to platform even if it has "//apex_available:platform"
852		mctx.VisitDirectDeps(func(child android.Module) {
853			if !am.DepIsInSameApex(mctx, child) {
854				// if the dependency crosses apex boundary, don't consider it
855				return
856			}
857			if dep, ok := child.(android.ApexModule); ok && dep.NotAvailableForPlatform() {
858				availableToPlatform = false
859				// TODO(b/154889534) trigger an error when 'am' has "//apex_available:platform"
860			}
861		})
862
863		// Exception 1: stub libraries and native bridge libraries are always available to platform
864		if cc, ok := mctx.Module().(*cc.Module); ok &&
865			(cc.IsStubs() || cc.Target().NativeBridge == android.NativeBridgeEnabled) {
866			availableToPlatform = true
867		}
868
869		// Exception 2: bootstrap bionic libraries are also always available to platform
870		if cc.InstallToBootstrap(mctx.ModuleName(), mctx.Config()) {
871			availableToPlatform = true
872		}
873
874		if !availableToPlatform {
875			am.SetNotAvailableForPlatform()
876		}
877	}
878}
879
880// If a module in an APEX depends on a module from an SDK then it needs an APEX
881// specific variant created for it. Refer to sdk.sdkDepsReplaceMutator.
882func inAnySdk(module android.Module) bool {
883	if sa, ok := module.(android.SdkAware); ok {
884		return sa.IsInAnySdk()
885	}
886
887	return false
888}
889
890// Create apex variations if a module is included in APEX(s).
891func apexMutator(mctx android.BottomUpMutatorContext) {
892	if !mctx.Module().Enabled() {
893		return
894	}
895	if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
896		am.CreateApexVariations(mctx)
897	} else if a, ok := mctx.Module().(*apexBundle); ok && !a.vndkApex {
898		// apex bundle itself is mutated so that it and its modules have same
899		// apex variant.
900		apexBundleName := mctx.ModuleName()
901		mctx.CreateVariations(apexBundleName)
902	} else if o, ok := mctx.Module().(*OverrideApex); ok {
903		apexBundleName := o.GetOverriddenModuleName()
904		if apexBundleName == "" {
905			mctx.ModuleErrorf("base property is not set")
906			return
907		}
908		mctx.CreateVariations(apexBundleName)
909	}
910
911}
912
913var (
914	apexFileContextsInfosKey   = android.NewOnceKey("apexFileContextsInfosKey")
915	apexFileContextsInfosMutex sync.Mutex
916)
917
918func apexFileContextsInfos(config android.Config) *[]string {
919	return config.Once(apexFileContextsInfosKey, func() interface{} {
920		return &[]string{}
921	}).(*[]string)
922}
923
924func addFlattenedFileContextsInfos(ctx android.BaseModuleContext, fileContextsInfo string) {
925	apexFileContextsInfosMutex.Lock()
926	defer apexFileContextsInfosMutex.Unlock()
927	apexFileContextsInfos := apexFileContextsInfos(ctx.Config())
928	*apexFileContextsInfos = append(*apexFileContextsInfos, fileContextsInfo)
929}
930
931func apexFlattenedMutator(mctx android.BottomUpMutatorContext) {
932	if !mctx.Module().Enabled() {
933		return
934	}
935	if ab, ok := mctx.Module().(*apexBundle); ok {
936		var variants []string
937		switch proptools.StringDefault(ab.properties.Payload_type, "image") {
938		case "image":
939			variants = append(variants, imageApexType, flattenedApexType)
940		case "zip":
941			variants = append(variants, zipApexType)
942		case "both":
943			variants = append(variants, imageApexType, zipApexType, flattenedApexType)
944		default:
945			mctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *ab.properties.Payload_type)
946			return
947		}
948
949		modules := mctx.CreateLocalVariations(variants...)
950
951		for i, v := range variants {
952			switch v {
953			case imageApexType:
954				modules[i].(*apexBundle).properties.ApexType = imageApex
955			case zipApexType:
956				modules[i].(*apexBundle).properties.ApexType = zipApex
957			case flattenedApexType:
958				modules[i].(*apexBundle).properties.ApexType = flattenedApex
959				if !mctx.Config().FlattenApex() && ab.Platform() {
960					modules[i].(*apexBundle).MakeAsSystemExt()
961				}
962			}
963		}
964	} else if _, ok := mctx.Module().(*OverrideApex); ok {
965		mctx.CreateVariations(imageApexType, flattenedApexType)
966	}
967}
968
969func apexUsesMutator(mctx android.BottomUpMutatorContext) {
970	if ab, ok := mctx.Module().(*apexBundle); ok {
971		mctx.AddFarVariationDependencies(nil, usesTag, ab.properties.Uses...)
972	}
973}
974
975var (
976	useVendorAllowListKey = android.NewOnceKey("useVendorAllowList")
977)
978
979// useVendorAllowList returns the list of APEXes which are allowed to use_vendor.
980// When use_vendor is used, native modules are built with __ANDROID_VNDK__ and __ANDROID_APEX__,
981// which may cause compatibility issues. (e.g. libbinder)
982// Even though libbinder restricts its availability via 'apex_available' property and relies on
983// yet another macro __ANDROID_APEX_<NAME>__, we restrict usage of "use_vendor:" from other APEX modules
984// to avoid similar problems.
985func useVendorAllowList(config android.Config) []string {
986	return config.Once(useVendorAllowListKey, func() interface{} {
987		return []string{
988			// swcodec uses "vendor" variants for smaller size
989			"com.android.media.swcodec",
990			"test_com.android.media.swcodec",
991		}
992	}).([]string)
993}
994
995// setUseVendorAllowListForTest overrides useVendorAllowList and must be
996// called before the first call to useVendorAllowList()
997func setUseVendorAllowListForTest(config android.Config, allowList []string) {
998	config.Once(useVendorAllowListKey, func() interface{} {
999		return allowList
1000	})
1001}
1002
1003type apexNativeDependencies struct {
1004	// List of native libraries
1005	Native_shared_libs []string
1006
1007	// List of native executables
1008	Binaries []string
1009
1010	// List of native tests
1011	Tests []string
1012}
1013
1014type apexMultilibProperties struct {
1015	// Native dependencies whose compile_multilib is "first"
1016	First apexNativeDependencies
1017
1018	// Native dependencies whose compile_multilib is "both"
1019	Both apexNativeDependencies
1020
1021	// Native dependencies whose compile_multilib is "prefer32"
1022	Prefer32 apexNativeDependencies
1023
1024	// Native dependencies whose compile_multilib is "32"
1025	Lib32 apexNativeDependencies
1026
1027	// Native dependencies whose compile_multilib is "64"
1028	Lib64 apexNativeDependencies
1029}
1030
1031type apexBundleProperties struct {
1032	// Json manifest file describing meta info of this APEX bundle. Default:
1033	// "apex_manifest.json"
1034	Manifest *string `android:"path"`
1035
1036	// AndroidManifest.xml file used for the zip container of this APEX bundle.
1037	// If unspecified, a default one is automatically generated.
1038	AndroidManifest *string `android:"path"`
1039
1040	// Canonical name of the APEX bundle. Used to determine the path to the activated APEX on
1041	// device (/apex/<apex_name>).
1042	// If unspecified, defaults to the value of name.
1043	Apex_name *string
1044
1045	// Determines the file contexts file for setting security context to each file in this APEX bundle.
1046	// For platform APEXes, this should points to a file under /system/sepolicy
1047	// Default: /system/sepolicy/apex/<module_name>_file_contexts.
1048	File_contexts *string `android:"path"`
1049
1050	// List of native shared libs that are embedded inside this APEX bundle
1051	Native_shared_libs []string
1052
1053	// List of executables that are embedded inside this APEX bundle
1054	Binaries []string
1055
1056	// List of java libraries that are embedded inside this APEX bundle
1057	Java_libs []string
1058
1059	// List of prebuilt files that are embedded inside this APEX bundle
1060	Prebuilts []string
1061
1062	// List of tests that are embedded inside this APEX bundle
1063	Tests []string
1064
1065	// Name of the apex_key module that provides the private key to sign APEX
1066	Key *string
1067
1068	// The type of APEX to build. Controls what the APEX payload is. Either
1069	// 'image', 'zip' or 'both'. Default: 'image'.
1070	Payload_type *string
1071
1072	// The name of a certificate in the default certificate directory, blank to use the default product certificate,
1073	// or an android_app_certificate module name in the form ":module".
1074	Certificate *string
1075
1076	// Whether this APEX is installable to one of the partitions. Default: true.
1077	Installable *bool
1078
1079	// For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
1080	// Default is false.
1081	Use_vendor *bool
1082
1083	// For telling the apex to ignore special handling for system libraries such as bionic. Default is false.
1084	Ignore_system_library_special_case *bool
1085
1086	Multilib apexMultilibProperties
1087
1088	// List of sanitizer names that this APEX is enabled for
1089	SanitizerNames []string `blueprint:"mutated"`
1090
1091	PreventInstall bool `blueprint:"mutated"`
1092
1093	HideFromMake bool `blueprint:"mutated"`
1094
1095	// Indicates this APEX provides C++ shared libaries to other APEXes. Default: false.
1096	Provide_cpp_shared_libs *bool
1097
1098	// List of providing APEXes' names so that this APEX can depend on provided shared libraries.
1099	Uses []string
1100
1101	// package format of this apex variant; could be non-flattened, flattened, or zip.
1102	// imageApex, zipApex or flattened
1103	ApexType apexPackaging `blueprint:"mutated"`
1104
1105	// List of SDKs that are used to build this APEX. A reference to an SDK should be either
1106	// `name#version` or `name` which is an alias for `name#current`. If left empty, `platform#current`
1107	// is implied. This value affects all modules included in this APEX. In other words, they are
1108	// also built with the SDKs specified here.
1109	Uses_sdks []string
1110
1111	// Whenever apex_payload.img of the APEX should include dm-verity hashtree.
1112	// Should be only used in tests#.
1113	Test_only_no_hashtree *bool
1114
1115	// Whenever apex_payload.img of the APEX should not be dm-verity signed.
1116	// Should be only used in tests#.
1117	Test_only_unsigned_payload *bool
1118
1119	IsCoverageVariant bool `blueprint:"mutated"`
1120
1121	// Whether this APEX is considered updatable or not. When set to true, this will enforce additional
1122	// rules for making sure that the APEX is truly updatable.
1123	// - To be updatable, min_sdk_version should be set as well
1124	// This will also disable the size optimizations like symlinking to the system libs.
1125	// Default is false.
1126	Updatable *bool
1127
1128	// The minimum SDK version that this apex must be compatible with.
1129	Min_sdk_version *string
1130}
1131
1132type apexTargetBundleProperties struct {
1133	Target struct {
1134		// Multilib properties only for android.
1135		Android struct {
1136			Multilib apexMultilibProperties
1137		}
1138
1139		// Multilib properties only for host.
1140		Host struct {
1141			Multilib apexMultilibProperties
1142		}
1143
1144		// Multilib properties only for host linux_bionic.
1145		Linux_bionic struct {
1146			Multilib apexMultilibProperties
1147		}
1148
1149		// Multilib properties only for host linux_glibc.
1150		Linux_glibc struct {
1151			Multilib apexMultilibProperties
1152		}
1153	}
1154}
1155
1156type overridableProperties struct {
1157	// List of APKs to package inside APEX
1158	Apps []string
1159
1160	// Names of modules to be overridden. Listed modules can only be other binaries
1161	// (in Make or Soong).
1162	// This does not completely prevent installation of the overridden binaries, but if both
1163	// binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1164	// from PRODUCT_PACKAGES.
1165	Overrides []string
1166
1167	// Logging Parent value
1168	Logging_parent string
1169
1170	// Apex Container Package Name.
1171	// Override value for attribute package:name in AndroidManifest.xml
1172	Package_name string
1173
1174	// A txt file containing list of files that are allowed to be included in this APEX.
1175	Allowed_files *string `android:"path"`
1176}
1177
1178type apexPackaging int
1179
1180const (
1181	imageApex apexPackaging = iota
1182	zipApex
1183	flattenedApex
1184)
1185
1186// The suffix for the output "file", not the module
1187func (a apexPackaging) suffix() string {
1188	switch a {
1189	case imageApex:
1190		return imageApexSuffix
1191	case zipApex:
1192		return zipApexSuffix
1193	default:
1194		panic(fmt.Errorf("unknown APEX type %d", a))
1195	}
1196}
1197
1198func (a apexPackaging) name() string {
1199	switch a {
1200	case imageApex:
1201		return imageApexType
1202	case zipApex:
1203		return zipApexType
1204	default:
1205		panic(fmt.Errorf("unknown APEX type %d", a))
1206	}
1207}
1208
1209type apexFileClass int
1210
1211const (
1212	etc apexFileClass = iota
1213	nativeSharedLib
1214	nativeExecutable
1215	shBinary
1216	pyBinary
1217	goBinary
1218	javaSharedLib
1219	nativeTest
1220	app
1221	appSet
1222)
1223
1224func (class apexFileClass) NameInMake() string {
1225	switch class {
1226	case etc:
1227		return "ETC"
1228	case nativeSharedLib:
1229		return "SHARED_LIBRARIES"
1230	case nativeExecutable, shBinary, pyBinary, goBinary:
1231		return "EXECUTABLES"
1232	case javaSharedLib:
1233		return "JAVA_LIBRARIES"
1234	case nativeTest:
1235		return "NATIVE_TESTS"
1236	case app, appSet:
1237		// b/142537672 Why isn't this APP? We want to have full control over
1238		// the paths and file names of the apk file under the flattend APEX.
1239		// If this is set to APP, then the paths and file names are modified
1240		// by the Make build system. For example, it is installed to
1241		// /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of
1242		// /system/apex/<apexname>/app/<Appname> because the build system automatically
1243		// appends module name (which is <apexname>.<Appname> to the path.
1244		return "ETC"
1245	default:
1246		panic(fmt.Errorf("unknown class %d", class))
1247	}
1248}
1249
1250// apexFile represents a file in an APEX bundle
1251type apexFile struct {
1252	builtFile  android.Path
1253	stem       string
1254	moduleName string
1255	installDir string
1256	class      apexFileClass
1257	module     android.Module
1258	// list of symlinks that will be created in installDir that point to this apexFile
1259	symlinks      []string
1260	transitiveDep bool
1261	moduleDir     string
1262
1263	requiredModuleNames       []string
1264	targetRequiredModuleNames []string
1265	hostRequiredModuleNames   []string
1266
1267	jacocoReportClassesFile android.Path     // only for javalibs and apps
1268	lintDepSets             java.LintDepSets // only for javalibs and apps
1269	certificate             java.Certificate // only for apps
1270	overriddenPackageName   string           // only for apps
1271}
1272
1273func newApexFile(ctx android.BaseModuleContext, builtFile android.Path, moduleName string, installDir string, class apexFileClass, module android.Module) apexFile {
1274	ret := apexFile{
1275		builtFile:  builtFile,
1276		moduleName: moduleName,
1277		installDir: installDir,
1278		class:      class,
1279		module:     module,
1280	}
1281	if module != nil {
1282		ret.moduleDir = ctx.OtherModuleDir(module)
1283		ret.requiredModuleNames = module.RequiredModuleNames()
1284		ret.targetRequiredModuleNames = module.TargetRequiredModuleNames()
1285		ret.hostRequiredModuleNames = module.HostRequiredModuleNames()
1286	}
1287	return ret
1288}
1289
1290func (af *apexFile) Ok() bool {
1291	return af.builtFile != nil && af.builtFile.String() != ""
1292}
1293
1294func (af *apexFile) apexRelativePath(path string) string {
1295	return filepath.Join(af.installDir, path)
1296}
1297
1298// Path() returns path of this apex file relative to the APEX root
1299func (af *apexFile) Path() string {
1300	return af.apexRelativePath(af.Stem())
1301}
1302
1303func (af *apexFile) Stem() string {
1304	if af.stem != "" {
1305		return af.stem
1306	}
1307	return af.builtFile.Base()
1308}
1309
1310// SymlinkPaths() returns paths of the symlinks (if any) relative to the APEX root
1311func (af *apexFile) SymlinkPaths() []string {
1312	var ret []string
1313	for _, symlink := range af.symlinks {
1314		ret = append(ret, filepath.Join(af.installDir, symlink))
1315	}
1316	return ret
1317}
1318
1319func (af *apexFile) AvailableToPlatform() bool {
1320	if af.module == nil {
1321		return false
1322	}
1323	if am, ok := af.module.(android.ApexModule); ok {
1324		return am.AvailableFor(android.AvailableToPlatform)
1325	}
1326	return false
1327}
1328
1329type apexBundle struct {
1330	android.ModuleBase
1331	android.DefaultableModuleBase
1332	android.OverridableModuleBase
1333	android.SdkBase
1334
1335	properties            apexBundleProperties
1336	targetProperties      apexTargetBundleProperties
1337	overridableProperties overridableProperties
1338
1339	// specific to apex_vndk modules
1340	vndkProperties apexVndkProperties
1341
1342	bundleModuleFile android.WritablePath
1343	outputFile       android.WritablePath
1344	installDir       android.InstallPath
1345
1346	prebuiltFileToDelete string
1347
1348	public_key_file  android.Path
1349	private_key_file android.Path
1350
1351	container_certificate_file android.Path
1352	container_private_key_file android.Path
1353
1354	fileContexts android.Path
1355
1356	// list of files to be included in this apex
1357	filesInfo []apexFile
1358
1359	// list of module names that should be installed along with this APEX
1360	requiredDeps []string
1361
1362	// list of module names that this APEX is including (to be shown via *-deps-info target)
1363	android.ApexBundleDepsInfo
1364
1365	testApex        bool
1366	vndkApex        bool
1367	artApex         bool
1368	primaryApexType bool
1369
1370	manifestJsonOut android.WritablePath
1371	manifestPbOut   android.WritablePath
1372
1373	// list of commands to create symlinks for backward compatibility.
1374	// these commands will be attached as LOCAL_POST_INSTALL_CMD to
1375	// apex package itself(for unflattened build) or apex_manifest(for flattened build)
1376	// so that compat symlinks are always installed regardless of TARGET_FLATTEN_APEX setting.
1377	compatSymlinks []string
1378
1379	// Suffix of module name in Android.mk
1380	// ".flattened", ".apex", ".zipapex", or ""
1381	suffix string
1382
1383	installedFilesFile android.WritablePath
1384
1385	// Whether to create symlink to the system file instead of having a file
1386	// inside the apex or not
1387	linkToSystemLib bool
1388
1389	// Struct holding the merged notice file paths in different formats
1390	mergedNotices android.NoticeOutputs
1391
1392	// Optional list of lint report zip files for apexes that contain java or app modules
1393	lintReports android.Paths
1394}
1395
1396func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
1397	native_shared_libs []string, binaries []string, tests []string,
1398	target android.Target, imageVariation string) {
1399	// Use *FarVariation* to be able to depend on modules having
1400	// conflicting variations with this module. This is required since
1401	// arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
1402	// for native shared libs.
1403	ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
1404		{Mutator: "image", Variation: imageVariation},
1405		{Mutator: "link", Variation: "shared"},
1406		{Mutator: "version", Variation: ""}, // "" is the non-stub variant
1407	}...), sharedLibTag, native_shared_libs...)
1408
1409	ctx.AddFarVariationDependencies(append(target.Variations(),
1410		blueprint.Variation{Mutator: "image", Variation: imageVariation}),
1411		executableTag, binaries...)
1412
1413	ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
1414		{Mutator: "image", Variation: imageVariation},
1415		{Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
1416	}...), testTag, tests...)
1417}
1418
1419func (a *apexBundle) combineProperties(ctx android.BottomUpMutatorContext) {
1420	if ctx.Os().Class == android.Device {
1421		proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Android.Multilib, nil)
1422	} else {
1423		proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Host.Multilib, nil)
1424		if ctx.Os().Bionic() {
1425			proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_bionic.Multilib, nil)
1426		} else {
1427			proptools.AppendProperties(&a.properties.Multilib, &a.targetProperties.Target.Linux_glibc.Multilib, nil)
1428		}
1429	}
1430}
1431
1432func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
1433	if proptools.Bool(a.properties.Use_vendor) && !android.InList(a.Name(), useVendorAllowList(ctx.Config())) {
1434		ctx.PropertyErrorf("use_vendor", "not allowed to set use_vendor: true")
1435	}
1436
1437	targets := ctx.MultiTargets()
1438	config := ctx.DeviceConfig()
1439
1440	a.combineProperties(ctx)
1441
1442	has32BitTarget := false
1443	for _, target := range targets {
1444		if target.Arch.ArchType.Multilib == "lib32" {
1445			has32BitTarget = true
1446		}
1447	}
1448	for i, target := range targets {
1449		// When multilib.* is omitted for native_shared_libs, it implies
1450		// multilib.both.
1451		ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
1452			{Mutator: "image", Variation: a.getImageVariation(config)},
1453			{Mutator: "link", Variation: "shared"},
1454		}...), sharedLibTag, a.properties.Native_shared_libs...)
1455
1456		// When multilib.* is omitted for tests, it implies
1457		// multilib.both.
1458		ctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
1459			{Mutator: "image", Variation: a.getImageVariation(config)},
1460			{Mutator: "test_per_src", Variation: ""}, // "" is the all-tests variant
1461		}...), testTag, a.properties.Tests...)
1462
1463		// Add native modules targetting both ABIs
1464		addDependenciesForNativeModules(ctx,
1465			a.properties.Multilib.Both.Native_shared_libs,
1466			a.properties.Multilib.Both.Binaries,
1467			a.properties.Multilib.Both.Tests,
1468			target,
1469			a.getImageVariation(config))
1470
1471		isPrimaryAbi := i == 0
1472		if isPrimaryAbi {
1473			// When multilib.* is omitted for binaries, it implies
1474			// multilib.first.
1475			ctx.AddFarVariationDependencies(append(target.Variations(),
1476				blueprint.Variation{Mutator: "image", Variation: a.getImageVariation(config)}),
1477				executableTag, a.properties.Binaries...)
1478
1479			// Add native modules targetting the first ABI
1480			addDependenciesForNativeModules(ctx,
1481				a.properties.Multilib.First.Native_shared_libs,
1482				a.properties.Multilib.First.Binaries,
1483				a.properties.Multilib.First.Tests,
1484				target,
1485				a.getImageVariation(config))
1486		}
1487
1488		switch target.Arch.ArchType.Multilib {
1489		case "lib32":
1490			// Add native modules targetting 32-bit ABI
1491			addDependenciesForNativeModules(ctx,
1492				a.properties.Multilib.Lib32.Native_shared_libs,
1493				a.properties.Multilib.Lib32.Binaries,
1494				a.properties.Multilib.Lib32.Tests,
1495				target,
1496				a.getImageVariation(config))
1497
1498			addDependenciesForNativeModules(ctx,
1499				a.properties.Multilib.Prefer32.Native_shared_libs,
1500				a.properties.Multilib.Prefer32.Binaries,
1501				a.properties.Multilib.Prefer32.Tests,
1502				target,
1503				a.getImageVariation(config))
1504		case "lib64":
1505			// Add native modules targetting 64-bit ABI
1506			addDependenciesForNativeModules(ctx,
1507				a.properties.Multilib.Lib64.Native_shared_libs,
1508				a.properties.Multilib.Lib64.Binaries,
1509				a.properties.Multilib.Lib64.Tests,
1510				target,
1511				a.getImageVariation(config))
1512
1513			if !has32BitTarget {
1514				addDependenciesForNativeModules(ctx,
1515					a.properties.Multilib.Prefer32.Native_shared_libs,
1516					a.properties.Multilib.Prefer32.Binaries,
1517					a.properties.Multilib.Prefer32.Tests,
1518					target,
1519					a.getImageVariation(config))
1520			}
1521
1522			if strings.HasPrefix(ctx.ModuleName(), "com.android.runtime") && target.Os.Class == android.Device {
1523				for _, sanitizer := range ctx.Config().SanitizeDevice() {
1524					if sanitizer == "hwaddress" {
1525						addDependenciesForNativeModules(ctx,
1526							[]string{"libclang_rt.hwasan-aarch64-android"},
1527							nil, nil, target, a.getImageVariation(config))
1528						break
1529					}
1530				}
1531			}
1532		}
1533
1534	}
1535
1536	// For prebuilt_etc, use the first variant (64 on 64/32bit device,
1537	// 32 on 32bit device) regardless of the TARGET_PREFER_* setting.
1538	// b/144532908
1539	archForPrebuiltEtc := config.Arches()[0]
1540	for _, arch := range config.Arches() {
1541		// Prefer 64-bit arch if there is any
1542		if arch.ArchType.Multilib == "lib64" {
1543			archForPrebuiltEtc = arch
1544			break
1545		}
1546	}
1547	ctx.AddFarVariationDependencies([]blueprint.Variation{
1548		{Mutator: "os", Variation: ctx.Os().String()},
1549		{Mutator: "arch", Variation: archForPrebuiltEtc.String()},
1550	}, prebuiltTag, a.properties.Prebuilts...)
1551
1552	ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
1553		javaLibTag, a.properties.Java_libs...)
1554
1555	// With EMMA_INSTRUMENT_FRAMEWORK=true the ART boot image includes jacoco library.
1556	if a.artApex && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
1557		ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
1558			javaLibTag, "jacocoagent")
1559	}
1560
1561	if String(a.properties.Key) == "" {
1562		ctx.ModuleErrorf("key is missing")
1563		return
1564	}
1565	ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
1566
1567	cert := android.SrcIsModule(a.getCertString(ctx))
1568	if cert != "" {
1569		ctx.AddDependency(ctx.Module(), certificateTag, cert)
1570	}
1571
1572	// TODO(jiyong): ensure that all apexes are with non-empty uses_sdks
1573	if len(a.properties.Uses_sdks) > 0 {
1574		sdkRefs := []android.SdkRef{}
1575		for _, str := range a.properties.Uses_sdks {
1576			parsed := android.ParseSdkRef(ctx, str, "uses_sdks")
1577			sdkRefs = append(sdkRefs, parsed)
1578		}
1579		a.BuildWithSdks(sdkRefs)
1580	}
1581}
1582
1583func (a *apexBundle) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
1584	if a.overridableProperties.Allowed_files != nil {
1585		android.ExtractSourceDeps(ctx, a.overridableProperties.Allowed_files)
1586	}
1587	ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
1588		androidAppTag, a.overridableProperties.Apps...)
1589}
1590
1591func (a *apexBundle) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1592	// direct deps of an APEX bundle are all part of the APEX bundle
1593	return true
1594}
1595
1596func (a *apexBundle) getCertString(ctx android.BaseModuleContext) string {
1597	moduleName := ctx.ModuleName()
1598	// VNDK APEXes share the same certificate. To avoid adding a new VNDK version to the OVERRIDE_* list,
1599	// we check with the pseudo module name to see if its certificate is overridden.
1600	if a.vndkApex {
1601		moduleName = vndkApexName
1602	}
1603	certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(moduleName)
1604	if overridden {
1605		return ":" + certificate
1606	}
1607	return String(a.properties.Certificate)
1608}
1609
1610func (a *apexBundle) OutputFiles(tag string) (android.Paths, error) {
1611	switch tag {
1612	case "":
1613		return android.Paths{a.outputFile}, nil
1614	default:
1615		return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1616	}
1617}
1618
1619func (a *apexBundle) installable() bool {
1620	return !a.properties.PreventInstall && (a.properties.Installable == nil || proptools.Bool(a.properties.Installable))
1621}
1622
1623func (a *apexBundle) testOnlyShouldSkipHashtreeGeneration() bool {
1624	return proptools.Bool(a.properties.Test_only_no_hashtree)
1625}
1626
1627func (a *apexBundle) testOnlyShouldSkipPayloadSign() bool {
1628	return proptools.Bool(a.properties.Test_only_unsigned_payload)
1629}
1630
1631func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
1632	if a.vndkApex {
1633		return cc.VendorVariationPrefix + a.vndkVersion(config)
1634	}
1635	if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
1636		return cc.VendorVariationPrefix + config.PlatformVndkVersion()
1637	} else {
1638		return android.CoreVariation
1639	}
1640}
1641
1642func (a *apexBundle) EnableSanitizer(sanitizerName string) {
1643	if !android.InList(sanitizerName, a.properties.SanitizerNames) {
1644		a.properties.SanitizerNames = append(a.properties.SanitizerNames, sanitizerName)
1645	}
1646}
1647
1648func (a *apexBundle) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
1649	if android.InList(sanitizerName, a.properties.SanitizerNames) {
1650		return true
1651	}
1652
1653	// Then follow the global setting
1654	globalSanitizerNames := []string{}
1655	if a.Host() {
1656		globalSanitizerNames = ctx.Config().SanitizeHost()
1657	} else {
1658		arches := ctx.Config().SanitizeDeviceArch()
1659		if len(arches) == 0 || android.InList(a.Arch().ArchType.Name, arches) {
1660			globalSanitizerNames = ctx.Config().SanitizeDevice()
1661		}
1662	}
1663	return android.InList(sanitizerName, globalSanitizerNames)
1664}
1665
1666func (a *apexBundle) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
1667	return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1668}
1669
1670func (a *apexBundle) PreventInstall() {
1671	a.properties.PreventInstall = true
1672}
1673
1674func (a *apexBundle) HideFromMake() {
1675	a.properties.HideFromMake = true
1676}
1677
1678func (a *apexBundle) MarkAsCoverageVariant(coverage bool) {
1679	a.properties.IsCoverageVariant = coverage
1680}
1681
1682// TODO(jiyong) move apexFileFor* close to the apexFile type definition
1683func apexFileForNativeLibrary(ctx android.BaseModuleContext, ccMod *cc.Module, handleSpecialLibs bool) apexFile {
1684	// Decide the APEX-local directory by the multilib of the library
1685	// In the future, we may query this to the module.
1686	var dirInApex string
1687	switch ccMod.Arch().ArchType.Multilib {
1688	case "lib32":
1689		dirInApex = "lib"
1690	case "lib64":
1691		dirInApex = "lib64"
1692	}
1693	dirInApex = filepath.Join(dirInApex, ccMod.RelativeInstallPath())
1694	if ccMod.Target().NativeBridge == android.NativeBridgeEnabled {
1695		dirInApex = filepath.Join(dirInApex, ccMod.Target().NativeBridgeRelativePath)
1696	}
1697	if handleSpecialLibs && cc.InstallToBootstrap(ccMod.BaseModuleName(), ctx.Config()) {
1698		// Special case for Bionic libs and other libs installed with them. This is
1699		// to prevent those libs from being included in the search path
1700		// /apex/com.android.runtime/${LIB}. This exclusion is required because
1701		// those libs in the Runtime APEX are available via the legacy paths in
1702		// /system/lib/. By the init process, the libs in the APEX are bind-mounted
1703		// to the legacy paths and thus will be loaded into the default linker
1704		// namespace (aka "platform" namespace). If the libs are directly in
1705		// /apex/com.android.runtime/${LIB} then the same libs will be loaded again
1706		// into the runtime linker namespace, which will result in double loading of
1707		// them, which isn't supported.
1708		dirInApex = filepath.Join(dirInApex, "bionic")
1709	}
1710
1711	fileToCopy := ccMod.OutputFile().Path()
1712	return newApexFile(ctx, fileToCopy, ccMod.Name(), dirInApex, nativeSharedLib, ccMod)
1713}
1714
1715func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFile {
1716	dirInApex := filepath.Join("bin", cc.RelativeInstallPath())
1717	if cc.Target().NativeBridge == android.NativeBridgeEnabled {
1718		dirInApex = filepath.Join(dirInApex, cc.Target().NativeBridgeRelativePath)
1719	}
1720	fileToCopy := cc.OutputFile().Path()
1721	af := newApexFile(ctx, fileToCopy, cc.Name(), dirInApex, nativeExecutable, cc)
1722	af.symlinks = cc.Symlinks()
1723	return af
1724}
1725
1726func apexFileForPyBinary(ctx android.BaseModuleContext, py *python.Module) apexFile {
1727	dirInApex := "bin"
1728	fileToCopy := py.HostToolPath().Path()
1729	return newApexFile(ctx, fileToCopy, py.Name(), dirInApex, pyBinary, py)
1730}
1731func apexFileForGoBinary(ctx android.BaseModuleContext, depName string, gb bootstrap.GoBinaryTool) apexFile {
1732	dirInApex := "bin"
1733	s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
1734	if err != nil {
1735		ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
1736		return apexFile{}
1737	}
1738	fileToCopy := android.PathForOutput(ctx, s)
1739	// NB: Since go binaries are static we don't need the module for anything here, which is
1740	// good since the go tool is a blueprint.Module not an android.Module like we would
1741	// normally use.
1742	return newApexFile(ctx, fileToCopy, depName, dirInApex, goBinary, nil)
1743}
1744
1745func apexFileForShBinary(ctx android.BaseModuleContext, sh *sh.ShBinary) apexFile {
1746	dirInApex := filepath.Join("bin", sh.SubDir())
1747	fileToCopy := sh.OutputFile()
1748	af := newApexFile(ctx, fileToCopy, sh.Name(), dirInApex, shBinary, sh)
1749	af.symlinks = sh.Symlinks()
1750	return af
1751}
1752
1753type javaDependency interface {
1754	DexJar() android.Path
1755	JacocoReportClassesFile() android.Path
1756	LintDepSets() java.LintDepSets
1757
1758	Stem() string
1759}
1760
1761var _ javaDependency = (*java.Library)(nil)
1762var _ javaDependency = (*java.SdkLibrary)(nil)
1763var _ javaDependency = (*java.DexImport)(nil)
1764var _ javaDependency = (*java.SdkLibraryImport)(nil)
1765
1766func apexFileForJavaLibrary(ctx android.BaseModuleContext, lib javaDependency, module android.Module) apexFile {
1767	dirInApex := "javalib"
1768	fileToCopy := lib.DexJar()
1769	// Remove prebuilt_ if necessary so the source and prebuilt modules have the same name.
1770	name := strings.TrimPrefix(module.Name(), "prebuilt_")
1771	af := newApexFile(ctx, fileToCopy, name, dirInApex, javaSharedLib, module)
1772	af.jacocoReportClassesFile = lib.JacocoReportClassesFile()
1773	af.lintDepSets = lib.LintDepSets()
1774	af.stem = lib.Stem() + ".jar"
1775	return af
1776}
1777
1778func apexFileForPrebuiltEtc(ctx android.BaseModuleContext, prebuilt prebuilt_etc.PrebuiltEtcModule, depName string) apexFile {
1779	dirInApex := filepath.Join("etc", prebuilt.SubDir())
1780	fileToCopy := prebuilt.OutputFile()
1781	return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, prebuilt)
1782}
1783
1784func apexFileForCompatConfig(ctx android.BaseModuleContext, config java.PlatformCompatConfigIntf, depName string) apexFile {
1785	dirInApex := filepath.Join("etc", config.SubDir())
1786	fileToCopy := config.CompatConfig()
1787	return newApexFile(ctx, fileToCopy, depName, dirInApex, etc, config)
1788}
1789
1790func apexFileForAndroidApp(ctx android.BaseModuleContext, aapp interface {
1791	android.Module
1792	Privileged() bool
1793	InstallApkName() string
1794	OutputFile() android.Path
1795	JacocoReportClassesFile() android.Path
1796	Certificate() java.Certificate
1797}) apexFile {
1798	appDir := "app"
1799	if aapp.Privileged() {
1800		appDir = "priv-app"
1801	}
1802	dirInApex := filepath.Join(appDir, aapp.InstallApkName())
1803	fileToCopy := aapp.OutputFile()
1804	af := newApexFile(ctx, fileToCopy, aapp.Name(), dirInApex, app, aapp)
1805	af.jacocoReportClassesFile = aapp.JacocoReportClassesFile()
1806	af.certificate = aapp.Certificate()
1807
1808	if app, ok := aapp.(interface {
1809		OverriddenManifestPackageName() string
1810	}); ok {
1811		af.overriddenPackageName = app.OverriddenManifestPackageName()
1812	}
1813	return af
1814}
1815
1816// Context "decorator", overriding the InstallBypassMake method to always reply `true`.
1817type flattenedApexContext struct {
1818	android.ModuleContext
1819}
1820
1821func (c *flattenedApexContext) InstallBypassMake() bool {
1822	return true
1823}
1824
1825// Function called while walking an APEX's payload dependencies.
1826//
1827// Return true if the `to` module should be visited, false otherwise.
1828type payloadDepsCallback func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool
1829
1830// Visit dependencies that contributes to the payload of this APEX
1831func (a *apexBundle) walkPayloadDeps(ctx android.ModuleContext, do payloadDepsCallback) {
1832	ctx.WalkDeps(func(child, parent android.Module) bool {
1833		am, ok := child.(android.ApexModule)
1834		if !ok || !am.CanHaveApexVariants() {
1835			return false
1836		}
1837
1838		// Check for the direct dependencies that contribute to the payload
1839		if dt, ok := ctx.OtherModuleDependencyTag(child).(dependencyTag); ok {
1840			if dt.payload {
1841				return do(ctx, parent, am, false /* externalDep */)
1842			}
1843			// As soon as the dependency graph crosses the APEX boundary, don't go further.
1844			return false
1845		}
1846
1847		// Check for the indirect dependencies if it is considered as part of the APEX
1848		if am.ApexName() != "" {
1849			return do(ctx, parent, am, false /* externalDep */)
1850		}
1851
1852		return do(ctx, parent, am, true /* externalDep */)
1853	})
1854}
1855
1856func (a *apexBundle) minSdkVersion(ctx android.BaseModuleContext) int {
1857	ver := proptools.StringDefault(a.properties.Min_sdk_version, "current")
1858	intVer, err := android.ApiStrToNum(ctx, ver)
1859	if err != nil {
1860		ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
1861	}
1862	return intVer
1863}
1864
1865// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
1866// a dependency tag.
1867var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:blueprint.BaseDependencyTag{}\E(, )?`)
1868
1869func PrettyPrintTag(tag blueprint.DependencyTag) string {
1870	// Use tag's custom String() method if available.
1871	if stringer, ok := tag.(fmt.Stringer); ok {
1872		return stringer.String()
1873	}
1874
1875	// Otherwise, get a default string representation of the tag's struct.
1876	tagString := fmt.Sprintf("%#v", tag)
1877
1878	// Remove the boilerplate from BaseDependencyTag as it adds no value.
1879	tagString = tagCleaner.ReplaceAllString(tagString, "")
1880	return tagString
1881}
1882
1883func (a *apexBundle) Updatable() bool {
1884	return proptools.Bool(a.properties.Updatable)
1885}
1886
1887var _ android.ApexBundleDepsInfoIntf = (*apexBundle)(nil)
1888
1889// Ensures that the dependencies are marked as available for this APEX
1890func (a *apexBundle) checkApexAvailability(ctx android.ModuleContext) {
1891	// Let's be practical. Availability for test, host, and the VNDK apex isn't important
1892	if ctx.Host() || a.testApex || a.vndkApex {
1893		return
1894	}
1895
1896	// Coverage build adds additional dependencies for the coverage-only runtime libraries.
1897	// Requiring them and their transitive depencies with apex_available is not right
1898	// because they just add noise.
1899	if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || a.IsNativeCoverageNeeded(ctx) {
1900		return
1901	}
1902
1903	a.walkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
1904		if externalDep {
1905			// As soon as the dependency graph crosses the APEX boundary, don't go further.
1906			return false
1907		}
1908
1909		apexName := ctx.ModuleName()
1910		fromName := ctx.OtherModuleName(from)
1911		toName := ctx.OtherModuleName(to)
1912
1913		// If `to` is not actually in the same APEX as `from` then it does not need apex_available and neither
1914		// do any of its dependencies.
1915		if am, ok := from.(android.DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
1916			// As soon as the dependency graph crosses the APEX boundary, don't go further.
1917			return false
1918		}
1919
1920		if to.AvailableFor(apexName) || baselineApexAvailable(apexName, toName) {
1921			return true
1922		}
1923		message := ""
1924		tagPath := ctx.GetTagPath()
1925		// Skip the first module as that will be added at the start of the error message by ctx.ModuleErrorf().
1926		walkPath := ctx.GetWalkPath()[1:]
1927		for i, m := range walkPath {
1928			message = fmt.Sprintf("%s\n           via tag %s\n    -> %s", message, PrettyPrintTag(tagPath[i]), m.String())
1929		}
1930		ctx.ModuleErrorf("%q requires %q that is not available for the APEX. Dependency path:%s", fromName, toName, message)
1931		// Visit this module's dependencies to check and report any issues with their availability.
1932		return true
1933	})
1934}
1935
1936func (a *apexBundle) checkUpdatable(ctx android.ModuleContext) {
1937	if a.Updatable() {
1938		if String(a.properties.Min_sdk_version) == "" {
1939			ctx.PropertyErrorf("updatable", "updatable APEXes should set min_sdk_version as well")
1940		}
1941
1942		a.checkJavaStableSdkVersion(ctx)
1943	}
1944}
1945
1946func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1947	buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
1948	switch a.properties.ApexType {
1949	case imageApex:
1950		if buildFlattenedAsDefault {
1951			a.suffix = imageApexSuffix
1952		} else {
1953			a.suffix = ""
1954			a.primaryApexType = true
1955
1956			if ctx.Config().InstallExtraFlattenedApexes() {
1957				a.requiredDeps = append(a.requiredDeps, a.Name()+flattenedSuffix)
1958			}
1959		}
1960	case zipApex:
1961		if proptools.String(a.properties.Payload_type) == "zip" {
1962			a.suffix = ""
1963			a.primaryApexType = true
1964		} else {
1965			a.suffix = zipApexSuffix
1966		}
1967	case flattenedApex:
1968		if buildFlattenedAsDefault {
1969			a.suffix = ""
1970			a.primaryApexType = true
1971		} else {
1972			a.suffix = flattenedSuffix
1973		}
1974	}
1975
1976	if len(a.properties.Tests) > 0 && !a.testApex {
1977		ctx.PropertyErrorf("tests", "property not allowed in apex module type")
1978		return
1979	}
1980
1981	a.checkApexAvailability(ctx)
1982	a.checkUpdatable(ctx)
1983
1984	handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
1985
1986	// native lib dependencies
1987	var provideNativeLibs []string
1988	var requireNativeLibs []string
1989
1990	// Check if "uses" requirements are met with dependent apexBundles
1991	var providedNativeSharedLibs []string
1992	useVendor := proptools.Bool(a.properties.Use_vendor)
1993	ctx.VisitDirectDepsBlueprint(func(m blueprint.Module) {
1994		if ctx.OtherModuleDependencyTag(m) != usesTag {
1995			return
1996		}
1997		otherName := ctx.OtherModuleName(m)
1998		other, ok := m.(*apexBundle)
1999		if !ok {
2000			ctx.PropertyErrorf("uses", "%q is not a provider", otherName)
2001			return
2002		}
2003		if proptools.Bool(other.properties.Use_vendor) != useVendor {
2004			ctx.PropertyErrorf("use_vendor", "%q has different value of use_vendor", otherName)
2005			return
2006		}
2007		if !proptools.Bool(other.properties.Provide_cpp_shared_libs) {
2008			ctx.PropertyErrorf("uses", "%q does not provide native_shared_libs", otherName)
2009			return
2010		}
2011		providedNativeSharedLibs = append(providedNativeSharedLibs, other.properties.Native_shared_libs...)
2012	})
2013
2014	var filesInfo []apexFile
2015	// TODO(jiyong) do this using walkPayloadDeps
2016	ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
2017		depTag := ctx.OtherModuleDependencyTag(child)
2018		if _, ok := depTag.(android.ExcludeFromApexContentsTag); ok {
2019			return false
2020		}
2021		depName := ctx.OtherModuleName(child)
2022		if _, isDirectDep := parent.(*apexBundle); isDirectDep {
2023			switch depTag {
2024			case sharedLibTag:
2025				if c, ok := child.(*cc.Module); ok {
2026					fi := apexFileForNativeLibrary(ctx, c, handleSpecialLibs)
2027					filesInfo = append(filesInfo, fi)
2028					// bootstrap bionic libs are treated as provided by system
2029					if c.HasStubsVariants() && !cc.InstallToBootstrap(c.BaseModuleName(), ctx.Config()) {
2030						provideNativeLibs = append(provideNativeLibs, fi.Stem())
2031					}
2032					return true // track transitive dependencies
2033				} else {
2034					ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
2035				}
2036			case executableTag:
2037				if cc, ok := child.(*cc.Module); ok {
2038					filesInfo = append(filesInfo, apexFileForExecutable(ctx, cc))
2039					return true // track transitive dependencies
2040				} else if sh, ok := child.(*sh.ShBinary); ok {
2041					filesInfo = append(filesInfo, apexFileForShBinary(ctx, sh))
2042				} else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
2043					filesInfo = append(filesInfo, apexFileForPyBinary(ctx, py))
2044				} else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
2045					filesInfo = append(filesInfo, apexFileForGoBinary(ctx, depName, gb))
2046				} else {
2047					ctx.PropertyErrorf("binaries", "%q is neither cc_binary, (embedded) py_binary, (host) blueprint_go_binary, (host) bootstrap_go_binary, nor sh_binary", depName)
2048				}
2049			case javaLibTag:
2050				switch child.(type) {
2051				case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport:
2052					af := apexFileForJavaLibrary(ctx, child.(javaDependency), child.(android.Module))
2053					if !af.Ok() {
2054						ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
2055						return false
2056					}
2057					filesInfo = append(filesInfo, af)
2058					return true // track transitive dependencies
2059				default:
2060					ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child))
2061				}
2062			case androidAppTag:
2063				if ap, ok := child.(*java.AndroidApp); ok {
2064					filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap))
2065					return true // track transitive dependencies
2066				} else if ap, ok := child.(*java.AndroidAppImport); ok {
2067					filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap))
2068				} else if ap, ok := child.(*java.AndroidTestHelperApp); ok {
2069					filesInfo = append(filesInfo, apexFileForAndroidApp(ctx, ap))
2070				} else if ap, ok := child.(*java.AndroidAppSet); ok {
2071					appDir := "app"
2072					if ap.Privileged() {
2073						appDir = "priv-app"
2074					}
2075					af := newApexFile(ctx, ap.OutputFile(), ap.Name(),
2076						filepath.Join(appDir, ap.BaseModuleName()), appSet, ap)
2077					af.certificate = java.PresignedCertificate
2078					filesInfo = append(filesInfo, af)
2079				} else {
2080					ctx.PropertyErrorf("apps", "%q is not an android_app module", depName)
2081				}
2082			case prebuiltTag:
2083				if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2084					filesInfo = append(filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2085				} else if prebuilt, ok := child.(java.PlatformCompatConfigIntf); ok {
2086					filesInfo = append(filesInfo, apexFileForCompatConfig(ctx, prebuilt, depName))
2087				} else {
2088					ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc and not a platform_compat_config module", depName)
2089				}
2090			case testTag:
2091				if ccTest, ok := child.(*cc.Module); ok {
2092					if ccTest.IsTestPerSrcAllTestsVariation() {
2093						// Multiple-output test module (where `test_per_src: true`).
2094						//
2095						// `ccTest` is the "" ("all tests") variation of a `test_per_src` module.
2096						// We do not add this variation to `filesInfo`, as it has no output;
2097						// however, we do add the other variations of this module as indirect
2098						// dependencies (see below).
2099						return true
2100					} else {
2101						// Single-output test module (where `test_per_src: false`).
2102						af := apexFileForExecutable(ctx, ccTest)
2103						af.class = nativeTest
2104						filesInfo = append(filesInfo, af)
2105					}
2106				} else {
2107					ctx.PropertyErrorf("tests", "%q is not a cc module", depName)
2108				}
2109			case keyTag:
2110				if key, ok := child.(*apexKey); ok {
2111					a.private_key_file = key.private_key_file
2112					a.public_key_file = key.public_key_file
2113				} else {
2114					ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
2115				}
2116				return false
2117			case certificateTag:
2118				if dep, ok := child.(*java.AndroidAppCertificate); ok {
2119					a.container_certificate_file = dep.Certificate.Pem
2120					a.container_private_key_file = dep.Certificate.Key
2121				} else {
2122					ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", depName)
2123				}
2124			case android.PrebuiltDepTag:
2125				// If the prebuilt is force disabled, remember to delete the prebuilt file
2126				// that might have been installed in the previous builds
2127				if prebuilt, ok := child.(prebuilt); ok && prebuilt.isForceDisabled() {
2128					a.prebuiltFileToDelete = prebuilt.InstallFilename()
2129				}
2130			}
2131		} else if !a.vndkApex {
2132			// indirect dependencies
2133			if am, ok := child.(android.ApexModule); ok {
2134				// We cannot use a switch statement on `depTag` here as the checked
2135				// tags used below are private (e.g. `cc.sharedDepTag`).
2136				if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
2137					if cc, ok := child.(*cc.Module); ok {
2138						if android.InList(cc.Name(), providedNativeSharedLibs) {
2139							// If we're using a shared library which is provided from other APEX,
2140							// don't include it in this APEX
2141							return false
2142						}
2143						af := apexFileForNativeLibrary(ctx, cc, handleSpecialLibs)
2144						af.transitiveDep = true
2145						if !a.Host() && !android.DirectlyInApex(ctx.ModuleName(), ctx.OtherModuleName(cc)) && (cc.IsStubs() || cc.HasStubsVariants()) {
2146							// If the dependency is a stubs lib, don't include it in this APEX,
2147							// but make sure that the lib is installed on the device.
2148							// In case no APEX is having the lib, the lib is installed to the system
2149							// partition.
2150							//
2151							// Always include if we are a host-apex however since those won't have any
2152							// system libraries.
2153							if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.requiredDeps) {
2154								a.requiredDeps = append(a.requiredDeps, cc.Name())
2155							}
2156							requireNativeLibs = append(requireNativeLibs, af.Stem())
2157							// Don't track further
2158							return false
2159						}
2160						filesInfo = append(filesInfo, af)
2161						return true // track transitive dependencies
2162					}
2163				} else if cc.IsTestPerSrcDepTag(depTag) {
2164					if cc, ok := child.(*cc.Module); ok {
2165						af := apexFileForExecutable(ctx, cc)
2166						// Handle modules created as `test_per_src` variations of a single test module:
2167						// use the name of the generated test binary (`fileToCopy`) instead of the name
2168						// of the original test module (`depName`, shared by all `test_per_src`
2169						// variations of that module).
2170						af.moduleName = filepath.Base(af.builtFile.String())
2171						// these are not considered transitive dep
2172						af.transitiveDep = false
2173						filesInfo = append(filesInfo, af)
2174						return true // track transitive dependencies
2175					}
2176				} else if java.IsJniDepTag(depTag) {
2177					// Because APK-in-APEX embeds jni_libs transitively, we don't need to track transitive deps
2178					return false
2179				} else if java.IsXmlPermissionsFileDepTag(depTag) {
2180					if prebuilt, ok := child.(prebuilt_etc.PrebuiltEtcModule); ok {
2181						filesInfo = append(filesInfo, apexFileForPrebuiltEtc(ctx, prebuilt, depName))
2182					}
2183				} else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
2184					ctx.ModuleErrorf("unexpected tag %s for indirect dependency %q", PrettyPrintTag(depTag), depName)
2185				}
2186			}
2187		}
2188		return false
2189	})
2190
2191	// Specific to the ART apex: dexpreopt artifacts for libcore Java libraries.
2192	// Build rules are generated by the dexpreopt singleton, and here we access build artifacts
2193	// via the global boot image config.
2194	if a.artApex {
2195		for arch, files := range java.DexpreoptedArtApexJars(ctx) {
2196			dirInApex := filepath.Join("javalib", arch.String())
2197			for _, f := range files {
2198				localModule := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
2199				af := newApexFile(ctx, f, localModule, dirInApex, etc, nil)
2200				filesInfo = append(filesInfo, af)
2201			}
2202		}
2203	}
2204
2205	if a.private_key_file == nil {
2206		ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
2207		return
2208	}
2209
2210	// remove duplicates in filesInfo
2211	removeDup := func(filesInfo []apexFile) []apexFile {
2212		encountered := make(map[string]apexFile)
2213		for _, f := range filesInfo {
2214			dest := filepath.Join(f.installDir, f.builtFile.Base())
2215			if e, ok := encountered[dest]; !ok {
2216				encountered[dest] = f
2217			} else {
2218				// If a module is directly included and also transitively depended on
2219				// consider it as directly included.
2220				e.transitiveDep = e.transitiveDep && f.transitiveDep
2221				encountered[dest] = e
2222			}
2223		}
2224		var result []apexFile
2225		for _, v := range encountered {
2226			result = append(result, v)
2227		}
2228		return result
2229	}
2230	filesInfo = removeDup(filesInfo)
2231
2232	// to have consistent build rules
2233	sort.Slice(filesInfo, func(i, j int) bool {
2234		return filesInfo[i].builtFile.String() < filesInfo[j].builtFile.String()
2235	})
2236
2237	a.installDir = android.PathForModuleInstall(ctx, "apex")
2238	a.filesInfo = filesInfo
2239
2240	if a.properties.ApexType != zipApex {
2241		if a.properties.File_contexts == nil {
2242			a.fileContexts = android.PathForSource(ctx, "system/sepolicy/apex", ctx.ModuleName()+"-file_contexts")
2243		} else {
2244			a.fileContexts = android.PathForModuleSrc(ctx, *a.properties.File_contexts)
2245			if a.Platform() {
2246				if matched, err := path.Match("system/sepolicy/**/*", a.fileContexts.String()); err != nil || !matched {
2247					ctx.PropertyErrorf("file_contexts", "should be under system/sepolicy, but %q", a.fileContexts)
2248				}
2249			}
2250		}
2251		if !android.ExistentPathForSource(ctx, a.fileContexts.String()).Valid() {
2252			ctx.PropertyErrorf("file_contexts", "cannot find file_contexts file: %q", a.fileContexts)
2253			return
2254		}
2255	}
2256	// Optimization. If we are building bundled APEX, for the files that are gathered due to the
2257	// transitive dependencies, don't place them inside the APEX, but place a symlink pointing
2258	// the same library in the system partition, thus effectively sharing the same libraries
2259	// across the APEX boundary. For unbundled APEX, all the gathered files are actually placed
2260	// in the APEX.
2261	a.linkToSystemLib = !ctx.Config().UnbundledBuild() &&
2262		a.installable() &&
2263		!proptools.Bool(a.properties.Use_vendor)
2264
2265	// We don't need the optimization for updatable APEXes, as it might give false signal
2266	// to the system health when the APEXes are still bundled (b/149805758)
2267	if a.Updatable() && a.properties.ApexType == imageApex {
2268		a.linkToSystemLib = false
2269	}
2270
2271	// We also don't want the optimization for host APEXes, because it doesn't make sense.
2272	if ctx.Host() {
2273		a.linkToSystemLib = false
2274	}
2275
2276	// prepare apex_manifest.json
2277	a.buildManifest(ctx, provideNativeLibs, requireNativeLibs)
2278
2279	a.setCertificateAndPrivateKey(ctx)
2280	if a.properties.ApexType == flattenedApex {
2281		a.buildFlattenedApex(ctx)
2282	} else {
2283		a.buildUnflattenedApex(ctx)
2284	}
2285
2286	a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
2287
2288	a.buildApexDependencyInfo(ctx)
2289
2290	a.buildLintReports(ctx)
2291}
2292
2293// Enforce that Java deps of the apex are using stable SDKs to compile
2294func (a *apexBundle) checkJavaStableSdkVersion(ctx android.ModuleContext) {
2295	// Visit direct deps only. As long as we guarantee top-level deps are using
2296	// stable SDKs, java's checkLinkType guarantees correct usage for transitive deps
2297	ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
2298		tag := ctx.OtherModuleDependencyTag(module)
2299		switch tag {
2300		case javaLibTag, androidAppTag:
2301			if m, ok := module.(interface{ CheckStableSdkVersion() error }); ok {
2302				if err := m.CheckStableSdkVersion(); err != nil {
2303					ctx.ModuleErrorf("cannot depend on \"%v\": %v", ctx.OtherModuleName(module), err)
2304				}
2305			}
2306		}
2307	})
2308}
2309
2310func baselineApexAvailable(apex, moduleName string) bool {
2311	key := apex
2312	moduleName = normalizeModuleName(moduleName)
2313
2314	if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
2315		return true
2316	}
2317
2318	key = android.AvailableToAnyApex
2319	if val, ok := apexAvailBaseline[key]; ok && android.InList(moduleName, val) {
2320		return true
2321	}
2322
2323	return false
2324}
2325
2326func normalizeModuleName(moduleName string) string {
2327	// Prebuilt modules (e.g. java_import, etc.) have "prebuilt_" prefix added by the build
2328	// system. Trim the prefix for the check since they are confusing
2329	moduleName = strings.TrimPrefix(moduleName, "prebuilt_")
2330	if strings.HasPrefix(moduleName, "libclang_rt.") {
2331		// This module has many arch variants that depend on the product being built.
2332		// We don't want to list them all
2333		moduleName = "libclang_rt"
2334	}
2335	if strings.HasPrefix(moduleName, "androidx.") {
2336		// TODO(b/156996905) Set apex_available/min_sdk_version for androidx support libraries
2337		moduleName = "androidx"
2338	}
2339	return moduleName
2340}
2341
2342func newApexBundle() *apexBundle {
2343	module := &apexBundle{}
2344	module.AddProperties(&module.properties)
2345	module.AddProperties(&module.targetProperties)
2346	module.AddProperties(&module.overridableProperties)
2347	module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
2348		return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
2349	})
2350	android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
2351	android.InitDefaultableModule(module)
2352	android.InitSdkAwareModule(module)
2353	android.InitOverridableModule(module, &module.overridableProperties.Overrides)
2354	return module
2355}
2356
2357func ApexBundleFactory(testApex bool, artApex bool) android.Module {
2358	bundle := newApexBundle()
2359	bundle.testApex = testApex
2360	bundle.artApex = artApex
2361	return bundle
2362}
2363
2364// apex_test is an APEX for testing. The difference from the ordinary apex module type is that
2365// certain compatibility checks such as apex_available are not done for apex_test.
2366func testApexBundleFactory() android.Module {
2367	bundle := newApexBundle()
2368	bundle.testApex = true
2369	return bundle
2370}
2371
2372// apex packages other modules into an APEX file which is a packaging format for system-level
2373// components like binaries, shared libraries, etc.
2374func BundleFactory() android.Module {
2375	return newApexBundle()
2376}
2377
2378//
2379// Defaults
2380//
2381type Defaults struct {
2382	android.ModuleBase
2383	android.DefaultsModuleBase
2384}
2385
2386func defaultsFactory() android.Module {
2387	return DefaultsFactory()
2388}
2389
2390func DefaultsFactory(props ...interface{}) android.Module {
2391	module := &Defaults{}
2392
2393	module.AddProperties(props...)
2394	module.AddProperties(
2395		&apexBundleProperties{},
2396		&apexTargetBundleProperties{},
2397		&overridableProperties{},
2398	)
2399
2400	android.InitDefaultsModule(module)
2401	return module
2402}
2403
2404//
2405// OverrideApex
2406//
2407type OverrideApex struct {
2408	android.ModuleBase
2409	android.OverrideModuleBase
2410}
2411
2412func (o *OverrideApex) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2413	// All the overrides happen in the base module.
2414}
2415
2416// override_apex is used to create an apex module based on another apex module
2417// by overriding some of its properties.
2418func overrideApexFactory() android.Module {
2419	m := &OverrideApex{}
2420	m.AddProperties(&overridableProperties{})
2421
2422	android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
2423	android.InitOverrideModule(m)
2424	return m
2425}
2426