build-keil

build-keil

Popular

Use when you need to build embedded projects via the Keil MDK command line, calling bundled scripts to parse project files, execute builds, and locate firmware artifacts.

975stars
164forks
Updated 8/20/2026
SKILL.md
read-only
name
build-keil
description

Use when you need to build embedded projects via the Keil MDK command line, calling bundled scripts to parse project files, execute builds, and locate firmware artifacts.

Build Keil MDK Project

Applicable Scenarios

  • The Project Profile specifies build_system: keil or .uvprojx / .uvproj files exist in the workspace.
  • The user wants to build, rebuild, or verify firmware artifacts for a Keil MDK project.
  • The flashing or debugging workflow requires a new AXF (ELF), HEX, or BIN file.
  • Need to verify whether the Keil MDK environment is ready (UV4.exe, ARMCC/ARMCLANG) before building.

Required Inputs

  • Workspace path, .uvprojx / .uvproj project file path, or an existing Project Profile.
  • Optional build target name, UV4.exe path, and log output path.

Automatic Detection

  • Upon startup, the script automatically reuses the last successful profile from .em_skill.json in the project root as default parameters (explicit parameters take precedence; manual --resume is not required). --resume is used only to assert that the cache must exist, exiting non-zero if missing. If there is no cache or the user explicitly requests a re-scan, the script automatically falls back to standard scanning.
  • The script automatically searches common Keil MDK installation paths and environment variables (KEIL_ROOT, MDK_ROOT) to locate UV4.exe.
  • Parses the Target list in .uvprojx project files, extracting target MCU models, output directories, and toolchains (ARMCC/ARMCLANG).
  • If no target is specified, the first Target in the project is used by default.
  • Searches the output directory for AXF, HEX, and BIN artifacts, ordered by ELF > HEX > BIN.
  • If multiple equally plausible project files or targets exist, candidates are listed rather than silently guessed.

Execution Steps

  1. Read references/usage.md first to determine whether this run is for environment detection, project scanning, listing targets, or executing a build.
  2. For common single-project, single-target scenarios, prefer a single invocation to complete detection + build:
    python scripts/keil_builder.py --detect --project <project_file> --target <target_name>
    
    This starts only one Python process, avoiding the overhead of three serial calls (--detect → --list-targets → --build).
  3. Execute --scan and --list-targets step-by-step only when interactive selection is required (multiple project files or targets needing user confirmation).
  4. Read the build results and artifact scan report output by the script, focusing on preferred artifacts (AXF/ELF > HEX > BIN), error/warning statistics, and failure classifications.
  5. Write the build target, artifact path, target MCU model, and toolchain information back to the Project Profile, handing them off to downstream skills as needed.

Failure Triage

  • Returns environment-missing when Keil MDK is not installed or UV4.exe is unavailable.
  • Returns project-config-error when project files are corrupted, target names are invalid, or compilation fails due to configuration issues.
  • Returns artifact-missing when compilation appears successful but no flashable or debuggable artifact is found.
  • Returns ambiguous-context when multiple equally plausible project files or targets exist and making an arbitrary choice is unsafe.

Platform Notes

  • Keil MDK runs natively only on Windows; command-line compilation via UV4.exe requires a Windows environment.
  • Bundled scripts can still execute project parsing (--list-targets), project scanning (--scan), and artifact scanning (--scan-artifacts) on non-Windows platforms, but actual compilation will be blocked.
  • Build logs may contain Chinese characters encoded in GBK; the script automatically attempts multiple encodings.
  • Artifact paths in the output should remain absolute paths to allow downstream flashing and debugging skills to reuse them directly.

Output Conventions

After script execution completes, the following key information must be extracted and presented to the user:

  • Build status (Success / Failure)
  • Project file and target name
  • Target MCU model and toolchain (e.g., STM32F103RC [ARMCC])
  • Firmware size details (Code, RO-data, RW-data, ZI-data) and Flash/RAM summary
  • Build elapsed time
  • Artifact list (AXF/HEX/BIN and file sizes)
  • Error/Warning counts
  • If failed: Failure classification and log evidence

Example output format:

Build Succeeded ✅
  Project: Demo02.uvprojx → Target: Demo02
  MCU: STM32F103RC | Toolchain: ARMCC
  Firmware Size: Flash ≈ 3.2 KB  RAM ≈ 1.6 KB
  Artifacts: Demo02.axf (518.9 KB), Demo02.hex (9.0 KB)
  Time Elapsed: 00:00:05
  • Update Project Profile with artifact_path, artifact_kind, target_mcu, and toolchain.
  • Upon success, recommend flash-keil, flash-openocd, or debug-gdb-openocd.

Hand-off Relationships

  • When the next intent is flashing hardware, pass successful build results to flash-keil (using Keil's built-in debugger) or flash-openocd (using OpenOCD).
  • When the next intent requires symbol information or a debug session, pass successful build results to debug-gdb-openocd.