-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
103 lines (83 loc) · 2.95 KB
/
Makefile
File metadata and controls
103 lines (83 loc) · 2.95 KB
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
# Variables for directories
SRC_DIR := src
TESTS_DIR := tests
OBJ_DIR := obj
BIN_DIR := bin
BUILD_DIR := build
# Compiler and flags
CC := clang
CFLAGS := -Wall -Wextra -Wpedantic -std=c99
DEBUG_FLAGS := -g -O0 -DDEBUG -fsanitize=address -fsanitize=undefined
RELEASE_FLAGS := -O2 -DNDEBUG
INCLUDE_FLAGS := -Iinclude
# Programs to build from src directory
LIB_SOURCES := assertc \
bin_search \
# Test programs to build from tests directory
TEST_PROGRAMS := example_test_ok \
example_test_all \
# Object files
LIB_OBJECTS := $(patsubst %,$(OBJ_DIR)/%.o,$(LIB_SOURCES))
TEST_OBJECTS := $(patsubst %,$(OBJ_DIR)/%.o,$(TEST_PROGRAMS))
# Binary targets
TEST_BINARIES := $(patsubst %,$(BIN_DIR)/%,$(TEST_PROGRAMS))
DEBUG_BINARIES := $(patsubst %,$(BUILD_DIR)/%,$(TEST_PROGRAMS))
# Default target
.PHONY: all clean debug release tests directories help check distcheck
all: directories debug release
# Create necessary directories
directories:
@mkdir -p $(OBJ_DIR) $(BIN_DIR) $(BUILD_DIR)
# Pattern rule for compiling library object files from src directory
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) $(RELEASE_FLAGS) $(INCLUDE_FLAGS) -c $< -o $@
# Pattern rule for compiling test object files from tests directory
$(TEST_OBJECTS): $(OBJ_DIR)/%.o: $(TESTS_DIR)/%.c
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) $(RELEASE_FLAGS) $(INCLUDE_FLAGS) -c $< -o $@
# Pattern rule for linking test binaries
$(BIN_DIR)/%: $(OBJ_DIR)/%.o $(LIB_OBJECTS)
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) $(RELEASE_FLAGS) $^ -o $@
# Debug build targets
debug: directories $(DEBUG_BINARIES)
$(BUILD_DIR)/%: $(TESTS_DIR)/%.c $(patsubst %,$(SRC_DIR)/%.c,$(LIB_SOURCES))
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $(DEBUG_FLAGS) $(INCLUDE_FLAGS) $^ -o $@
# Release build
release: directories $(TEST_BINARIES)
# Build and Run all tests
tests: $(TEST_BINARIES)
@for test in $(TEST_BINARIES); do \
echo "Running $$test..."; \
./$$test || exit 1; \
done
# Check target - build and run tests
check: release tests
@echo "All tests passed successfully!"
# Distcheck target - comprehensive testing with clean build
distcheck: clean all tests
@echo "All tests passed successfully!"
# Clean build artifacts
clean:
rm -rf $(OBJ_DIR) $(BIN_DIR) $(BUILD_DIR)
# Help target
help:
@echo "Available targets:"
@echo " all - Build all release binaries (default)"
@echo " debug - Build debug versions"
@echo " release - Build release versions"
@echo " tests - Run all tests"
@echo " check - Build and run tests"
@echo " distcheck - Clean build and comprehensive testing"
@echo " clean - Remove all build artifacts"
@echo " directories - Create build directories"
@echo " help - Show this help message"
@echo ""
@echo "Build directories:"
@echo " $(OBJ_DIR)/ - Object files"
@echo " $(BIN_DIR)/ - Release binaries"
@echo " $(BUILD_DIR)/ - Debug binaries"
# Prevent make from removing intermediate files
.PRECIOUS: $(OBJ_DIR)/%.o