-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
110 lines (89 loc) · 2.52 KB
/
Makefile
File metadata and controls
110 lines (89 loc) · 2.52 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
# Compiler and flags
CC := gcc
CFLAGS := -Wall -Wextra -Werror -pedantic -std=gnu11 -O2 -Wno-format -fPIC
DEBUGFLAGS := -g -O0
# Directories
TESTDIR := tests
INCDIR := include
# Sources and Object files
SRCS := \
dispatcher.c \
handle_binary.c \
handle_char.c \
handle_decimal.c \
handle_hexadecimal.c \
handle_octal.c \
handle_percent.c \
handle_pointer.c \
handle_reverse.c \
handle_rot13.c \
handle_string.c \
handle_unprintable.c \
handle_unsigned.c \
parser.c \
printf.c \
utils.c
OBJS := $(SRCS:.c=.o)
TEST_SRC := $(TESTDIR)/main.c
TEST_OBJ := $(TESTDIR)/main.o
TARGET := printf
# Libraries
STATICLIB := libprintf.a
SHAREDLIB := libprintf.so
INCLUDES := -I$(INCDIR)
# Default target builds libraries
all: library
# Library target (builds both static and shared)
library: $(STATICLIB) $(SHAREDLIB)
# Static library
$(STATICLIB): $(OBJS)
ar rcs $@ $^
# Shared library
$(SHAREDLIB): $(OBJS)
$(CC) -shared -o $@ $^
# Test executable links against static lib
test: library $(TEST_OBJ)
$(CC) $(CFLAGS) $(INCLUDES) $(TEST_OBJ) -static -L. -lprintf -o $(TARGET)
# Produce object files
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
$(TEST_OBJ): $(TEST_SRC)
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
# Install both libraries and headers system-wide
install: library
sudo cp $(STATICLIB) /usr/local/lib/
sudo cp $(SHAREDLIB) /usr/local/lib/
sudo mkdir -p /usr/local/include/printf
sudo cp $(INCDIR)/*.h /usr/local/include/printf/
sudo ldconfig
# Uninstall everything
uninstall:
sudo rm -f /usr/local/lib/$(STATICLIB)
sudo rm -f /usr/local/lib/$(SHAREDLIB)
sudo rm -rf /usr/local/include/printf
sudo ldconfig
# Debug mode
debug: CFLAGS := $(CFLAGS) $(DEBUGFLAGS)
debug: clean test
# Clean build artifacts
clean:
rm -f $(OBJS) $(TEST_OBJ) $(TARGET) $(STATICLIB) $(SHAREDLIB)
# Clean and Rebuild
re: clean all
# Format code
format:
clang-format -i *.c include/*.h
# Help
help:
@echo "Available targets:"
@echo " all - Build static and shared libraries (default)"
@echo " library - Build both static and shared libraries"
@echo " test - Build test executable linked with libprintf"
@echo " install - Install libraries and headers to /usr/local"
@echo " uninstall - Remove installed files"
@echo " clean - Remove build artifacts"
@echo " re - Clean and rebuild"
@echo " debug - Build with debug flags"
@echo " format - Format source code"
@echo " help - Show this help message"
.PHONY: all clean re format help debug install uninstall test library