Makefile defines: recursive extension question

In the makefile, I define the variable using the directive define. This variable will contain a custom list of commands that I want to execute.

I would like this variable to get a list of files (e.g. files .foo). These files are created at runtime of the makefile. 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 to be MY_VAR2priced after the announcement.

Is there a way to get the desired behavior?


change

The command $(shell), as indicated by sateesh , works for the example above. However, this does not work for the example below. The main difference in this example is that new files are created internally 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 

, . ?

+3
6

, make, script make.

script, script. , , .

touch foo.foo
touch bar.foo
your-command `ls *.foo`

, make, , define, .

foo: create_files
    your-command $(wildcard *.foo)
    rm -f *.foo

create_files:
    touch foo.foo
    touch bar.foo
+2

, GNU Make. , "shell", GNU Make.

make, , :

MY_VAR = $(wildcard *.foo) 
TEST_VAR = $(shell ls *.foo)

define MY_VAR2 
    @echo "MY_VAR" $(1) $(MY_VAR)
    @echo "TEST_VAR" $(1) $(TEST_VAR)
endef 

foo: create_files 
    $(call MY_VAR2, ls) 
    @rm -f *.foo 

create_files: 
    @touch foo.foo 
    @touch bar.foo

make "foo" :

MY_VAR ls
TEST_VAR ls bar.foo foo.foo

shell .

GNU:

shell , backquotes ('') shells: . , . ( / ) .
...
, , . ... files: = $(shell echo.c) .c. make , , $$ (wildcard *.c) ( .c).

, (= form) shell, .

+2

4.4.3

. , . ,

eval.

, .

:

+1

? .

0

include?
, make .
-, , ,
.

define MY_VAR2
    echo $(1) $(MY_VAR)
    $(1) $(MY_VAR)
endef

-include .listoffiles

foo: create_files
    $(call MY_VAR2, ls -al)
    rm -f *.foo .listoffiles

create_files: .listoffiles

.listoffiles:
    touch foo.foo
    touch bar.foo
    @echo "MY_VAR = \$$(wildcard *.foo)" > $@

, , .
, . .

% make -f test.mk
touch foo.foo
touch bar.foo
echo  ls -al bar.foo foo.foo
ls -al bar.foo foo.foo
ls -al bar.foo foo.foo
-rw-rw-r-- 1 yo-hei clearusers 0 Jan  8 08:44 bar.foo
-rw-rw-r-- 1 yo-hei clearusers 0 Jan  8 08:44 foo.foo
rm -f *.foo .listoffiles
0

, make?

MY_VAR = $$(ls *.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

double-$ . , , .

:

MY_VAR = $(wildcard *.foo) 
TEST_VAR = $$(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
0
source

Source: https://habr.com/ru/post/1727135/


All Articles