-
Notifications
You must be signed in to change notification settings - Fork 44
/
Makefile
128 lines (100 loc) · 3.18 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
# -- VARIABLES
# Project name
NAME = m1ddc
LIB = lib$(NAME)
# Compiler
CC = clang
CFLAGS = -Wall -Werror -Wextra -fmodules
CPPFLAGS = -I $(INC_DIR)
DEPFLAGS = -MMD
# Libraries
LDLIBS = -framework CoreDisplay
# Commands
RM = rm -f
RMDIR = rm -rf
MKDIR = mkdir -p
MAKE = make -C
AR = ar -rcs
# Paths
INC_DIR = headers
SRC_DIR = sources
LIB_DIR = library
BIN_DIR = /usr/local/bin
# Sources & Objects - Binary
SOURCES = i2c \
ioregistry \
m1ddc \
OBJ_DIR = .objects
OBJECTS = $(patsubst %,$(OBJ_DIR)/%,$(SOURCES:=.o))
# Sources & Objects - Library
LIB_SRCS = $(filter-out m1ddc, $(SOURCES))
LIB_OBJS = $(patsubst %,$(OBJ_DIR)/%,$(LIB_SRCS:=.o))
LIB_HDRS = $(patsubst %,$(INC_DIR)/%,$(LIB_SRCS:=.h))
# -- IMPLICIT RULES / LINKING
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.m Makefile
@$(CC) -c $< -o $@ $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS)
$(OBJ_DIR):
@$(MKDIR) $(OBJ_DIR)
$(LIB_DIR):
@$(MKDIR) $(LIB_DIR)
$(NAME): $(OBJ_DIR) $(OBJECTS)
@$(CC) $(LDLIBS) $(OBJECTS) -o $@
@printf "Created binary \"$(NAME)\"\n"
$(LIB).a: $(LIB_DIR) $(OBJ_DIR) $(LIB_OBJS)
@$(AR) $(LIB_DIR)/$@ $(LIB_OBJS)
@printf "Created library \"$(LIB_DIR)/$@\"\n"
# For each header file, we do the following, using regular expressions:
# - Ignore the #ifndef _FILE/# define _FILE/#endif directives that begin/end the file
# - Extract the #import directives
# - Extract all the remaining # directives
# - Extract the rest: types and functions declarations.
# All the extracted lines are then appendend to the final header file.
$(LIB).h: $(LIB_DIR) $(LIB_HDRS)
@imports=""; \
directives=""; \
declarations=""; \
for file in $(LIB_HDRS); do \
declarations="$$declarations\n\n/*\n * -- $$(basename $$file .h | tr '[:lower:]' '[:upper:]')\n*/\n"; \
fileguard=$$(echo "_$$(basename $$file .h | tr '[:lower:]' '[:upper:]')_H"); \
if [ "$$(head -n 1 $$file)" = "#ifndef $$fileguard" ]; then \
filecontent=$$(sed -e '1,2d' -e '$$d' $$file); \
else \
filecontent=$$(cat $$file); \
fi; \
imports="$$imports\n$$(echo "$$filecontent" | grep -E "^#\s*import" | sort | uniq)"; \
directives="$$directives\n$$(echo "$$filecontent" | grep -E "^#" | grep -vE "^#\s*import" | grep -vE "^#\s*include\s*\".*.h\"$$" )"; \
declarations="$$declarations\n$$(echo "$$filecontent" | grep -vE "^#" )"; \
done; \
guard=$$(echo "_$(LIB)_H" | tr '[:lower:]' '[:upper:]'); \
printf "#ifndef $$guard\n# define $$guard\n\n" > $(LIB_DIR)/$@; \
printf "$$directives\n\n" >> $(LIB_DIR)/$@; \
printf "$$imports\n\n" >> $(LIB_DIR)/$@; \
printf "$$declarations\n\n" >> $(LIB_DIR)/$@; \
printf "#endif" >> $(LIB_DIR)/$@; \
sed -i '' -e '/^$$/N;/^\n$$/D' $(LIB_DIR)/$@; \
@printf "Created header \"$(LIB_DIR)/$@\"\n"
# -- RULES
.DEFAULT_GOAL := binary
all: binary lib
binary: $(NAME)
lib: $(LIB).a $(LIB).h
clean:
@if [ -e $(OBJ_DIR) ]; then \
$(RMDIR) $(OBJ_DIR); \
printf "Objects deleted\n"; \
fi;
fclean: clean
@if [ -e $(NAME) ]; then \
$(RM) $(NAME); \
printf "Binary deleted\n"; \
fi;
@if [ -e $(LIB_DIR) ]; then \
$(RMDIR) $(LIB_DIR); \
printf "Library deleted\n"; \
fi;
re: fclean all
install:
/bin/mkdir -p $(BIN_DIR)
sudo /usr/bin/install -s -m 0755 $(NAME) $(BIN_DIR)
.PHONY: all lib clean fclean re install
-include $(OBJECTS:.o=.d)