makefile - One child process per for loop in make? -
let me first write quick makefile showcase:
#!/bin/make -f folders := $(shell find -mindepth 1 -maxdepth 1 -type d -print) make_dir: @mkdir -p "test0" pwd_test: @cd "test0" && pwd @pwd pwd_all: @for f in $(folders); \ cd "$${f}" && pwd; \ pwd; \ cd ..; \ done
first make make_dir
, see different results:
➜ make pwd_test /data/cache/tmp/so/test0 /data/cache/tmp/so ➜ make pwd_all /data/cache/tmp/so/test0 /data/cache/tmp/so/test0
you see in loop necessary cd ..
. apparently, there no child process spawn cd x && pwd
command, while case. behaviour specific make or specific shell?
make spawns new process each command in rule. since loop 1 command 1 process.
take @ recipe execution
edit:
each line in makefile gets own subshell. commands have \
tells make next line should part of current line.
the reason loop own subshell because make see line
@for f in $(folders); cd "$${f}" && pwd; pwd; cd ..; done
madscientist explains well. command can type in shell in 1 line executed make in 1 subshell or process.
if run in ksh, ksh passed for f in $(folders); cd "$${f}" && pwd; pwd; cd ..; done
, run in 1 subshell. if ksh did not have loop implemented error , make command returned error code.
explanation of pwd_test
pwd_test: @cd "test0" && pwd @pwd
@cd "test0" && pwd
seen 1 line subshell updates current working directory , prints out current working is.
@pwd
@ line make spawns new subshell contains old working directory (or directory make called form) , pwd
prints directory.
Comments
Post a Comment