From aaa867aade389708cc8fe429f1365f2755fe75a8 Mon Sep 17 00:00:00 2001 From: Avimitin Date: Sat, 5 Oct 2024 14:36:00 +0800 Subject: [PATCH] [nix] move linting into derivation This commit move code linting to derivation, so we can force developer to format their code at local build. Signed-off-by: Avimitin --- .github/workflows/lint.yml | 74 -------------------------------------- build.sc | 5 ++- difftest/default.nix | 13 +++++++ nix/pkgs/mill-builder.nix | 8 +++++ nix/t1/mill-modules.nix | 35 +++++++++++++++++- 5 files changed, 59 insertions(+), 76 deletions(-) delete mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml deleted file mode 100644 index a58944e73..000000000 --- a/.github/workflows/lint.yml +++ /dev/null @@ -1,74 +0,0 @@ -name: Code linting -on: - pull_request: - types: - - opened - - synchronize - - reopened - - ready_for_review - - labeled -env: - USER: runner - -# Cancel the current workflow when new commit pushed -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number }} - cancel-in-progress: true - -jobs: - checkfmt: - name: "Check formats" - strategy: - fail-fast: false - runs-on: [self-hosted, linux, nixos] - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - ref: ${{ github.head_ref }} - - name: "Run testcases" - run: | - RET=0 - warnOnFileChanged() { - if ! git diff -q --exit-code; then - RET=1 - local msg="$1"; shift - echo "$msg" - echo "$msg" >> $GITHUB_STEP_SUMMARY - git reset --hard - fi - } - - nix develop '.#t1.elaborator' -c mill -i configgen.reformat - nix develop '.#t1.elaborator' -c mill -i elaborator.reformat - nix develop '.#t1.elaborator' -c mill -i omreader.reformat - nix develop '.#t1.elaborator' -c mill -i omreaderlib.reformat - nix develop '.#t1.elaborator' -c mill -i rocketv.reformat - nix develop '.#t1.elaborator' -c mill -i rocketemu.reformat - nix develop '.#t1.elaborator' -c mill -i t1.reformat - nix develop '.#t1.elaborator' -c mill -i t1emu.reformat - nix develop '.#t1.elaborator' -c mill -i t1rocket.reformat - nix develop '.#t1.elaborator' -c mill -i t1rocketemu.reformat - warnOnFileChanged "* Scala format fail, please run 'nix develop '.#t1.elaborator' -c mill -i _.reformat'" - - nix fmt - warnOnFileChanged "* Nix format fail, please run 'nix fmt'" - - pushd difftest - nix shell '.#cargo' '.#rustfmt' -c cargo fmt - warnOnFileChanged "* Cargo format fail, please run 'cd difftest; cargo -- format'" - popd - - # Disable for now - # if nix run '.#ripgrep' -- '\p{Script=Han}' t1 > zh-hans.txt; then - # RET=1 - # msg="* Found ZH_CN comments" - # echo "$msg" - # echo "$msg" >> $GITHUB_STEP_SUMMARY - # echo '```text' >> $GITHUB_STEP_SUMMARY - # cat zh-hans.txt >> $GITHUB_STEP_SUMMARY - # echo '```' >> $GITHUB_STEP_SUMMARY - # git reset --hard - # fi - - exit $RET diff --git a/build.sc b/build.sc index c57817d0f..42332b5c5 100644 --- a/build.sc +++ b/build.sc @@ -16,6 +16,9 @@ import $file.dependencies.`berkeley-hardfloat`.common import $file.dependencies.rvdecoderdb.common import $file.common +// Required for scalafmt to recognize which file to format +def buildSources = T.sources(os.pwd / "build.sc") + object v { val scala = "2.13.14" val mainargs = ivy"com.lihaoyi::mainargs:0.5.0" @@ -150,7 +153,7 @@ trait T1Emulator extends millbuild.common.T1EmulatorModule with ScalafmtModule { def chiselIvy = None } -object rocketemu extends RocketEmulator +object rocketemu extends RocketEmulator trait RocketEmulator extends millbuild.common.RocketEmulatorModule with ScalafmtModule { def scalaVersion = T(v.scala) diff --git a/difftest/default.nix b/difftest/default.nix index acdff20d2..38a672186 100644 --- a/difftest/default.nix +++ b/difftest/default.nix @@ -1,5 +1,6 @@ { lib , rustPlatform +, rustfmt , libspike , libspike_interfaces , rtlDesignMetadata @@ -31,9 +32,21 @@ rustPlatform.buildRustPackage { ./dpi_t1rocket ./Cargo.lock ./Cargo.toml + ./.rustfmt.toml ]; }; + 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/pkgs/mill-builder.nix b/nix/pkgs/mill-builder.nix index 8b5f3efbc..c74b6379d 100644 --- a/nix/pkgs/mill-builder.nix +++ b/nix/pkgs/mill-builder.nix @@ -23,6 +23,14 @@ let mill -i __.prepareOffline mill -i __.scalaCompilerClasspath + + # mill doesn't put scalafmt version into module ivy deps. + # It downloads scalafmt only when checkFormat/reformat is explicitly trigger. + # And we don't want to wait too long for a dependencies task, so here is the solution: + # "checkFormat" the "build.sc" file so that mill will download scalafmt for us, + # and we don't need to wait too long for formatting. + mill -i mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll buildSources + runHook postBuild ''; diff --git a/nix/t1/mill-modules.nix b/nix/t1/mill-modules.nix index c451ce860..5575d2e25 100644 --- a/nix/t1/mill-modules.nix +++ b/nix/t1/mill-modules.nix @@ -33,6 +33,7 @@ let ./../../t1rocket/src ./../../t1rocketemu/src ./../../rocketemu/src + ./../../.scalafmt.conf ]; }; @@ -43,9 +44,10 @@ let fileset = unions [ ./../../build.sc ./../../common.sc + ./../../.scalafmt.conf ]; }; - millDepsHash = "sha256-ZK3m6VKG3PChoj6U2b6bVd+Z2/xkZrPxqaLRVvj7QgQ="; + millDepsHash = "sha256-gBxEO6pGD0A1RxZW2isjPcHDf+b9Sr++7eq6Ezngiio="; nativeBuildInputs = [ dependencies.setupHook ]; }; @@ -80,11 +82,42 @@ let outputs = [ "out" "configgen" "elaborator" "t1package" ]; + # Check code format before starting build, so that we can enforce all developer run reformat before build. + configurePhase = '' + runHook preConfigure + + _targetsToCheck=( + "configgen" + "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 + ''; + buildPhase = '' + runHook preBuild + mill -i '__.assembly' mill -i t1package.sourceJar mill -i t1package.chiselPluginJar + + runHook postBuild ''; installPhase = ''