Skip to content

Commit

Permalink
Add nix builder, extract pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigmanificient committed Dec 4, 2024
1 parent dbb7aa7 commit 25370f2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ CC := gcc

CFLAGS += -std=c99 -pedantic
CFLAGS += -iquote $/
CFLAGS += -O2

ifneq ($(EXPLICIT_FLAGS),1)
CFLAGS += @$/base_warnings
Expand Down Expand Up @@ -35,15 +34,15 @@ include $/mk-recipes.mk

release-flags := -O3 -DNDEBUG
#? crash: build the release binary, crash
$(eval $(call mk-binary, crash, SRC, $(release-flags)))
$(eval $(call mk-recipe-binary, crash, SRC, $(release-flags)))

#? debug: build with debug logs an eponym binary
debug-flags := -fanalyzer -DDEBUG=1 -g3
$(eval $(call mk-binary, debug, SRC, $(debug-flags)))
debug-flags := -O2 -fanalyzer -DDEBUG=1 -g3
$(eval $(call mk-recipe-binary, debug, SRC, $(debug-flags)))

#? check: build with all warnings and sanitizers an eponym binary
check-flags := $(debug-flags) -fsanitize=address,leak,undefined -Wpadded
$(eval $(call mk-binary, check, SRC, $(check-flags)))
$(eval $(call mk-recipe-binary, check, SRC, $(check-flags)))

.PHONY: all
all: $(out-crash)
Expand Down Expand Up @@ -76,8 +75,8 @@ help: #? help: show this help message

#? install: package within the provided dir
.PHONY: install
install: $(OUT)
install -Dm755 -t $(PREFIX)/bin $(OUT)
install: $(out-crash)
install -Dm755 -t $(PREFIX)/bin $(out-crash)

ifneq ($(shell command -v tput),)
ifneq ($(shell tput colors),0)
Expand Down
26 changes: 7 additions & 19 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,7 @@
formatter = forAllSystems (pkgs: pkgs.alejandra);

checks = forAllSystems (pkgs: let
pyrun = name: pkgs.python310.interpreter + " " + name;
hooks = {
alejandra.enable = true;
black.enable = true;
trim-trailing-whitespace.enable = true;

commit-name = {
enable = true;
name = "commit name";
stages = ["commit-msg"];
entry = pyrun ./scripts/check_commit_msg.py;
};

check-include-order = {
enable = true;
name = "commit name";
entry = pyrun ./scripts/check_include_order.py;
};
};
hooks = import ./nix/hooks.nix {inherit pkgs;};
in {
pre-commit-check = pre-commit-hooks.lib.${pkgs.system}.run {
inherit hooks;
Expand All @@ -60,5 +42,11 @@
];
};
});

packages = forAllSystems (pkgs: {
default = self.packages.${pkgs.system}.crash;
crash = pkgs.callPackage ./nix/crash.nix {};
bundle = pkgs.callPackage ./nix/crash.nix {withDevBinaries = true;};
});
};
}
4 changes: 2 additions & 2 deletions mk-recipes.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ $$(BUILD_DIR)/$(strip $1)/%.o: %.c
$$Q $$(CC) \
$$(CFLAGS) $$(CFLAGS_@$$(notdir $$(@:.o=))) $3 \
-o $$@ -c $$<
@ $$(LOG_TIME) "CC $$(C_PURPLE) $$(notdir $$@) $$(C_RESET)"
@ $$(log) "CC $$(C_PURPLE) $$(notdir $$@) $$(C_RESET)"

$$(out-$(strip $1)): $$(obj-$(strip $1))
@ mkdir -p $$(dir $$@)
$$Q $$(CC) -o $$@ $$(obj-$(strip $1)) \
$$(CFLAGS) $$(CFLAGS_@$$(notdir $$(@:.o=))) $3 \
$$(LDLIBS) $$(LDFLAGS)
@ $$(LOG_TIME) "LD $$(C_GREEN) $$@ $$(C_RESET)"
@ $$(log) "LD $$(C_GREEN) $$@ $$(C_RESET)"

_clean += $$(obj-$(strip $1))
_fclean += $$(out-$(strip $1))
Expand Down
27 changes: 27 additions & 0 deletions nix/crash.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
stdenv,
lib,
withDevBinaries ? false,
}:
stdenv.mkDerivation {
name = "crash";
version = "0.1";

src = ./..;
makeFlags = ["PREFIX=${placeholder "out"}"];

buildFlags = ["crash"] ++ (lib.optional withDevBinaries ["debug" "check"]);

postInstall = lib.optional withDevBinaries ''
install -Dm755 -t $out/bin debug
install -Dm755 -t $out/bin check
'';

meta = {
description = "Crash resilient auspicious shell.";
maintainers = with lib.maintainers; [savalet sigmanificient];
license = lib.licenses.mit;
platforms = lib.platforms.linux;
mainProgram = "crash";
};
}
20 changes: 20 additions & 0 deletions nix/hooks.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{pkgs}: let
pyrun = name: pkgs.python310.interpreter + " " + name;
in {
alejandra.enable = true;
black.enable = true;
trim-trailing-whitespace.enable = true;

commit-name = {
enable = true;
name = "check commit name";
stages = ["commit-msg"];
entry = pyrun ../scripts/check_commit_msg.py;
};

check-include-order = {
enable = true;
name = "check include order";
entry = pyrun ../scripts/check_include_order.py;
};
}

0 comments on commit 25370f2

Please sign in to comment.