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# Absolute path of the present working direcotry.
17# This overrides the shell variable $PWD, which does not necessarily points to
18# the top of the source tree, for example when "make -C" is used in m/mm/mmm.
19PWD := $(shell pwd)
20
21TOP := .
22TOPDIR :=
23
24BUILD_SYSTEM := $(TOPDIR)build/make/core
25
26# Set up various standard variables based on configuration
27# and host information.
28include $(BUILD_SYSTEM)/config.mk
29
30include $(SOONG_MAKEVARS_MK)
31
32include $(BUILD_SYSTEM)/clang/config.mk
33
34# CTS-specific config.
35-include cts/build/config.mk
36# VTS-specific config.
37-include test/vts/tools/vts-tradefed/build/config.mk
38# device-tests-specific-config.
39-include tools/tradefederation/build/suites/device-tests/config.mk
40# general-tests-specific-config.
41-include tools/tradefederation/build/suites/general-tests/config.mk
42
43INTERNAL_CLEAN_STEPS :=
44
45# Builds up a list of clean steps.  Creates a unique
46# id for each step by taking makefile path, INTERNAL_CLEAN_BUILD_VERSION
47# and appending an increasing number of '@' characters.
48#
49# $(1): shell command to run
50# $(2): indicate to not use makefile path as part of step id if not empty.
51#       $(2) should only be used in build/make/core/cleanspec.mk: just for compatibility.
52define _add-clean-step
53  $(if $(strip $(INTERNAL_CLEAN_BUILD_VERSION)),, \
54      $(error INTERNAL_CLEAN_BUILD_VERSION not set))
55  $(eval _acs_makefile_prefix := $(lastword $(MAKEFILE_LIST)))
56  $(eval _acs_makefile_prefix := $(subst /,_,$(_acs_makefile_prefix)))
57  $(eval _acs_makefile_prefix := $(subst .,-,$(_acs_makefile_prefix)))
58  $(eval _acs_makefile_prefix := $(_acs_makefile_prefix)_acs)
59  $(if $($(_acs_makefile_prefix)),,\
60      $(eval $(_acs_makefile_prefix) := $(INTERNAL_CLEAN_BUILD_VERSION)))
61  $(eval $(_acs_makefile_prefix) := $($(_acs_makefile_prefix))@)
62  $(if $(strip $(2)),$(eval _acs_id := $($(_acs_makefile_prefix))),\
63      $(eval _acs_id := $(_acs_makefile_prefix)$($(_acs_makefile_prefix))))
64  $(eval INTERNAL_CLEAN_STEPS += $(_acs_id))
65  $(eval INTERNAL_CLEAN_STEP.$(_acs_id) := $(1))
66  $(eval _acs_id :=)
67  $(eval _acs_makefile_prefix :=)
68endef
69define add-clean-step
70$(eval # for build/make/core/cleanspec.mk, dont use makefile path as part of step id) \
71$(if $(filter %/cleanspec.mk,$(lastword $(MAKEFILE_LIST))),\
72    $(eval $(call _add-clean-step,$(1),true)),\
73    $(eval $(call _add-clean-step,$(1))))
74endef
75
76# Defines INTERNAL_CLEAN_BUILD_VERSION and the individual clean steps.
77# cleanspec.mk is outside of the core directory so that more people
78# can have permission to touch it.
79include $(BUILD_SYSTEM)/cleanspec.mk
80INTERNAL_CLEAN_BUILD_VERSION := $(strip $(INTERNAL_CLEAN_BUILD_VERSION))
81INTERNAL_CLEAN_STEPS := $(strip $(INTERNAL_CLEAN_STEPS))
82
83# If the clean_steps.mk file is missing (usually after a clean build)
84# then we won't do anything.
85CURRENT_CLEAN_BUILD_VERSION := MISSING
86CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)
87
88# Read the current state from the file, if present.
89# Will set CURRENT_CLEAN_BUILD_VERSION and CURRENT_CLEAN_STEPS.
90#
91clean_steps_file := $(PRODUCT_OUT)/clean_steps.mk
92-include $(clean_steps_file)
93
94ifeq ($(CURRENT_CLEAN_BUILD_VERSION),MISSING)
95  # Do nothing
96else ifneq ($(CURRENT_CLEAN_BUILD_VERSION),$(INTERNAL_CLEAN_BUILD_VERSION))
97  # The major clean version is out-of-date.  Do a full clean, and
98  # don't even bother with the clean steps.
99  $(info *** A clean build is required because of a recent change.)
100  $(shell rm -rf $(OUT_DIR))
101  $(info *** Done with the cleaning, now starting the real build.)
102else
103  # The major clean version is correct.  Find the list of clean steps
104  # that we need to execute to get up-to-date.
105  steps := \
106      $(filter-out $(CURRENT_CLEAN_STEPS),$(INTERNAL_CLEAN_STEPS))
107  $(foreach step,$(steps), \
108    $(info Clean step: $(INTERNAL_CLEAN_STEP.$(step))) \
109    $(shell $(INTERNAL_CLEAN_STEP.$(step))) \
110   )
111
112  # Rewrite the clean step for the second arch.
113  ifdef TARGET_2ND_ARCH
114  # $(1): the clean step cmd
115  # $(2): the prefix to search for
116  # $(3): the prefix to replace with
117  define -cs-rewrite-cleanstep
118  $(if $(filter $(2)/%,$(1)),\
119    $(eval _crs_new_cmd := $(patsubst $(2)/%,$(3)/%,$(1)))\
120    $(info Clean step: $(_crs_new_cmd))\
121    $(shell $(_crs_new_cmd)))
122  endef
123  $(foreach step,$(steps), \
124    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$(TARGET_OUT_INTERMEDIATES),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_INTERMEDIATES))\
125    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$(TARGET_OUT_SHARED_LIBRARIES),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_SHARED_LIBRARIES))\
126    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$(TARGET_OUT_VENDOR_SHARED_LIBRARIES),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_VENDOR_SHARED_LIBRARIES))\
127    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_INTERMEDIATES),$(TARGET_OUT_INTERMEDIATES))\
128    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_SHARED_LIBRARIES),$(TARGET_OUT_SHARED_LIBRARIES))\
129    $(call -cs-rewrite-cleanstep,$(INTERNAL_CLEAN_STEP.$(step)),$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_VENDOR_SHARED_LIBRARIES),$(TARGET_OUT_VENDOR_SHARED_LIBRARIES))\
130    )
131  endif
132  _crs_new_cmd :=
133  steps :=
134endif
135
136# Write the new state to the file.
137#
138ifneq ($(CURRENT_CLEAN_BUILD_VERSION)-$(CURRENT_CLEAN_STEPS),$(INTERNAL_CLEAN_BUILD_VERSION)-$(INTERNAL_CLEAN_STEPS))
139$(shell mkdir -p $(dir $(clean_steps_file)))
140$(file >$(clean_steps_file).tmp,CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)$(newline)CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)$(newline))
141$(shell if ! cmp -s $(clean_steps_file).tmp $(clean_steps_file); then \
142          mv $(clean_steps_file).tmp $(clean_steps_file); \
143        else \
144          rm $(clean_steps_file).tmp; \
145        fi)
146endif
147
148CURRENT_CLEAN_BUILD_VERSION :=
149CURRENT_CLEAN_STEPS :=
150clean_steps_file :=
151INTERNAL_CLEAN_STEPS :=
152INTERNAL_CLEAN_BUILD_VERSION :=
153