From f91684f3c81ffcf2c32152c54260c4f1b43e5c2b Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 26 Jul 2016 10:57:05 +0100 Subject: [PATCH] rework build structure for static link make .gitignore exclude default move tests into tests/c/ --- .gitignore | 41 ++++----------------------- Makefile | 27 +++++++++++------- main.c | 11 +++++++ test/minunit.h | 5 ---- tests/c/.gitignore | 4 +++ tests/c/minunit.h | 22 ++++++++++++++ {test => tests/c}/test_error_string.c | 21 ++------------ 7 files changed, 61 insertions(+), 70 deletions(-) create mode 100644 main.c delete mode 100644 test/minunit.h create mode 100644 tests/c/.gitignore create mode 100644 tests/c/minunit.h rename {test => tests/c}/test_error_string.c (54%) diff --git a/.gitignore b/.gitignore index f652eea..bed6bdf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,36 +1,7 @@ -# Object files -*.o -*.ko -*.obj -*.elf +* -# 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 - -# Build system -.depend +!.gitignore +!*.c +!*.h +!Makefile +!tests diff --git a/Makefile b/Makefile index ea265d6..e494601 100644 --- a/Makefile +++ b/Makefile @@ -13,34 +13,39 @@ CFLAGS = -fPIC -g -O2 -std=c99 \ LDFLAGS = -g LDLIBS = -TARGET_LIB = mulithash.so +TARGET_LIB = mulithash.a +TARGET_BIN = multihash SRCS = errors.c OBJS = $(SRCS:.c=.o) -all: ${TARGET_LIB} +all: $(TARGET_LIB) $(TARGET_BIN) $(TARGET_LIB): $(OBJS) - $(CC) -g -shared -o $@ $^ + ar rcs $@ $^ + +$(TARGET_BIN): main.o $(TARGET_LIB) + $(CC) $(LDFLAGS) $^ -o $@ # Tests -TEST_SRCS = $(wildcard test/test_*.c) +TEST_SRCS = $(wildcard tests/c/test_*.c) TEST_OBJS = $(TEST_SRCS:.c=.o) -TEST_BINS = $(TEST_OBJS:.o=.out) +TEST_BINS = $(TEST_OBJS:.o=) PHONY += tests tests: $(TEST_BINS) @for t in $^; do \ echo; \ - echo '***' "$$t" '***'; \ - LD_LIBRARY_PATH=. "./$$t"; \ - echo; \ + echo '***' "$$t.c" '***'; \ + "./$$t"; \ done +tests/c/test_%.o: tests/c/test_%.c + $(CC) -c -I. $^ -o $@ -test/test_%.out: test/test_%.o $(TARGET_LIB) - $(CC) $(LDFLAGS) $(TARGET_LIB) $^ -o $@ +tests/c/test_%: tests/c/test_%.o $(TARGET_LIB) + $(CC) $(LDFLAGS) $^ -o $@ # Utils @@ -48,9 +53,9 @@ PHONY += clean dist-clean clean: $(RM) $(OBJS) - $(RM) $(TEST_OBJS) $(RM) $(TEST_BINS) $(RM) $(TARGET_LIB) + $(RM) $(TARGET_BIN) dist-clean: clean diff --git a/main.c b/main.c new file mode 100644 index 0000000..6de7f52 --- /dev/null +++ b/main.c @@ -0,0 +1,11 @@ +/* vim: set ts=8 sw=8 noexpandtab: */ +#include + + +#include "errors.h" + +int main(void) { + printf("%s\n", mh_error_string(MH_E_TOO_LONG)); + printf("Hello World\n"); + return 0; +} diff --git a/test/minunit.h b/test/minunit.h deleted file mode 100644 index cc3d602..0000000 --- a/test/minunit.h +++ /dev/null @@ -1,5 +0,0 @@ -/* vim: set ts=8 sw=8 noexpandtab: */ -#define mu_assert(message, test) do { if (!(test)) return message; } while (0) -#define mu_run_test(test) do { char *message = test(); tests_run++; \ - if (message) return message; } while (0) -extern int tests_run; diff --git a/tests/c/.gitignore b/tests/c/.gitignore new file mode 100644 index 0000000..3376138 --- /dev/null +++ b/tests/c/.gitignore @@ -0,0 +1,4 @@ +* +!.gitignore +!minunit.h +!*.c diff --git a/tests/c/minunit.h b/tests/c/minunit.h new file mode 100644 index 0000000..52ad29b --- /dev/null +++ b/tests/c/minunit.h @@ -0,0 +1,22 @@ +/* vim: set ts=8 sw=8 noexpandtab: */ +#include + +#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_run_test(test) do { char *message = test(); tests_run++; \ + if (message) return message; } while (0) +int tests_run = 0; +static char *mu_all_tests(void); + +int main(void) { + char *result = mu_all_tests(); + if (result != 0) { + printf("%s\n", result); + } + else { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return result != 0; +} + diff --git a/test/test_error_string.c b/tests/c/test_error_string.c similarity index 54% rename from test/test_error_string.c rename to tests/c/test_error_string.c index 95a164a..2baa875 100644 --- a/test/test_error_string.c +++ b/tests/c/test_error_string.c @@ -1,13 +1,10 @@ -#include #include #include "minunit.h" -#include "../errors.h" +#include "errors.h" -int tests_run = 0; char error_buf[256]; - static char *test_error_messages_exist(void) { int i = -1; for (; i > MH_E_LAST; i--) { @@ -18,21 +15,7 @@ static char *test_error_messages_exist(void) { return 0; } -static char *all_tests(void) { +static char *mu_all_tests(void) { mu_run_test(test_error_messages_exist); return 0; } - -int main(void) { - char *result = all_tests(); - if (result != 0) { - printf("%s\n", result); - } - else { - printf("ALL TESTS PASSED\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; -} -