aboutsummaryrefslogtreecommitdiff
path: root/Makefile
diff options
context:
space:
mode:
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile79
1 files changed, 40 insertions, 39 deletions
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