記錄makefile一些小地方
更新記錄
item | note |
---|---|
20160917 | 第一版 |
20170208 | 了解makefile錯誤停止 |
目錄
結論
make預設若遇到錯誤(即make fail 或shell status($?))就會停止
若使用pushd或if(等一連串的shell 指令,使得status被最後指令覆蓋)
因此抓不到錯誤的情況,一直執行到最後
在使用pushd或if等裡面的程序需要自已判斷,若遇到錯誤需要停止
了解makefile錯誤停止
來源:Testing the Compilation of a Program
Normally, when an error happens in executing a shell command, make gives up immediately, returning a nonzero status.
由上面可以知預設make是遇到錯誤就會停止
但若遇到下列例子(return code被其寫指令變成0),這時make得到就不是fail,因此就不會停止
測試make fail
- make fail
由於沒有存在/root/file-1,因此停止(即沒有打印t22)1
2
3
4
5
6
7
8
9root@1cc5d8f59283:[~]$ cat Makefile
all:
@echo "t11"
@ls -l /root/file-1
@echo "t22"
root@1cc5d8f59283:[~]$ make
t11
ls: cannot access /root/file-1: No such file or directory
make: *** [all] Error 2
makefile 使用pushd及popd
錯誤抓不到
即回傳到make的status被popd取代
因此若為fail,則還是沒有被抓到(即有打印t22)1
2
3
4
5
6
7
8
9
10
11
12root@1cc5d8f59283:[~]$ cat Makefile
all:
@echo "t11"
@pushd /root; ls -l file-1;popd
@echo "t22"
root@1cc5d8f59283:[~]$ make
t11
~ ~
ls: cannot access file-1: No such file or directory
~
t22修正: 錯誤抓不到
增加判斷錯誤之後離開1
2
3
4
5
6
7
8
9
10
11root@1cc5d8f59283:[~]$ cat Makefile
all:
@echo "t11"
@pushd /root; ls -l file-1 || exit;popd
@echo "t22"
root@1cc5d8f59283:[~]$ make
t11
~ ~
ls: cannot access file-1: No such file or directory
make: *** [all] Error 2
makefiel 使用 if
錯誤抓的到
if最後指令 有產生的錯誤,因此抓的到1
2
3
4
5
6
7
8
9
10
11
12root@1cc5d8f59283:[~]$ cat Makefile
all:
@echo "t11"
@if [ -d /root ]; then \
ls -l /root/file-1; \
fi
@echo "t22"
root@1cc5d8f59283:[~]$ make
t11
ls: cannot access /root/file-1: No such file or directory
make: *** [all] Error 2錯誤抓不到
增加判斷錯誤之後離開
if最後指令 沒有產生的錯誤,因此不抓到1
2
3
4
5
6
7
8
9
10
11
12
13
14root@1cc5d8f59283:[~]$ cat Makefile
all:
@echo "t11"
@if [ -d /root ]; then \
ls -l /root/file-1; \
ls -d /root; \
fi
@echo "t22"
root@1cc5d8f59283:[~]$ make
t11
ls: cannot access /root/file-1: No such file or directory
/root
t22修正: 錯誤抓不到
1
2
3
4
5
6
7
8
9
10
11
12
13root@1cc5d8f59283:[~]$ cat Makefile
all:
@echo "t11"
@if [ -d /root ]; then \
ls -l /root/file-1 || exit; \
ls -d /root; \
fi
@echo "t22"
root@1cc5d8f59283:[~]$ make
t11
ls: cannot access /root/file-1: No such file or directory
make: *** [all] Error 2
make status
來源:[How to Run make]
The exit status of make is always one of three values:
0
The exit status is zero if make is successful.2
The exit status is two if make encounters any errors. It will print messages describing the particular errors.1
The exit status is one if you use the ‘-q’ flag and make determines that some target is not already up to date. See Instead of Executing Recipes.
Makefile
如何停止當gcc fail
- 當make nvr時gcc fail,後的.c檔是不會再compile
因為makefile預設,為遇到fail則停止,若想不停止則需要使用make -k 但此時還會執行make -C ipserver及後續的其它操作
1
2
3
4
5
6all:
make -C $(GIT_TOP_DIR)/tools/host chk_host_rootfs
make clean
pushd nvr; make ; popd
make -C ipserver
make release改成下例,則不會有後續的其它操作(如make -C ipserver)
1
2
3
4
5
6all:
make -C $(GIT_TOP_DIR)/tools/host chk_host_rootfs
make clean
pushd nvr; make || exit; popd
make -C ipserver || exit
make release