<# .SYNOPSIS Run idf.py for this project with a local ESP-IDF environment activated. .DESCRIPTION Activates ESP-IDF (located through the Espressif Installation Manager's eim_idf.json, unless IDF_PATH is already set) and forwards every remaining argument to idf.py. Non-ASCII project path workaround --------------------------------- ESP-IDF v6.1 rewrites the compiler's "-specs=..." flag from components/esp_libc/project_include.cmake through CMake's file(STRINGS), which silently drops every non-ASCII byte. When this project lives under a path containing non-ASCII characters (e.g. "D:\宣筹\..."), the rewritten flag points at a nonexistent file and every compile fails with: riscv32-esp-elf-gcc.exe: fatal error: cannot read spec file ... This script detects that case and builds out-of-tree into an ASCII build directory instead, which keeps the flag (and every generated path) ASCII. Override the location with the IDF_BUILD_DIR environment variable. .EXAMPLE .\idf.ps1 build .\idf.ps1 menuconfig .\idf.ps1 -p COM9 flash monitor .\idf.ps1 erase-flash #> # NOTE: deliberately no param() block. An advanced/declared parameter list makes # PowerShell try to bind idf.py's own short options to its common parameters # ("-p" is ambiguous with -ProgressAction / -PipelineVariable). A simple script # passes every unbound token through $args verbatim. $IdfArgs = @($args) $ErrorActionPreference = 'Stop' function Find-IdfActivation { $candidates = @( (Join-Path $env:USERPROFILE '.espressif\eim_idf.json'), 'C:\Espressif\tools\eim_idf.json', (Join-Path $env:LOCALAPPDATA 'Espressif\eim_idf.json') ) foreach ($path in $candidates) { if (-not (Test-Path $path)) { continue } $cfg = Get-Content $path -Raw | ConvertFrom-Json $installed = $cfg.idfInstalled | Where-Object { $_.id -eq $cfg.idfSelectedId } | Select-Object -First 1 if (-not $installed) { $installed = $cfg.idfInstalled | Select-Object -First 1 } if ($installed -and (Test-Path $installed.activationScript)) { return $installed.activationScript } } return $null } if (-not $env:IDF_PATH) { $activation = Find-IdfActivation if (-not $activation) { throw 'ESP-IDF not found. Activate your ESP-IDF environment first (export.ps1 / eim).' } . $activation *> $null } if (-not $IdfArgs -or $IdfArgs.Count -eq 0) { $IdfArgs = @('build') } # --- non-ASCII project path workaround ------------------------------------- $projectDir = if ($PSScriptRoot) { $PSScriptRoot } else { (Get-Location).Path } $userPickedBuildDir = $IdfArgs -contains '-B' -or $IdfArgs -contains '--build-dir' if ($projectDir -notmatch '^[\x20-\x7E]+$' -and -not $userPickedBuildDir) { if ($env:IDF_BUILD_DIR) { $buildDir = $env:IDF_BUILD_DIR } else { $qualifier = Split-Path -Qualifier $projectDir # e.g. "D:" $buildDir = Join-Path $qualifier 'esp-idf-build\armor' } Write-Host "Non-ASCII project path detected; building out-of-tree in '$buildDir'." -ForegroundColor Yellow Write-Host 'Set IDF_BUILD_DIR to use a different build directory.' -ForegroundColor Yellow $IdfArgs = @('-B', $buildDir) + $IdfArgs } & idf.py @IdfArgs exit $LASTEXITCODE