diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 000000000..e25e9b3a6 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,30 @@ +name: Code format check +on: + pull_request: + types: + - opened + - synchronize + - reopened + - ready_for_review + - labeled + +# Cancel the current workflow when new commit pushed +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + check-format: + if: '! github.event.pull_request.draft' + name: "Check code format" + runs-on: [self-hosted, linux, nixos] + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + - name: "Check Scala code format" + run: nix run ".#t1.elaborator.format" check + - name: "Check difftest code format" + run: | + nix shell '.#cargo' '.#rustfmt' -c bash -c 'cd difftest && cargo fmt --check' + diff --git a/difftest/default.nix b/difftest/default.nix index dc7f760d0..bd2d56743 100644 --- a/difftest/default.nix +++ b/difftest/default.nix @@ -1,6 +1,5 @@ { lib , rustPlatform -, rustfmt , libspike , libspike_interfaces , rtlDesignMetadata @@ -36,17 +35,6 @@ rustPlatform.buildRustPackage { ]; }; - nativeBuildInputs = [ - rustfmt - ]; - - postConfigure = '' - if ! cargo fmt --check; then - echo "Please run 'cd difftest && cargo fmt' before building emulator!" >&2 - exit 1 - fi - ''; - buildFeatures = [ ] ++ lib.optionals (lib.hasPrefix "dpi" moduleType) [ "dpi_common/${emuType}" ] ++ lib.optionals enableTrace [ "dpi_common/trace" ]; buildAndTestSubdir = "./${moduleType}"; diff --git a/nix/t1/mill-modules.nix b/nix/t1/mill-modules.nix index 5eaa5d1fa..af84df9fc 100644 --- a/nix/t1/mill-modules.nix +++ b/nix/t1/mill-modules.nix @@ -11,6 +11,7 @@ , circt-full , jextract-21 , add-determinism +, writeShellApplication , dependencies }: @@ -80,47 +81,10 @@ let CIRCT_INSTALL_PATH = circt-full; JEXTRACT_INSTALL_PATH = jextract-21; JAVA_TOOL_OPTIONS = "--enable-preview"; - formatHook = '' - targets=( $(mill -i resolve _.reformat) ) - localTargets=() - for t in ''${targets[@]}; do - if ! mill -i show "''${t//reformat/sources}" | grep -q dependencies; then - localTargets+=($t) - fi - done - for t in ''${localTargets[@]}; do - mill -i "$t" - done - ''; }; - outputs = [ "out" "omreader" "elaborator" "t1package" ]; - # Check code format before starting build, so that we can enforce all developer run reformat before build. - configurePhase = '' - runHook preConfigure - - _targetsToCheck=( - "elaborator" - "omreader" - "omreaderlib" - "rocketemu" - "rocketv" - "t1" - "t1emu" - "t1rocket" - "t1rocketemu" - ) - for _t in ''${_targetsToCheck[@]}; do - if ! mill -i "$_t".checkFormat; then - echo "[ERROR] Please run 'mill -i $_t.reformat' before elaborate!" >&2 - exit 1 - fi - done - unset _targetsToCheck - - runHook postConfigure - ''; + outputs = [ "out" "omreader" "elaborator" "t1package" ]; buildPhase = '' runHook preBuild @@ -165,6 +129,52 @@ let --add-flags "-Djava.library.path=${circt-full}/lib" \ --add-flags "-cp $out/share/java/omreader.jar" ''; + + passthru.format = writeShellApplication { + name = "mill-format-for-t1"; + runtimeInputs = [ mill ]; + text = '' + # shellcheck disable=SC1091 + source ${dependencies.setupHook}/nix-support/setup-hook + setupSubmodules + + subcmd="''${1:-}" + [[ -z "$subcmd" ]] && \ + echo "no subcmd specify, available: (check, run)" >&2 && exit 1 + + _targetsToCheck=( + "elaborator" + "omreader" + "omreaderlib" + "rocketemu" + "rocketv" + "t1" + "t1emu" + "t1rocket" + "t1rocketemu" + ) + + case "$subcmd" in + check) + for _t in "''${_targetsToCheck[@]}"; do + if ! mill -i "$_t".checkFormat; then + echo "[ERROR] Please run 'mill -i $_t.reformat' before elaborate!" >&2 + exit 1 + fi + done + ;; + run) + for _t in "''${_targetsToCheck[@]}"; do + mill -i "$_t".reformat || true + done + ;; + *) + echo "Invalid subcmd $subcmd, available: (check, run)" >&2 + exit 1 + ;; + esac + ''; + }; }; in self