-$(eval obj := $1)
-$(eval nested-vars := $2)
-$(foreach v,$(nested-vars),$(call fix-obj-vars,$v,$(if $(obj),$(obj)/)))
-$(eval old-nested-dirs := )
-$(call unnest-vars-1)
-$(if $1,$(foreach v,$(nested-vars),$(eval \
- $v := $(addprefix $1/,$($v)))))
-$(foreach var,$(nested-vars),$(eval $(var) := $(filter-out %/, $($(var)))))
-$(shell mkdir -p $(sort $(foreach var,$(nested-vars),$(dir $($(var))))))
-$(foreach var,$(nested-vars), $(eval \
- -include $(addsuffix *.d, $(sort $(dir $($(var)))))))
-$(foreach v,$(filter %-m,$(nested-vars)), \
- $(call process-modules,$v))
+ # In the case of target build (i.e. $1 == ..), fix path for top level
+ # Makefile.objs objects
+ $(if $1,$(call fix-paths,$1/,,$2))
+
+ # Descend and include every subdir Makefile.objs
+ $(foreach v, $2,
+ $(call unnest-var-recursive,$1,$2,$v)
+ # Pass the .mo-cflags and .mo-libs along to its member objects
+ $(foreach o, $(filter %.mo,$($v)),
+ $(foreach p,$($o-objs),
+ $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
+ $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
+
+ # For all %.mo objects that are directly added into -y, just expand them
+ $(foreach v,$(filter %-y,$2),
+ $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
+
+ $(foreach v,$(filter %-m,$2),
+ # All .o found in *-m variables are single object modules, create .mo
+ # for them
+ $(foreach o,$(filter %.o,$($v)),
+ $(eval $(o:%.o=%.mo)-objs := $o))
+ # Now unify .o in -m variable to .mo
+ $(eval $v := $($v:%.o=%.mo))
+ $(eval modules-m += $($v))
+
+ # For module build, build shared libraries during "make modules"
+ # For non-module build, add -m to -y
+ $(if $(CONFIG_MODULES),
+ $(foreach o,$($v),
+ $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
+ $(eval $o: $($o-objs)))
+ $(eval $(patsubst %-m,%-y,$v) += $($v))
+ $(eval modules: $($v:%.mo=%$(DSOSUF))),
+ $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
+
+ # Post-process all the unnested vars
+ $(foreach v,$2,
+ $(foreach o, $(filter %.mo,$($v)),
+ # Find all the .mo objects in variables and add dependency rules
+ # according to .mo-objs. Report error if not set
+ $(if $($o-objs),
+ $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
+ $(error $o added in $v but $o-objs is not set)))
+ $(shell mkdir -p ./ $(sort $(dir $($v))))
+ # Include all the .d files
+ $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
+ $(eval $v := $(filter-out %/,$($v))))