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