diff --git a/.github/workflows/check_formatting.yml b/.github/workflows/check_formatting.yml index 95f714913..c80daaecb 100644 --- a/.github/workflows/check_formatting.yml +++ b/.github/workflows/check_formatting.yml @@ -3,7 +3,7 @@ name: Check formatting on: [pull_request] jobs: - check_date: + check_format: runs-on: ubuntu-latest name: Check formatting strategy: @@ -13,12 +13,20 @@ jobs: - "firmware/application" - "firmware/baseband" steps: - - uses: actions/checkout@v2 - - name: print latest_commit - run: echo ${{ github.sha }} - - name: clang-format Check - uses: jidicula/clang-format-action@v4.11.0 - with: - clang-format-version: '18' - check-path: ${{ matrix.path }} - fallback-style: Chromium + - uses: actions/checkout@v4 + + - name: clang-format check (repo pinned) + run: | + set -euo pipefail + + CF="./tools/clang-format.sh" + chmod +x "$CF" || true + + "$CF" --version + + ROOT="${{ matrix.path }}" + find "$ROOT" -type f \( \ + -name '*.c' -o -name '*.cpp' -o \ + -name '*.h' -o -name '*.hpp' \ + \) -print0 \ + | xargs -0 -r "$CF" --dry-run --Werror --style=file diff --git a/.gitignore b/.gitignore index 7136b0ee3..5dd1a8ef6 100644 --- a/.gitignore +++ b/.gitignore @@ -83,4 +83,5 @@ venv/ # generated bitmap arr file # TODO: generate bitmap during build, since we use python during build anyway, lemme know if this is a bad idea @zxkmm /firmware/tools/bitmap.hpp -firmware/flashsize.h +# flashsize is generated by cmake +/firmware/flashsize.h diff --git a/firmware/application/keeloq_file.cpp b/firmware/application/keeloq_file.cpp index 6cc7d2433..ffb3b613e 100644 --- a/firmware/application/keeloq_file.cpp +++ b/firmware/application/keeloq_file.cpp @@ -43,7 +43,7 @@ bool read_keeloq_file(const fs::path& path, KeeloqData& data) { data.mf_name = std::string{chunks[0]}; data.serial = std::strtoul(chunks[1].data(), NULL, 16); - data.counter = (uint16_t)std::atoi(chunks[2].data()); + data.counter = std::atoi(chunks[2].data()); data.btn = (uint8_t)std::atoi(chunks[3].data()); return true; diff --git a/firmware/application/keeloq_file.hpp b/firmware/application/keeloq_file.hpp index ca0ff1b42..349511c05 100644 --- a/firmware/application/keeloq_file.hpp +++ b/firmware/application/keeloq_file.hpp @@ -30,7 +30,7 @@ struct KeeloqData { std::string mf_name{}; uint32_t serial = 0; - uint16_t counter = 0; + uint32_t counter = 0; uint8_t btn = 0; }; diff --git a/firmware/flashsize.h b/firmware/flashsize.h deleted file mode 100644 index 5728d18e4..000000000 --- a/firmware/flashsize.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -// DO NOT EDIT: IT IS AUTO GENERATED BY CMAKE!!!! - -// clang-format off -//Allowed fw size in MB -#define FLASH_SIZE_MB 4 -//Current compiled fw size in MB -#define FLASH_SIZE_LIMIT_MB 3.5 -// clang-format on diff --git a/format-code.ps1 b/format-code.ps1 new file mode 100644 index 000000000..0b4fa7d6e --- /dev/null +++ b/format-code.ps1 @@ -0,0 +1,37 @@ +$ErrorActionPreference = "Stop" + +# Run from repo root (script is in repo root) +$CF = Join-Path $PSScriptRoot "tools\clang-format.ps1" + +if (-not (Test-Path $CF)) { + throw "ERROR: $CF not found. Did you commit tools\clang-format.ps1 and the pinned binaries?" +} + +# Print version for traceability +& $CF --version | Write-Host + +# Collect files (PowerShell-native; avoids xargs/find differences on Windows) +$roots = @( + "firmware/common", + "firmware/baseband", + "firmware/application", + "firmware/test/application", + "firmware/test/baseband" +) + +$files = foreach ($r in $roots) { + if (Test-Path $r) { + Get-ChildItem -Path $r -Recurse -File -ErrorAction SilentlyContinue | + Where-Object { $_.Extension -in @(".h", ".hpp", ".c", ".cpp") } | + ForEach-Object { $_.FullName } + } +} + +if (-not $files -or $files.Count -eq 0) { + Write-Host "No matching source files found." + exit 0 +} + +# Format in place +& $CF -style=file -i -- $files +exit $LASTEXITCODE diff --git a/format-code.sh b/format-code.sh index d1190c5d5..c065ab21f 100755 --- a/format-code.sh +++ b/format-code.sh @@ -1,4 +1,18 @@ #!/bin/sh +set -eu + +# Run from repo root (script is in repo root) +CF="./tools/clang-format.sh" + +# Sanity check +if [ ! -x "$CF" ]; then + echo "ERROR: $CF not found or not executable." >&2 + echo "Did you commit the pinned clang-format binaries and wrappers under tools/ ?" >&2 + exit 2 +fi + +# Print version for traceability +"$CF" --version find firmware/common \ firmware/baseband \ @@ -7,5 +21,4 @@ find firmware/common \ firmware/test/baseband \ \( -iname '*.h' -o -iname '*.hpp' -o -iname '*.c' -o -iname '*.cpp' \) \ -print0 | \ - xargs -0 clang-format-18 -style=file -i - + xargs -0 -r "$CF" -style=file -i diff --git a/tools/clang-format-bin/18.1.8/linux-arm64/clang-format b/tools/clang-format-bin/18.1.8/linux-arm64/clang-format new file mode 100755 index 000000000..bb75ea2a3 Binary files /dev/null and b/tools/clang-format-bin/18.1.8/linux-arm64/clang-format differ diff --git a/tools/clang-format-bin/18.1.8/linux-arm64/lib/libtinfo.so.5 b/tools/clang-format-bin/18.1.8/linux-arm64/lib/libtinfo.so.5 new file mode 100644 index 000000000..8fbf4440b Binary files /dev/null and b/tools/clang-format-bin/18.1.8/linux-arm64/lib/libtinfo.so.5 differ diff --git a/tools/clang-format-bin/18.1.8/linux-x86_64/clang-format b/tools/clang-format-bin/18.1.8/linux-x86_64/clang-format new file mode 100755 index 000000000..36e8a0db9 Binary files /dev/null and b/tools/clang-format-bin/18.1.8/linux-x86_64/clang-format differ diff --git a/tools/clang-format-bin/18.1.8/linux-x86_64/lib/libtinfo.so.5 b/tools/clang-format-bin/18.1.8/linux-x86_64/lib/libtinfo.so.5 new file mode 100644 index 000000000..6caaddc9f Binary files /dev/null and b/tools/clang-format-bin/18.1.8/linux-x86_64/lib/libtinfo.so.5 differ diff --git a/tools/clang-format-bin/18.1.8/macos-arm64/clang-format b/tools/clang-format-bin/18.1.8/macos-arm64/clang-format new file mode 100755 index 000000000..e12a38676 Binary files /dev/null and b/tools/clang-format-bin/18.1.8/macos-arm64/clang-format differ diff --git a/tools/clang-format-bin/18.1.8/windows-x86_64/clang-format.exe b/tools/clang-format-bin/18.1.8/windows-x86_64/clang-format.exe new file mode 100755 index 000000000..84beede49 Binary files /dev/null and b/tools/clang-format-bin/18.1.8/windows-x86_64/clang-format.exe differ diff --git a/tools/clang-format.ps1 b/tools/clang-format.ps1 new file mode 100644 index 000000000..e510348bb --- /dev/null +++ b/tools/clang-format.ps1 @@ -0,0 +1,12 @@ +$ErrorActionPreference = "Stop" + +$Ver = "18.1.8" +$Root = Resolve-Path (Join-Path $PSScriptRoot "..") + +$Bin = Join-Path $Root "tools\clang-format-bin\$Ver\windows-x86_64\clang-format.exe" +if (-not (Test-Path $Bin)) { + throw "Missing clang-format binary: $Bin" +} + +& $Bin @args +exit $LASTEXITCODE diff --git a/tools/clang-format.sh b/tools/clang-format.sh new file mode 100755 index 000000000..6b0d48625 --- /dev/null +++ b/tools/clang-format.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -euo pipefail + +VER="18.1.8" +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" + +OS="$(uname -s)" +ARCH="$(uname -m)" + +case "$OS" in + Linux) OS_ID="linux" ;; + Darwin) OS_ID="macos" ;; + *) echo "Unsupported OS: $OS" >&2; exit 2 ;; +esac + +case "$ARCH" in + x86_64|amd64) ARCH_ID="x86_64" ;; + arm64|aarch64) ARCH_ID="arm64" ;; + *) echo "Unsupported arch: $ARCH" >&2; exit 2 ;; +esac + +BIN="$ROOT/tools/clang-format-bin/$VER/$OS_ID-$ARCH_ID/clang-format" +if [[ ! -x "$BIN" ]]; then + echo "Missing clang-format binary: $BIN" >&2 + exit 3 +fi + +# Fix Ubuntu-built clang-format runtime deps on Debian/Arch by vendoring needed libs +if [[ "$OS_ID" == "linux" ]]; then + LIBDIR="$ROOT/tools/clang-format-bin/$VER/$OS_ID-$ARCH_ID/lib" + if [[ -d "$LIBDIR" ]]; then + export LD_LIBRARY_PATH="$LIBDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" + fi +fi + +exec "$BIN" "$@" diff --git a/tools/fetch-clang-format.sh b/tools/fetch-clang-format.sh new file mode 100755 index 000000000..3f875273f --- /dev/null +++ b/tools/fetch-clang-format.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +set -euo pipefail + +VER="18.1.8" +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +DEST="$ROOT/tools/clang-format-bin/$VER" +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +base="https://github.com/llvm/llvm-project/releases/download/llvmorg-${VER}" + +# Archives you said you want to support +LINUX_X86="clang+llvm-${VER}-x86_64-linux-gnu-ubuntu-18.04.tar.xz" +LINUX_A64="clang+llvm-${VER}-aarch64-linux-gnu.tar.xz" +MAC_A64="clang+llvm-${VER}-arm64-apple-macos11.tar.xz" +WIN_X86="clang+llvm-${VER}-x86_64-pc-windows-msvc.tar.xz" + +mkdir -p \ + "$DEST/linux-x86_64" \ + "$DEST/linux-arm64" \ + "$DEST/macos-arm64" \ + "$DEST/windows-x86_64" + +fetch_and_extract() { + local archive="$1" + local outpath="$2" + local inner="$3" + + echo "Downloading $archive" + curl -fsSL "$base/$archive" -o "$TMP/$archive" + + echo "Extracting $inner" + tar -xf "$TMP/$archive" -C "$TMP" + + # The tarball extracts into a single top-level directory; find it + local topdir + topdir="$(find "$TMP" -maxdepth 1 -type d -name "clang+llvm-${VER}-*" | head -n 1)" + if [[ -z "${topdir:-}" ]]; then + echo "Could not locate extracted directory for $archive" >&2 + exit 4 + fi + + if [[ ! -f "$topdir/$inner" ]]; then + echo "Missing expected file inside archive: $inner" >&2 + exit 5 + fi + + cp "$topdir/$inner" "$outpath" + rm -rf "$topdir" +} + +# Linux x86_64 -> clang-format +fetch_and_extract "$LINUX_X86" "$DEST/linux-x86_64/clang-format" "bin/clang-format" +chmod +x "$DEST/linux-x86_64/clang-format" + +# Linux arm64 -> clang-format +fetch_and_extract "$LINUX_A64" "$DEST/linux-arm64/clang-format" "bin/clang-format" +chmod +x "$DEST/linux-arm64/clang-format" + +# macOS arm64 -> clang-format +fetch_and_extract "$MAC_A64" "$DEST/macos-arm64/clang-format" "bin/clang-format" +chmod +x "$DEST/macos-arm64/clang-format" + +# Windows x86_64 -> clang-format.exe +fetch_and_extract "$WIN_X86" "$DEST/windows-x86_64/clang-format.exe" "bin/clang-format.exe" + +echo +echo "Done. Installed pinned clang-format binaries under:" +echo " $DEST" +echo +echo "Verify:" +"$ROOT/tools/clang-format" --version || true + diff --git a/tools/get_libtinfo_linux_x86_64_arm64.sh b/tools/get_libtinfo_linux_x86_64_arm64.sh new file mode 100755 index 000000000..3dd1c1333 --- /dev/null +++ b/tools/get_libtinfo_linux_x86_64_arm64.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail + +VER="18.1.8" +X86_DEST="clang-format-bin/${VER}/linux-x86_64/lib" +ARM_DEST="clang-format-bin/${VER}/linux-arm64/lib" + +mkdir -p "$X86_DEST" "$ARM_DEST" + +docker run --rm \ + --platform linux/amd64 \ + -v "$PWD":/repo \ + ubuntu:18.04 \ + bash -lc " + set -euo pipefail + export DEBIAN_FRONTEND=noninteractive + + apt-get update + apt-get install -y --no-install-recommends ca-certificates dpkg-dev curl + + mkdir -p /tmp/dl /tmp/t + cd /tmp/dl + + echo '[*] amd64: download + extract libtinfo5' + apt-get download libtinfo5 + rm -rf /tmp/t && mkdir -p /tmp/t + dpkg-deb -x /tmp/dl/libtinfo5_*_amd64.deb /tmp/t + cp -av /tmp/t/lib/x86_64-linux-gnu/libtinfo.so.5* /repo/${X86_DEST}/ + + echo '[*] arm64: download libtinfo5 .deb directly + extract' + # This is the Ubuntu 18.04 (bionic-updates) arm64 libtinfo5 package matching the amd64 one above. + ARM_DEB='libtinfo5_6.1-1ubuntu1.18.04.1_arm64.deb' + ARM_URL='http://ports.ubuntu.com/ubuntu-ports/pool/main/n/ncurses/'\"\$ARM_DEB\" + + curl -fsSL \"\$ARM_URL\" -o \"/tmp/dl/\$ARM_DEB\" + + rm -rf /tmp/t && mkdir -p /tmp/t + dpkg-deb -x \"/tmp/dl/\$ARM_DEB\" /tmp/t + cp -av /tmp/t/lib/aarch64-linux-gnu/libtinfo.so.5* /repo/${ARM_DEST}/ + + echo + echo '[*] Done. Repo now contains:' + ls -la /repo/${X86_DEST} || true + ls -la /repo/${ARM_DEST} || true + "