make — Build Automation
💬 "make command"
make is a build tool that records 'which commands to rerun when a file changes' in a Makefile and runs only that work when needed.
It lets you run a six-step deployment command with a single make deploy instead of memorizing each step, and it reduces repeated build time by processing only changed files.
- Makefile: a rule file that lists task names and the commands to run
- target: a unit of work invoked with make <name>
- dependencies: the condition that when this file changes, this task runs again
- PHONY: marks tasks that do not create files (for example, clean)
Commonly used options
| Option | Description |
|---|---|
| make | Runs the default target (usually the first target) |
| make test | Runs the test target |
| make clean | Cleans up build artifacts |
| make -j4 | Runs up to 4 jobs in parallel |
| make -n deploy | Previews which commands will run without executing them |
| make --dry-run | Same as -n (preview) |
3 essential command combinations
make clean && make -j4— Clean first, then build in parallelmake -n deploy— Visually check the deploy command before actually running itmake test && make deploy— Deploy after tests pass
💬 "Show me an example of wrapping this command in a script"
Here are common situations on production servers, organized in order.
Defining tasks
Create a Makefile that lists three targets: build, test, and deploy.
printf 'build:\n\tgo build -o bin/app .\n\ntest:\n\tgo test ./...\n\ndeploy:\n\tscp bin/app deploy@srv:/opt/app/\n' > Makefile
Preview
Only print and check which commands will run.
make -n build
Run
After tests pass, build and deploy.
make test\nmake build\nmake deploy
How it is used in practice
1. Seven deploy steps in one line
Situation — When you have to manually type the sequence of test, build, compress, transfer, and restart every time.
make deploy
Result — All steps run in the order written in the Makefile.
Key point — Make a habit of visually checking the command order with make -n deploy before deploying.
2. Process only changed sources
Situation — A project with thousands of files where only some files changed but the build takes a long time.
make build
Result — Only targets whose dependencies changed are rerun, saving time.
Key point — The more accurately you record dependencies at the .o file level, the more work can be skipped.
3. Reuse the same Makefile in CI
Situation — When you want to use the same commands you run locally in the CI pipeline.
make test && make build
Result — The CI script only invokes Makefile targets and performs the same build as locally.
Key point — Keep values that only CI needs in environment variables, and leave placeholders in the Makefile.
Things to watch out for
- Makefile recognizes only lines that begin with a tab as commands. Using spaces causes an error.
- If a target like clean does not create a file, declare it with .PHONY so it does not conflict with a file of the same name.
- Always include a preview (-n) step with deploy targets to reduce mistakes.
Real execution output
This is a simulated example showing a preview of the deploy target and a parallel build (not actual execution output).
!make preview and parallel build output example
Actual production server
Source (HTML): https://games.aidebatehub.com/programs/dev/build/make.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments