Linux
-makefile
by Kevin Wu 2014/11/21
1
0
Agenda
2
 用途
 基本語法
 Athena應用
 練習
Makefile
3
 自動化編譯
 寫自動化的指令 (安裝、壓縮...)
 適用不同的編譯器make工具
 Delphi的make,Visual C++的nmake,Linux下GNU的make
Makefile
4
 Machine-readable
 Workflow
 any dependent files are regenerated with minimal effort.
 Testable
 Syntax:
 targetfile: sourcefile
command
Example - makefile
5
 # comments
 #一些變數宣告

 target1: dependencies of target1
 <TAB>command;
 target2: dependencies of target2
 <TAB>command;
 ...
 ...
 clean:
 <TAB>rm -rf *.o
Example - make
6
 make
 make [-f makefile|Makefile]
 make clean
 make foo=bar target
 makeARGS="asdf" run
 recall how we install tmux :
wget http://downloads.sourceforge.net/tmux/tmux-1.9a.tar.gz
tar -xvzf tmux-1.9a.tar.gz
cd tmux-1.9a
./configure
make
make install
Scenario
7
 Download a zip archive from the Census Bureau.
 Extract the shapefile from the archive.
 Convert the shapefile toTopoJSON.
Scenario
8
 counties.zip:
curl -o counties.zip‘http:url/file.zip‘
 counties.shp: counties.zip
unzip counties.zip
touch counties.shp
 counties.json: counties.shp
topojson -o counties.json -- counties=counties.shp
makefile vs shell
9
 變數與變數內容以『=』隔開,同時兩邊可以具有空格;
 為了Makefile的可移植性,建議使用具有空格的表示方法
 變數左邊不可以有 <tab>
 變數與變數內容在『=』兩邊不能具有『:』;
 運用變數時,以 ${變數} 或 $(變數) 使用;
 parentheses seems better than curly brackets
 在指令列模式也可以給予變數。
 變數值決定
 1st make 指令列後面加上的環境變數為優先;
 2nd makefile 裡面指定的環境變數;
 3rd shell 原本具有的環境變數。
 makefile only:
 $?:代表需要重建(被修改)的相依性項目。
 $@:目前的目標項目名稱。
 $<:代表目前的相依性項目。
 $* :代表目前的相依性項目,不過不含副檔名。
 - :make 會忽略命令的錯誤
 @ :make 不會在標準輸出上,顯示要執行的命令。
Athena應用
10
 系統安裝
 Install Python2.7.8
 Build Symbolic Link
 Yum Setting
 Install pip
 Install the third-party lib for Python2.7.8
 Install the necessary library for tmux
 Install the tmux
 Copy the SSH key from remote athena and pandora
 Create migo folder under /data
 Luigi Job檢核
 執行前檢查
 檢查requires檔案是否存在
 刪除中繼檔案
 刪除output檔案
 執行後檢查
 中繼檔案是否存在
 output檔案是否存在
 刪除中繼檔案
Case (See Problem?)
11
 test: gen-a gen-p
@echo "$@ done“
rm *.o
rm *.pip
 gen-a:
rm $@.o
touch $@.o
 gen-p: a.pip b.pip
 @echo "$@ done"
 %.pip: gen-a.o
 touch $@
 echo "$@ done“
 [hint] : rm -> -rm, rm -> rm –f, gen-a -> install
Solved
12
 rm *.o
• -rm *.o
• -rm –f *.o
• don’t let it parallel go
*Example - Advanced
13
 #usage:make main OR make
 SHELL = /usr/bin/bash #宣告command所使用的shell環境為bash
 CC = gcc #欲使用的C compiler
 CFLAGS = -O3 -ansi #欲使用的參數
 INC = -I /usr/foo/include
 LIB = -L /usr/foo/lib

 .SUFFIXS: .c .cpp .f77 .f #加入所列副檔名到隱含規則裡
 main: main.o foo1.o foo2.o
 ${CC} main.o foo1.o ${CFLAGS} ${INC} ${LIB} -o $@
 %.o: %.c target.h
 ${CC} $< ${CFLAGS} ${INC} ${LIB} -lpthread -c
 .PHONY: clean
 clean:
 @rm -rf *.o
Practice
14
 建立一個makefile, 至少包含
 build : 可touch 出a.out b.out c.out, 若檔案都存在則echo出”build done”
 clean : 刪除所有.out檔
 rebuild : 會clean再build, 若檔案都存在則echo出”rebuild done”
 相依性條件:
 a.out為主要任務(檔案),需要 b.out存在才能產出
 b.out 需要 c.out存在才能產出
 c.out不須任何相依檔案,
 測試結果是否正常
 make rebuild
 rm any .out file (you choose)
 make build, 自動讓缺少的檔案產生出來
 提示
 將a/b/c分開寫各自的dependency與command
 使用$@變數
Answer
15
 clean:
 -rm -f *.out
 a.out: | b.out
 touch a.out
 b.out: | c.out
 touch b.out
 c.out:
 touch c.out
 build: a.out
 @echo $@ done
 rebuild: clean build
 @echo $@ done
Thank you
16