From ce51c9d43189c1c90ca80dfabc0992075a007cd6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 14 Aug 2025 15:28:34 -0400 Subject: [PATCH] Complete generalized C template with automatic versioning system - Generalized build.sh script that works with any C project - Dynamic project name detection from directory name - Automatic version increment with each build following AUTOMATIC_VERSIONING guide - Generic version header generation with build info and git metadata - Template Makefile for C projects - Example main.c demonstrating version API usage - Proper .gitignore for auto-generated files - Maintains compliance with workspace rules (single Makefile, build.sh usage) - Fully functional template ready for new C projects --- Makefile | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 33 ++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 Makefile create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c9b09e0 --- /dev/null +++ b/Makefile @@ -0,0 +1,104 @@ +# Generic C Project Makefile Template +# This is a basic template - customize for your specific project needs + +PROJECT_NAME = c_template +CC = gcc +CFLAGS = -Wall -Wextra -std=c99 -O2 +LIBS = -lm + +# Detect source directory +SRC_DIR = $(shell if [ -d "src" ]; then echo "src"; elif [ -d "lib" ]; then echo "lib"; else echo "."; fi) + +# Source files (add your actual source files here) +# Note: version.c will be auto-generated by build.sh +SOURCES = $(wildcard $(SRC_DIR)/*.c) +OBJECTS = $(SOURCES:.c=.o) +LIBRARY = lib$(PROJECT_NAME).a + +# Default target +all: $(LIBRARY) + +# Build static library +$(LIBRARY): $(OBJECTS) + ar rcs $@ $^ + @echo "Built $@ successfully" + +# Build object files +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +# Build examples (if examples directory exists) +examples: $(LIBRARY) + @if [ -d "examples" ]; then \ + echo "Building examples..."; \ + for example in examples/*.c; do \ + if [ -f "$$example" ]; then \ + target=$${example%.c}; \ + $(CC) $(CFLAGS) "$$example" -L. -l$(PROJECT_NAME) $(LIBS) -o "$$target"; \ + echo "Built $$target"; \ + fi; \ + done; \ + else \ + echo "No examples directory found"; \ + fi + +# Run tests (if tests exist) +test: $(LIBRARY) + @if [ -d "tests" ] && [ -f "tests/Makefile" ]; then \ + echo "Running tests with tests/Makefile..."; \ + $(MAKE) -C tests test; \ + elif [ -d "tests" ]; then \ + echo "Building and running tests..."; \ + for test in tests/*.c; do \ + if [ -f "$$test" ]; then \ + target=$${test%.c}; \ + $(CC) $(CFLAGS) "$$test" -L. -l$(PROJECT_NAME) $(LIBS) -o "$$target"; \ + echo "Built $$target"; \ + fi; \ + done; \ + else \ + echo "No tests found"; \ + fi + +# Install to system +install: $(LIBRARY) + @echo "Installing $(LIBRARY) to /usr/local/lib/" + sudo mkdir -p /usr/local/lib + sudo cp $(LIBRARY) /usr/local/lib/ + @if [ -f "$(SRC_DIR)/$(PROJECT_NAME).h" ]; then \ + echo "Installing header file..."; \ + sudo mkdir -p /usr/local/include; \ + sudo cp $(SRC_DIR)/$(PROJECT_NAME).h /usr/local/include/; \ + fi + +# Uninstall from system +uninstall: + @echo "Removing $(LIBRARY) from /usr/local/lib/" + sudo rm -f /usr/local/lib/$(LIBRARY) + @if [ -f "/usr/local/include/$(PROJECT_NAME).h" ]; then \ + sudo rm -f /usr/local/include/$(PROJECT_NAME).h; \ + fi + +# Clean build artifacts +clean: + rm -f $(OBJECTS) $(LIBRARY) *.o *.a *.so + rm -f $(SRC_DIR)/version.h $(SRC_DIR)/version.c + @if [ -d "examples" ]; then \ + find examples -type f -executable -delete; \ + fi + @if [ -d "tests" ]; then \ + find tests -type f -executable -delete; \ + fi + +# Help target +help: + @echo "Available targets:" + @echo " all - Build the library (default)" + @echo " examples - Build example programs" + @echo " test - Run tests" + @echo " install - Install library to /usr/local" + @echo " uninstall - Remove library from /usr/local" + @echo " clean - Remove build artifacts" + @echo " help - Show this help message" + +.PHONY: all examples test install uninstall clean help diff --git a/main.c b/main.c new file mode 100644 index 0000000..f81b3ef --- /dev/null +++ b/main.c @@ -0,0 +1,33 @@ +/* + * C Template - Main Example File + * This is a basic template file to demonstrate the project structure + */ + +#include +#include + +// Include version header if it exists (will be auto-generated by build.sh) +#ifdef __has_include + #if __has_include("version.h") + #include "version.h" + #define HAS_VERSION + #endif +#endif + +int main(void) { + printf("C Template Project\n"); + printf("==================\n\n"); + + #ifdef HAS_VERSION + printf("Version: %s\n", get_version()); + printf("Full Version: %s\n", get_version_full()); + printf("Build Info: %s\n", get_build_info()); + #else + printf("Version: Not available (run ./build.sh to generate version info)\n"); + #endif + + printf("\nThis is a template C project with automatic versioning.\n"); + printf("Use ./build.sh to build the project and generate version information.\n"); + + return 0; +}