From d981bc409ecd1da0eaa0908165e452438b60cee1 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Tue, 7 May 2024 09:08:20 +0200 Subject: [PATCH] fix(c): simplify the template config files --- c/.gitattributes | 2 -- c/.gitignore | 63 ++++++++++++++++++++++++++++++++++++++++----- c/Dockerfile | 29 --------------------- c/Makefile | 19 +++++--------- c/compile_flags.txt | 18 ------------- c/default.nix | 13 ---------- c/flake.nix | 29 +++++---------------- c/hello.nix | 7 +++++ c/main.c | 17 +++++------- c/tokei.toml | 4 --- 10 files changed, 82 insertions(+), 119 deletions(-) delete mode 100644 c/.gitattributes delete mode 100644 c/Dockerfile delete mode 100644 c/compile_flags.txt delete mode 100644 c/default.nix create mode 100644 c/hello.nix delete mode 100644 c/tokei.toml diff --git a/c/.gitattributes b/c/.gitattributes deleted file mode 100644 index 1457a77..0000000 --- a/c/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -* text=lf -* eol=lf diff --git a/c/.gitignore b/c/.gitignore index fc5e2c9..a07179f 100644 --- a/c/.gitignore +++ b/c/.gitignore @@ -1,15 +1,64 @@ # binaries hello -# build -*.[aod] - -# config +# language support compile_commands.json -.pre-commit-config.yaml .cache # nix .direnv -.envrc -result +result* +repl-result-* + +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/c/Dockerfile b/c/Dockerfile deleted file mode 100644 index 4551ac5..0000000 --- a/c/Dockerfile +++ /dev/null @@ -1,29 +0,0 @@ -# Credit to Mitchell Hashimoto -# post: https://mitchellh.com/writing/nix-with-dockerfiles - -# Nix builder -FROM nixos/nix:latest AS builder - -# Copy our source and setup our working dir. -COPY . /tmp/build -WORKDIR /tmp/build - -# Build our Nix environment -RUN nix \ - --extra-experimental-features "nix-command flakes" \ - --option filter-syscalls false \ - build - -# Copy the Nix store closure into a directory. The Nix store closure is the -# entire set of Nix store values that we need for our build. -RUN mkdir /tmp/nix-store-closure -RUN cp -R $(nix-store -qR result/) /tmp/nix-store-closure - -# Final image is based on scratch. We copy a bunch of Nix dependencies -# but they're fully self-contained so we don't need Nix anymore. -FROM scratch - -# Copy /nix/store -COPY --from=builder /tmp/nix-store-closure /nix/store -COPY --from=builder /tmp/build/result /app -CMD ["/app/bin/hello"] diff --git a/c/Makefile b/c/Makefile index a7f3da0..a4d834c 100644 --- a/c/Makefile +++ b/c/Makefile @@ -1,21 +1,12 @@ -.POSIX: -.SUFFIXES: .o - CC ?= gcc +CFLAGS += -pedantic -Wall -Wextra -O2 + OUT := hello +BINDIR ?= /usr/bin SRC += main.c OBJ := $(SRC:.c=.o) -CFLAGS += @compile_flags.txt -CFLAGS += -ffunction-sections -fdata-sections - -LDFLAGS := -fwhole-program -flto -LDFLAGS += -Wl,--gc-sections -s - -RM ?= rm -f - -.DEFAULT_GOAL: all .PHONY: all all: $(OUT) @@ -33,3 +24,7 @@ fclean: clean .PHONY: re .NOTPARALLEL: re re: fclean all + +.PHONY: install +install: + install -D hello ${BINDIR}/hello --mode 0755 diff --git a/c/compile_flags.txt b/c/compile_flags.txt deleted file mode 100644 index 19adf53..0000000 --- a/c/compile_flags.txt +++ /dev/null @@ -1,18 +0,0 @@ --std=c99 --pedantic --pipe - --Wall --Wcast-qual --Wconversion --Werror=return-type --Werror=vla-larger-than=0 --Wextra --Wmissing-prototypes --Wshadow --Wstrict-prototypes --Wwrite-strings - --O2 --march=native --mtune=native diff --git a/c/default.nix b/c/default.nix deleted file mode 100644 index 444469b..0000000 --- a/c/default.nix +++ /dev/null @@ -1,13 +0,0 @@ -{ stdenv, gnumake }: -stdenv.mkDerivation { - name = "hello"; - - src = ./.; - nativeBuildInputs = [ gnumake ]; - - enableParallelBuilding = true; - - installPhase = '' - install -D hello $out/bin/hello --mode 0755 - ''; -} diff --git a/c/flake.nix b/c/flake.nix index d1fa496..4798ff9 100644 --- a/c/flake.nix +++ b/c/flake.nix @@ -8,37 +8,20 @@ let forAllSystems = function: - nixpkgs.lib.genAttrs [ - "x86_64-linux" - "aarch64-linux" - "x86_64-darwin" - "aarch64-darwin" - "i686-linux" - "mipsel-linux" - "powerpc64le-linux" - ] (system: function nixpkgs.legacyPackages.${system}); + nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed ( + system: function nixpkgs.legacyPackages.${system} + ); in rec { devShells = forAllSystems (pkgs: { - default = pkgs.mkShell { - hardeningDisable = [ "fortify" ]; - inputsFrom = pkgs.lib.attrsets.attrValues packages; - }; + default = pkgs.mkShell { inputsFrom = [ packages.${pkgs.system}.hello ]; }; }); packages = forAllSystems (pkgs: rec { default = hello; - hello = pkgs.callPackage ./default.nix { }; + hello = pkgs.callPackage ./hello.nix { }; }); - overlays.default = final: prev: { hello = final.callPackage ./default.nix { }; }; - - apps = forAllSystems (pkgs: rec { - default = hello; - hello = { - program = "${packages.${pkgs.system}.hello}/bin/hello"; - type = "app"; - }; - }); + overlays.default = final: prev: { hello = prev.callPackage ./default.nix { }; }; }; } diff --git a/c/hello.nix b/c/hello.nix new file mode 100644 index 0000000..9b81015 --- /dev/null +++ b/c/hello.nix @@ -0,0 +1,7 @@ +{ stdenv }: +stdenv.mkDerivation { + name = "hello"; + src = ./.; + + env.BINDIR = "${placeholder "out"}/bin"; +} diff --git a/c/main.c b/c/main.c index 30a4d72..9d42f19 100644 --- a/c/main.c +++ b/c/main.c @@ -1,16 +1,11 @@ +#include #include -#include - -#define lengthof(sstr) (sizeof (sstr) / sizeof *(sstr)) -#define sstr_len(sstr) (lengthof(sstr) - 1) -#define sstr_unpack(sstr) (sstr), (sstr_len(sstr)) - -static const char GREETING[] = "hello, world!\n"; int main(void) { - return ( - write(STDOUT_FILENO, sstr_unpack(GREETING)) - == sstr_len(GREETING) - ) ? EXIT_SUCCESS : EXIT_FAILURE; + char greet[] = "hello, world!\n"; + int written = printf("%s", greet); + + return written == (sizeof(greet) - 1) + ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/c/tokei.toml b/c/tokei.toml deleted file mode 100644 index 8f7e17d..0000000 --- a/c/tokei.toml +++ /dev/null @@ -1,4 +0,0 @@ -columns = 80 -sort = "lines" -types = ["C", "C Header", "Makefile", "Markdown"] -treat_doc_strings_as_comments = true