---
# System prepended metadata

title: 語法 - makefile

---

1.常用的賦值運算符
===

1.1.「=」運算符用法
---
- make 會將整個 makefile 展開後，才決定變數的值。也就是說，==變數的值==會是整個 makefile 中==最後被指定的值==
```makefile
user /home > ls
makefile
user /home > vim makefile

x = hello
y = $(x) world!
x = hi

all:
    @echo $(y)

user /home > make all
hi world!
```

1.2.「:=」運算符用法
---
- ==最常用==。==變數的值==在 makefile ==展開途中就會被給定==，而不是整個 makefile 展開後的最终值
```makefile
user /home > ls
makefile
user /home > vim makefile

x := hello
y := $(x) world!
x := hi

all:
    @echo $(y)

user /home > make all
hello world!
```

1.3.「+=」運算符用法
---
- ==較常用==。將值==附加到變數的後面==
```makefile
user /home > ls
makefile
user /home > vim makefile

x = hello
y = $(x) world!
x += hi

all:
    @echo $(y)

user /home > make all
hello hi world!
```

2.常用的萬用字元函數
===

2.1.wildcard 用法
---
- ==較常用==。==獲取檔案列表==
```makefile
user /home > ls
a.c b.c makefile sub
user /home > ls ./home/sub
sa.c sb.c
user /home > vim makefile

src0 = $(wildcard *.c)
src1 = $(wildcard *.c ./sub/*.c)

all:
    @echo $(src0)
    @echo $(src1)

user /home > make all
a.c b.c
a.c b.c ./sub/sa.c ./sub/sb.c
```

2.2.notdir 用法
---
- ==去除路徑==
```makefile
user /home > ls
a.c b.c makefile sub
user /home > ls ./home/sub
sa.c sb.c
user /home > vim makefile

src = $(wildcard *.c ./sub/*.c)
dir = $(notdir $(src))

all:
    @echo $(dir)

user /home > make all
a.c b.c sa.c sb.c
```

2.3.patsubst 用法
---
- ==替換字元==
```makefile
user /home > ls
a.c b.c makefile sub
user /home > ls ./home/sub
sa.c sb.c
user /home > vim makefile

src = $(wildcard *.c ./sub/*.c)
dir = $(notdir $(src))
obj = $(patsubst %.c, %.o, $(dir))

all:
    @echo $(obj)

user /home > make all
a.o b.o sa.o sb.o
```

2.4.filter-out 用法
---
- ==濾掉字元==
```makefile
user /home > ls
makefile
user /home > vim makefile

src = main1.o foo.o main2.o bar.o
filter = main1.o main2.o
dst = $(filter-out $(filter), $(src))

all:
    @echo $(dst)

user /home > make all
foo.o bar.o
```

3.sed command
===

3.1. sed -i 's/before_str/after_str/g' ./your_file
---
- ==替換字串==。而碰到斜線時，前面要多加反斜線
```makefile
user /home > ls
a.txt
user /home > vim a.txt

1. 123
2. 456
3. 789

user /home > sed -i 's/123/456/g' ./a.txt
user /home > vim a.txt

1. 456
2. 456
3. 789

user /home > sed -i 's/789/\/\/\//g' ./a.txt
user /home > vim a.txt

1. 456
2. 456
3. ///
```
3.2. sed -i 'a append_str' ./your_file
---
- 對==指定行數==來==增添字串==。而碰到斜線時，前面要多加反斜線
```makefile
user /home > ls
a.txt
user /home > vim a.txt

1. 123
2. 456
3. 789

user /home > sed -i '1a 321' ./a.txt
user /home > vim a.txt

1. 123
2. 321
3. 456
4. 789

user /home > sed -i 'a \/\/\/' ./a.txt
user /home > vim a.txt

1. 123
2. 321
3. 456
4. 789
5. ///
```
3.3. sed -i "/object_str/a\\append_str" ./your_file
---
- 對==指定字串==來==增添字串==。而碰到斜線時，前面要多加反斜線
```makefile
user /home > ls
dff.v
user /home > vim dff.v

1. DFF_SYNC U_DFF(
2.     .CLK (CLK),
3.     .D   (D),
4.     .Q   (Q));

user /home > sed -i 's/SYNC/ASYNC/g' ./dff.v
user /home > sed -i "/DFF_SYNC U_DFF(/a\\    .RST (RST)," ./dff.v
user /home > vim dff.v

1. DFF_ASYNC U_DFF(
2.     .RST (RST),
3.     .CLK (CLK),
4.     .D   (D),
5.     .Q   (Q));
```

4.test command
===

4.1.test -e
---
- 該==檔名是否存在==
```makefile
user /home > ls
a.c b
user /home > test -e a.c && echo "exist" || echo "not exist"
exist
user /home > test -e b && echo "exist" || echo "not exist"
exist
```

4.2.test -f
---
- 該檔名是否存在==且為檔案==
```makefile
user /home > ls
a.c b
user /home > test -f a.c && echo "exist" || echo "not exist"
exist
user /home > test -f b && echo "exist" || echo "not exist"
not exist
```

4.3.test -d
---
- 該檔名是否存在==且為資料夾==
```makefile
user /home > ls
a.c b
user /home > test -d a.c && echo "exist" || echo "not exist"
not exist
user /home > test -d b && echo "exist" || echo "not exist"
exist
```