1

The command "make clean" causes this simple Makefile to go into an infinite loop, spawning process after process, if the directory m1 does not exist:

clean-1:
        cd m1; make clean

clean: clean-1

I guess I'm doing something wrong with recursive make. Can anyone explain why this happens, and the best way to prevent it, just in case some user has decided they didn't need directory m1?

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115

1 Answers1

4

What you want is basically call make clean in the subdirectory m1.
But if m1 is not a directory, you don't want make to do anything at all?

If yes, this is for you:

clean-1:
    cd m1 && make clean

clean: clean-1

There is one thing to keep in mind with this solution: If there is no directory m1, the make-command will exit with a non-zero status-code.

╭─ /tmp
╰─❯ cat Makefile   

clean-1:
    cd m1 && make clean

clean: clean-1

╭─ /tmp
╰─❯ make clean

cd m1 && make clean
/bin/sh: line 0: cd: m1: No such file or directory
make: *** [Makefile:2: clean-1] Error 1

Hermsi1337
  • 241
  • 2
  • 9
  • You don't need to use `[ -d m1 ]`. You can just try the `cd` directly and it will fail if `m1` is not a valid directory. The nice thing about that is that you'll actually get an error message from `cd` which you can use to understand what happened, rather than simply a message like `exit 1` when the test fails. – MadScientist Apr 03 '20 at 03:55
  • And you're absolutely right. I've updated my answer. – Hermsi1337 Apr 03 '20 at 11:33