الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

make

Run the tasks of a project (build, test, lint, deploy) from one Makefile, with short names like make test.

المستوى
أداة DevOps
المجموعة
أدوات البناء ولغات البرمجة
يحتاج root
لا
الحزمة
make

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

make reads a Makefile with targets. Each target has a list of things it depends on and the shell lines (the recipe) that build it. make runs a recipe only when a file is missing or older than the files it depends on, so a big build only does the work that changed.

In DevOps teams a Makefile is often just a task runner: make build, make test, make up, make deploy. The same short commands work on every laptop and in every CI job. Such targets are marked .PHONY because they do not make a file.

Recipe lines must start with a Tab, not spaces.

طريقة الكتابة

make [OPTIONS] [TARGET ...] [VAR=value ...]

الخيارات 13

الأكثر استخدامًا

Common options

-f FILE
Use FILE instead of Makefile.
-C DIR
Change to DIR first (run the Makefile of a sub-project).
-j N
Run N recipes at the same time (parallel build).
-n, --dry-run
Print the commands, do not run them.
-B, --always-make
Build every target again, also when it is up to date.
-k, --keep-going
Go on with other targets after an error.
-s, --silent
Do not print the commands.
-i, --ignore-errors
Ignore errors in recipes.
-p
Print all rules and variables (debug).
-q, --question
Exit 0 if the target is up to date, 1 if not; run nothing.
-e
Environment variables win over Makefile variables.
--debug=b
Explain why a target is rebuilt.
VAR=value
Set a variable on the command line (wins over the Makefile).

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Run the default target (the first one in the file)

    make
  2. 2A task-runner Makefile for a web service

    cat Makefile
    .PHONY: build test lint up down
    IMAGE ?= shop
    TAG   ?= $(shell git rev-parse --short HEAD)
    build:
    	docker build -t $(IMAGE):$(TAG) .
    test:
    	go test ./...
    lint:
    	golangci-lint run
    up:
    	docker compose up -d
    down:
    	docker compose down
    make build test

    Recipe lines start with a Tab. ?= sets a default that the environment or the command line can change.

  3. 3Set a variable from the command line

    make build TAG=v1.4.0
  4. 4See what would run, without running it

    make -n deploy
  5. 5Build faster with 8 jobs at once

    make -j8
  6. 6Run the Makefile of a sub-folder

    make -C services/api test
  7. 7A help target that lists all targets

    cat Makefile
    help: ## Show this help
    	@grep -E '^[a-zA-Z_-]+:.*## ' $(MAKEFILE_LIST) | awk -F':.*## ' '{printf "%-12s %s\n", $$1, $$2}'
    make help
    help         Show this help

    @ hides the command itself. $$ is a single $ for the shell.

  8. 8Rebuild everything

    make -B
  9. 9Use make in CI (GitHub Actions)

    cat .github/workflows/ci.yml
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - run: make lint test
  10. 10Find out why a target runs again

    make --debug=b build
  11. 11Check if a target is up to date (exit code)

    make -q dist/app || echo "needs a build"

انتبه

  • "missing separator" means a recipe line starts with spaces instead of a Tab.
  • Each recipe line runs in its own shell: cd dir on one line does not change the next line. Use cd dir && cmd.
  • Mark task targets .PHONY, or a file with the same name (for example a folder called test) stops them.

الملفات

Makefile
The default file make reads (also makefile, GNUmakefile).

نصائح للحفظ

  • Keep the real logic in scripts and call them from make; the Makefile stays short.
  • make help with ## comments makes a project easy for new team members.

رموز الخروج

0All targets done.
1-q and a target is not up to date.
2An error.