1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17NANO_BUILD := $(NANOHUB_OS_PATH)/build
18
19NANO_OS := nanohub
20NANO_ALL_CLASSES := phone watch
21NANO_ALL_TARGETS := APP BL OS
22
23NANO_ALL_ALL := $(AUX_ALL_VARIANTS) $(AUX_ALL_OSES) $(AUX_ALL_ARCHS) $(AUX_ALL_SUBARCHS) $(AUX_ALL_CPUS) $(NANO_ALL_CLASSES) $(NANO_ALL_TARGETS)
24
25GLOBAL_NANO_OBJCOPY_FLAGS_cortexm4 := -I elf32-littlearm -O binary
26
27CLEAR_NANO_VARS := $(NANO_BUILD)/clear_vars.mk
28NANOHUB_BL_CONFIG := $(NANO_BUILD)/bl_config.mk
29NANOHUB_OS_CONFIG := $(NANO_BUILD)/os_config.mk
30NANOHUB_APP_CONFIG := $(NANO_BUILD)/app_config.mk
31
32BUILD_NANOHUB_BL_STATIC_LIBRARY := $(NANO_BUILD)/bl_static_library.mk
33BUILD_NANOHUB_OS_STATIC_LIBRARY := $(NANO_BUILD)/os_static_library.mk
34BUILD_NANOHUB_APP_STATIC_LIBRARY := $(NANO_BUILD)/app_static_library.mk
35BUILD_NANOHUB_EXECUTABLE := $(NANO_BUILD)/nanohub_executable.mk
36BUILD_NANOHUB_BL_EXECUTABLE := $(NANO_BUILD)/bl_executable.mk
37BUILD_NANOHUB_OS_EXECUTABLE := $(NANO_BUILD)/os_executable.mk
38BUILD_NANOHUB_OS_IMAGE := $(NANO_BUILD)/os_image.mk
39BUILD_NANOHUB_APP_EXECUTABLE := $(NANO_BUILD)/app_executable.mk
40BUILD_NANOHUB_APP_CHRE10_EXECUTABLE := $(NANO_BUILD)/app_chre10_executable.mk
41BUILD_NANOHUB_APP_CHRE11_EXECUTABLE := $(NANO_BUILD)/app_chre11_executable.mk
42BUILD_NANOHUB_APP_CHRE_EXECUTABLE := $(NANO_BUILD)/app_chre_executable.mk
43
44NANOAPP_POSTPROCESS := $(HOST_OUT_EXECUTABLES)/nanoapp_postprocess
45NANOAPP_SIGN := $(HOST_OUT_EXECUTABLES)/nanoapp_sign
46
47# $(1) - optional value to assign to
48#        pass empty to clear for repeated includes
49#        pass some definitive invalid value otherwise (after last repeat)
50define nano-reset-built-env
51$(eval OVERRIDE_BUILT_MODULE_PATH:=$(1)) \
52$(eval LOCAL_BUILT_MODULE:=$(1)) \
53$(eval LOCAL_INSTALLED_MODULE:=$(1)) \
54$(eval LOCAL_INTERMEDIATE_TARGETS:=$(1))
55endef
56
57# variant may declare it's class; default is phone
58define nano-variant-class
59$(if $(filter $(NANO_ALL_CLASSES),$(NANO_CLASS_$(AUX_OS_VARIANT))),$(NANO_CLASS_$(AUX_OS_VARIANT)),phone)
60endef
61
62# $(1) - target file
63# $(2) - list of dependencies
64define nano-gen-linker-script-from-list-body
65    mkdir -p $(dir $(1)) && \
66    rm -f $(1).tmp && \
67    touch $(1).tmp && \
68    $(foreach file,$(2),echo "INCLUDE $(file)"  >> $(1).tmp &&) \
69    mv -f $(1).tmp $(1)
70endef
71
72# $(1) - target file
73# $(2) - list of dependencies
74define nano-gen-linker-script-from-list
75$(1): $(2)
76	$(call nano-gen-linker-script-from-list-body,$(1),$(2))
77endef
78
79# create linker script rule
80#
81# $(1) - target file
82# $(2) - { os | bl }
83# $(3) - platform name
84# $(4) - platform class
85# $(5) - platform dir
86define nano-gen-linker-script
87$(eval $(call nano-gen-linker-script-from-list,$(1),$(patsubst %,$(NANOHUB_OS_PATH)/os/platform/$(5)/lkr/%.lkr,$(3).map $(4).$(2) $(4).common)))
88endef
89
90# create linker script rule
91#
92# $(1) - target file
93# $(2) - bl|os
94# $(3) - platform name
95# $(4) - platform class
96# $(5) - platform dir
97#
98# NOTE: ($(2), $(3) - unused; keep for argument compatibility with nano-gen-linker-script
99define nano-gen-linker-script-native
100$(eval $(call nano-gen-linker-script-from-list,$(1),$(NANOHUB_OS_PATH)/os/platform/$(5)/lkr/$(4).extra.lkr))
101endef
102
103# create linker script rule
104#
105# $(1) - target file
106# $(2) - { os | bl }
107# $(3) - full path to map file
108# $(4) - platform class
109# $(5) - platform dir
110define nano-gen-linker-script-map
111$(eval $(call nano-gen-linker-script-from-list,$(1),$(3) $(patsubst %,$(NANOHUB_OS_PATH)/os/platform/$(5)/lkr/%.lkr,$(4).$(2) $(4).common)))
112endef
113
114# variables that Android.mk or our config files may define per-cpu, per-arch etc;
115# must include all LOCAL* variables we modify in any place within the scope of for-each-variant.
116#
117# workflow is as follows:
118# before starting iterations:
119#   all the LOCAL_<var> and LOCAL_<var>_<cpu,arch,subarch,variant,os,class> from NANO_VAR_LIST
120#   are copied to LOCAL_NANO_*; original LOCAL_* are erased to avoid conflicts fith underlaying build which also does suffix handling.
121#   this part is performed by nano-user-vars-save-all
122# on every iteration, before includeing file:
123#   all the LOCAL_NANO_<var>_<cpu,arch,subarch,variant,os,class> vars are loaded for the current variant,
124#   and then concatenated all toghether and also with
125#   NANO_VARIANT_<target>_<var>_<variant> (where target is OS or BL for system builds, or APP for nanoapp builds) and
126#   NANO_VARIANT_<var>_<variant>; result is stored in LOCAL_<var>
127#   this var is used by underlaying build infrastructure as usual
128#   this part is performed by nano-user-vars-load-all
129# on every iteration, after includeing file:
130#   reset "BUILT" variables in order to let next iteration going
131# after all iterations done:
132#   erase all volatile AUX* enviraonment, cleanup all the LOCAL* and LOCAL_NANO* vars
133NANO_VAR_LIST :=                \
134    C_INCLUDES                  \
135    CFLAGS                      \
136    ASFLAGS                     \
137    CPPFLAGS                    \
138    SRC_FILES                   \
139    STATIC_LIBRARIES            \
140    WHOLE_STATIC_LIBRARIES      \
141    LDFLAGS                     \
142    OBJCOPY_SECT                \
143
144# collect anything that user might define per-cpu, per-arch etc
145# $(1) - one of $(NANO_VAR_LIST)
146# $(2) - optional: one of { APP,BL,OS }
147define nano-user-var-load
148$(eval LOCAL_$(1) :=                                                    \
149    $(LOCAL_NANO_$(1))                                                  \
150    $(NANO_VARIANT_$(1)_$(AUX_OS_VARIANT))                              \
151    $(NANO_VARIANT_$(2)_$(1)_$(AUX_OS_VARIANT))                         \
152    $(LOCAL_NANO_$(1)_$(AUX_OS_VARIANT))                                \
153    $(LOCAL_NANO_$(1)_$(AUX_CPU))                                       \
154    $(LOCAL_NANO_$(1)_$(AUX_ARCH))                                      \
155    $(LOCAL_NANO_$(1)_$(AUX_SUBARCH))                                   \
156    $(LOCAL_NANO_$(1)_$(AUX_OS))                                        \
157    $(LOCAL_NANO_$(1)_$(call nano-variant-class,$(AUX_OS_VARIANT)))     \
158    $(LOCAL_NANO_$(1)_$(LOCAL_NANO_MODULE_TYPE))                        \
159)
160endef
161
162define nano-user-var-reset-final
163$(eval LOCAL_$(1):=) \
164$(eval LOCAL_NANO_$(1):=) \
165$(foreach v,$(NANO_ALL_ALL),\
166    $(eval LOCAL_NANO_$(1)_$(v):=) \
167    $(eval LOCAL_$(1)_$(v):=) \
168)
169endef
170
171define nano-user-vars-reset-final
172$(foreach _nuvrf_var,$(NANO_VAR_LIST),$(call nano-user-var-reset-final,$(_nuvrf_var)))
173endef
174
175# $(1) - optional: one of APP,BL,OS
176define nano-user-vars-load-all
177$(foreach _nuvla_var,$(NANO_VAR_LIST),$(call nano-user-var-load,$(_nuvla_var),$(1)))
178endef
179
180define nano-user-vars-save-all
181$(foreach _nuvsa_var,$(NANO_VAR_LIST),\
182    $(eval LOCAL_NANO_$(_nuvsa_var) := $(LOCAL_$(_nuvsa_var))) \
183    $(eval LOCAL_$(_nuvsa_var):=) \
184    $(foreach v,$(NANO_ALL_ALL),\
185        $(eval LOCAL_NANO_$(_nuvsa_var)_$(v):=$(LOCAL_$(_nuvsa_var)_$(v))) \
186        $(eval LOCAL_$(_nuvsa_var)_$(v):=) \
187    ) \
188)
189endef
190
191# $(1) - variant list
192# $(2) - optional: one of APP,BL,OS
193# $(3) - path to makefile which has to be included for each variant
194define for-each-variant-unchecked
195    $(eval AUX_RECURSIVE_VARIANT_LIST:=$(1)) \
196    $(call nano-user-vars-save-all) \
197    $(foreach _fev_variant,$(1),\
198        $(call aux-variant-load-env,$(_fev_variant)) \
199        $(call nano-user-vars-load-all,$(2)) \
200        $(info $(LOCAL_PATH): $(LOCAL_MODULE): OS=$(AUX_OS) ARCH=$(AUX_ARCH) SUBARCH=$(AUX_SUBARCH) CPU=$(AUX_CPU)) \
201        $(eval include $(3)) \
202        $(call nano-reset-built-env,) \
203    ) \
204    $(eval AUX_RECURSIVE_VARIANT_LIST:=) \
205    $(call aux-variant-load-env,) \
206    $(call nano-reset-built-env,$(LOCAL_MODULE)-module-is-poisoned) \
207    $(call nano-user-vars-reset-final) \
208
209endef
210
211# $(1),$(2) - two sets to compare for equality
212# returns true, if sets have the same items (not necessarily in the same order)
213# returns empty string on mismatch
214define equal-sets
215$(if $(strip $(filterout $(1),$(2))$(filterout $(2),$(1))),,true)
216endef
217
218# this call would include a given makefile in the loop,
219# and would iterate through available variants from $(1)
220# $(1) - variant list
221# $(2) - optional: one of APP,BL,OS
222# $(3) - path to makefile to include for each variant
223define for-each-variant
224$(if $(AUX_RECURSIVE_VARIANT_LIST),$(if \
225        $(call equal-sets,$(AUX_RECURSIVE_VARIANT_LIST),$(1)),,\
226        $(error $(LOCAL_PATH): Recursive variant list mismatch: "$(AUX_RECURSIVE_VARIANT_LIST)" and "$(1))),\
227    $(call for-each-variant-unchecked,$(1),$(2),$(3)))
228endef
229