1# Package up modules to a zip file.
2# It preserves the install path of the modules' installed files.
3#
4# Input variables:
5#   my_modules: a list of module names
6#   my_package_name: the name of the output zip file.
7#   my_copy_pairs: a list of extra files to install (in src:dest format)
8# Optional input variables:
9#   my_modules_strict: what happens when a module from my_modules does not exist
10#     "true": error out when a module is missing
11#     "false": print a warning when a module is missing
12#     "": defaults to false currently
13# Output variables:
14#   my_package_zip: the path to the output zip file.
15#
16#
17
18my_makefile := $(lastword $(filter-out $(lastword $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
19my_staging_dir := $(call intermediates-dir-for,PACKAGING,$(my_package_name))
20my_built_modules := $(foreach p,$(my_copy_pairs),$(call word-colon,1,$(p)))
21my_copy_pairs := $(foreach p,$(my_copy_pairs),$(call word-colon,1,$(p)):$(my_staging_dir)/$(call word-colon,2,$(p)))
22my_pickup_files :=
23my_missing_error :=
24
25# Iterate over the modules and include their direct dependencies stated in the
26# LOCAL_REQUIRED_MODULES.
27my_modules_and_deps := $(my_modules)
28$(foreach m,$(my_modules),\
29  $(eval _explicitly_required := \
30    $(strip $(ALL_MODULES.$(m).EXPLICITLY_REQUIRED_FROM_TARGET)\
31    $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).EXPLICITLY_REQUIRED_FROM_TARGET)))\
32  $(eval my_modules_and_deps += $(_explicitly_required))\
33)
34
35ifneq ($(filter-out true false,$(my_modules_strict)),)
36  $(shell $(call echo-error,$(my_makefile),$(my_package_name): Invalid value for 'my_module_strict' = '$(my_modules_strict)'. Valid values: 'true', 'false', ''))
37  $(error done)
38endif
39
40my_missing_files = $(shell $(call echo-warning,$(my_makefile),$(my_package_name): Unknown installed file for module '$(1)'))
41ifeq ($(ALLOW_MISSING_DEPENDENCIES),true)
42  # Ignore unknown installed files on partial builds
43  my_missing_files =
44else ifneq ($(my_modules_strict),false)
45  my_missing_files = $(shell $(call echo-error,$(my_makefile),$(my_package_name): Unknown installed file for module '$(1)'))$(eval my_missing_error := true)
46endif
47
48# Iterate over modules' built files and installed files;
49# Calculate the dest files in the output zip file.
50
51$(foreach m,$(my_modules_and_deps),\
52  $(eval _pickup_files := $(strip $(ALL_MODULES.$(m).PICKUP_FILES)\
53    $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).PICKUP_FILES)))\
54  $(eval _built_files := $(strip $(ALL_MODULES.$(m).BUILT_INSTALLED)\
55    $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).BUILT_INSTALLED)))\
56  $(eval _module_class_folder := $($(strip MODULE_CLASS_$(word 1, $(strip $(ALL_MODULES.$(m).CLASS)\
57    $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).CLASS))))))\
58  $(if $(_pickup_files)$(_built_files),,\
59    $(call my_missing_files,$(m)))\
60  $(eval my_pickup_files += $(_pickup_files))\
61  $(foreach i, $(_built_files),\
62    $(eval bui_ins := $(subst :,$(space),$(i)))\
63    $(eval ins := $(word 2,$(bui_ins)))\
64    $(if $(filter $(TARGET_OUT_ROOT)/%,$(ins)),\
65      $(eval bui := $(word 1,$(bui_ins)))\
66      $(eval my_built_modules += $(bui))\
67      $(if $(filter $(_module_class_folder), nativetest benchmarktest),\
68        $(eval module_class_folder_stem := $(_module_class_folder)$(findstring 64, $(patsubst $(PRODUCT_OUT)/%,%,$(ins)))),\
69        $(eval module_class_folder_stem := $(_module_class_folder)))\
70      $(eval my_copy_dest := $(patsubst data/%,DATA/%,\
71                               $(patsubst testcases/%,DATA/$(module_class_folder_stem)/%,\
72                                 $(patsubst testcases/$(m)/$(TARGET_ARCH)/%,DATA/$(module_class_folder_stem)/$(m)/%,\
73                                   $(patsubst testcases/$(m)/$(TARGET_2ND_ARCH)/%,DATA/$(module_class_folder_stem)/$(m)/%,\
74                                     $(patsubst system/%,DATA/%,\
75                                       $(patsubst $(PRODUCT_OUT)/%,%,$(ins))))))))\
76      $(eval my_copy_pairs += $(bui):$(my_staging_dir)/$(my_copy_dest)))\
77  ))
78
79ifneq ($(my_missing_error),)
80  $(error done)
81endif
82
83my_package_zip := $(my_staging_dir)/$(my_package_name).zip
84$(my_package_zip): PRIVATE_COPY_PAIRS := $(my_copy_pairs)
85$(my_package_zip): PRIVATE_PICKUP_FILES := $(my_pickup_files)
86$(my_package_zip) : $(my_built_modules)
87	@echo "Package $@"
88	@rm -rf $(dir $@) && mkdir -p $(dir $@)
89	$(foreach p, $(PRIVATE_COPY_PAIRS),\
90	  $(eval pair := $(subst :,$(space),$(p)))\
91	  mkdir -p $(dir $(word 2,$(pair))) && \
92	  cp -Rf $(word 1,$(pair)) $(word 2,$(pair)) && ) true
93	$(hide) $(foreach f, $(PRIVATE_PICKUP_FILES),\
94	  cp -RfL $(f) $(dir $@) && ) true
95	$(hide) cd $(dir $@) && zip -rqX $(notdir $@) *
96
97my_makefile :=
98my_staging_dir :=
99my_built_modules :=
100my_copy_dest :=
101my_copy_pairs :=
102my_pickup_files :=
103my_missing_files :=
104my_missing_error :=
105my_modules_and_deps :=
106my_modules_strict :=
107