From 7e2a83a08d7b8bd8930aed0e9fcda614319d97f5 Mon Sep 17 00:00:00 2001 From: Mosder Date: Wed, 16 Sep 2026 16:50:23 +0200 Subject: feat: generalized and improved Makefile + added config options to it --- .gitignore | 1 + .make.example | 13 ++++++++++ Makefile | 79 ++++++++++++++++++++++++++++++----------------------------- 3 files changed, 54 insertions(+), 39 deletions(-) create mode 100644 .make.example diff --git a/.gitignore b/.gitignore index 9bf3746..8c69cd2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ compile_commands.json +.make diff --git a/.make.example b/.make.example new file mode 100644 index 0000000..ceb431f --- /dev/null +++ b/.make.example @@ -0,0 +1,13 @@ +# Makefile config + +# Change which C compiler will be used +# +# default: CC=cc +CC= + +# Change where the header and lib files will land +# .h files will land in $(TARGET_PREFIX)/include +# .so and/or .a file will land in $(TARGET_PREFIX)/lib +# +# default: TARGET_PREFIX=/usr/local +TARGET_PREFIX= diff --git a/Makefile b/Makefile index 087021e..989edcb 100644 --- a/Makefile +++ b/Makefile @@ -1,47 +1,48 @@ -CC=cc -CFLAGS=-std=c99 -Wall -Wextra -Wpedantic -fPIC -I./include -O2 -CFLAGS_TEST=-std=c99 -Wall -Wextra -Wpedantic -Og -ggdb +-include .make +ifeq ($(CC),) + CC=cc +endif +ifeq ($(TARGET_PREFIX),) + TARGET_PREFIX=/usr/local +endif + AR=ar rcs -RUN_TESTS=./run_tests -ifeq ($(shell uname -s),Darwin) - RUN_TESTS=DYLD_FALLBACK_LIBRARY_PATH="/usr/local/lib:$DYLD_FALLBACK_LIBRARY_PATH" - RUN_TESTS+= ./run_tests; rm -rf run_tests.dSYM/ -endif +LIB_DIR=$(TARGET_PREFIX)/lib +LIB_SUDO:=$(shell test -w $(LIB_DIR) || echo sudo) +INC_DIR=$(TARGET_PREFIX)/include +INC_SUDO:=$(shell test -w $(INC_DIR) || echo sudo) + +CFLAGS=-std=c99 -Wall -Wextra -Wpedantic -fPIC -I./include -O2 -c +SRCS:=$(shell find src | grep \\.c$) -both: shared static clean +TESTS_CFLAGS=-std=c99 -Wall -Wextra -Wpedantic -I$(INC_DIR) -Og -ggdb -c +TESTS_LDFLAGS=-L$(LIB_DIR) -Wl,-rpath,$(LIB_DIR) -lmoslib +TESTS_SRCS:=$(shell find tests | grep \\.c$) -shared: compile headers - $(CC) -shared *.o -o libmoslib.so - strip -x libmoslib.so - sudo mv libmoslib.so /usr/local/lib +both: clean shared static + $(MAKE) clean -static: compile headers - $(AR) libmoslib.a *.o - strip -S libmoslib.a - sudo mv libmoslib.a /usr/local/lib +shared: clean compile headers + $(LIB_SUDO) $(CC) -shared *.o -o $(LIB_DIR)/libmoslib.so + $(LIB_SUDO) strip -x $(LIB_DIR)/libmoslib.so + +static: clean compile headers + $(LIB_SUDO) $(AR) $(LIB_DIR)/libmoslib.a *.o + $(LIB_SUDO) strip -S $(LIB_DIR)/libmoslib.a headers: - sudo cp -r include/* /usr/local/include - -compile: structures - $(CC) $(CFLAGS) -c src/tester.c - $(CC) $(CFLAGS) -c src/safe.c - $(CC) $(CFLAGS) -c src/string.c - -structures: - $(CC) $(CFLAGS) -c src/ds/array.c - $(CC) $(CFLAGS) -c src/ds/hashmap.c - -test: - $(CC) $(CFLAGS_TEST) -c tests/safe_tests.c - $(CC) $(CFLAGS_TEST) -c tests/ds/array_tests.c - $(CC) $(CFLAGS_TEST) -c tests/string_tests.c - $(CC) $(CFLAGS_TEST) -c tests/ds/hashmap_tests.c - $(CC) $(CFLAGS_TEST) -lmoslib *.o tests/tests.c -o run_tests - $(RUN_TESTS) - rm run_tests *.o - -.PHONY: clean + $(INC_SUDO) cp -r include/* $(INC_DIR) + +compile: + $(CC) $(CFLAGS) $(SRCS) + +test: clean + $(CC) $(TESTS_CFLAGS) $(TESTS_SRCS) + $(CC) $(TESTS_LDFLAGS) *.o -o run_tests + ./run_tests + $(MAKE) clean + clean: - rm *.o + rm *.o run_tests 2>/dev/null || true + rm -rf *.dSYM/ 2>/dev/null || true -- cgit v1.2.3