在 Makefile 中,$(info ...)
是 Make 工具内置的函数,不是终端命令。它用于在 Make 执行过程中输出信息到终端。类似的内置函数还有 $(warning ...)
和 $(error ...)
。
详细解释:
-
$(info text)
- 作用:在 Make 解析阶段输出纯信息文本(不中断执行)。
- 特点:
- 文本会被直接打印到终端。
- 不会中断 Make 的执行流程。
- 示例:
$(info "Build started at $(shell date)")
-
$(warning text)
- 作用:输出警告信息(黄色警告标识,不中断执行)。
- 特点:
- 格式:
Makefile:行号: 警告文本
。 - 示例:
$(warning "Configuration is out of date")
- 格式:
-
$(error text)
- 作用:输出错误信息并终止 Make 执行(红色错误标识)。
- 特点:
- 格式:
Makefile:行号: *** 错误文本。
。 - 示例:
$(error "Fatal: Missing dependency")
- 格式:
在您的例子中:
$(info "The defconfig was generated successfully at $(PWD)/defconfig...")
- 行为:当 Make 执行到这一行时,会直接输出引号内的字符串。
$(PWD)
:这是 Make 的内置变量,代表当前工作目录(等价于 shell 的$PWD
)。
关键注意事项:
-
执行时机
这些函数在 Make 的 解析阶段 立即执行(早于实际构建规则)。例如:$(info "This prints FIRST") all: @echo "This prints LATER"
-
与 shell 命令的区别
$(info)
是 Make 函数,直接由 Make 解析。- Shell 命令(如
@echo "text"
)需要写在规则(recipe)中,在构建阶段执行。
-
实际应用场景
- 调试 Makefile 变量(如
$(info CFLAGS = $(CFLAGS)
)。 - 提示用户关键信息(如您的 defconfig 生成位置)。
- 强制终止无效构建(
$(error)
)。
- 调试 Makefile 变量(如
完整示例:
# 信息提示(不中断)
$(info "Starting build...")
# 警告提示(不中断)
$(warning "Using default configuration")
all:
@echo "Building target..."
# 错误提示(中断执行)
$(error "Stop: Required tool not found")
输出:
"Starting build..."
Makefile:4: "Using default configuration"
Makefile:8: *** "Stop: Required tool not found"。 停止。
建议阅读 GNU Make 文档:Chapter 8 - Functions for Transforming Text