1#
2# Copyright (C) 2016 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_CHRE_EXECUTABLE := $(NANO_BUILD)/app_chre_executable.mk
41
42NANOAPP_POSTPROCESS := $(HOST_OUT_EXECUTABLES)/nanoapp_postprocess
43NANOAPP_SIGN := $(HOST_OUT_EXECUTABLES)/nanoapp_sign
44
45# $(1) - optional value to assign to
46#        pass empty to clear for repeated includes
47#        pass some definitive invalid value otherwise (after last repeat)
48define nano-reset-built-env
49$(eval OVERRIDE_BUILT_MODULE_PATH:=$(1)) \
50$(eval LOCAL_BUILT_MODULE:=$(1)) \
51$(eval LOCAL_INSTALLED_MODULE:=$(1)) \
52$(eval LOCAL_INTERMEDIATE_TARGETS:=$(1))
53endef
54
55# variant may declare it's class; default is phone
56define nano-variant-class
57$(if $(filter $(NANO_ALL_CLASSES),$(NANO_CLASS_$(AUX_OS_VARIANT))),$(NANO_CLASS_$(AUX_OS_VARIANT)),phone)
58endef
59
60# $(1) - target file
61# $(2) - list of dependencies
62define nano-gen-linker-script-from-list-body
63    mkdir -p $(dir $(1)) && \
64    rm -f $(1).tmp && \
65    touch $(1).tmp && \
66    $(foreach file,$(2),echo "INCLUDE $(file)"  >> $(1).tmp &&) \
67    mv -f $(1).tmp $(1)
68endef
69
70# $(1) - target file
71# $(2) - list of dependencies
72define nano-gen-linker-script-from-list
73$(1): $(2)
74	$(call nano-gen-linker-script-from-list-body,$(1),$(2))
75endef
76
77# create linker script rule
78#
79# $(1) - target file
80# $(2) - { os | bl }
81# $(3) - platform name
82# $(4) - platform class
83# $(5) - platform dir
84define nano-gen-linker-script
85$(eval $(call nano-gen-linker-script-from-list,$(1),$(patsubst %,$(NANOHUB_OS_PATH)/os/platform/$(5)/lkr/%.lkr,$(3).map $(4).$(2) $(4).common)))
86endef
87
88# create linker script rule
89#
90# $(1) - target file
91# $(2) - bl|os
92# $(3) - platform name
93# $(4) - platform class
94# $(5) - platform dir
95#
96# NOTE: ($(2), $(3) - unused; keep for argument compatibility with nano-gen-linker-script
97define nano-gen-linker-script-native
98$(eval $(call nano-gen-linker-script-from-list,$(1),$(NANOHUB_OS_PATH)/os/platform/$(5)/lkr/$(4).extra.lkr))
99endef
100
101# variables that Android.mk or our config files may define per-cpu, per-arch etc;
102# must include all LOCAL* variables we modify in any place within the scope of for-each-variant.
103#
104# workflow is as follows:
105# before starting iterations:
106#   all the LOCAL_<var> and LOCAL_<var>_<cpu,arch,subarch,variant,os,class> from NANO_VAR_LIST
107#   are copied to LOCAL_NANO_*; original LOCAL_* are erased to avoid conflicts fith underlaying build which also does suffix handling.
108#   this part is performed by nano-user-vars-save-all
109# on every iteration, before includeing file:
110#   all the LOCAL_NANO_<var>_<cpu,arch,subarch,variant,os,class> vars are loaded for the current variant,
111#   and then concatenated all toghether and also with
112#   NANO_VARIANT_<target>_<var>_<variant> (where target is OS or BL for system builds, or APP for nanoapp builds) and
113#   NANO_VARIANT_<var>_<variant>; result is stored in LOCAL_<var>
114#   this var is used by underlaying build infrastructure as usual
115#   this part is performed by nano-user-vars-load-all
116# on every iteration, after includeing file:
117#   reset "BUILT" variables in order to let next iteration going
118# after all iterations done:
119#   erase all volatile AUX* enviraonment, cleanup all the LOCAL* and LOCAL_NANO* vars
120NANO_VAR_LIST :=                \
121    C_INCLUDES                  \
122    CFLAGS                      \
123    ASFLAGS                     \
124    CPPFLAGS                    \
125    SRC_FILES                   \
126    STATIC_LIBRARIES            \
127    WHOLE_STATIC_LIBRARIES      \
128    LDFLAGS                     \
129    OBJCOPY_SECT                \
130
131# collect anything that user might define per-cpu, per-arch etc
132# $(1) - one of $(NANO_VAR_LIST)
133# $(2) - optional: one of { APP,BL,OS }
134define nano-user-var-load
135$(eval LOCAL_$(1) :=                                                    \
136    $(LOCAL_NANO_$(1))                                                  \
137    $(NANO_VARIANT_$(1)_$(AUX_OS_VARIANT))                              \
138    $(NANO_VARIANT_$(2)_$(1)_$(AUX_OS_VARIANT))                         \
139    $(LOCAL_NANO_$(1)_$(AUX_OS_VARIANT))                                \
140    $(LOCAL_NANO_$(1)_$(AUX_CPU))                                       \
141    $(LOCAL_NANO_$(1)_$(AUX_ARCH))                                      \
142    $(LOCAL_NANO_$(1)_$(AUX_SUBARCH))                                   \
143    $(LOCAL_NANO_$(1)_$(AUX_OS))                                        \
144    $(LOCAL_NANO_$(1)_$(call nano-variant-class,$(AUX_OS_VARIANT)))     \
145    $(LOCAL_NANO_$(1)_$(LOCAL_NANO_MODULE_TYPE))                        \
146)
147endef
148
149define nano-user-var-reset-final
150$(eval LOCAL_$(1):=) \
151$(eval LOCAL_NANO_$(1):=) \
152$(foreach v,$(NANO_ALL_ALL),\
153    $(eval LOCAL_NANO_$(1)_$(v):=) \
154    $(eval LOCAL_$(1)_$(v):=) \
155)
156endef
157
158define nano-user-vars-reset-final
159$(foreach _nuvrf_var,$(NANO_VAR_LIST),$(call nano-user-var-reset-final,$(_nuvrf_var)))
160endef
161
162# $(1) - optional: one of APP,BL,OS
163define nano-user-vars-load-all
164$(foreach _nuvla_var,$(NANO_VAR_LIST),$(call nano-user-var-load,$(_nuvla_var),$(1)))
165endef
166
167define nano-user-vars-save-all
168$(foreach _nuvsa_var,$(NANO_VAR_LIST),\
169    $(eval LOCAL_NANO_$(_nuvsa_var) := $(LOCAL_$(_nuvsa_var))) \
170    $(eval LOCAL_$(_nuvsa_var):=) \
171    $(foreach v,$(NANO_ALL_ALL),\
172        $(eval LOCAL_NANO_$(_nuvsa_var)_$(v):=$(LOCAL_$(_nuvsa_var)_$(v))) \
173        $(eval LOCAL_$(_nuvsa_var)_$(v):=) \
174    ) \
175)
176endef
177
178# $(1) - variant list
179# $(2) - optional: one of APP,BL,OS
180# $(3) - path to makefile which has to be included for each variant
181define for-each-variant-unchecked
182    $(eval AUX_RECURSIVE_VARIANT_LIST:=$(1)) \
183    $(call nano-user-vars-save-all) \
184    $(foreach _fev_variant,$(1),\
185        $(call aux-variant-load-env,$(_fev_variant)) \
186        $(call nano-user-vars-load-all,$(2)) \
187        $(info $(LOCAL_PATH): $(LOCAL_MODULE): OS=$(AUX_OS) ARCH=$(AUX_ARCH) SUBARCH=$(AUX_SUBARCH) CPU=$(AUX_CPU)) \
188        $(eval include $(3)) \
189        $(call nano-reset-built-env,) \
190    ) \
191    $(eval AUX_RECURSIVE_VARIANT_LIST:=) \
192    $(call aux-variant-load-env,) \
193    $(call nano-reset-built-env,$(LOCAL_MODULE)-module-is-poisoned) \
194    $(call nano-user-vars-reset-final) \
195
196endef
197
198# $(1),$(2) - two sets to compare for equality
199# returns true, if sets have the same items (not necessarily in the same order)
200# returns empty string on mismatch
201define equal-sets
202$(if $(strip $(filterout $(1),$(2))$(filterout $(2),$(1))),,true)
203endef
204
205# this call would include a given makefile in the loop,
206# and would iterate through available variants from $(1)
207# $(1) - variant list
208# $(2) - optional: one of APP,BL,OS
209# $(3) - path to makefile to include for each variant
210define for-each-variant
211$(if $(AUX_RECURSIVE_VARIANT_LIST),$(if \
212        $(call equal-sets,$(AUX_RECURSIVE_VARIANT_LIST),$(1)),,\
213        $(error $(LOCAL_PATH): Recursive variant list mismatch: "$(AUX_RECURSIVE_VARIANT_LIST)" and "$(1))),\
214    $(call for-each-variant-unchecked,$(1),$(2),$(3)))
215endef
216