-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
202 lines (159 loc) · 5.11 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#
#
#
# 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 := make/bin
SRCDIR := src
LOGDIR := make/logs
LIBDIR := make/lib
TESTDIR := test
UTILSDIR = $(SRCDIR)/utils
ifeq ($(OS),Windows_NT)
# define CPLEXDIR
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
CPLEXDIR := /opt/ibm/ILOG/CPLEX_Studio2211/cplex
CC := gcc
CFLAGS := -O3 $(STD) $(STACK) $(WARNS) -Wno-gnu-statement-expression
endif
ifeq ($(UNAME_S),Darwin)
CPLEXDIR := /Applications/CPLEX_Studio2211/cplex
CC := clang
CFLAGS := -O3 $(STD) $(STACK) $(WARNS) -Wno-gnu-statement-expression -fsanitize=address
endif
endif
# Source code file extension
SRCEXT := c
# 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
# Debug options
DEBUG := -g3 -DDEBUG=1 -gdwarf-4
# Dependency libraries
ifeq ($(OS),Windows_NT)
ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
# define libs
else
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
# define libs
endif
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
# define libs
endif
endif
else
UNAME_S := $(shell uname -s)
UNAME_P := $(shell uname -p)
ifeq ($(UNAME_S),Linux)
LIBS := -L${CPLEXDIR}/lib/x86-64_linux/static_pic -L. -lcplex -lm -lpthread -ldl
endif
ifeq ($(UNAME_S),Darwin)
ifeq ($(UNAME_P),x86_64)
LIBS := -L${CPLEXDIR}/lib/x86_64_osx/static_pic -L. -lcplex -lm -lpthread -ldl
endif
ifneq ($(filter arm%,$(UNAME_P)),)
LIBS := -L${CPLEXDIR}/lib/arm64_osx/static_pic -L. -lcplex -lpthread -ldl
endif
endif
endif
INC := -I. -I$(CPLEXDIR)/include/ilcplex
# Test libraries
TEST_LIBS := -l cmocka -L /usr/local/lib -rpath /usr/local/lib
# Tests binary file
TEST_BINARY := $(BINARY)_test_runner
# Find all .c files in SRCDIR and its subdirectories
SRCS := $(wildcard $(SRCDIR)/**/*.c)
SRCS += $(wildcard $(SRCDIR)/*.c)
# Extract the names of the files with their relative paths
NAMES := $(patsubst $(SRCDIR)/%.$(SRCEXT),%,$(SRCS))
# Generate object file paths
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/"
all: $(OBJECTS)
@echo -en "$(BROWN)LD $(END_COLOR)";
$(CC) -o $(BINDIR)/$(BINARY) $+ $(DEBUG) $(CFLAGS) $(LIBS)
$(LIBDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@echo -en "$(BROWN)CC $(END_COLOR)";
$(CC) -c $< -o $@ $(DEBUG) $(CFLAGS) $(INC)
# Rule for run valgrind tool
valgrind:
valgrind \
--track-origins=yes \
--leak-check=full \
--show-leak-kinds=all \
--leak-resolution=high \
--log-file=$(LOGDIR)/[email protected] \
$(BINDIR)/$(BINARY) $(ARGS)
@echo -en "\nCheck the log file: $(LOGDIR)/[email protected]\n"
# Compile tests and run the test binary
tests:
@echo -en "$(BROWN)CC $(END_COLOR)";
$(CC) $(TESTDIR)/main.c $(shell find src ! -name 'main.c' -type f -name "*.c") -I $(SRCDIR) -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)/utils/* $(LIBDIR)/algorithms/* $(LIBDIR)/*.o $(LOGDIR)/*;