This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
109 lines (89 loc) · 2.61 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
NAME = cdefer
LIBNAME = lib$(NAME)
# whre test course code is
TSTDIR = tests
# where target object files go
OBJDIR = obj
# where target executables (tests) go
BINDIR = bin
# where installation stuff goes
LIBDIR = lib
# target directories
DIRS = $(OBJDIR) $(BINDIR) $(LIBDIR)
USRDIR = /usr/local
USRLIB = $(USRDIR)/lib
USRINC = $(USRDIR)/include
# names of source files
src = $(notdir $(wildcard *.c))
obj = $(patsubst %.c, %.o, $(src))
# paths of target object files
OBJ = $(addprefix $(OBJDIR)/, $(obj))
# paths of target library files
LAO = $(addprefix $(LIBDIR)/, $(obj))
# paths of test files
tst = $(basename $(notdir $(wildcard $(TSTDIR)/*.c)))
# pats of target executables (tests)
TST = $(addprefix $(BINDIR)/, $(tst))
FINALLIB = $(USRLIB)/$(LIBNAME).a
LOCALLIB = $(LIBDIR)/$(LIBNAME).a
# C compiler
CC = gcc
# Wall - warns about questionable things
# Werror - makes all warnings errors
# Wextra - enables some extra warning flags that `all` doesn't set
CFLAGS = -Wall -Wextra
# g - add symbol table for debugging
# std= - sets the language standard, in this case c99
# O - the level of optimization (3)
CFLAGS += -g -std=c99 -O3
INFOPRMPT = MAKE ------>
ifeq ($(shell uname -s), Linux)
CC = clang
LFLAGS = -lBlocksRuntime
CFLAGS += -fblocks
endif
.PHONY: all install include test build_test
all: $(OBJ)
install: $(FINALLIB)
$(info $(INFOPRMPT) installing...)
@make include
test: $(TST)
$(info $(INFOPRMPT) running tests...)
@for i in $(TST); do echo "$$i:"; $$i; done
# build object files--no library
$(OBJ): $(OBJDIR)/%.o: %.c $(OBJDIR)
$(info $(INFOPRMPT) building object file $*.o...)
@$(CC) $(CFLAGS) -o $@ -c $<
# move library file into /usr/lical/lib
$(FINALLIB): $(LOCALLIB)
$(info $(INFOPRMPT) copying $< to $@...)
@cp -i $< $@
# build library file
$(LOCALLIB): $(LAO)
$(info $(INFOPRMPT) making and linking library archive $@...)
@ar cru $@ $^
@ranlib $@
@chmod 755 $@
# build object file for library
$(LAO): $(LIBDIR)/%.o: %.c $(LIBDIR)
$(info $(INFOPRMPT) building library object file $@...)
@$(CC) $(CFLAGS) -fPIC -o $@ -c $<
# symlink header files
include:
$(info $(INFOPRMPT) copying header files to $(USRINC)/$(NAME)...)
@mkdir -p $(USRINC)/$(NAME)
@cp -i $(wildcard *.h) $(USRINC)/$(NAME)/
# build tests
$(TST): $(BINDIR)/%: $(TSTDIR)/%.c $(BINDIR) $(OBJ)
$(info $(INFOPRMPT) building test $*.c...)
@$(CC) $(CFLAGS) $(LFLAGS) -I"." -o $@ $(OBJ) $<
# make directories
$(DIRS):
@mkdir -p $@
.PHONY: clean uninstall
clean:
$(info $(INFOPRMPT) removing build directories $(DIRS)...)
@rm -rf $(DIRS)
uninstall:
$(info $(INFOPRMPT) uninstalling from system...)
@rm -rf $(USRINC)/$(NAME) $(FINALLIB)