cancel
Showing results for 
Search instead for 
Did you mean: 

Line endings - bug in CubeMX, all versions

trzeci
Associate III

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()).

20 REPLIES 20
Mikk Leini
Senior III

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.

MikkLeini_0-1770283334307.png

post_gen.bat shall be placed in the same folder as IOC file. Content:

powershell .\post_gen.ps1

And 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.