xref: /optee_os/mk/subdir.mk (revision fc9ea0db8ddf8150754aac716691616c7e3f404a)
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
27# $1 is 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
53
54define process-subdir-srcs-y
55ifeq ($$(sub-dir),.)
56srcs 				+= $1
57oname				:= $(out-dir)/$(base-prefix)$(basename $1).o
58else
59ifneq ($(filter /%,$(1)),)
60# $1 is an absolute path - start with "/"
61srcs 				+= $1
62oname				:= $(out-dir)/$(base-prefix)$(basename $1).o
63else
64srcs				+= $(sub-dir)/$1
65oname				:= $(out-dir)/$(base-prefix)$(basename $$(sub-dir)/$1).o
66endif
67endif
68$$(eval $$(call process-file-vars,$1,$$(oname)))
69endef #process-subdir-srcs-y
70
71define process-subdir-gensrcs-helper
72# $1 gensrc-y element
73# $2 full path and name of generated source file
74# $3 full path and name of object file compiled from source file
75# $4 full path to out directory
76# $5 y if $2 must be generated before $(sm) starts building (e.g., .h file)
77
78gen-srcs			+= $2
79cleanfiles			+= $2
80oname				:= $3
81
82FORCE-GENSRC$(sm): $(if $(filter y,$5),$2,)
83
84$$(addprefix $4,$$(produce-additional-$1)): $2
85
86subdir-$2 := $$(sub-dir)
87recipe-$2 := $$(recipe-$1)
88$2: $$(depends-$1)
89	@$(cmd-echo-silent) '  GEN     $2'
90	$(q)mkdir -p $4
91	$(q)$$(recipe-$2)
92
93$$(eval $$(call process-file-vars,$1,$$(oname)))
94endef #process-subdir-gensrcs-helper
95
96define process-subdir-gensrcs-y
97$$(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)))
98endef #process-subdir-gensrcs-y
99
100define process-subdir-asm-defines-y
101asm-defines-files += $(sub-dir)/$1
102endef #process-subdir-asm-defines-y
103
104define process-subdir
105sub-dir := $1
106ifeq ($1,.)
107sub-dir-out := $(patsubst %/,%,$(out-dir)/$(base-prefix))
108else
109sub-dir-out := $(out-dir)/$(base-prefix)$1
110endif
111
112include $1/sub.mk
113sub-subdirs := $$(addprefix $1/,$$(subdirs-y)) $$(subdirs_ext-y)
114cppflags$(sm) := $$(cppflags$(sm)) $$(global-cppflags-y)
115incdirs$(sm) := $(incdirs$(sm)) $$(addprefix $1/,$$(global-incdirs-y))
116thissubdir-incdirs := $(out-dir)/$(base-prefix)$1 $$(addprefix $1/,$$(incdirs-y)) $$(incdirs_ext-y)
117ifneq ($$(libname),)
118incdirs-lib$$(libname)-$$(sm) := $$(incdirs-lib$$(libname)-$$(sm)) $$(addprefix $1/,$$(incdirs-lib-y))
119cflags-lib$$(libname)-$$(sm) := $$(cflags-lib$$(libname)-$$(sm)) $$(cflags-lib-y)
120cxxflags-lib$$(libname)-$$(sm) := $$(cxxflags-lib$$(libname)-$$(sm)) $$(cxxflags-lib-y)
121cppflags-lib$$(libname)-$$(sm) := $$(cppflags-lib$$(libname)-$$(sm)) $$(cppflags-lib-y)
122endif
123
124# Process files in current directory
125$$(foreach g, $$(gensrcs-y), $$(eval $$(call process-subdir-gensrcs-y,$$(g))))
126$$(foreach s, $$(srcs-y), $$(eval $$(call process-subdir-srcs-y,$$(s))))
127$$(foreach a, $$(asm-defines-y), $$(eval $$(call process-subdir-asm-defines-y,$$(a))))
128# Clear flags used when processing current directory
129srcs-y :=
130cflags-y :=
131cflags-lib-y :=
132cxxflags-y :=
133cxxflags-lib-y :=
134cppflags-y :=
135cppflags-lib-y :=
136aflags-y :=
137cflags-remove-y :=
138cxxflags-remove-y :=
139aflags-remove-y :=
140subdirs-y :=
141subdirs_ext-y :=
142global-incdirs-y :=
143global-cppflags-y :=
144incdirs-lib-y :=
145incdirs-y :=
146incdirs_ext-y :=
147gensrcs-y :=
148this-out-dir :=
149asm-defines-y :=
150
151# Process subdirectories in current directory
152$$(foreach sd, $$(call uniq,$$(sub-subdirs)), $$(eval $$(call process-subdir,$$(sd))))
153endef #process-subdir
154
155# Top subdirectories
156$(foreach sd, $(call uniq,$(subdirs)), $(eval $(call process-subdir,$(sd))))
157