1 # Copyright (C) 2007 The Android Open Source Project
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 INTERNAL_CLEAN_STEPS :=
18 # Builds up a list of clean steps. Creates a unique
19 # id for each step by taking makefile path, INTERNAL_CLEAN_BUILD_VERSION
20 # and appending an increasing number of '@' characters.
22 # $(1): shell command to run
23 # $(2): indicate to not use makefile path as part of step id if not empty.
24 # $(2) should only be used in build/core/cleanspec.mk: just for compatibility.
25 define _add-clean-step
26 $(if $(strip $(INTERNAL_CLEAN_BUILD_VERSION)),, \
27 $(error INTERNAL_CLEAN_BUILD_VERSION not set))
28 $(eval _acs_makefile_prefix := $(lastword $(MAKEFILE_LIST)))
29 $(eval _acs_makefile_prefix := $(subst /,_,$(_acs_makefile_prefix)))
30 $(eval _acs_makefile_prefix := $(subst .,-,$(_acs_makefile_prefix)))
31 $(eval _acs_makefile_prefix := $(_acs_makefile_prefix)_acs)
32 $(if $($(_acs_makefile_prefix)),,\
33 $(eval $(_acs_makefile_prefix) := $(INTERNAL_CLEAN_BUILD_VERSION)))
34 $(eval $(_acs_makefile_prefix) := $($(_acs_makefile_prefix))@)
35 $(if $(strip $(2)),$(eval _acs_id := $($(_acs_makefile_prefix))),\
36 $(eval _acs_id := $(_acs_makefile_prefix)$($(_acs_makefile_prefix))))
37 $(eval INTERNAL_CLEAN_STEPS += $(_acs_id))
38 $(eval INTERNAL_CLEAN_STEP.$(_acs_id) := $(1))
40 $(eval _acs_makefile_prefix :=)
43 $(eval # for build/core/cleanspec.mk, dont use makefile path as part of step id) \
44 $(if $(filter %/cleanspec.mk,$(lastword $(MAKEFILE_LIST))),\
45 $(eval $(call _add-clean-step,$(1),true)),\
46 $(eval $(call _add-clean-step,$(1))))
49 # Defines INTERNAL_CLEAN_BUILD_VERSION and the individual clean steps.
50 # cleanspec.mk is outside of the core directory so that more people
51 # can have permission to touch it.
52 include $(BUILD_SYSTEM)/cleanspec.mk
53 INTERNAL_CLEAN_BUILD_VERSION := $(strip $(INTERNAL_CLEAN_BUILD_VERSION))
55 # If the clean_steps.mk file is missing (usually after a clean build)
56 # then we won't do anything.
57 CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)
58 CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)
60 # Read the current state from the file, if present.
61 # Will set CURRENT_CLEAN_BUILD_VERSION and CURRENT_CLEAN_STEPS.
63 clean_steps_file := $(PRODUCT_OUT)/clean_steps.mk
64 -include $(clean_steps_file)
66 ifneq ($(CURRENT_CLEAN_BUILD_VERSION),$(INTERNAL_CLEAN_BUILD_VERSION))
67 # The major clean version is out-of-date. Do a full clean, and
68 # don't even bother with the clean steps.
69 $(info *** A clean build is required because of a recent change.)
70 $(shell rm -rf $(OUT_DIR))
71 $(info *** Done with the cleaning, now starting the real build.)
73 # The major clean version is correct. Find the list of clean steps
74 # that we need to execute to get up-to-date.
76 $(filter-out $(CURRENT_CLEAN_STEPS),$(INTERNAL_CLEAN_STEPS))
77 $(foreach step,$(steps), \
78 $(info Clean step: $(INTERNAL_CLEAN_STEP.$(step))) \
79 $(shell $(INTERNAL_CLEAN_STEP.$(step))) \
83 CURRENT_CLEAN_BUILD_VERSION :=
84 CURRENT_CLEAN_STEPS :=
86 # Write the new state to the file.
89 mkdir -p $(dir $(clean_steps_file)) && \
90 echo "CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)" > \
91 $(clean_steps_file) ;\
92 echo "CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)" >> \
97 INTERNAL_CLEAN_STEPS :=
98 INTERNAL_CLEAN_BUILD_VERSION :=
101 # Since products and build variants (unfortunately) share the same
102 # PRODUCT_OUT staging directory, things can get out of sync if different
103 # build configurations are built in the same tree. The following logic
104 # will notice when the configuration has changed and remove the files
105 # necessary to keep things consistent.
107 previous_build_config_file := $(PRODUCT_OUT)/previous_build_config.mk
109 # TODO: this special case for the sdk is only necessary while "sdk"
110 # is a valid make target. Eventually, it will just be a product, at
111 # which point TARGET_PRODUCT will handle it and we can avoid this check
112 # of MAKECMDGOALS. The "addprefix" is just to keep things pretty.
113 ifneq ($(TARGET_PRODUCT),sdk)
114 building_sdk := $(addprefix -,$(filter sdk,$(MAKECMDGOALS)))
116 # Don't bother with this extra part when explicitly building the sdk product.
120 # A change in the list of locales warrants an installclean, too.
121 locale_list := $(subst $(space),$(comma),$(strip $(PRODUCT_LOCALES)))
123 current_build_config := \
124 $(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)$(building_sdk)-{$(locale_list)}
127 force_installclean := false
129 # Read the current state from the file, if present.
130 # Will set PREVIOUS_BUILD_CONFIG.
132 PREVIOUS_BUILD_CONFIG :=
133 -include $(previous_build_config_file)
134 PREVIOUS_BUILD_CONFIG := $(strip $(PREVIOUS_BUILD_CONFIG))
135 ifdef PREVIOUS_BUILD_CONFIG
136 ifneq "$(current_build_config)" "$(PREVIOUS_BUILD_CONFIG)"
137 $(info *** Build configuration changed: "$(PREVIOUS_BUILD_CONFIG)" -> "$(current_build_config)")
138 ifneq ($(DISABLE_AUTO_INSTALLCLEAN),true)
139 force_installclean := true
141 $(info DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.)
144 endif # else, this is the first build, so no need to clean.
145 PREVIOUS_BUILD_CONFIG :=
147 # Write the new state to the file.
150 mkdir -p $(dir $(previous_build_config_file)) && \
151 echo "PREVIOUS_BUILD_CONFIG := $(current_build_config)" > \
152 $(previous_build_config_file) \
154 previous_build_config_file :=
155 current_build_config :=
161 # The files/dirs to delete during an installclean. This includes the
162 # non-common APPS directory, which may contain the wrong resources.
164 # Deletes all of the files that change between different build types,
165 # like "make user" vs. "make sdk". This lets you work with different
166 # build types without having to do a full clean each time. E.g.:
169 # $ make installclean
171 # $ make installclean
174 installclean_files := \
175 $(HOST_OUT)/obj/NOTICE_FILES \
177 $(PRODUCT_OUT)/*.img \
178 $(PRODUCT_OUT)/*.txt \
179 $(PRODUCT_OUT)/*.xlb \
180 $(PRODUCT_OUT)/*.zip \
181 $(PRODUCT_OUT)/data \
182 $(PRODUCT_OUT)/obj/APPS \
183 $(PRODUCT_OUT)/obj/NOTICE_FILES \
184 $(PRODUCT_OUT)/obj/PACKAGING \
185 $(PRODUCT_OUT)/recovery \
186 $(PRODUCT_OUT)/root \
187 $(PRODUCT_OUT)/system \
188 $(PRODUCT_OUT)/dex_bootjars \
189 $(PRODUCT_OUT)/obj/JAVA_LIBRARIES
191 # The files/dirs to delete during a dataclean, which removes any files
192 # in the staging and emulator data partitions.
194 $(PRODUCT_OUT)/data/* \
195 $(PRODUCT_OUT)/data-qemu/* \
196 $(PRODUCT_OUT)/userdata-qemu.img
198 # make sure *_OUT is set so that we won't result in deleting random parts
200 ifneq (2,$(words $(HOST_OUT) $(PRODUCT_OUT)))
201 $(error both HOST_OUT and PRODUCT_OUT should be set at this point.)
204 # Define the rules for commandline invocation.
206 dataclean: FILES := $(dataclean_files)
208 $(hide) rm -rf $(FILES)
209 @echo "Deleted emulator userdata images."
212 installclean: FILES := $(installclean_files)
213 installclean: dataclean
214 $(hide) rm -rf $(FILES)
215 @echo "Deleted images and staging directories."
217 ifeq "$(force_installclean)" "true"
218 $(info *** Forcing "make installclean"...)
219 $(info *** rm -rf $(dataclean_files) $(installclean_files))
220 $(shell rm -rf $(dataclean_files) $(installclean_files))
221 $(info *** Done with the cleaning, now starting the real build.)
223 force_installclean :=