-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
executable file
·159 lines (131 loc) · 4.74 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
################################################################################
# C-ML Machine Learning Library Makefile
################################################################################
# Compiler and flags
CC := gcc
CFLAGS := -g -Wall -MMD -Iinclude
LDFLAGS := -lm
# Project structure
SRC_DIR := src
INCLUDE_DIR := include
OBJ_DIR := obj
BIN_DIR := bin
LIB_DIR := lib
TEST_DIR := test
TEST_BIN_DIR := test_bin
EXAMPLES_DIR := examples
EXAMPLES_BIN_DIR := examples_bin
# Library configuration
LIB_NAME := c_ml
LIB_VERSION := 1.0.0
STATIC_LIB := $(LIB_DIR)/lib$(LIB_NAME).a
# Find all source files
SRC_FILES := $(shell find $(SRC_DIR) -name "*.c")
MAIN_FILE := main.c
ALL_SRCS := $(SRC_FILES) $(MAIN_FILE)
# Generate object file paths
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))
MAIN_OBJ := $(OBJ_DIR)/main.o
ALL_OBJS := $(OBJ_FILES) $(MAIN_OBJ)
LIB_OBJS := $(OBJ_FILES)
# Find all test files
TEST_FILES := $(shell find $(TEST_DIR) -name "*.c")
# Find all example files
EXAMPLE_FILES := $(wildcard $(EXAMPLES_DIR)/*.c)
EXAMPLES := $(patsubst $(EXAMPLES_DIR)/%.c,$(EXAMPLES_BIN_DIR)/%,$(EXAMPLE_FILES))
# Dependencies
DEPS := $(ALL_OBJS:.o=.d)
################################################################################
# Main targets
################################################################################
# Default target
.PHONY: all
all: $(BIN_DIR)/main
# Build the main executable
$(BIN_DIR)/main: $(ALL_OBJS)
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@
@echo "Built main executable: $@"
# Build the static library
.PHONY: lib
lib: $(STATIC_LIB)
$(STATIC_LIB): $(LIB_OBJS)
@mkdir -p $(LIB_DIR)
ar rcs $@ $^
@echo "Built static library: $@"
# Release build with optimizations
.PHONY: release
release: CFLAGS := -Wall -O2 -DNDEBUG -MMD
release: clean all
@echo "Built release version"
# Debug build with sanitizers
.PHONY: debug
debug: CFLAGS += -fsanitize=address -fsanitize=undefined
debug: all
@echo "Built debug version with sanitizers"
################################################################################
# Object file compilation
################################################################################
# Compile source files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -c $< -o $@
# Compile main file
$(OBJ_DIR)/main.o: $(MAIN_FILE)
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
################################################################################
# Examples
################################################################################
.PHONY: examples
examples: $(STATIC_LIB) $(EXAMPLES)
@echo "Built all examples"
$(EXAMPLES_BIN_DIR)/%: $(EXAMPLES_DIR)/%.c $(STATIC_LIB)
@mkdir -p $(EXAMPLES_BIN_DIR)
$(CC) $(CFLAGS) $< -L$(LIB_DIR) -l$(LIB_NAME) $(LDFLAGS) -lm -o $@
@echo "Built example: $@"
# Debug version of examples with sanitizers
.PHONY: debug_examples
debug_examples: EXAMPLE_FLAGS := -fsanitize=address -fsanitize=undefined
debug_examples: $(STATIC_LIB)
@mkdir -p $(EXAMPLES_BIN_DIR)
@for example_src in $(EXAMPLE_FILES); do \
example_bin=$$(basename $$example_src .c); \
echo "Compiling $$example_bin with debug flags..."; \
$(CC) $(CFLAGS) $(EXAMPLE_FLAGS) $$example_src -L$(LIB_DIR) -l$(LIB_NAME) -lm $(LDFLAGS) \
-o $(EXAMPLES_BIN_DIR)/$$example_bin; \
done
@echo "Built all examples with debug flags"
################################################################################
# Tests
################################################################################
.PHONY: test
test: $(STATIC_LIB)
@mkdir -p $(TEST_BIN_DIR)
@echo "Running tests..."
@for test_src in $(TEST_FILES); do \
test_bin=$$(basename $$test_src .c); \
src_file=$$(echo $$test_src | sed 's|^test/|src/|; s|test_||'); \
echo "\nCompiling and running $$test_bin..."; \
$(CC) $(CFLAGS) $$test_src $$src_file -L$(LIB_DIR) -l$(LIB_NAME) $(LDFLAGS) \
-o $(TEST_BIN_DIR)/$$test_bin -fsanitize=address -fsanitize=undefined && \
ASAN_OPTIONS=allocator_may_return_null=1 ./$(TEST_BIN_DIR)/$$test_bin || exit 1; \
done
@echo "\nALL TESTS PASSED"
# Test logging system specifically
.PHONY: test_logging
test_logging: $(STATIC_LIB)
@mkdir -p $(TEST_BIN_DIR)
@echo "Running logging tests..."
$(CC) $(CFLAGS) test/Core/test_logging.c src/Core/logging.c -o $(TEST_BIN_DIR)/test_logging
./$(TEST_BIN_DIR)/test_logging
################################################################################
# Utility targets
################################################################################
# Clean build artifacts
.PHONY: clean
clean:
rm -rf $(BIN_DIR) $(OBJ_DIR) $(TEST_BIN_DIR) $(EXAMPLES_BIN_DIR) $(LIB_DIR) a.out
@echo "Cleaned all build artifacts"
# Include dependency files
-include $(DEPS)