In a makefile, I define a variable using the define
directive. This variable will hold a configurable list of commands that I want to execute.
I would like this variable to get a list of files (.foo
files, for example). These files are created during the makefile execution. For example makefile:
MY_VAR = $(wildcard *.foo)
define MY_VAR2
echo $(1) $(MY_VAR)
endef
foo: create_files
$(call MY_VAR2, ls)
rm -f *.foo
create_files:
touch foo.foo
touch bar.foo
I do not get the desired results. It appears that MY_VAR2
is evaluated upon declaration.
Is there a way to get the desired behavior?
edit:
The $(shell)
command, as sateesh correctly pointed out, works for the example above. However, it does not work for the example below. The main difference in this example is that the new files are created inside MY_VAR2
.
MY_VAR = $(wildcard *.foo)
TEST_VAR = $(shell ls *.foo)
define MY_VAR2
@touch foo.foo
@touch bar.foo
@echo "MY_VAR" $(1) $(MY_VAR)
@echo "TEST_VAR" $(1) $(TEST_VAR)
endef
foo:
$(call MY_VAR2, ls)
@rm -f *.foo
I can solve the above by adding rules. Is there a simpler method?