From e38c25e73d0ac47ddfcdb8ace6b8535f46da3706 Mon Sep 17 00:00:00 2001 From: Randy Jordan Date: Sat, 20 Jun 2026 13:55:01 -0500 Subject: [PATCH] C templates --- c_dev/C.gitignore | 61 +++++++++++++++++++ c_dev/Makefile | 152 ++++++++++++++++++++++++++++++++++++++++++++++ c_dev/template.c | 33 ++++++++++ c_dev/template.h | 27 ++++++++ 4 files changed, 273 insertions(+) create mode 100644 c_dev/C.gitignore create mode 100644 c_dev/Makefile create mode 100644 c_dev/template.c create mode 100644 c_dev/template.h diff --git a/c_dev/C.gitignore b/c_dev/C.gitignore new file mode 100644 index 0000000..fa57844 --- /dev/null +++ b/c_dev/C.gitignore @@ -0,0 +1,61 @@ +# 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 + +# debug information files +*.dwo + +#ctags +/tags + +# test binaries +/tests/bin/* diff --git a/c_dev/Makefile b/c_dev/Makefile new file mode 100644 index 0000000..7b47d24 --- /dev/null +++ b/c_dev/Makefile @@ -0,0 +1,152 @@ +# Compiler Flags +CC := gcc +CFLAGS := -g -Wall -Wextra -Werror -pedantic -fno-omit-frame-pointer +DEPFLAGS = -MMD -MP + +# Directory Variables +LIBDIR := lib +OBJ := obj +INC := include +SRC := src +TEST := tests + +# Install Paths (override with: make install PREFIX=/usr/local) +PREFIX ?= /usr/local +INSTALL_LIB = $(PREFIX)/lib +INSTALL_INC = $(PREFIX)/include + +# Platform Detection and Library Linking +ifeq ($(OS), Windows_NT) + PLATFORM := win32 + PLATFORM_SRCS := $(wildcard $(SRC)/win32_*.c) + PLATFORM_OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(PLATFORM_SRCS)) + PLATFORM_FLAGS := -D WIN32 + PLATFORM_LIBS := -mwindows +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Linux) + PLATFORM := linux + PLATFORM_SRCS := $(wildcard $(SRC)/linux_*.c) + PLATFORM_OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(PLATFORM_SRCS)) + PLATFORM_FLAGS := -D LINUX + PLATFORM_LIBS := +else ifeq ($(UNAME_S), Darwin) + PLATFORM := mac + PLATFORM_SRCS := $(wildcard $(SRC)/mac_*.c) + PLATFORM_OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(PLATFORM_SRCS)) + PLATFORM_FLAGS := -D MACOS + PLATFORM_LIBS := +else + $(error Unsupported platform: $(UNAME_S)) +endif +endif + +# Add platform flags +CFLAGS += $(PLATFORM_FLAGS) + +# Filepath Pattern Matching +LIB := $(LIBDIR)/lib.a + +# Filter out ALL platform-prefixed files, then add back the correct platform's +SRCS := $(filter-out \ + $(wildcard $(SRC)/win32_*.c) \ + $(wildcard $(SRC)/linux_*.c) \ + $(wildcard $(SRC)/macos_*.c), \ + $(wildcard $(SRC)/*.c)) \ + $(PLATFORM_SRCS) + +OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS)) +TESTS := $(wildcard $(TEST)/*.c) +TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS)) + +# Commands must be labeled PHONY +.PHONY: all release clean test install uninstall + +# Compiler Release Flags (no sanitizers — those belong in CI/debug builds) +release: CFLAGS := -Wall -Wextra -Werror -pedantic -fno-omit-frame-pointer -O2 -DNDEBUG $(PLATFORM_FLAGS) +release: clean $(LIB) + @echo "\nRelease build complete for platform: $(PLATFORM)\n" + +# Target for compilation +all: $(LIB) + @echo "\nBuilt for platform: $(PLATFORM)\n" + +# Target / Dependencies +$(LIB): $(OBJS) | $(LIBDIR) + ar -cvrs $@ $^ + +# Compile with automatic dependency tracking +$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ) + $(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@ + +$(OBJ)/%.o: $(SRC)/%.c | $(OBJ) + $(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@ + +$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin + $(CC) $(CFLAGS) $< $(LIB) $(PLATFORM_LIBS) -o $@ + +# Make directories if none +$(LIBDIR): + mkdir $@ +$(OBJ): + mkdir $@ +$(TEST)/bin: + mkdir -p $@ + +# Run the tests in the bin folder and track results +# Uses perl for nanosecond timing — portable across Linux and macOS +test: $(LIB) $(TESTBINS) + @SUCCESS=0; FAILURE=0; \ + RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'; \ + for t in $(TESTBINS); do \ + NAME=$$(basename $$t); \ + START=$$(perl -MTime::HiRes=time -e 'printf "%d\n", time()*1000'); \ + if $$t; then \ + RET=0; \ + else \ + RET=$$?; \ + fi; \ + END=$$(perl -MTime::HiRes=time -e 'printf "%d\n", time()*1000'); \ + ELAPSED_MS=$$((END - START)); \ + if [ $$RET -eq 0 ]; then \ + printf "%-20s %bPASS%b (%b%4d ms%b)\n" "$$NAME" "$$GREEN" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \ + SUCCESS=$$((SUCCESS + 1)); \ + else \ + printf "%-20s %bFAIL%b (%b%4d ms%b)\n" "$$NAME" "$$RED" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \ + FAILURE=$$((FAILURE + 1)); \ + fi; \ + done; \ + printf "\nTests completed\n"; \ + printf "SUCCESS: %b%d%b\n" "$$GREEN" "$$SUCCESS" "$$NC"; \ + printf "FAILURE: %b%d%b\n" "$$RED" "$$FAILURE" "$$NC"; \ + test $$FAILURE -eq 0 + +# Install library and public headers +install: $(LIB) + @echo "Installing to $(PREFIX)..." + install -d $(INSTALL_LIB) + install -d $(INSTALL_INC) + install -m 644 $(LIB) $(INSTALL_LIB)/ + @if [ -d $(INC) ]; then \ + install -m 644 $(INC)/*.h $(INSTALL_INC)/; \ + echo "Installed headers from $(INC)/"; \ + else \ + echo "Warning: no $(INC)/ directory found — skipping header install"; \ + fi + @echo "Install complete: lib -> $(INSTALL_LIB) headers -> $(INSTALL_INC)" + +uninstall: + @echo "Removing installed files from $(PREFIX)..." + $(RM) $(INSTALL_LIB)/lib.a + @if [ -d $(INC) ]; then \ + for h in $(INC)/*.h; do \ + $(RM) $(INSTALL_INC)/$$(basename $$h); \ + done; \ + fi + @echo "Uninstall complete" + +clean: + $(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/ + +# Pull in generated dependency files (silently ignored if absent) +-include $(DEPS) diff --git a/c_dev/template.c b/c_dev/template.c new file mode 100644 index 0000000..b0b8042 --- /dev/null +++ b/c_dev/template.c @@ -0,0 +1,33 @@ +/* - | Copyright | ------------------------------------------------------------ + Copyright (c) 2026 Randy Jordan + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + * --------------------------------------------------------------------------*/ +#define DEBUG +#include "../include/template.h" + +#include +#include + +int main(int argc, char *argv[]){ + (void) argc; + (void) argv; + return EXIT_SUCCESS; +} diff --git a/c_dev/template.h b/c_dev/template.h new file mode 100644 index 0000000..92700ad --- /dev/null +++ b/c_dev/template.h @@ -0,0 +1,27 @@ +/* - | Copyright | ------------------------------------------------------------ + Copyright (c) 2026 Randy Jordan + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + * --------------------------------------------------------------------------*/ +#ifndef TEMPLATE_H +#define TEMPLATE_H + + +#endif // template.h