keil-mdk-build
Use when building, flashing, or packaging firmware with Keil MDK (UV4 CLI, ARMCLANG), analyzing .map files for ROM/RAM optimization and memory budget, or diagnosing Keil-specific build failures. NOT for non-Keil build systems (Makefile, CMake, IAR, GCC-only). For crash triage see hardfault-triage.
AmethystLuna · 已有静态检查记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add AmethystLuna/embedded-workbench --skill keil-mdk-build技能说明
name: keil-mdk-build description: "Use when building, flashing, or packaging firmware with Keil MDK (UV4 CLI, ARMCLANG), analyzing .map files for ROM/RAM optimization and memory budget, or diagnosing Keil-specific build failures. NOT for non-Keil build systems (Makefile, CMake, IAR, GCC-only). For crash triage see hardfault-triage."
Keil MDK Build
Patterns for building embedded firmware with Keil MDK. Covers both ARM Compiler 5 (armcc) and ARM Compiler 6 (armclang).
UV4 CLI Build (Authoritative)
UV4.exe batch mode is the canonical build path. A CI reimplementation is possible but may miss include paths.
<Keil>\UV4\UV4.exe -b project.uvprojx -t TargetName -j0 -o <log_path>
Replace <Keil> with the Keil install root. Auto-discover by checking common locations or ask the user when unknown.
Flags:
| Flag | Meaning |
|---|---|
-b | Batch mode (no GUI) |
-t <target> | Target name within the multi-target project |
-j0 | Auto-parallelism (use all cores) |
-o <path> | Log output file |
Exit codes:
| Code | Meaning |
|---|---|
| 0 | Success (no errors, no warnings) |
| 1 | Warnings but no errors |
| 2 | Errors |
| 3 | Errors (when <StopOnExitCode>3</StopOnExitCode> is set in uvprojx) |
Critical: Log path resolution — The -o path is resolved relative to the .uvprojx file's directory, not the current working directory. Always use an absolute path or a path under a known-existing subdirectory (e.g., objects\) of the project directory.
Recommended invocation — UV4 produces no stdout; use Start-Process with -Wait -PassThru and check ExitCode:
$keil = "<Keil_install_root>" # Ask user or auto-discover
$log = Join-Path (Get-Location) "build.log"
$p = Start-Process -FilePath "$keil\UV4\UV4.exe" `
-ArgumentList "-b project.uvprojx -t Target -j0 -o $log" `
-Wait -PassThru -NoNewWindow
if ($p.ExitCode -ne 0) { throw "Build failed (exit $($p.ExitCode))" }
Compiler Selection
Keil MDK supports two compiler generations. Identify which one the project uses before generating commands.
| Compiler | Keil Name | Binary | Install Path | Check Version |
|---|---|---|---|---|
| ARM Compiler 5 | AC5 | armcc | <Keil>\ARM\ARMCC | armcc --vsn |
| ARM Compiler 6 | AC6 | armclang | <Keil>\ARM\ARMCLANG* | armclang --version |
How to identify: Check the .uvprojx XML for <ARMCC> (AC5) or <ARMCLANG> (AC6) sections. A project can mix both — check per-file or per-group settings.
ARM Compiler 5 (armcc)
Legacy compiler, still common in long-lived projects. Uses its own flag syntax, incompatible with AC6.
Detection: Installed at <Keil>\ARM\ARMCC. Run armcc --vsn to verify.
Optimization levels — AC5 has two orthogonal axes: optimization level (-On) and optimization goal (-Ospace vs -Otime). Keil's UI combines them into a single dropdown:
| Keil Level | AC5 Flags (actual) | Effect |
|---|---|---|
| 0 | -O0 | Minimum optimization, best debug view |
| 1 | -O1 | Restricted optimization, good debug view |
| 2 | -O2 | High optimization (AC5 default) |
| 3 | -O3 | Maximum optimization |
| 4 | -O3 -Otime | Max optimization + favor speed over size |
-Ospace is the default goal at levels 0-3 (favor smaller code). -Otime swaps to favor speed. These are separate from the -On level.
Sources: ARM Compiler v5.06 User Guide (DUI0472M), §3.154-3.159.
Key flags:
--cpu Cortex-M4(adjust to target MCU; use--cpu=listto see supported targets)--c99or--c11(language standard; C90 is the AC5 default)--gnu(enable GNU extensions if project relies on them)--apcs=/interwork(ARM/Thumb interworking)-c(compile only, no link)--split_sections(equivalent to-ffunction-sections -fdata-sections)
Warning control: --diag_suppress=<id> to suppress specific warnings; --diag_error=<id> to promote to error.
ARM Compiler 6 (armclang)
LLVM-based, current generation. Installed at <Keil>\ARM\ARMCLANG*.
Detection: Search common install roots for directories matching ARMCLANG*. Validate with armclang --version. If detection fails, ask the user.
Optimization levels:
| Keil Level | AC6 Flag |
|---|---|
| 0 | -O0 |
| 1 | -O1 |
| 2 | -O2 |
| 3 | -O3 |
| 4 | -Os |
| 5 | -Ofast |
Key flags for Cortex-M:
--target=arm-arm-none-eabi -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard(adjust MCU/FPU to target)-c(compile only, no link)-ffunction-sections -fdata-sections(enable linker garbage collection)-fshort-enums -fshort-wchar(common embedded defaults)
AC5 to AC6 Migration Traps
Projects migrating from AC5 to AC6 commonly hit these issues. Sources: ARM Compiler Migration Guide (DUI0742), Arm Compiler for Embedded FuSa Migration Guide.
| AC5 | AC6 | Trap |
|---|---|---|
__packed struct { ... } | struct __attribute__((packed, aligned(1))) { ... } | AC5 keyword silently ignored by AC6; struct layout changes. aligned(1) ensures no implicit alignment. |
__irq void Handler() | void Handler(void) __attribute__((interrupt)) | AC5 attribute not recognized; ISR stack frame broken. Alternatively use CMSIS IRQn_Type. |
__asm { ... } | __asm volatile ("..." : : : ) | Inline assembly switches from armasm syntax to GAS (GNU assembler) syntax with GCC-style operand constraints. |