Makefile 使用介绍

发布时间: 更新时间: 总字数:1022 阅读时间:3m 作者: IP上海 分享 网址

Makefile 示例

介绍

  • make 命令在当前目录依次搜索 MakefilemakefileGNUmakefile 文件,若找不到就报错
  • make 命令支持通过时间戳判断源文件和编译产物的先后关系,减少文件重新编译,提高编译速度

help

make --help ...
$ make --help
Usage: make [options] [target] ...
Options:
  -b, -m                      Ignored for compatibility.
  -B, --always-make           Unconditionally make all targets.
  -C DIRECTORY, --directory=DIRECTORY
                              Change to DIRECTORY before doing anything.
  -d                          Print lots of debugging information.
  --debug[=FLAGS]             Print various types of debugging information.
  -e, --environment-overrides
                              Environment variables override makefiles.
  --eval=STRING               Evaluate STRING as a makefile statement.
  -f FILE, --file=FILE, --makefile=FILE
                              Read FILE as a makefile.
  -h, --help                  Print this message and exit.
  -i, --ignore-errors         Ignore errors from recipes.
  -I DIRECTORY, --include-dir=DIRECTORY
                              Search DIRECTORY for included makefiles.
  -j [N], --jobs[=N]          Allow N jobs at once; infinite jobs with no arg.
  -k, --keep-going            Keep going when some targets can't be made.
  -l [N], --load-average[=N], --max-load[=N]
                              Don't start multiple jobs unless load is below N.
  -L, --check-symlink-times   Use the latest mtime between symlinks and target.
  -n, --just-print, --dry-run, --recon
                              Don't actually run any recipe; just print them.
  -o FILE, --old-file=FILE, --assume-old=FILE
                              Consider FILE to be very old and don't remake it.
  -O[TYPE], --output-sync[=TYPE]
                              Synchronize output of parallel jobs by TYPE.
  -p, --print-data-base       Print make's internal database.
  -q, --question              Run no recipe; exit status says if up to date.
  -r, --no-builtin-rules      Disable the built-in implicit rules.
  -R, --no-builtin-variables  Disable the built-in variable settings.
  -s, --silent, --quiet       Don't echo recipes.
  -S, --no-keep-going, --stop
                              Turns off -k.
  -t, --touch                 Touch targets instead of remaking them.
  --trace                     Print tracing information.
  -v, --version               Print the version number of make and exit.
  -w, --print-directory       Print the current directory.
  --no-print-directory        Turn off -w, even if it was turned on implicitly.
  -W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE
                              Consider FILE to be infinitely new.
  --warn-undefined-variables  Warn when an undefined variable is referenced.

This program built for x86_64-pc-linux-gnu
Report bugs to <bug-make@gnu.org>

说明:

  • -C 指定路径
  • make -j 4 指定并行编译的任务数,可以大大缩短编译时间,-j 指定CPU个数
-j [N], --jobs[=N]          Allow N jobs at once; infinite jobs with no arg.

规则

Makefile 由多条规则组成

目标: [依赖1[ 依赖2 [ 依赖3 [...]]]]
\t命令

[target] ... : [prerequisites] ...
<tab>[command]
    ...
    ...
  • 目标 一个 target 表示一条规则,也称为 伪目标(PHONY)

    • 一个 Makefile 中可以有多个目标,一般第一个为默认目标
    • .PHONY 的作用有两个
      • .PHONY跟伪目标,直接在 Makefile 中执行 伪目标 的命令。忽略 Makefile 同级目录下的同名的文件
      • 二是提高执行makefile时的效率
  • 依赖 是可选的,通常是多个文件名、伪目标,必须是已有的规则

    • 可以通过shell获取,如 $(shell find . -type f -name "*.sh")
  • 命令 构建一个 target 的具体命令集合

    • 也可以为空,用来表示一种依赖关系
    • 命令以横杠-开头,表示忽略命令执行的状态
    • make 默认会打印每条命令,再执行,该行为被称为回声,命令前加 @ 可以禁用该打印
  • 多目标时,可以使用 % 进行匹配,如下压缩命令:

xxx-%.gz: xxx-%
    gzip --force --keep xxx-$*.exe
  • .DEFAULT_GOAL 设置默认目标,不设置默认为第一个
.DEFAULT_GOAL := default

default:...

shell

  • shell 函数 参数是操作系统的 shell 命令,功能和使用 (`) 相同
    • 示例 ipaddress := $(shell ip a)

include

将别的 Makefile 包含进来,这很像 C 语言的 #include

# 假设有 Makefile a.mk、b.mk、c.mk,$(bar) 包含 e.mk f.mk,则
include foo.make *.mk $(bar)
# 等价于
include foo.make a.mk b.mk c.mk e.mk f.mk

# include 当文件不存在时会报错,使用 `-` 忽略错误,也可以使用 `sinclude` 替代
-include <filename>

类似的参数:

  • make -I 或 --include-dir

Makefile 示例

Golang

# https://www.xiexianbin.cn/program/tools/2016-01-09-makefile/index.html
.PHONY: all test clean build build-linux build-mac build-windows

GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
BINARY_NAME=main
BINARY_LINUX=$(BINARY_NAME)-linux
BINARY_MAC=$(BINARY_NAME)-darwin
BINARY_WIN=$(BINARY_NAME)-windows

help:  ## Show this help.
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

all: clean test build build-linux build-mac build-windows  ## Build all
test:  ## run test
	$(GOTEST) -v ./...
clean: ## run clean bin files
	$(GOCLEAN)
	rm -f bin/$(BINARY_NAME)
build:  ## build for current os
	$(GOBUILD) -o bin/$(BINARY_NAME) -v

build-linux:  ## build linux amd64
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_LINUX) -v
build-mac:  ## build mac amd64
	CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_MAC) -v
build-windows:  ## build windows amd64
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_WIN) -v
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数