-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
147 lines (125 loc) · 3.4 KB
/
Makefile
File metadata and controls
147 lines (125 loc) · 3.4 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
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
# Makefile for mac-free
#
# A 'free' command replacement for macOS
# License: MIT
# ============================================================================
# Configuration
# ============================================================================
# Compiler and flags
CC = clang
CFLAGS = -Wall -Wextra -Werror -pedantic -std=c11
CFLAGS += -O2 -fstack-protector-strong
LDFLAGS =
# Debug build flags
DEBUG_CFLAGS = -g -O0 -DDEBUG -fsanitize=address,undefined
DEBUG_LDFLAGS = -fsanitize=address,undefined
# Directories
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
# Target
TARGET = $(BIN_DIR)/free
# Source files
SOURCES = $(wildcard $(SRC_DIR)/*.c)
OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES))
DEPS = $(OBJECTS:.o=.d)
# Include paths
INCLUDES = -I$(SRC_DIR)
# Installation
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man/man1
# ============================================================================
# Targets
# ============================================================================
.PHONY: all clean debug install uninstall test help
# Default target
all: $(TARGET)
# Link target
$(TARGET): $(OBJECTS) | $(BIN_DIR)
$(CC) $(LDFLAGS) -o $@ $^
@echo "Build complete: $@"
# Compile source files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) $(INCLUDES) -MMD -MP -c -o $@ $<
# Create directories
$(OBJ_DIR):
mkdir -p $(OBJ_DIR)
$(BIN_DIR):
mkdir -p $(BIN_DIR)
# Include dependency files
-include $(DEPS)
# Debug build
debug: CFLAGS += $(DEBUG_CFLAGS)
debug: LDFLAGS += $(DEBUG_LDFLAGS)
debug: clean $(TARGET)
@echo "Debug build complete"
# Clean build artifacts
clean:
rm -rf $(OBJ_DIR) $(BIN_DIR)
@echo "Clean complete"
# Install
install: $(TARGET)
install -d $(DESTDIR)$(BINDIR)
install -m 755 $(TARGET) $(DESTDIR)$(BINDIR)/free
@echo "Installed to $(DESTDIR)$(BINDIR)/free"
@echo ""
@echo "Note: This will shadow the system 'free' command (if any)."
@echo "You may need to restart your shell or run 'hash -r'"
# Uninstall
uninstall:
rm -f $(DESTDIR)$(BINDIR)/free
@echo "Uninstalled from $(DESTDIR)$(BINDIR)/free"
# Run basic tests
test: $(TARGET)
@echo "Running basic tests..."
@echo ""
@echo "Test 1: Default output"
@$(TARGET)
@echo ""
@echo "Test 2: Human-readable output"
@$(TARGET) -h
@echo ""
@echo "Test 3: Wide output"
@$(TARGET) -w
@echo ""
@echo "Test 4: Human-readable wide output with totals"
@$(TARGET) -h -w -t
@echo ""
@echo "Test 5: Bytes output"
@$(TARGET) -b
@echo ""
@echo "Test 6: Mebibytes output"
@$(TARGET) -m
@echo ""
@echo "Test 7: Gibibytes output"
@$(TARGET) -g
@echo ""
@echo "Test 8: Version"
@$(TARGET) -V
@echo ""
@echo "All tests passed!"
# Help
help:
@echo "mac-free Makefile"
@echo ""
@echo "Targets:"
@echo " all Build the project (default)"
@echo " debug Build with debug flags and sanitizers"
@echo " clean Remove build artifacts"
@echo " install Install to $(BINDIR)"
@echo " uninstall Remove from $(BINDIR)"
@echo " test Run basic tests"
@echo " help Show this help message"
@echo ""
@echo "Variables:"
@echo " PREFIX Installation prefix (default: /usr/local)"
@echo " CC C compiler (default: clang)"
@echo " CFLAGS Additional compiler flags"
@echo " LDFLAGS Additional linker flags"
@echo ""
@echo "Examples:"
@echo " make"
@echo " make debug"
@echo " make install PREFIX=/opt/local"
@echo " make clean"