AgentSkillsCN

gnu-make

提供专家级的 GNU Make 服务,涵盖 Makefile 编写、调试、优化以及项目架构等全方位内容。当用户需要处理 Makefile、编写 make 规则、排查构建问题、咨询 GNU Make 的语法或语义、探讨基于 make 的构建系统,或提及“make”、“Makefile”、“gmake”、“.PHONY”、模式规则、自动变量或递归 make 时,均可寻求本服务的帮助。服务内容包括规则、变量、函数、命令、隐式规则、大型项目组织、调试以及可移植性等方面。

SKILL.md
--- frontmatter
name: gnu-make
description: >
  Expert-level GNU Make assistance covering Makefile authoring, debugging,
  optimization, and project architecture. Use when the user is working with
  Makefiles, writing make rules, debugging build issues, asking about GNU Make
  syntax or semantics, discussing build systems based on make, or mentions
  "make", "Makefile", "gmake", ".PHONY", pattern rules, automatic variables,
  or recursive make. Covers rules, variables, functions, commands, implicit
  rules, large project organization, debugging, and portability.

GNU Make Expert

Core Quick Reference

Rule anatomy

makefile
target: prerequisite1 prerequisite2
	command1
	command2

Recipes MUST use literal tab characters, not spaces.

Essential automatic variables

VariableMeaning
$@Target filename
$<First prerequisite
$^All prerequisites (deduped)
$+All prerequisites (with dupes)
$*Stem matched by % in pattern rule
$(@D) / $(@F)Directory / filename part of $@
$(<D) / $(<F)Directory / filename part of $<

Variable flavors

makefile
RECURSIVE = $(OTHER)       # expanded every time referenced
IMMEDIATE := $(OTHER)      # expanded once at assignment
CONDITIONAL ?= default     # set only if not already defined
APPEND += more             # append to existing value
SHELL_OUT != command       # assign output of shell command (GNU 4.0+)

Key built-in functions

makefile
$(subst from,to,text)          $(patsubst pattern,replacement,text)
$(filter pattern...,text)      $(filter-out pattern...,text)
$(sort list)                   $(word n,text)
$(words text)                  $(firstword text)
$(wildcard pattern)            $(dir names)
$(notdir names)                $(suffix names)
$(basename names)              $(addsuffix suffix,names)
$(addprefix prefix,names)      $(join list1,list2)
$(foreach var,list,text)       $(call func,args...)
$(if cond,then,else)           $(or cond1,cond2...)
$(and cond1,cond2...)          $(eval text)
$(value variable)              $(origin variable)
$(flavor variable)             $(shell command)
$(error text)                  $(warning text)
$(info text)                   $(file op,filename,text)

Critical special targets

makefile
.PHONY: clean install test          # not real files
.DEFAULT_GOAL := all                # override default target
.SUFFIXES:                          # disable built-in suffix rules
.DELETE_ON_ERROR:                   # delete targets on recipe failure
.SECONDEXPANSION:                   # enable $$(...) in prerequisites
.ONESHELL:                          # run entire recipe in one shell

Quick debugging

Print any variable:

makefile
$(info DEBUG: VAR is [$(VAR)])

Dry run: make -n Print database: make -p Debug output: make -d or make --debug=basic Trace remake decisions: make --debug=why (GNU Make 4.0+) Remake with explanation: make -r --debug=basic

For in-depth debugging techniques, read references/debugging.md.

Reference Documents

Load these as needed based on the specific topic:

TopicFileWhen to read
Rules & Targetsreferences/rules.mdPattern rules, implicit rules, static patterns, multiple targets, order-only prerequisites, VPATH/vpath
Variables & Macrosreferences/variables.mdVariable scoping, define/endef, target-specific variables, override, private, unexport, computed variable names
Functionsreferences/functions.mdString/file/list manipulation functions, $(call), $(eval), $(foreach), user-defined functions, function programming patterns
Commands & Recipesreferences/commands.mdRecipe execution model, .ONESHELL, error handling, parallel make (-j), command modifiers (@, -, +), .SHELLFLAGS, canned recipes
Large Projectsreferences/large-projects.mdRecursive vs non-recursive make, include, multi-directory builds, generated dependencies, auto-dependency generation, build trees
Debuggingreferences/debugging.mdSystematic debugging, --warn-undefined-variables, $(warning) tracing, remake database, common pitfalls, debugging recursive make
Performancereferences/performance.mdParallel builds, job server, .NOTPARALLEL, reducing shell invocations, lazy evaluation pitfalls, caching patterns
Portabilityreferences/portability.mdPOSIX make vs GNU extensions, cross-platform recipes, Windows considerations, portable shell idioms
Macrosreferences/macros.mddefine/endef templates, $(call) parameterization, $(eval) code generation, call+eval+foreach metaprogramming, escaping, canned recipes
C/C++ Patternsreferences/c-cpp-patterns.mdAuto-dependency generation with -MMD -MP, separate build directories, library building, compiler flag management
Java Patternsreferences/java-patterns.mdjavac batch compilation, classpath management, JAR creation, multi-module projects, JNI, sentinel file pattern