cleanbuild.mk revision 14da1424e0e8d96aee66676b8735001dbe6bd86b
1# Copyright (C) 2007 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#
15
16# Don't bother with the cleanspecs if you are running mm/mmm
17ifeq ($(ONE_SHOT_MAKEFILE)$(dont_bother),)
18
19INTERNAL_CLEAN_STEPS :=
20
21# Builds up a list of clean steps.  Creates a unique
22# id for each step by taking makefile path, INTERNAL_CLEAN_BUILD_VERSION
23# and appending an increasing number of '@' characters.
24#
25# $(1): shell command to run
26# $(2): indicate to not use makefile path as part of step id if not empty.
27#       $(2) should only be used in build/core/cleanspec.mk: just for compatibility.
28define _add-clean-step
29  $(if $(strip $(INTERNAL_CLEAN_BUILD_VERSION)),, \
30      $(error INTERNAL_CLEAN_BUILD_VERSION not set))
31  $(eval _acs_makefile_prefix := $(lastword $(MAKEFILE_LIST)))
32  $(eval _acs_makefile_prefix := $(subst /,_,$(_acs_makefile_prefix)))
33  $(eval _acs_makefile_prefix := $(subst .,-,$(_acs_makefile_prefix)))
34  $(eval _acs_makefile_prefix := $(_acs_makefile_prefix)_acs)
35  $(if $($(_acs_makefile_prefix)),,\
36      $(eval $(_acs_makefile_prefix) := $(INTERNAL_CLEAN_BUILD_VERSION)))
37  $(eval $(_acs_makefile_prefix) := $($(_acs_makefile_prefix))@)
38  $(if $(strip $(2)),$(eval _acs_id := $($(_acs_makefile_prefix))),\
39      $(eval _acs_id := $(_acs_makefile_prefix)$($(_acs_makefile_prefix))))
40  $(eval INTERNAL_CLEAN_STEPS += $(_acs_id))
41  $(eval INTERNAL_CLEAN_STEP.$(_acs_id) := $(1))
42  $(eval _acs_id :=)
43  $(eval _acs_makefile_prefix :=)
44endef
45define add-clean-step
46$(eval # for build/core/cleanspec.mk, dont use makefile path as part of step id) \
47$(if $(filter %/cleanspec.mk,$(lastword $(MAKEFILE_LIST))),\
48    $(eval $(call _add-clean-step,$(1),true)),\
49    $(eval $(call _add-clean-step,$(1))))
50endef
51
52# Defines INTERNAL_CLEAN_BUILD_VERSION and the individual clean steps.
53# cleanspec.mk is outside of the core directory so that more people
54# can have permission to touch it.
55include $(BUILD_SYSTEM)/cleanspec.mk
56INTERNAL_CLEAN_BUILD_VERSION := $(strip $(INTERNAL_CLEAN_BUILD_VERSION))
57
58# If the clean_steps.mk file is missing (usually after a clean build)
59# then we won't do anything.
60CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)
61CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)
62
63# Read the current state from the file, if present.
64# Will set CURRENT_CLEAN_BUILD_VERSION and CURRENT_CLEAN_STEPS.
65#
66clean_steps_file := $(PRODUCT_OUT)/clean_steps.mk
67-include $(clean_steps_file)
68
69ifneq ($(CURRENT_CLEAN_BUILD_VERSION),$(INTERNAL_CLEAN_BUILD_VERSION))
70  # The major clean version is out-of-date.  Do a full clean, and
71  # don't even bother with the clean steps.
72  $(info *** A clean build is required because of a recent change.)
73  $(shell rm -rf $(OUT_DIR))
74  $(info *** Done with the cleaning, now starting the real build.)
75else
76  # The major clean version is correct.  Find the list of clean steps
77  # that we need to execute to get up-to-date.
78  steps := \
79      $(filter-out $(CURRENT_CLEAN_STEPS),$(INTERNAL_CLEAN_STEPS))
80  $(foreach step,$(steps), \
81    $(info Clean step: $(INTERNAL_CLEAN_STEP.$(step))) \
82    $(shell $(INTERNAL_CLEAN_STEP.$(step))) \
83   )
84
85  # Rewrite the clean step for the second arch.
86  ifdef TARGET_2ND_ARCH
87  # $(1): the clean step cmd
88  # $(2): the prefix to search for
89  # $(3): the prefix to replace with
90  define -cs-rewrite-cleanstep
91  $(if $(filter $(2)/%,$(1)),\
92    $(eval _crs_new_cmd := $(patsubst $(2)/%,$(3)/%,$(1)))\
93    $(info Clean step: $(_crs_new_cmd))\
94    $(shell $(_crs_new_cmd)))
95  endef
96  $(foreach step,$(steps), \
97    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$(TARGET_OUT_INTERMEDIATES),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_INTERMEDIATES))\
98    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$(TARGET_OUT_SHARED_LIBRARIES),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_SHARED_LIBRARIES))\
99    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$(TARGET_OUT_VENDOR_SHARED_LIBRARIES),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_VENDOR_SHARED_LIBRARIES))\
100    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_INTERMEDIATES),$(TARGET_OUT_INTERMEDIATES))\
101    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_SHARED_LIBRARIES),$(TARGET_OUT_SHARED_LIBRARIES))\
102    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_VENDOR_SHARED_LIBRARIES),$(TARGET_OUT_VENDOR_SHARED_LIBRARIES))\
103    )
104  endif
105  _crs_new_cmd :=
106  steps :=
107endif
108
109# Write the new state to the file.
110#
111ifneq ($(CURRENT_CLEAN_BUILD_VERSION)-$(CURRENT_CLEAN_STEPS),$(INTERNAL_CLEAN_BUILD_VERSION)-$(INTERNAL_CLEAN_STEPS))
112$(shell \
113  mkdir -p $(dir $(clean_steps_file)) && \
114  echo "CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)" > \
115      $(clean_steps_file) ;\
116  echo "CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)" >> \
117      $(clean_steps_file) \
118 )
119endif
120
121CURRENT_CLEAN_BUILD_VERSION :=
122CURRENT_CLEAN_STEPS :=
123clean_steps_file :=
124INTERNAL_CLEAN_STEPS :=
125INTERNAL_CLEAN_BUILD_VERSION :=
126
127endif  # if not ONE_SHOT_MAKEFILE dont_bother
128
129# Since products and build variants (unfortunately) share the same
130# PRODUCT_OUT staging directory, things can get out of sync if different
131# build configurations are built in the same tree.  The following logic
132# will notice when the configuration has changed and remove the files
133# necessary to keep things consistent.
134
135previous_build_config_file := $(PRODUCT_OUT)/previous_build_config.mk
136
137# A change in the list of aapt configs warrants an installclean, too.
138aapt_config_list := $(strip $(PRODUCT_AAPT_CONFIG) $(PRODUCT_AAPT_PREF_CONFIG))
139
140current_build_config := \
141    $(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)-{$(aapt_config_list)}
142current_sanitize_target := $(strip $(SANITIZE_TARGET))
143ifeq (,$(current_sanitize_target))
144  current_sanitize_target := false
145endif
146aapt_config_list :=
147force_installclean := false
148force_objclean := false
149
150# Read the current state from the file, if present.
151# Will set PREVIOUS_BUILD_CONFIG.
152#
153PREVIOUS_BUILD_CONFIG :=
154PREVIOUS_SANITIZE_TARGET :=
155-include $(previous_build_config_file)
156PREVIOUS_BUILD_CONFIG := $(strip $(PREVIOUS_BUILD_CONFIG))
157PREVIOUS_SANITIZE_TARGET := $(strip $(PREVIOUS_SANITIZE_TARGET))
158
159ifdef PREVIOUS_BUILD_CONFIG
160  ifneq "$(current_build_config)" "$(PREVIOUS_BUILD_CONFIG)"
161    $(info *** Build configuration changed: "$(PREVIOUS_BUILD_CONFIG)" -> "$(current_build_config)")
162    ifneq ($(DISABLE_AUTO_INSTALLCLEAN),true)
163      force_installclean := true
164    else
165      $(info DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.)
166    endif
167  endif
168endif  # else, this is the first build, so no need to clean.
169
170ifdef PREVIOUS_SANITIZE_TARGET
171  ifneq "$(current_sanitize_target)" "$(PREVIOUS_SANITIZE_TARGET)"
172    $(info *** SANITIZE_TARGET changed: "$(PREVIOUS_SANITIZE_TARGET)" -> "$(current_sanitize_target)")
173    force_objclean := true
174  endif
175endif  # else, this is the first build, so no need to clean.
176
177# Write the new state to the file.
178#
179ifneq ($(PREVIOUS_BUILD_CONFIG)-$(PREVIOUS_SANITIZE_TARGET),$(current_build_config)-$(current_sanitize_target))
180$(shell \
181  mkdir -p $(dir $(previous_build_config_file)) && \
182  echo "PREVIOUS_BUILD_CONFIG := $(current_build_config)" > \
183      $(previous_build_config_file) && \
184  echo "PREVIOUS_SANITIZE_TARGET := $(current_sanitize_target)" >> \
185      $(previous_build_config_file) \
186 )
187endif
188PREVIOUS_BUILD_CONFIG :=
189PREVIOUS_SANITIZE_TARGET :=
190previous_build_config_file :=
191current_build_config :=
192
193#
194# installclean logic
195#
196
197# The files/dirs to delete during an installclean.  This includes the
198# non-common APPS directory, which may contain the wrong resources.
199#
200# Deletes all of the files that change between different build types,
201# like "make user" vs. "make sdk".  This lets you work with different
202# build types without having to do a full clean each time.  E.g.:
203#
204#     $ make -j8 all
205#     $ make installclean
206#     $ make -j8 user
207#     $ make installclean
208#     $ make -j8 sdk
209#
210installclean_files := \
211	$(HOST_OUT)/obj/NOTICE_FILES \
212	$(HOST_OUT)/sdk \
213	$(PRODUCT_OUT)/*.img \
214	$(PRODUCT_OUT)/*.ini \
215	$(PRODUCT_OUT)/*.txt \
216	$(PRODUCT_OUT)/*.xlb \
217	$(PRODUCT_OUT)/*.zip \
218	$(PRODUCT_OUT)/kernel \
219	$(PRODUCT_OUT)/data \
220	$(PRODUCT_OUT)/skin \
221	$(PRODUCT_OUT)/obj/APPS \
222	$(PRODUCT_OUT)/obj/NOTICE_FILES \
223	$(PRODUCT_OUT)/obj/PACKAGING \
224	$(PRODUCT_OUT)/recovery \
225	$(PRODUCT_OUT)/root \
226	$(PRODUCT_OUT)/system \
227	$(PRODUCT_OUT)/vendor \
228	$(PRODUCT_OUT)/oem \
229	$(PRODUCT_OUT)/dex_bootjars \
230	$(PRODUCT_OUT)/obj/JAVA_LIBRARIES \
231	$(PRODUCT_OUT)/obj/FAKE \
232	$(PRODUCT_OUT)/obj/EXECUTABLES/adbd_intermediates \
233	$(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libfs_mgr_intermediates \
234	$(PRODUCT_OUT)/obj/EXECUTABLES/init_intermediates \
235	$(PRODUCT_OUT)/obj/ETC/mac_permissions.xml_intermediates \
236	$(PRODUCT_OUT)/obj/ETC/sepolicy_intermediates \
237	$(PRODUCT_OUT)/obj/ETC/init.environ.rc_intermediates
238
239# The files/dirs to delete during a dataclean, which removes any files
240# in the staging and emulator data partitions.
241dataclean_files := \
242	$(PRODUCT_OUT)/data/* \
243	$(PRODUCT_OUT)/data-qemu/* \
244	$(PRODUCT_OUT)/userdata-qemu.img
245
246# The files/dirs to delete during an objclean, which removes any files
247# in the staging and emulator data partitions.
248objclean_files := \
249	$(TARGET_OUT_INTERMEDIATES) \
250	$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_INTERMEDIATES)
251
252# make sure *_OUT is set so that we won't result in deleting random parts
253# of the filesystem.
254ifneq (2,$(words $(HOST_OUT) $(PRODUCT_OUT)))
255  $(error both HOST_OUT and PRODUCT_OUT should be set at this point.)
256endif
257
258# Define the rules for commandline invocation.
259.PHONY: dataclean
260dataclean: FILES := $(dataclean_files)
261dataclean:
262	$(hide) rm -rf $(FILES)
263	@echo "Deleted emulator userdata images."
264
265.PHONY: installclean
266installclean: FILES := $(installclean_files)
267installclean: dataclean
268	$(hide) rm -rf $(FILES)
269	@echo "Deleted images and staging directories."
270
271.PHONY: objclean
272objclean: FILES := $(objclean_files)
273objclean:
274	$(hide) rm -rf $(FILES)
275	@echo "Deleted images and staging directories."
276
277ifeq "$(force_installclean)" "true"
278  $(info *** Forcing "make installclean"...)
279  $(info *** rm -rf $(dataclean_files) $(installclean_files))
280  $(shell rm -rf $(dataclean_files) $(installclean_files))
281  $(info *** Done with the cleaning, now starting the real build.)
282endif
283force_installclean :=
284
285ifeq "$(force_objclean)" "true"
286  $(info *** Forcing cleanup of intermediate files...)
287  $(info *** rm -rf $(objclean_files))
288  $(shell rm -rf $(objclean_files))
289  $(info *** Done with the cleaning, now starting the real build.)
290endif
291force_objclean :=
292
293###########################################################
294# Clean build tools when swithcing between prebuilt host tools (such as in
295# apps_only build) and tools built from source (platform build).
296previous_prebuilt_tools_config_file := $(HOST_OUT)/previous_prebuilt_tools_config.mk
297ifneq (,$(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK)))
298current_prebuilt_tools := true
299else
300current_prebuilt_tools := false
301endif
302PREVIOUS_PREBUILT_TOOLS :=
303-include $(previous_prebuilt_tools_config_file)
304force_tools_clean :=
305ifdef PREVIOUS_PREBUILT_TOOLS
306ifneq ($(PREVIOUS_PREBUILT_TOOLS),$(current_prebuilt_tools))
307force_tools_clean := true
308endif
309endif # else, this is the first build, so no need to clean.
310
311# Write the new state to the file.
312ifneq ($(PREVIOUS_PREBUILT_TOOLS),$(current_prebuilt_tools))
313$(shell \
314  mkdir -p $(dir $(previous_prebuilt_tools_config_file)) && \
315  echo "PREVIOUS_PREBUILT_TOOLS:=$(current_prebuilt_tools)" > \
316    $(previous_prebuilt_tools_config_file))
317endif
318
319ifeq ($(force_tools_clean),true)
320# For this list of prebuilt tools, see prebuilts/sdk/tools/Android.mk.
321tools_clean_files := \
322  $(HOST_OUT_COMMON_INTERMEDIATES)/JAVA_LIBRARIES/signapk_intermediates \
323  $(HOST_OUT_COMMON_INTERMEDIATES)/JAVA_LIBRARIES/dx_intermediates \
324  $(HOST_OUT_COMMON_INTERMEDIATES)/JAVA_LIBRARIES/shrinkedAndroid_intermediates \
325  $(HOST_OUT)/obj*/EXECUTABLES/aapt_intermediates \
326  $(HOST_OUT)/obj*/EXECUTABLES/aidl_intermediates \
327  $(HOST_OUT)/obj*/EXECUTABLES/zipalign_intermediates \
328  $(HOST_OUT)/obj*/lib/libc++$(HOST_SHLIB_SUFFIX) \
329
330$(info *** build type changed, clean host tools...)
331$(info *** rm -rf $(tools_clean_files))
332$(shell rm -rf $(tools_clean_files))
333endif
334
335###########################################################
336
337.PHONY: clean-jack-files
338clean-jack-files: clean-dex-files
339	$(hide) find $(OUT_DIR) -name "*.jack" | xargs rm -f
340	$(hide) find $(OUT_DIR) -type d -name "jack" | xargs rm -rf
341	@echo "All jack files have been removed."
342
343.PHONY: clean-dex-files
344clean-dex-files:
345	$(hide) find $(OUT_DIR) -name "*.dex" ! -path "*/jack-incremental/*" | xargs rm -f
346	$(hide) for i in `find $(OUT_DIR) -name "*.jar" -o -name "*.apk"` ; do ((unzip -l $$i 2> /dev/null | \
347				grep -q "\.dex$$" && rm -f $$i) || continue ) ; done
348	@echo "All dex files and archives containing dex files have been removed."
349
350.PHONY: clean-jack-incremental
351clean-jack-incremental:
352	$(hide) find $(OUT_DIR) -name "jack-incremental" -type d | xargs rm -rf
353	@echo "All jack incremental dirs have been removed."
354