bake

EbodShojaeiMIT活跃维护

mbake 是 Makefile 的格式化与静态检查工具,能自动统一 Makefile 排版并揪出写法问题,补上了 Makefile 生态里长期缺失的 fmt/lint 环节。

mbake is a Makefile formatter and linter. It only took 50 years!

v1.5.0发布于 2026-11-09 07:37 UTC766star20fork同步于 2026-11-10 17:56 UTC

怎么上手

由 README 提炼,执行前请确认
pip install mbake
mbake format Makefile
硬件要求按 README 说明

README 未说明,仅要求 Python 3.9+,无 GPU 或显存要求

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

mbake 是 Makefile 的格式化与语法检查工具,用来统一缩进、空格和 .PHONY 声明

mbake 是 Makefile 的格式化与静态检查工具,能自动统一 Makefile 排版并揪出写法问题,补上了 Makefile 生态里长期缺失的 fmt/lint 环节。

核心能力

  • 用 Tab 缩进 recipe、统一赋值与冒号两侧空格、清理续行
  • 分析 recipe 命令自动识别并插入 .PHONY 声明
  • 调用 GNU make 校验格式化前后的 Makefile 语法
  • 通过 .bake.toml 配置对齐、.PHONY 分组等规则
  • 提供 --check 与退出码,可直接接入 CI/CD 流水线

容易踩的坑

validate 依赖本机 GNU make,且语法检查默认超时 10 秒;自动插入 .PHONY 默认关闭,需在 .bake.toml 里开启 auto_insert_phony_declarations;配置文件按当前目录、父目录、用户目录顺序查找,改错位置会静默回退到默认值

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v1.5.0
  1. v1.5.0最新正式版2026-11-09 · 2026-11-09 07:37 UTC

    This release improves GNU Make compatibility, formatting reliability, and configuration handling. Notable changes include:

    • Validation: Reduced false positives for duplicate targets, suffix rules, and recipe syntax.
    • Configuration: Added parent-directory discovery for .bake.toml and fixed timeout handling.

    See https://github.com/EbodShojaei/bake/pull/110 for details.

  2. v1.4.62026-03-31 · 2026-03-31 08:10 UTC
    查看更新说明

    Includes:

    • (fix) explicitly tell pip to install pre-release versions #88
    • (add) special makefile validation rules for .DELETE_ON_ERROR #86
    • Feat(Config): Add support for XDG_CONFIG_HOME. #84 (Thanks for implementing this feature @Ren-B-7!)
  3. v1.4.52026-02-04 · 2026-02-04 01:09 UTC
    查看更新说明

    Added alignment of variable assignments (see https://github.com/EbodShojaei/bake/pull/79)

    Thanks for implementing this feature @Nettozx!

  4. v1.4.42025-12-23 · 2025-12-23 01:46 UTC
    查看更新说明

    Includes:

  5. v1.4.32025-10-20 · 2025-10-20 02:42 UTC
    查看更新说明

    Add validation to detect common typos where assignment operators are reversed. The formatter now generates helpful warnings (see https://github.com/EbodShojaei/bake/pull/63):

    • =? (should be ?=) for conditional assignment
    • =: (should be :=) for immediate assignment
    • =+ (should be +=) for append assignment

    Resolves issue where suffix rules and file targets were incorrectly suggested as phony targets (see https://github.com/EbodShojaei/bake/pull/62). Includes:

    • Fix PhonyAnalyzer to detect 'target name as last argument' pattern (e.g., cp input.txt clean)
    • Update phony detection to properly exclude suffix rules (.a.b:) and file targets (foo.b:)
    • Add comprehensive suffix validation with duplicate detection and pattern warnings
    • Add extensive test coverage for new validation features
    • Fix .POSIX and other special targets being incorrectly suggested as phony

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    🍞 mbake

    Features

    • Smart formatting: Tabs for recipes, consistent spacing, line continuation cleanup
    • Intelligent .PHONY detection: Automatically identifies and manages phony targets
    • Syntax validation: Ensures Makefiles are valid before and after formatting
    • Configurable rules: Customize behavior via a project .bake.toml or {XDG_CONFIG_HOME}/bake.toml
    • CI/CD ready: Check mode for automated formatting validation
    • VSCode extension: Full editor integration available

    Installation

    # Install from PyPI
    pip install mbake
    
    # Or install VSCode extension
    # Search for "mbake Makefile Formatter" in VSCode Extensions
    

    Quick Start

    # Format a Makefile
    mbake format Makefile
    
    # Check if formatting is needed (CI/CD mode)
    mbake format --check Makefile
    
    # Validate Makefile syntax
    mbake validate Makefile
    
    # Initialize configuration
    mbake init
    

    Usage

    Basic Commands

    # Format files
    mbake format Makefile                   # Format single file
    mbake format --check Makefile           # Check formatting (CI/CD)
    mbake format --diff Makefile            # Show changes without modifying
    
    # Validate syntax
    mbake validate Makefile                 # Check Makefile syntax with GNU make
    
    # Configuration
    mbake init                              # Create config file
    mbake config                            # Show current settings
    mbake config --path                     # Show which config file is used
    mbake update                            # Update to latest version
    

    Key Options

    • --check: Check formatting without making changes (perfect for CI/CD)
    • --diff: Show what changes would be made
    • --backup: Create backup before formatting
    • --validate: Validate syntax after formatting
    • --timeout: Timeout in seconds for the --validate syntax check (default: syntax_check_timeout from the config file, or 10)
    • --config: Use custom configuration file

    Configuration

    mbake looks for a configuration file in this order:

    1. --config PATH, if given
    2. .bake.toml in the current directory, or else in the nearest parent directory that has one
    3. ~/.bake.toml if it exists, otherwise $XDG_CONFIG_HOME/bake.toml (default ~/.config/bake.toml), which mbake init creates

    A .bake.toml from step 2 that cannot be loaded is skipped with a warning on stderr, and the search continues in the parent directories. A file from step 3 that cannot be loaded is also skipped with a warning, and the built-in defaults are used; mbake config reports it as an error instead. An invalid --config file is always an error. Run mbake config to see which file is used and its settings.

    Create a config file with mbake init. Key settings:

    [formatter]
    # Spacing and formatting
    space_around_assignment = true          # CC=gcc to CC = gcc
    space_after_colon = true                # myapp.o:myapp.c to myapp.o: myapp.c
    normalize_line_continuations = true     # Clean up backslash continuations
    remove_trailing_whitespace = true       # Remove trailing spaces
    fix_missing_recipe_tabs = true          # Convert spaces to tabs in recipes
    
    # Variable alignment
    align_variable_assignments = false      # Align consecutive assignments
    align_across_comments = false           # Continue alignment across comments
    
    # .PHONY management
    auto_insert_phony_declarations = false  # Auto-detect and insert .PHONY
    group_phony_declarations = false        # Group multiple .PHONY lines
    phony_at_top = false                    # Place .PHONY at file top
    

    Smart .PHONY Detection

    mbake intelligently detects phony targets by analyzing recipe commands:

    # These are automatically detected as phony
    test:
     npm test
    
    clean:
     rm -f *.o
    
    deploy:
     ssh user@server 'systemctl restart app'
    
    # This creates a file, so it's NOT phony
    myapp.o: myapp.c
     gcc -c myapp.c -o myapp.o
    

    Enable auto-insertion in your config:

    [formatter]
    auto_insert_phony_declarations = true
    

    Examples

    Before and After

    # Before: Inconsistent spacing and indentation
    CC:=gcc
    CFLAGS= -Wall -g
    SOURCES=main.c \
      utils.c \
        helper.c
    
    all: $(TARGET)
        $(CC) $(CFLAGS) -o $@ $^
    
    clean:
        rm -f *.o
    
    # After: Clean, consistent formatting
    CC := gcc
    CFLAGS = -Wall -g
    SOURCES = main.c \
      utils.c \
      helper.c
    
    all: $(TARGET)
    	$(CC) $(CFLAGS) -o $@ $^
    
    clean:
    	rm -f *.o
    

    Disable Formatting

    Use special comments to disable formatting in specific regions:

    # bake-format off
    CUSTOM_FORMAT= \
          1 \
      45678 \
    #bake-format on
    

    The lines between the markers are not reformatted and stay directly after their # bake-format off line, even when formatting adds or removes lines elsewhere. At the end of the file, the usual final-newline handling still applies. Line numbers in error and warning messages count the lines inside disabled regions.

    CI/CD Integration

    # GitHub Actions example
    - name: Check Makefile formatting
      run: |
        pip install mbake
        mbake format --check Makefile
    

    Exit codes: 0 (success), 1 (needs formatting), 2 (error)

    Contributing

    Contributions welcome! See CONTRIBUTING.md for details.

    # Development setup
    git clone https://github.com/ebodshojaei/bake.git
    cd mbake
    pip install -e ".[dev]"
    pytest  # Run tests
    

    License

    MIT License - see LICENSE for details.