Loading...
Loading...
Compare original and translation side by side
| Topic | Reference |
|---|---|
| Rules, prerequisites, targets | syntax.md |
| Variable types and assignment | variables.md |
| Built-in functions | functions.md |
| Special and phony targets | targets.md |
| Recipe execution, parallel | recipes.md |
| Implicit and pattern rules | implicit.md |
| Common practical patterns | patterns.md |
| 主题 | 参考文档 |
|---|---|
| 规则、依赖项、目标 | syntax.md |
| 变量类型与赋值 | variables.md |
| 内置函数 | functions.md |
| 特殊与伪目标 | targets.md |
| 脚本执行、并行构建 | recipes.md |
| 隐式与模式规则 | implicit.md |
| 常见实用模式 | patterns.md |
target: prerequisites
recipetarget: prerequisites
recipeundefinedundefinedundefinedundefined| Operator | Name | When Expanded |
|---|---|---|
| Simple | Once, at definition |
| Conditional | If not already set |
| Recursive | Each use (late binding) |
| Append | Adds to existing value |
CC := gcc # Immediate
CFLAGS ?= -O2 # Default, overridable
DEBUG = $(VERBOSE) # Late binding
CFLAGS += -Wall # Append| 运算符 | 名称 | 展开时机 |
|---|---|---|
| 简单赋值 | 定义时一次性展开 |
| 条件赋值 | 仅当变量未设置时赋值 |
| 递归赋值 | 每次使用时展开(延迟绑定) |
| 追加赋值 | 向现有值添加内容 |
CC := gcc # 立即展开
CFLAGS ?= -O2 # 默认值,可被覆盖
DEBUG = $(VERBOSE) # 延迟绑定
CFLAGS += -Wall # 追加内容| Variable | Meaning |
|---|---|
| Target |
| First prerequisite |
| All prerequisites (unique) |
| Prerequisites newer than target |
| Stem in pattern rules |
| 变量 | 含义 |
|---|---|
| 目标文件 |
| 第一个依赖文件 |
| 所有依赖文件(去重) |
| 比目标文件新的依赖文件 |
| 模式规则中的匹配部分 |
.DEFAULT_GOAL := help
help: ## Show available targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf " %-15s %s\n", $$1, $$2}'
install: ## Install dependencies
uv sync
test: ## Run tests
uv run pytest.DEFAULT_GOAL := help
help: ## 显示可用目标
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf " %-15s %s\n", $$1, $$2}'
install: ## 安装依赖
uv sync
test: ## 运行测试
uv run pytestUNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
OPEN := open
else ifeq ($(UNAME_S),Linux)
OPEN := xdg-open
endifUNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
OPEN := open
else ifeq ($(UNAME_S),Linux)
OPEN := xdg-open
endifBUILDDIR := build
SOURCES := $(wildcard src/*.c)
OBJECTS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(SOURCES))
$(BUILDDIR)/%.o: src/%.c | $(BUILDDIR)
$(CC) -c $< -o $@
$(BUILDDIR):
mkdir -p $@BUILDDIR := build
SOURCES := $(wildcard src/*.c)
OBJECTS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(SOURCES))
$(BUILDDIR)/%.o: src/%.c | $(BUILDDIR)
$(CC) -c $< -o $@
$(BUILDDIR):
mkdir -p $@export PYTHONPATH := $(PWD)/src
export DATABASE_URL
test:
pytest tests/ # sees exported variablesexport PYTHONPATH := $(PWD)/src
export DATABASE_URL
test:
pytest tests/ # 子进程可访问导出的变量.PHONY: lint format check test
lint: ## Run linters
ruff check .
mypy src/
format: ## Format code
ruff format .
check: format lint test ## All quality checks.PHONY: lint format check test
lint: ## 运行代码检查
ruff check .
mypy src/
format: ## 格式化代码
ruff format .
check: format lint test ## 执行所有质量检查.PHONY: clean clean-all
clean: ## Remove build artifacts
rm -rf build/ dist/ *.egg-info
find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
clean-all: clean ## Remove all generated files
rm -rf .venv .pytest_cache .mypy_cache.PHONY: clean clean-all
clean: ## 移除构建产物
rm -rf build/ dist/ *.egg-info
find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
clean-all: clean ## 移除所有生成文件
rm -rf .venv .pytest_cache .mypy_cacheIMAGE := myapp
VERSION := $(shell git describe --tags --always)
docker-build: ## Build Docker image
docker build -t $(IMAGE):$(VERSION) .
docker-run: ## Run container
docker run -d -p 8000:8000 $(IMAGE):$(VERSION)IMAGE := myapp
VERSION := $(shell git describe --tags --always)
docker-build: ## 构建Docker镜像
docker build -t $(IMAGE):$(VERSION) .
docker-run: ## 运行容器
docker run -d -p 8000:8000 $(IMAGE):$(VERSION)undefinedundefinedundefinedundefinedtarget:
@echo "@ suppresses command echo"
-rm -f maybe.txt # - ignores errorstarget:
@echo "@ 用于抑制命令回显"
-rm -f maybe.txt # - 用于忽略错误make -j4 # 4 parallel jobs
make -j4 lint test # Run lint and test in parallelmake -j4 # 4个并行任务
make -j4 lint test # 并行运行lint和testundefinedundefined
---
---DEBUG ?= 0
ifeq ($(DEBUG),1)
CFLAGS += -g -O0
else
CFLAGS += -O2
endif
ifdef CI
TEST_FLAGS := --ci
endifDEBUG ?= 0
ifeq ($(DEBUG),1)
CFLAGS += -g -O0
else
CFLAGS += -O2
endif
ifdef CI
TEST_FLAGS := --ci
endifundefinedundefined
---
---| Pitfall | Problem | Solution |
|---|---|---|
| Spaces in recipes | Recipes need TAB | Use actual TAB character |
| Missing .PHONY | | Declare |
| cd in recipes | Each line is new shell | Use |
| Unexpected late expansion | Use |
| Unexported vars | Subprocesses don't see vars | |
| Complex shell in make | Hard to maintain | Move to external script |
| 陷阱 | 问题 | 解决方案 |
|---|---|---|
| 脚本中使用空格 | 脚本行需要使用TAB字符 | 使用真实的TAB字符 |
| 缺少.PHONY | 若存在 | 声明 |
| 脚本中的cd命令 | 每行对应新的Shell | 使用 |
| 意外的延迟展开 | 默认使用 |
| 未导出变量 | 子进程无法访问变量 | |
| Make中嵌入复杂Shell脚本 | 难以维护 | 移至外部脚本 |
undefinedundefined
---
---