-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile
178 lines (126 loc) · 4.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#
#
# J S O N C A T
#
#
# Include generic Makefile variables
include Makefile.conf
# Source code directory structure
BINDIR := bin
SRCDIR := src
LOGDIR := log
LIBDIR := lib
TESTSDIR := tests
SO_PATH := /usr/bin
BIN_SO_PATH := /bin
BUILD_DIR := build
# Source code file extension
SRCEXT := c
# Defines the C Compiler
CC := gcc
# Defines the language standards for GCC
STD := -std=gnu99 # See man gcc for more options
# Protection for stack-smashing attack
STACK := -fstack-protector-all -Wstack-protector
# Specifies to GCC the required warnings
WARNS := -Wall -Wextra -pedantic # -pedantic warns on language standards
# Flags for compiling
CFLAGS := -O3 -g $(STD) $(STACK) $(WARNS)
# Debug options
DEBUG := # -g3 -D DEBUG=1
#
# The binary file name
#
BINARY := jsoncat
# %.o file names
NAMES := $(notdir $(basename $(wildcard $(SRCDIR)/*.$(SRCEXT))))
OBJECTS := $(patsubst %,$(LIBDIR)/%.o,$(NAMES))
#
# COMPILATION RULES
#
# Rule for link and generate the binary file
all: $(OBJECTS)
@echo $(ECHO_OPTS) "$(BROWN)LD $(END_COLOR)";
$(CC) -o $(BINDIR)/$(BINARY) $+ $(DEBUG) $(CFLAGS)
@echo $(ECHO_OPTS) "\n--\nBinary file placed at" \
"$(BROWN)$(BINDIR)/$(BINARY)$(END_COLOR)\n";
# Rule for object binaries compilation
$(LIBDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@echo $(ECHO_OPTS) "$(BROWN)CC $(END_COLOR)";
$(CC) -c $^ -o $@ $(DEBUG) $(CFLAGS)
# installs the binary file at operating system path
install: $(BINDIR)/$(BINARY) $(DOCS)/$(MANPAGE)
@echo $(ECHO_OPTS) "Installing binary file at" \
"$(BROWN)$(SO_PATH)/$(BINARY)$(END_COLOR)\n";
@install $^ $(SO_PATH)/
@echo $(ECHO_OPTS) "Creating a symbolic link of the binary file on" \
"$(BROWN)$(BIN_SO_PATH)/$(BINARY)$(END_COLOR)\n";
@ln -sf $(SO_PATH)/$(BINARY) $(BIN_SO_PATH)/$(BINARY)
@echo $(ECHO_OPTS) "Installing manpages at" \
"$(BROWN)$(MANPAGE_OS_DIR)/$(MANPAGE)$(END_COLOR)\n";
@install $(DOCS)/$(MANPAGE) $(MANPAGE_OS_DIR)/$(MANPAGE)
@echo $(ECHO_OPTS) "--\n\n$(BROWN)Installation complete$(END_COLOR)\n"
# Uninstall the jsoncat binary
uninstall: $(SO_PATH)/$(BINARY)
@echo $(ECHO_OPTS) "$(BLUE)Uninstalling Jsoncat..$(END_COLOR)\n";
@rm -vf $(BIN_SO_PATH)/$(BINARY)
@rm -vf $^
@rm -vf $(MANPAGE_OS_DIR)/$(MANPAGE)
# Build project as distribution packages
build: $(BUILD_DIR)/Makefile
@make -C $^ all
# Rule for run valgrind tool
valgrind:
valgrind \
--track-origins=yes \
--leak-check=full \
--leak-resolution=high \
--log-file=$(LOGDIR)/[email protected] \
$(BINDIR)/$(BINARY) $(TESTSDIR)/samples/input_08.json
@echo $(ECHO_OPTS) "\nCheck the log file: $(LOGDIR)/[email protected]\n"
# Rule to run the resulted binary file
run: $(BINDIR)/$(BINARY)
@./$^ $(TESTSDIR)/samples/input_06.json
# Debugs jsoncat using gdb
debug:
gdb --args $(BINDIR)/$(BINARY) tests/samples/input_06.json
# Rule for cleaning the project
clean:
@rm -rvf $(BINDIR)/*;
@rm -rvf $(LIBDIR)/*;
@rm -rvf $(LOGDIR)/*;
@rm -rvf $(DOCS)/*;
# Rule to run the project tests
test: command_line_test strings_test constants_test arrays_test \
objects_test samples_test
# Rule to run the command line arguments tests
command_line_test: $(TESTSDIR)/command_args.sh
@$(SHELL) $^
# Rule to run tests on different kind of strings
strings_test: $(TESTSDIR)/strings.sh
@$(SHELL) $^
# Rule to run tests on constants as json value
constants_test: $(TESTSDIR)/constants.sh
@$(SHELL) $^
# Rule to run tests on different types of arrays
arrays_test: $(TESTSDIR)/arrays.sh
@$(SHELL) $^
# Rule to run tests on different types of objects
objects_test: $(TESTSDIR)/objects.sh
@$(SHELL) $^
# Rule to run samples parsing test
samples_test: $(TESTSDIR)/samples.sh
@$(SHELL) $^
help:
@echo $(ECHO_OPTS) "\n$(BROWN)# J s o n C a t M a k e f i l e$(END_COLOR)\n\n"
@echo $(ECHO_OPTS) "Available target rules for this Makefile:\n\n"
@echo $(ECHO_OPTS) "\t$(BLUE)all (default)$(END_COLOR)\tCompile and link jsoncat binary\n"
@echo $(ECHO_OPTS) "\t$(BLUE)install$(END_COLOR)\t\tInstalls binary and man pages files on system\n"
@echo $(ECHO_OPTS) "\t$(BLUE)uninstall$(END_COLOR)\tRemove binary files from system\n"
@echo $(ECHO_OPTS) "\t$(BLUE)build$(END_COLOR)\t\tBuilds linux distributions packages (rpm && deb)\n"
@echo $(ECHO_OPTS) "\t$(BLUE)run$(END_COLOR)\t\tRun the compiled binary with a sample input file\n"
@echo $(ECHO_OPTS) "\t$(BLUE)valgrind$(END_COLOR)\tRun binary file with valgrind tool\n"
@echo $(ECHO_OPTS) "\t$(BLUE)debug$(END_COLOR)\t\tRuns the binary using gdb tool\n"
@echo $(ECHO_OPTS) "\t$(BLUE)test$(END_COLOR)\t\tRun entire project test suite\n"
@echo $(ECHO_OPTS) "\t$(BLUE)clean$(END_COLOR)\t\tCleans project files\n"
@echo $(ECHO_OPTS) "\t$(BLUE)help$(END_COLOR)\t\tPrints this help message\n\n"