2025-03-19 2:39 AM - last edited on 2025-03-20 4:23 AM by Andrew Neil
Code generated by CubeMX use Windows line-endings, which generate huge problems under other operating systems (repository, editors, etc.). You have to strip CR-LFs after every code generation.
Somebody should use standard method of platform-independent text file generation (StringBuffer and System.lineSeparator()).
2026-02-05 1:30 AM
I just did the same with a batch file and AI generated powershell script:
Need to call post_gen.bat from CubeMX as a post-processing user action. Working directory for script is the IOC file directory.
post_gen.bat shall be placed in the same folder as IOC file. Content:
powershell .\post_gen.ps1And then post_gen.ps1 also into same folder:
function Convert-ToCRLF {
param(
[Parameter(Mandatory = $true)]
[string]$SubPath
)
$fullPath = Join-Path -Path (Get-Location) -ChildPath $SubPath
if (-not (Test-Path $fullPath)) {
Write-Host "Path not found: $fullPath"
return
}
Write-Host "Processing folder: $fullPath"
Get-ChildItem -Path $fullPath -Recurse -Include *.c, *.h, *.txt, LICENSE |
ForEach-Object {
Write-Host "Converting $($_.FullName)"
$content = Get-Content $_.FullName -Raw
$content = $content -replace "`r?`n", "`r`n"
Set-Content $_.FullName $content -NoNewline
}
}
# Fix line ending to Windows style
Convert-ToCRLF "Drivers/CMSIS"
Convert-ToCRLF "Drivers/STM32N6xx_HAL_Driver"
Convert-ToCRLF "Middlewares/ST"But some license file has some odd characters that it screws up. That wasn't UTF-8 encoding. So it's not perfect. I solved that with a git restore command in the batch file.