In gnu make, is there a way to get hold of the original target that initiated the whole chain and lead the execution to the current recipe?
.PHONY : all clean common
all: common
clean: common
common:
@echo $@
@echo $(MAKECMDGOALS)
@for a in $$(ls); do \
if [ -d $$a ]; then \
echo -C $$a $(MAKECMDGOALS); \
#$(MAKE) -C $$a $(MAKECMDGOALS); \
fi; \
done;
@echo "Done!"
As in the above example, when I run the 'make all' or 'make clean', I can use $(MAKECMDGOALS) to tell which target was passed to make from the command line. However, if someone calls it as 'make clean all', then 'common' is called once and we get 'make: Nothing to be done for `clean'.'.
So, is there a way to call 'common' for every time it is referenced as prerequisite for a target and also work out the originating target name so it can be passed into the next make? Even though 'common' is a PHONY target, it is only called once and I am not sure why.
I've got below alternatives but and I think the first one is the neatest
makefile 1
just_a_func = @echo "just_a_func from " $(1)
.PHONY : all clean
all:
@echo $@ " enter"
$(call just_a_func, $@)
clean:
@echo $@ " enter"
$(call just_a_func, $@)
makefile 2
.PHONY : all clean common
all:
@echo $@ " enter"
$(MAKE) -f makefile common
clean:
@echo $@ " enter"
$(MAKE) -f makefile common
common:
@echo $@ " enter"