Reputation: 139
Assume that I have a target called install, which builds binaries from the whole code base.
And within the makefile I have another target called foo.src, which generates a source file "foo.src" with environmental variables, and it is needed for install.
A third target called test, which will run a bunch of tests.
So apparently test depends on install, install depends on foo.src.
I am trying to avoid regenerating file foo.src every time I build test, as foo.src is prerequisite of a lot of targets, but I still want to make sure it gets regenerated if I am directly building install.
what I've got so far:
BUILD_INSTALL =
install: BUILD_INSTALL = dummy
install: foo.src
@echo "INSTALL!!!"
@echo BUILD_INSTALL=$(BUILD_INSTALL)
touch install
foo.src: $(BUILD_INSTALL)
@echo "foo.src!!"
@echo BUILD_INSTALL=$(BUILD_INSTALL)
touch foo.src
test: install
@echo "TEST!!!"
.PHONY: test dummy
dummy:
From what I can see, repeating building install does not regenerate foo.src. $(BUILD_INSTALL) does not expand to dummy as a prerequisite of foo.src when I am building install a second time.
Upvotes: 0
Views: 62
Reputation: 100781
Your attempt fails because of this, from the GNU make manual:
As with automatic variables, these values are only available within the
context of a target’s recipe (and in other target-specific assignments)
You are trying to use the variable in a prerequisite list, which is not part of the target's recipe.
One way to do what you want is to declare foo.src
phony if (and only if) install
is given on the command line:
install: foo.src
@echo "INSTALL!!!"
@echo BUILD_INSTALL=$(BUILD_INSTALL)
touch install
foo.src:
@echo "foo.src!!"
@echo BUILD_INSTALL=$(BUILD_INSTALL)
touch foo.src
test: install
@echo "TEST!!!"
ifeq (install,$(filter install,$(MAKECMDGOALS)))
.PHONY: foo.src
endif
Upvotes: 1