recursive clean in a Makefile

makefile, recursion, shell

Solution

Read the for loop again:

for d in [...]; do make --directory=$f

Didn't you mean `$d`, as in

for d in [...]; do make --directory=$d

So, the Makefile should look:

for d in common/gcas_debug common/gcas_nvdata; \
do \
    make --directory=$d clean; \
done

Problem

I'm trying to create a Makefile which can build/clean recursively when it's called. I have the build working, but I'm getting errors with the clean command. My directory structure is something like: ``` main | +-- Makefile | +-- common | | | +-- gcas_debug | | | | | +-- Makefile | + -- gcas_nvdata | | | +-- Makefile +-- gcinit +-- Makefile ``` When I call `make` in the main directory it goes through and builds everything as I desire, but when I call `make clean` it does with: ``` mike@mike-VirtualBox:~/iCOM/framework$ make clean for d in common/gcas_debug common/gcas_nvdata; \ do \ make --directory=$f clean; \ done make: the `-C' option requires a non-empty string argument Usage: make [options] [target] ... Options: -b, -m Ignored for compatibility. ... ``` I don't understand why the clean command isn't working... Any suggestions? Full (main) Makefile: ``` lib_gcas_debug := common/gcas_debug lib_gcas_nvdata := common/gcas_nvdata libraries := $(lib_gcas_debug) $(lib_gcas_nvdata) DESTDIR=$(PWD)/output bindir= gc_init := gcinit EXE := $(gc_init)/$(gc_init) .PHONY: all $(gc_init) $(libraries) all: $(gc_init) $(gc_init) $(libraries): $(MAKE) --directory=$@ $(gc_init): $(libraries) install: $(gc_init) install -d -m 0755 $(DESTDIR)$(bindir) install -m 0755 $(EXE) $(DESTDIR)$(bindir) clean: for d in $(libraries); \ do \ $(MAKE) --directory=$$f clean; \ done ``` EDIT: If I swap the `for d` with `for $d` I get instead: ``` mike@mike-VirtualBox:~/iCOM/framework$ make clean for in common/gcas_debug common/gcas_nvdata; \ do \ make --directory= clean; \ done /bin/sh: -c: line 0: syntax error near unexpected token `common/gcas_debug' /bin/sh: -c: line 0: `for in common/gcas_debug common/gcas_nvdata; \' make: *** [clean] Error 1 ```

Original source