Skip to content

Commit

Permalink
[chore] replace local format check with CI format check
Browse files Browse the repository at this point in the history
Signed-off-by: Avimitin <[email protected]>
  • Loading branch information
Avimitin authored and sequencer committed Oct 22, 2024
1 parent 20b0367 commit 92d642e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 78 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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'
3 changes: 0 additions & 3 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ 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.15"
val mainargs = ivy"com.lihaoyi::mainargs:0.5.0"
Expand Down
12 changes: 0 additions & 12 deletions difftest/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{ lib
, rustPlatform
, rustfmt
, libspike
, libspike_interfaces
, rtlDesignMetadata
Expand Down Expand Up @@ -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}";

Expand Down
10 changes: 0 additions & 10 deletions nix/pkgs/mill-builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ 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.
if ! mill -i mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll buildSources; then
echo "[ERROR] build.sc is misformatted, please run 'mill -i mill.scalalib.scalafmt.ScalafmtModule/reformatAll buildSources'" >&2
exit 1
fi
runHook postBuild
'';

Expand Down
90 changes: 49 additions & 41 deletions nix/t1/mill-modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
, circt-full
, jextract-21
, add-determinism
, writeShellApplication

, dependencies
}:
Expand All @@ -33,7 +34,6 @@ let
./../../t1rocket/src
./../../t1rocketemu/src
./../../rocketemu/src
./../../.scalafmt.conf
];
};

Expand All @@ -44,10 +44,9 @@ let
fileset = unions [
./../../build.sc
./../../common.sc
./../../.scalafmt.conf
];
};
millDepsHash = "sha256-pixG96IxJsYlgIU+DVxGHky6G5nMfHXphEq5A/xLP7Q=";
millDepsHash = "sha256-XvGLNLOC7OEwfC7SB5zBdB64VjROBkwgIcHx+9FHmSs=";
nativeBuildInputs = [ dependencies.setupHook ];
};

Expand Down Expand Up @@ -80,47 +79,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
Expand Down Expand Up @@ -165,6 +127,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
3 changes: 0 additions & 3 deletions script/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import mill.scalalib.TestModule.Utest
import mill.util.Jvm
import coursier.maven.MavenRepository

// Required for scalafmt to recognize which file to format
def buildSources = T.sources(os.pwd / "build.sc")

trait ScriptModule extends ScalaModule with ScalafmtModule {
val scala3 = "3.3.3"
val mainargs = ivy"com.lihaoyi::mainargs:0.5.0"
Expand Down
10 changes: 1 addition & 9 deletions script/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ let
fileset = unions [
scriptSrc
./build.sc
./.scalafmt.conf
];
};

Expand All @@ -29,10 +28,9 @@ let
root = ./.;
fileset = unions [
./build.sc
./.scalafmt.conf
];
};
millDepsHash = "sha256-y8tAFwctiU6ehghuf7KP73DuWCGCnAAdIXOIPwT+QOo=";
millDepsHash = "sha256-QQ5gCbvovC55t9MmfCNTvNFdD6FcNqmLmfhT9qJhQQc=";
};

passthru.withLsp = self.overrideAttrs (old: {
Expand Down Expand Up @@ -66,12 +64,6 @@ let
buildPhase = ''
runHook preBuild
# Not debug build, check source format
if (( $enableNativeExe )); then
echo "Checking format"
mill -i ${moduleName}.checkFormat
fi
echo "Building JAR"
mill -i ${moduleName}.assembly
Expand Down

0 comments on commit 92d642e

Please sign in to comment.