简体   繁体   中英

Makefile PHONY targets fail using test command

The following simple PHONY target for Makefile is failing:

dir=/etc
file=fstab
install:
        -test ! -f $(dir)/$(file)

Error displayed:

make install
-I/root/module
test ! -f /etc/fstab
make: [install] Error 1 (ignored)

If I remove the ! symbol from the condition, there is no issue. Can someone help me to sort out this issue? I hope this is a bug in bash/Makefile targets.

Your recipe:

-test ! -f $(dir)/$(file)

will succeed (return 0) if $(dir)/$(file) does not exist and fail (return non-zero) if $(dir)/$(file) exists. Because of the - prefix, make will ignore failure, will by default report that it is ignoring failure, and will carry on instead of stopping.

Look:

Makefile

dir=/etc
file=fstab

.PHONY: all install

all: install
    echo "Success"

install:
    -test ! -f $(dir)/$(file)

Run it:

$ make
test ! -f /etc/fstab
Makefile:8: recipe for target 'install' failed
make: [install] Error 1 (ignored)
echo "Success"
Success

bash is not broken. make is not broken. There is nothing wrong.

If you do not even wish to see make 's progess and diagnostic output then you can silence them:

$ make --silent
Success

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM