返回顶部
m

makefile-buildMakefile构建

Write Makefiles for any project type. Use when setting up build automation, defining multi-target builds, managing dependencies between tasks, creating project task runners, or using Make for non-C projects (Go, Python, Docker, Node.js). Also covers Just and Task as modern alternatives.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
1,637
下载量
免费
免费
3
收藏
概述
安装方式
版本历史

makefile-build

Makefile 与构建系统

编写跨任意语言的Makefile以实现项目自动化。涵盖目标、依赖项、变量、模式规则、伪目标,以及如何在Go、Python、Docker和Node.js项目中使用Make。同时包含Just和Task作为现代替代方案。

何时使用

  • - 自动化构建、测试、代码检查、部署命令
  • 定义任务之间的依赖关系(构建前先测试)
  • 创建项目级任务运行器(团队内保持一致)
  • 用简短易记的目标替代冗长的CLI命令
  • 管理多步骤构建流程
  • 任何需要make build && make test && make deploy工作流的项目

Makefile基础

结构

makefile

目标: 前置条件


配方(必须使用TAB缩进,不能用空格)

build: src/main.go
go build -o bin/app src/main.go

test: build
go test ./...

clean:
rm -rf bin/

第一个目标是默认目标(直接运行make时执行)

变量

makefile

简单赋值


CC = gcc
CFLAGS = -Wall -O2

延迟赋值(使用时才展开)

FILES = $(wildcard src/*.go)

立即赋值(定义时即展开)

VERSION := $(shell git describe --tags --always)

条件赋值(仅当未设置时)

PORT ?= 8080

使用变量

build: $(CC) $(CFLAGS) -o app main.c @echo 版本: $(VERSION)

自动变量

makefile

$@ = 目标名称


$< = 第一个前置条件


$^ = 所有前置条件


$* = 词干(模式匹配)


$(@D) = 目标所在目录


$(@F) = 目标文件名

bin/app: src/main.go src/util.go
go build -o $@ $^

$@ = bin/app


$^ = src/main.go src/util.go


$< = src/main.go

模式规则

%.o: %.c $(CC) -c -o $@ $<

对于 foo.o: $@ = foo.o, $< = foo.c, $* = foo

伪目标(非文件)

makefile

没有.PHONY时,如果存在名为clean的文件,make clean将不执行任何操作


.PHONY: build test clean lint fmt help

build:
go build -o bin/app ./cmd/app

test:
go test ./...

clean:
rm -rf bin/ dist/

列出所有目标

help: @grep -E ^[a-zA-Z-]+:.?## .$$ $(MAKEFILELIST) | sort | \ awk BEGIN {FS = :.*?## }; {printf \033[36m%-15s\033[0m %s\n, $$1, $$2}

自文档化Makefile

makefile
.DEFAULT_GOAL := help

build: ## 构建应用程序
go build -o bin/app ./cmd/app

test: ## 运行所有测试
go test -v ./...

lint: ## 运行代码检查工具
golangci-lint run

clean: ## 清理构建产物
rm -rf bin/ dist/

help: ## 显示此帮助信息
@grep -E ^[a-zA-Z-]+:.?## .$$ $(MAKEFILELIST) | sort | \
awk BEGIN {FS = :.*?## }; {printf \033[36m%-15s\033[0m %s\n, $$1, $$2}

特定语言的Makefile

Go

makefile
BINARY_NAME := myapp
VERSION := $(shell git describe --tags --always --dirty)
LDFLAGS := -ldflags -X main.version=$(VERSION)
GOFILES := $(shell find . -name .go -not -path ./vendor/)

.PHONY: all build test lint clean run

all: lint test build

build: ## 构建二进制文件
CGOENABLED=0 go build $(LDFLAGS) -o bin/$(BINARYNAME) ./cmd/$(BINARY_NAME)

test: ## 运行测试
go test -race -coverprofile=coverage.out ./...

test-coverage: test ## 显示覆盖率报告
go tool cover -html=coverage.out

lint: ## 运行代码检查工具
golangci-lint run ./...

fmt: ## 格式化代码
gofmt -w $(GOFILES)

run: build ## 构建并运行
./bin/$(BINARY_NAME)

clean: ## 清理构建产物
rm -rf bin/ coverage.out

交叉编译

build-linux: ## 为Linux构建 GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o bin/$(BINARYNAME)-linux-amd64 ./cmd/$(BINARYNAME)

build-all: ## 为所有平台构建
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o bin/$(BINARYNAME)-linux-amd64 ./cmd/$(BINARYNAME)
GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o bin/$(BINARYNAME)-darwin-arm64 ./cmd/$(BINARYNAME)
GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o bin/$(BINARYNAME)-windows-amd64.exe ./cmd/$(BINARYNAME)

Python

makefile
PYTHON := python3
VENV := .venv
BIN := $(VENV)/bin

.PHONY: all install test lint fmt clean run

all: install lint test

$(VENV)/bin/activate:
$(PYTHON) -m venv $(VENV)
$(BIN)/pip install --upgrade pip

install: $(VENV)/bin/activate ## 安装依赖
$(BIN)/pip install -r requirements.txt
$(BIN)/pip install -r requirements-dev.txt

test: ## 运行测试
$(BIN)/pytest -v --cov=src --cov-report=term-missing

lint: ## 运行代码检查工具
$(BIN)/ruff check src/ tests/
$(BIN)/mypy src/

fmt: ## 格式化代码
$(BIN)/ruff format src/ tests/

run: ## 运行应用程序
$(BIN)/python -m src.main

clean: ## 移除虚拟环境和缓存
rm -rf $(VENV) pycache .pytestcache .mypycache .ruff_cache
find . -type d -name pycache -exec rm -rf {} + 2>/dev/null || true

Node.js / TypeScript

makefile
.PHONY: all install build test lint clean dev

all: install lint test build

node_modules: package.json
npm install
@touch node_modules

install: node_modules ## 安装依赖

build: node_modules ## 构建TypeScript
npx tsc

test: node_modules ## 运行测试
npx vitest run

test-watch: node_modules ## 以监视模式运行测试
npx vitest

lint: node_modules ## 代码检查
npx eslint src/ --ext .ts,.tsx
npx tsc --noEmit

fmt: node_modules ## 格式化代码
npx prettier --write src//*.{ts,tsx}

dev: node_modules ## 以开发模式运行
npx tsx watch src/index.ts

clean: ## 清理构建产物
rm -rf dist/ node_modules/.cache

Docker

makefile
IMAGE_NAME := myapp
VERSION := $(shell git describe --tags --always)
REGISTRY := ghcr.io/myorg

.PHONY: build push run stop clean

build: ## 构建Docker镜像
docker build -t $(IMAGENAME):$(VERSION) -t $(IMAGENAME):latest .

push: build ## 推送到仓库
docker tag $(IMAGENAME):$(VERSION) $(REGISTRY)/$(IMAGENAME):$(VERSION)
docker tag $(IMAGENAME):latest $(REGISTRY)/$(IMAGENAME):latest
docker push $(REGISTRY)/$(IMAGE_NAME):$(VERSION)
docker push $(REGISTRY)/$(IMAGE_NAME):latest

run: ## 运行容器
docker run --rm -p 8080:8080 --name $(IMAGENAME) $(IMAGENAME):latest

stop: ## 停止容器
docker stop $(IMAGE_NAME) 2>/dev/null || true

clean: ## 移除镜像
docker rmi $(IMAGENAME):$(VERSION) $(IMAGENAME):latest 2>/dev/null || true

compose-up: ## 使用docker compose启动
docker compose up -d --build

compose-down: ##

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 makefile-build-1776365843 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 makefile-build-1776365843 技能

通过命令行安装

skillhub install makefile-build-1776365843

下载

⬇ 下载 makefile-build v1.0.0(免费)

文件大小: 5.09 KB | 发布时间: 2026-4-17 15:49

v1.0.0 最新 2026-4-17 15:49
Initial release: Make basics, automatic variables, pattern rules, Go/Python/Node/Docker Makefiles, multi-directory builds, Just and Task alternatives

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部