1# Input
2#
3# subdirs	tells the subdirectories to descend
4#
5# Output
6#
7# set     srcs gen-srcs
8# set     cflags-$(oname) cflags-remove-$(oname)
9#         cxxflags-$(oname) cxxflags-remove-$(oname)
10#         aflags-$(oname) aflags-remove-$(oname)
11#         cppflags-$(oname) cppflags-remove-$(oname)
12#         incdirs-$(oname)
13#         incdirs-lib$(libname)-$(sm)  [if libname is defined]
14#         cppflags-lib$(libname)-$(sm) [if libname is defined]
15#         cflags-lib$(libname)-$(sm)   [if libname is defined]
16#         cxxflags-lib$(libname)-$(sm) [if libname is defined]
17# for each file found, oname is the name of the object file for corresponding
18# source file
19
20srcs :=
21gen-srcs :=
22asm-defines-files :=
23
24define process-subdir-srcs-y
25ifeq ($$(sub-dir),.)
26srcs 				+= $1
27oname				:= $(out-dir)/$(base-prefix)$(basename $1).o
28else
29ifneq ($(filter /%,$(1)),)
30# $1 is an absolute path - start with "/"
31srcs 				+= $1
32oname				:= $(out-dir)/$(base-prefix)$(basename $1).o
33else
34srcs				+= $(sub-dir)/$1
35oname				:= $(out-dir)/$(base-prefix)$(basename $$(sub-dir)/$1).o
36endif
37endif
38cflags-$$(oname) 		:= $$(cflags-y) $$(cflags-$(1)-y)
39cflags-remove-$$(oname) 	:= $$(cflags-remove-y) \
40					$$(cflags-remove-$(1)-y)
41cxxflags-$$(oname) 		:= $$(cxxflags-y) $$(cxxflags-$(1)-y)
42cxxflags-remove-$$(oname) 	:= $$(cxxflags-remove-y) \
43					$$(cxxflags-remove-$(1)-y)
44cppflags-$$(oname) 		:= $$(cppflags-y) $$(cppflags-$(1)-y)
45cppflags-remove-$$(oname) 	:= $$(cppflags-remove-y) \
46					$$(cppflags-remove-$(1)-y)
47aflags-$$(oname) 		:= $$(aflags-y) $$(aflags-$(1)-y)
48aflags-remove-$$(oname) 	:= $$(aflags-remove-y) \
49					$$(aflags-remove-$(1)-y)
50incdirs-$$(oname)		:= $$(thissubdir-incdirs) $$(addprefix $(sub-dir)/,$$(incdirs-$(1)-y))
51# Clear local filename specific variables to avoid accidental reuse
52# in another subdirectory
53cflags-$(1)-y 			:=
54cflags-remove-$(1)-y		:=
55cflags-lib-y			:=
56cxxflags-$(1)-y 		:=
57cxxflags-remove-$(1)-y		:=
58cxxflags-lib-y			:=
59cppflags-$(1)-y			:=
60cppflags-remove-$(1)-y		:=
61cppflags-lib-y			:=
62aflags-$(1)-y 			:=
63aflags-remove-$(1)-y		:=
64incdirs-$(1)-y			:=
65fname				:=
66oname				:=
67endef #process-subdir-srcs-y
68
69define process-subdir-gensrcs-helper
70# $1 gensrc-y element
71# $2 full path and name of generated source file
72# $3 full path and name of object file compiled from source file
73# $4 full path to out directory
74# $5 y if $2 must be generated before $(sm) starts building (e.g., .h file)
75
76gen-srcs			+= $2
77cleanfiles			+= $2
78oname				:= $3
79
80FORCE-GENSRC$(sm): $(if $(filter y,$5),$2,)
81
82$$(addprefix $4,$$(produce-additional-$1)): $2
83
84subdir-$2 := $$(sub-dir)
85recipe-$2 := $$(recipe-$1)
86$2: $$(depends-$1)
87	@$(cmd-echo-silent) '  GEN     $2'
88	$(q)mkdir -p $4
89	$(q)$$(recipe-$2)
90
91cflags-$$(oname) 		:= $$(cflags-y) $$(cflags-$(1)-y)
92cflags-remove-$$(oname) 	:= $$(cflags-remove-y) \
93					$$(cflags-remove-$(1)-y)
94cxxflags-$$(oname) 		:= $$(cxxflags-y) $$(cxxflags-$(1)-y)
95cxxflags-remove-$$(oname) 	:= $$(cxxflags-remove-y) \
96					$$(cxxflags-remove-$(1)-y)
97cppflags-$$(oname) 		:= $$(cppflags-y) $$(cppflags-$(1)-y)
98cppflags-remove-$$(oname) 	:= $$(cppflags-remove-y) \
99					$$(cppflags-remove-$(1)-y)
100aflags-$$(oname) 		:= $$(aflags-y) $$(aflags-$(1)-y)
101aflags-remove-$$(oname) 	:= $$(aflags-remove-y) \
102					$$(aflags-remove-$(1)-y)
103incdirs-$$(oname)		:= $$(thissubdir-incdirs) $$(addprefix $(sub-dir)/,$$(incdirs-$(1)-y))
104# Clear local filename specific variables to avoid accidental reuse
105# in another subdirectory
106cflags-$(1)-y 			:=
107cflags-remove-$(1)-y		:=
108cflags-lib-y			:=
109cxxflags-$(1)-y 			:=
110cxxflags-remove-$(1)-y		:=
111cxxflags-lib-y			:=
112cppflags-$(1)-y			:=
113cppflags-remove-$(1)-y		:=
114cppflags-lib-y			:=
115aflags-$(1)-y 			:=
116aflags-remove-$(1)-y		:=
117incdirs-$(1)-y			:=
118fname				:=
119oname				:=
120
121endef #process-subdir-gensrcs-helper
122
123define process-subdir-gensrcs-y
124$$(eval $$(call process-subdir-gensrcs-helper,$1,$(sub-dir-out)/$$(produce-$1),$(sub-dir-out)/$(basename $(produce-$1)).o,$(sub-dir-out),$(force-gensrc-$1)))
125endef #process-subdir-gensrcs-y
126
127define process-subdir-asm-defines-y
128asm-defines-files += $(sub-dir)/$1
129endef #process-subdir-asm-defines-y
130
131define process-subdir
132sub-dir := $1
133ifeq ($1,.)
134sub-dir-out := $(patsubst %/,%,$(out-dir)/$(base-prefix))
135else
136sub-dir-out := $(out-dir)/$(base-prefix)$1
137endif
138
139include $1/sub.mk
140sub-subdirs := $$(addprefix $1/,$$(subdirs-y)) $$(subdirs_ext-y)
141incdirs$(sm) := $(incdirs$(sm)) $$(addprefix $1/,$$(global-incdirs-y))
142thissubdir-incdirs := $(out-dir)/$(base-prefix)$1 $$(addprefix $1/,$$(incdirs-y)) $$(incdirs_ext-y)
143ifneq ($$(libname),)
144incdirs-lib$$(libname)-$$(sm) := $$(incdirs-lib$$(libname)-$$(sm)) $$(addprefix $1/,$$(incdirs-lib-y))
145cflags-lib$$(libname)-$$(sm) := $$(cflags-lib$$(libname)-$$(sm)) $$(cflags-lib-y)
146cxxflags-lib$$(libname)-$$(sm) := $$(cxxflags-lib$$(libname)-$$(sm)) $$(cxxflags-lib-y)
147cppflags-lib$$(libname)-$$(sm) := $$(cppflags-lib$$(libname)-$$(sm)) $$(cppflags-lib-y)
148endif
149
150# Process files in current directory
151$$(foreach g, $$(gensrcs-y), $$(eval $$(call process-subdir-gensrcs-y,$$(g))))
152$$(foreach s, $$(srcs-y), $$(eval $$(call process-subdir-srcs-y,$$(s))))
153$$(foreach a, $$(asm-defines-y), $$(eval $$(call process-subdir-asm-defines-y,$$(a))))
154# Clear flags used when processing current directory
155srcs-y :=
156cflags-y :=
157cflags-lib-y :=
158cxxflags-y :=
159cxxflags-lib-y :=
160cppflags-y :=
161cppflags-lib-y :=
162aflags-y :=
163cflags-remove-y :=
164cxxflags-remove-y :=
165aflags-remove-y :=
166subdirs-y :=
167subdirs_ext-y :=
168global-incdirs-y :=
169incdirs-lib-y :=
170incdirs-y :=
171incdirs_ext-y :=
172gensrcs-y :=
173this-out-dir :=
174asm-defines-y :=
175
176# Process subdirectories in current directory
177$$(foreach sd, $$(sub-subdirs), $$(eval $$(call process-subdir,$$(sd))))
178endef #process-subdir
179
180# Top subdirectories
181$(foreach sd, $(subdirs), $(eval $(call process-subdir,$(sd))))
182