-
Notifications
You must be signed in to change notification settings - Fork 30
/
Makefile
165 lines (120 loc) · 3.57 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
#
#
#
# Makefile template for C code
#
# Author: Gustavo Pantuza Coelho Pinto
# Since: 24.03.2016
#
#
#
# Includes the project configurations
include project.conf
#
# Validating project variables defined in project.conf
#
ifndef PROJECT_NAME
$(error Missing PROJECT_NAME. Put variables at project.conf file)
endif
ifndef BINARY
$(error Missing BINARY. Put variables at project.conf file)
endif
ifndef PROJECT_PATH
$(error Missing PROJECT_PATH. Put variables at project.conf file)
endif
# Gets the Operating system name
OS := $(shell uname -s)
# Default shell
SHELL := bash
# Color prefix for Linux distributions
COLOR_PREFIX := e
ifeq ($(OS),Darwin)
COLOR_PREFIX := 033
endif
# Color definition for print purpose
BROWN=\$(COLOR_PREFIX)[0;33m
BLUE=\$(COLOR_PREFIX)[1;34m
END_COLOR=\$(COLOR_PREFIX)[0m
# Source code directory structure
BINDIR := bin
SRCDIR := src
LOGDIR := log
LIBDIR := lib
TESTDIR := test
# 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 $(STD) $(STACK) $(WARNS)
# Debug options
DEBUG := -g3 -DDEBUG=1
# Dependency libraries
LIBS := # -lm -I some/path/to/library
# Test libraries
TEST_LIBS := -l cmocka -L /usr/lib
# Tests binary file
TEST_BINARY := $(BINARY)_test_runner
# %.o file names
NAMES := $(notdir $(basename $(wildcard $(SRCDIR)/*.$(SRCEXT))))
OBJECTS :=$(patsubst %,$(LIBDIR)/%.o,$(NAMES))
#
# COMPILATION RULES
#
default: all
# Help message
help:
@echo "C Project Template"
@echo
@echo "Target rules:"
@echo " all - Compiles and generates binary file"
@echo " tests - Compiles with cmocka and run tests binary file"
@echo " start - Starts a new project using C project template"
@echo " valgrind - Runs binary file using valgrind tool"
@echo " clean - Clean the project by removing binaries"
@echo " help - Prints a help message with target rules"
# Starts a new project using C project template
start:
@echo "Creating project: $(PROJECT_NAME)"
@mkdir -pv $(PROJECT_PATH)
@echo "Copying files from template to new directory:"
@cp -rvf ./* $(PROJECT_PATH)/
@echo
@echo "Go to $(PROJECT_PATH) and compile your project: make"
@echo "Then execute it: bin/$(BINARY) --help"
@echo "Happy hacking o/"
# Rule for link and generate the binary file
all: $(OBJECTS)
@echo -en "$(BROWN)LD $(END_COLOR)";
$(CC) -o $(BINDIR)/$(BINARY) $+ $(DEBUG) $(CFLAGS) $(LIBS)
@echo -en "\n--\nBinary file placed at" \
"$(BROWN)$(BINDIR)/$(BINARY)$(END_COLOR)\n";
# Rule for object binaries compilation
$(LIBDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@echo -en "$(BROWN)CC $(END_COLOR)";
$(CC) -c $^ -o $@ $(DEBUG) $(CFLAGS) $(LIBS)
# Rule for run valgrind tool
valgrind:
valgrind \
--track-origins=yes \
--leak-check=full \
--leak-resolution=high \
--log-file=$(LOGDIR)/$@.log \
$(BINDIR)/$(BINARY)
@echo -en "\nCheck the log file: $(LOGDIR)/$@.log\n"
# Compile tests and run the test binary
tests:
@echo -en "$(BROWN)CC $(END_COLOR)";
$(CC) $(TESTDIR)/main.c -o $(BINDIR)/$(TEST_BINARY) $(DEBUG) $(CFLAGS) $(LIBS) $(TEST_LIBS)
@which ldconfig && ldconfig -C /tmp/ld.so.cache || true # caching the library linking
@echo -en "$(BROWN) Running tests: $(END_COLOR)";
./$(BINDIR)/$(TEST_BINARY)
# Rule for cleaning the project
clean:
@rm -rvf $(BINDIR)/* $(LIBDIR)/* $(LOGDIR)/*;