Makefile

这个文件是用来干嘛的?

通常情况下,它们用于编写C程序,以告诉make命令需要怎么样的去编译和链接程序。

那跟我写Python有什么关系

基本格式

# Makefile
# 这是注释
<target> : <prerequisites> 
[tab]  <commands>
  • target: 目标
  • prerequisites: 依赖
  • commands: 命令
# Makefile
hello:
    echo hello
>>> make hello
echo hello
hello

默认情况下会将命令打印出来,可以在命令前面加上@,不打印命令

# Makefile
hello:
    @echo hello
>>> make hello
hello

伪目标

PHONY 伪目标。声明"伪目标"之后,make就不会去检查是否存在一个叫做hello的文件,而是每次运行都执行对应的命令。

# Makefile
.PHONY hello
hello:
    @echo hello

使用变量

PORT=8080
HOST=127.0.0.1

run:
    python app.py runerver --host=$(HOST) --port=$(PORT)
>>> make run PORT=5000 HOST=0.0.0.0
python app.py runserver --host=0.0.0.0 --port=5000
...

依赖

PACKAGE='mypackage'


.PHONY: test lint coverage cov test-all

lint:
    flake8 --max-complexity 10 --max-line-length=120 $(PACKAGE)

test:
    pytest -v tests

coverage:
    pytest --cov=$(PACKAGE) --cov-report=html -v tests

cov: coverage

test-all: lint test coverage
    @echo "test finished"
>>> make test-all

参考:

注意: Makefile 中的缩进必须使用 TAB 来完成,而不是空格。