Hi. Is there a good way to set a specific variable 'BUILDING_FOO' while Make is building the target 'foo' including its prerequisites?
Let's say we have two targets 'vmlinux' and 'modules', but the actual build rules are factored out into '__common'. [Not-working code] ------------------>8------------------------- .PHONY: vmlinux modules __common vmlinux: export BUILDING_VMLINUX=y modules: export BUILDING_MODULES=y vmlinux modules: __common @: __common: @echo BUILDING_VMLINUX is $(BUILDING_VMLINUX) @echo BUILDING_MODULES is $(BUILDING_MODULES) [ common build rules, which internally uses BUILDING_{VMLINUX,MODULES} conditionals ] ------------------>8------------------------- My hope is: - 'make vmlinux' builds __common with BUILDING_VMLINUX=y - 'make modules' builds __common with BUILDING_MODULES=y - 'make vmlinux modules' builds __common with BUILDING_VMLINUX=y and BUILDING_MODULES=y I know the example above does not work like that. [Working code] ------------------>8------------------------- .PHONY: vmlinux modules __common ifneq ($(filter vmlinux,$(MAKECMDGOALS)),) export BUILDING_VMLINUX=y endif ifneq ($(filter modules,$(MAKECMDGOALS)),) export BUILDING_MODULES=y endif vmlinux modules: __common @: __common: @echo BUILDING_VMLINUX is $(BUILDING_VMLINUX) @echo BUILDING_MODULES is $(BUILDING_MODULES) [ common build rules, which internally uses BUILDING_{VMLINUX,MODULES} conditionals ] ------------------>8------------------------- 'make foo bar' builds __common, setting BUILDING_VMLINUX and BUILDING_MODULES, but we need to add more filtering code to handle 'make', 'make all', and other targets that depend on 'vmlinux' and 'modules'. The Linux kernel build system actually does this: https://github.com/torvalds/linux/blob/v5.12/Makefile#L633 https://github.com/torvalds/linux/blob/v5.12/Makefile#L637 but, the filtering pattern tends to grow uglily if we try to cover all the possible targets. Is there any way to do this better? -- Best Regards Masahiro Yamada