-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor makefiles and fix a few bad bugs
- Loading branch information
1 parent
09b0fe5
commit 0543dc1
Showing
14 changed files
with
296 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,76 @@ | ||
CC ?= gcc | ||
AR ?= ar | ||
LINTER ?= clang-format | ||
|
||
LIB := libhash | ||
|
||
PREFIX := /usr/local | ||
INCDIR := $(PREFIX)/include | ||
LIBDIR := $(PREFIX)/lib | ||
SRCDIR := src | ||
DEPSDIR := deps | ||
TESTDIR := t | ||
EXAMPLEDIR := examples | ||
LINCDIR := include | ||
|
||
DYNAMIC_TARGET := $(LIB).so | ||
STATIC_TARGET := $(LIB).a | ||
EXAMPLE_TARGET := example | ||
TEST_TARGET := test | ||
|
||
SRC := $(wildcard $(SRCDIR)/*.c) | ||
TEST_DEPS := $(wildcard $(DEPSDIR)/tap.c/*.c) | ||
DEPS := $(filter-out $(wildcard $(DEPSDIR)/tap.c/*), $(wildcard $(DEPSDIR)/*/*.c)) | ||
OBJ := $(addprefix obj/, $(notdir $(SRC:.c=.o)) $(notdir $(DEPS:.c=.o))) | ||
|
||
CFLAGS := -I$(LINCDIR) -I$(DEPSDIR) -Wall -Wextra -pedantic -std=c17 | ||
LIBS := -lm | ||
|
||
TESTS := $(wildcard $(TESTDIR)/*.c) | ||
|
||
SEPARATOR := --------------------------- | ||
|
||
all: $(DYNAMIC_TARGET) $(STATIC_TARGET) | ||
|
||
include Makefile.config | ||
|
||
.PHONY: all obj install uninstall clean unit_test unit_test_dev valgrind fmt | ||
.DELETE_ON_ERROR: | ||
|
||
PREFIX := /usr/local | ||
INCDIR := $(PREFIX)/include | ||
LIBDIR := $(PREFIX)/lib | ||
SRCDIR := src | ||
DEPSDIR := deps | ||
TESTDIR := t | ||
EXAMPLEDIR := examples | ||
INCDIR := include | ||
|
||
DYNAMIC_TARGET := $(LIBNAME).so | ||
STATIC_TARGET := $(LIBNAME).a | ||
EXAMPLE_TARGET := example | ||
TEST_TARGET := test | ||
|
||
SRC := $(wildcard $(SRCDIR)/*.c) | ||
TESTS := $(wildcard $(TESTDIR)/*.c) | ||
DEPS := $(filter-out $(wildcard $(DEPSDIR)/libtap/*), $(wildcard $(DEPSDIR)/*/*.c)) | ||
TEST_DEPS := $(wildcard $(DEPSDIR)/libtap/*.c) | ||
OBJ := $(addprefix obj/, $(notdir $(SRC:.c=.o)) $(notdir $(DEPS:.c=.o))) | ||
|
||
INCLUDES := -I$(INCDIR) -I$(DEPSDIR) -I$(SRCDIR) | ||
LIBS := -lm | ||
CFLAGS := -Wall -Wextra -pedantic -std=c17 $(INCLUDES) | ||
|
||
$(DYNAMIC_TARGET): CFLAGS += -shared | ||
$(DYNAMIC_TARGET): $(OBJ) | ||
$(CC) $(CFLAGS) $(OBJ) -shared $(LIBS) -o $(DYNAMIC_TARGET) | ||
$(CC) $(CFLAGS) $^ $(LIBS) -o $@ | ||
|
||
$(STATIC_TARGET): $(OBJ) | ||
$(AR) rcs $@ $(OBJ) | ||
$(AR) rcs $@ $^ | ||
|
||
obj/%.o: $(SRCDIR)/%.c $(LINCDIR)/$(LIB).h | obj | ||
obj/%.o: $(SRCDIR)/%.c $(INCDIR)/$(LIBNAME).h | obj | ||
$(CC) $< -c $(CFLAGS) -o $@ | ||
|
||
obj/%.o: $(DEPSDIR)/*/%.c | obj | ||
$(CC) $< -c $(CFLAGS) -o $@ | ||
|
||
$(EXAMPLE_TARGET): $(STATIC_TARGET) | ||
$(CC) $(CFLAGS) $(EXAMPLEDIR)/main.c $< $(LIBS) -o $@ | ||
|
||
all: $(DYNAMIC_TARGET) $(STATIC_TARGET) | ||
|
||
obj: | ||
mkdir -p obj | ||
@mkdir -p obj | ||
|
||
install: $(STATIC_TARGET) | ||
mkdir -p ${LIBDIR} && cp -f ${STATIC_TARGET} ${LIBDIR}/$(STATIC_TARGET) | ||
mkdir -p ${INCDIR} && cp -r $(LINCDIR)/$(LIB).h ${INCDIR} | ||
@mkdir -p ${LIBDIR} && cp -f ${STATIC_TARGET} ${LIBDIR}/$@ | ||
@mkdir -p ${INCDIR} && cp -r $(INCDIR)/$(LIBNAME).h ${INCDIR} | ||
|
||
uninstall: | ||
rm -f ${LIBDIR}/$(STATIC_TARGET) | ||
rm -f ${INCDIR}/libys.h | ||
|
||
$(EXAMPLE_TARGET): $(STATIC_TARGET) | ||
$(CC) $(CFLAGS) $(EXAMPLEDIR)/main.c $(STATIC_TARGET) $(LIBS) -o $(EXAMPLE_TARGET) | ||
@rm -f ${LIBDIR}/$(STATIC_TARGET) | ||
@rm -f ${INCDIR}/libys.h | ||
|
||
clean: | ||
rm -f $(OBJ) $(STATIC_TARGET) $(DYNAMIC_TARGET) $(EXAMPLE_TARGET) $(TEST_TARGET) | ||
@rm -f $(OBJ) $(STATIC_TARGET) $(DYNAMIC_TARGET) $(EXAMPLE_TARGET) $(TEST_TARGET) | ||
|
||
test: $(STATIC_TARGET) | ||
$(foreach test,$(TESTS), \ | ||
$(MAKE) .compile_test file=$(test); \ | ||
printf "\033[1;32m\nRunning test $(patsubst $(TESTDIR)/%,%,$(test))...\n$(SEPARATOR)\n\033[0m"; \ | ||
./test;\ | ||
) | ||
rm $(TEST_TARGET) | ||
unit_test: $(STATIC_TARGET) | ||
$(CC) $(CFLAGS) $(TESTS) $(TEST_DEPS) $(STATIC_TARGET) -I$(SRCDIR) $(LIBS) -o $(TEST_TARGET) | ||
./$(TEST_TARGET) | ||
$(MAKE) clean | ||
|
||
.compile_test: | ||
$(CC) $(CFLAGS) $(file) $(TEST_DEPS) $(STATIC_TARGET) -I$(SRCDIR) -I$(DEPSDIR) $(LIBS) -o $(TEST_TARGET) | ||
unit_test_dev: | ||
ls $(SRCDIR)/*.{h,c} $(TESTDIR)/*.{h,c} | entr -s 'make -s unit_test' | ||
|
||
lint: | ||
$(LINTER) -i $(wildcard $(SRCDIR)/*) $(wildcard $(TESTDIR)/*) $(wildcard $(LINCDIR)/*) $(wildcard $(EXAMPLEDIR)/*) | ||
valgrind: $(STATIC_TARGET) | ||
$(CC) $(CFLAGS) $(TESTS) $(TEST_DEPS) $< $(LIBS) -o $(TEST_TARGET) | ||
$(VALGRIND) --leak-check=full --track-origins=yes -s ./$(TEST_TARGET) | ||
@$(MAKE) clean | ||
|
||
.PHONY: clean test .compile_test all obj install uninstall lint | ||
fmt: | ||
@$(FMT) -i $(wildcard $(SRCDIR)/*) $(wildcard $(TESTDIR)/*) $(wildcard $(INCDIR)/*) $(wildcard $(EXAMPLEDIR)/*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CC ?= gcc | ||
AR ?= ar | ||
FMT ?= clang-format | ||
VALGRIND ?= valgrind | ||
|
||
LIBNAME := libhash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/bin/sh -l | ||
|
||
ret="$(make -s test 2>/dev/null)" | ||
ret="$(make -s unit_test 2>/dev/null)" | ||
|
||
echo "$ret" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef LIBHASH_LIST_H | ||
#define LIBHASH_LIST_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct node node_t; | ||
struct node { | ||
int value; | ||
node_t *next; | ||
}; | ||
|
||
static node_t LIST_SENTINEL_NODE = {0, NULL}; | ||
|
||
static node_t *list_node_create_head(void) { return &LIST_SENTINEL_NODE; } | ||
|
||
static node_t *list_node_create(const int value) { | ||
node_t *n = (node_t *)malloc(sizeof(node_t)); | ||
n->value = value; | ||
return n; | ||
} | ||
|
||
static void list_prepend(node_t **head, int value) { | ||
// TODO: xmalloc | ||
node_t *new_node = (node_t *)malloc(sizeof(node_t)); | ||
new_node->value = value; | ||
|
||
node_t *tmp = *head; | ||
*head = new_node; | ||
new_node->next = tmp; | ||
} | ||
|
||
static void list_remove(node_t **head, int value) { | ||
node_t *current = *head; | ||
node_t *prev = NULL; | ||
|
||
while (current != NULL) { | ||
if (current->value == value) { | ||
if (prev == NULL) { | ||
*head = current->next; | ||
} else { | ||
prev->next = current->next; | ||
} | ||
free(current); | ||
return; | ||
} | ||
prev = current; | ||
current = current->next; | ||
} | ||
} | ||
|
||
// TODO: Use and test | ||
static void free_list(node_t *head) { | ||
node_t *tmp; | ||
while (head) { | ||
tmp = head; | ||
head = head->next; | ||
free(tmp); | ||
} | ||
} | ||
|
||
#endif /* LIBHASH_LIST_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#ifndef PRIME_H | ||
#define PRIME_H | ||
#ifndef LIBHASH_PRIME_H | ||
#define LIBHASH_PRIME_H | ||
|
||
int is_prime(const int x); | ||
int next_prime(int x); | ||
|
||
#endif /* PRIME_H */ | ||
#endif /* LIBHASH_PRIME_H */ |
Oops, something went wrong.