|
| 1 | +# Define the compiler and compiler flags |
| 2 | +CC = gcc |
| 3 | +CFLAGS = -Iinclude -Wall -Wextra |
| 4 | + |
| 5 | +# Define RM, MV and SEP command specific to platform |
| 6 | +ifeq ($(OS),Windows_NT) |
| 7 | + RM = del /Q /F |
| 8 | + MV = move |
| 9 | + SEP = \\ |
| 10 | +else |
| 11 | + RM = $(RM) -f |
| 12 | + MV = mv |
| 13 | + SEP = / |
| 14 | +endif |
| 15 | + |
| 16 | +# Define directories |
| 17 | +SRC_MD_DIR = src/md |
| 18 | +SRC_RFC_DIR = src/rfc |
| 19 | +TEST_DIR = test |
| 20 | +BIN_DIR = bin |
| 21 | +LIB_DIR = lib |
| 22 | + |
| 23 | +# Find all .c files in the src/md directory |
| 24 | +SRC_MD_FILES = $(wildcard $(SRC_MD_DIR)/*.c) |
| 25 | +MD_BIN_FILES = $(patsubst $(SRC_MD_DIR)/%.c,$(BIN_DIR)/%,$(SRC_MD_FILES)) |
| 26 | + |
| 27 | +# Find all .c files in the src/rfc directory |
| 28 | +SRC_RFC_FILES = $(wildcard $(SRC_RFC_DIR)/*.c) |
| 29 | +RFC_OBJ_FILES = $(patsubst $(SRC_RFC_DIR)/%.c,$(SRC_RFC_DIR)/%.o,$(SRC_RFC_FILES)) |
| 30 | + |
| 31 | +# Find all .c files in the test directory |
| 32 | +TEST_FILES = $(wildcard $(TEST_DIR)/*.c) |
| 33 | +TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c,$(BIN_DIR)/%,$(TEST_FILES)) |
| 34 | + |
| 35 | +# Define the static library |
| 36 | +STATIC_LIB = $(LIB_DIR)/libmd.a |
| 37 | + |
| 38 | +# Define the shared library |
| 39 | +ifeq ($(OS),Windows_NT) |
| 40 | + SHARED_LIB = $(LIB_DIR)/md.dll |
| 41 | +else |
| 42 | + SHARED_LIB = $(LIB_DIR)/md.so |
| 43 | +endif |
| 44 | + |
| 45 | +# Default target |
| 46 | +all: build static test |
| 47 | + |
| 48 | +# Build target for src/md files |
| 49 | +build: $(MD_BIN_FILES) |
| 50 | + |
| 51 | +# Rule to compile each .c file in src/md into its corresponding binary |
| 52 | +$(BIN_DIR)/%: $(SRC_MD_DIR)/%.c | $(BIN_DIR) $(STATIC_LIB) |
| 53 | + $(CC) $(CFLAGS) -L$(LIB_DIR) -o $@ $< -lm -lmd |
| 54 | + |
| 55 | +# Build target for static library |
| 56 | +static: $(STATIC_LIB) |
| 57 | + |
| 58 | +# Rule to create the static library from src/rfc files |
| 59 | +$(STATIC_LIB): $(RFC_OBJ_FILES) | $(LIB_DIR) |
| 60 | + ar rcs $@ $(RFC_OBJ_FILES) |
| 61 | + $(RM) $(subst /,$(SEP),$(RFC_OBJ_FILES)) |
| 62 | + |
| 63 | +# Build target for shared library |
| 64 | +shared : $(SHARED_LIB) |
| 65 | + |
| 66 | +# Rule to create the shared library from src/rfc files |
| 67 | +$(SHARED_LIB): $(RFC_OBJ_FILES) | $(LIB_DIR) |
| 68 | + $(CC) -shared -o $@ $(RFC_OBJ_FILES) |
| 69 | + $(RM) $(subst /,$(SEP),$(RFC_OBJ_FILES)) |
| 70 | + |
| 71 | +# Rule to compile each .c file in src/rfc into position-independent code (.o files) |
| 72 | +$(SRC_RFC_DIR)/%.o: $(SRC_RFC_DIR)/%.c |
| 73 | + $(CC) $(CFLAGS) -fPIC -c -o $@ $< |
| 74 | + |
| 75 | +# Build target for test files |
| 76 | +test: $(TEST_BIN_FILES) |
| 77 | + |
| 78 | +# Rule to compile each .c file in test into its corresponding binary |
| 79 | +$(BIN_DIR)/%: $(TEST_DIR)/%.c | $(BIN_DIR) $(STATIC_LIB) |
| 80 | + $(CC) $(CFLAGS) -o $@ $< -L$(LIB_DIR) -lmd |
| 81 | + |
| 82 | +# Create the bin directory if it doesn't exist |
| 83 | +$(BIN_DIR): |
| 84 | + mkdir -p $(BIN_DIR) |
| 85 | + |
| 86 | +# Create the lib directory if it doesn't exist |
| 87 | +$(LIB_DIR): |
| 88 | + mkdir -p $(LIB_DIR) |
| 89 | + |
| 90 | +# Clean target to remove compiled binaries and libraries |
| 91 | +clean: |
| 92 | + $(RM) $(subst /,$(SEP),$(BIN_DIR)/* $(LIB_DIR)/* $(SRC_RFC_DIR)/*.o) |
| 93 | + |
| 94 | +.PHONY: all build static test clean |
0 commit comments