fix(c): simplify the template config files

This commit is contained in:
Sigmanificient 2024-05-07 09:08:20 +02:00
parent 57769ca9ed
commit d981bc409e
10 changed files with 82 additions and 119 deletions

2
c/.gitattributes vendored
View file

@ -1,2 +0,0 @@
* text=lf
* eol=lf

63
c/.gitignore vendored
View file

@ -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

View file

@ -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"]

View file

@ -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

View file

@ -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

View file

@ -1,13 +0,0 @@
{ stdenv, gnumake }:
stdenv.mkDerivation {
name = "hello";
src = ./.;
nativeBuildInputs = [ gnumake ];
enableParallelBuilding = true;
installPhase = ''
install -D hello $out/bin/hello --mode 0755
'';
}

View file

@ -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 { }; };
};
}

7
c/hello.nix Normal file
View file

@ -0,0 +1,7 @@
{ stdenv }:
stdenv.mkDerivation {
name = "hello";
src = ./.;
env.BINDIR = "${placeholder "out"}/bin";
}

View file

@ -1,16 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#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;
}

View file

@ -1,4 +0,0 @@
columns = 80
sort = "lines"
types = ["C", "C Header", "Makefile", "Markdown"]
treat_doc_strings_as_comments = true