-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
167 lines (140 loc) · 5.93 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
#==============================================================================
# MAKEFILE VARIABLES
#==============================================================================
# Variables for the make help printout
PROJECT_NAME := LinearSystemSolver
VERSION := v0.0.1
# Binary names (not path, only file name)
PRODUCTION_BINARY_NAME := LinearSystemSolver
TEST_SUITE_BINARY_NAME := TestSystemSolver
# Entry file names (not path, only file name)
PRODUCTION_ENTRY_NAME := main.c
TEST_SUITE_ENTRY_NAME := start_test.c
# Directory definitions
INC_DIR := include
SRC_DIR := src
TST_DIR := test
OBJ_DIR := build
BIN_DIR := bin
# Testing configuration
TEST_CFLAGS := -Wall -g -I$(INC_DIR) -DTEST
TEST_LDLIBS := -lcheck -lm -lpthread -lrt -lsubunit -lOpenCL
# Production source preparation
PRODUCTION_ENTRY_FILE := $(SRC_DIR)/$(PRODUCTION_ENTRY_NAME)
HEADER_FILES := $(wildcard $(INC_DIR)/*.h)
PRODUCTION_FILES := $(filter-out $(PRODUCTION_ENTRY_FILE),$(wildcard $(SRC_DIR)/*.c))
PRODUCTION_ENTRY_OBJECT := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(PRODUCTION_ENTRY_FILE))
PRODUCTION_OBJECTS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(PRODUCTION_FILES))
# Unit testing source preparation
TEST_ENTRY_FILE := $(TST_DIR)/$(TEST_SUITE_ENTRY_NAME)
TEST_FILES := $(filter-out $(TEST_ENTRY_FILE),$(wildcard $(TST_DIR)/*.cpp))
TEST_ENTRY_OBJECT := $(patsubst $(TST_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(TEST_ENTRY_FILE))
TEST_OBJECTS := $(patsubst $(TST_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(TEST_FILES))
# Compilation related flags and parameters
CC=gcc
CFLAGS=-Wall -g -I$(INC_DIR)
LDLIBS=-lOpenCL
# Makefile formatting variables
SILENCE:=@
ECHO_FLAG:=-n
RED := $(shell tput setaf 1)
GREEN := $(shell tput setaf 2)
YELLOW := $(shell tput setaf 3)
BOLD := $(shell tput bold)
RESET:= $(shell tput sgr0)
#==============================================================================
# MAKE TARGETS
#==============================================================================
.PHONY: test clean leak_detection files help main
# First make target = help
help:
@echo "---------------------------------------------------------------------"
@echo " $(BOLD)$(YELLOW)$(PROJECT_NAME)$(RESET) $(BOLD)$(VERSION)$(RESET) make interface"
@echo "---------------------------------------------------------------------"
@echo " $(BOLD)make [help]$(RESET) - Prints out this help message."
@echo " $(BOLD)make test$(RESET) - Compiles the whole test suite and runs it."
@echo " $(BOLD)make main$(RESET) - Compiles the project."
@echo " $(BOLD)make files$(RESET) - Prints out the files registered by make."
@echo " $(BOLD)make clean$(RESET) - Cleans up the build directory."
#==============================================================================
# PRODUCTION TARGETS
#==============================================================================
# Building prodution files (remove -D TEST)
$(PRODUCTION_ENTRY_OBJECT) $(PRODUCTION_OBJECTS): $(PRODUCTION_ENTRY_FILE) $(PRODUCTION_FILES) $(HEADER_FILES)
@echo $(ECHO_FLAG) "Compiling production code.. "
$(SILENCE)$(CC) $(CFLAGS) $(LDLIBS) -D TEST -c $^ \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
$(SILENCE)if [ -f _error_flag ]; then \
rm -f _error_flag; \
echo "$(RED)$(BOLD)Error$(RESET)"; \
cat temp_error_file; \
rm -f temp_error_file; \
echo "$(RED)$(BOLD)Make aborted!$(RESET)"; \
false; \
else \
if [ -s temp_error_file ]; then \
echo "$(YELLOW)$(BOLD)Warning$(RESET)"; \
cat temp_error_file; \
else \
echo "$(GREEN)$(BOLD)Done$(RESET)"; \
fi \
fi
$(SILENCE)rm -f temp_error_file
$(SILENCE)rm -f $(INC_DIR)/*.gch
$(SILENCE)mv *.o $(OBJ_DIR)
# Building the whole application
main: $(OBJ_DIR) $(BIN_DIR) $(PRODUCTION_ENTRY_OBJECT)
$(SILENCE)$(CC) -o $(BIN_DIR)/$(PRODUCTION_BINARY_NAME) $(PRODUCTION_ENTRY_OBJECT) $(PRODUCTION_OBJECTS) $(CFLAGS) $(LDLIBS)
#==============================================================================
# TESTING TARGETS
#==============================================================================
# Main target for unit testing
test: $(OBJ_DIR) $(BIN_DIR) $(PRODUCTION_ENTRY_OBJECT)
$(SILENCE)$(CC) $(TEST_ENTRY_FILE) $(PRODUCTION_OBJECTS) -o $(BIN_DIR)/$(TEST_SUITE_BINARY_NAME) $(TEST_CFLAGS) $(TEST_LDLIBS)
@echo "Testing... $(YELLOW)$(BOLD)Start$(RESET)"
$(SILENCE)./$(BIN_DIR)/$(TEST_SUITE_BINARY_NAME) \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
$(SILENCE)if [ -f _error_flag ]; then \
rm -f _error_flag; \
echo "Testing... $(RED)$(BOLD)Error$(RESET)"; \
cat temp_error_file; \
rm -f temp_error_file; \
false; \
else \
if [ -s temp_error_file ]; then \
echo "Testing... $(YELLOW)$(BOLD)Warning$(RESET)"; \
cat temp_error_file; \
else \
echo "Testing... $(GREEN)$(BOLD)Success$(RESET)"; \
fi \
fi
$(SILENCE)rm -f temp_error_file
#==============================================================================
# UTILITY TARGETS
#==============================================================================
# Creating the build directory
$(OBJ_DIR):
$(SILENCE)mkdir -p $@
# Creating the bin directory
$(BIN_DIR):
$(SILENCE)mkdir -p $@
# Cleaning up the build directory
clean:
@echo $(ECHO_FLAG) "Cleaning up.. "
$(SILENCE)rm -rf $(OBJ_DIR)
$(SILENCE)rm -rf $(BIN_DIR)
@echo "$(GREEN)$(BOLD)Done$(RESET)"
# File and target listing
files:
@echo ' $(YELLOW)$(BOLD)Production files$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(PRODUCTION_ENTRY_FILE)'
@echo ' $(BOLD)Headers$(RESET): $(HEADER_FILES)'
@echo ' $(BOLD)Sources$(RESET): $(PRODUCTION_FILES)'
@echo ' $(YELLOW)$(BOLD)Test files$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(TEST_ENTRY_FILE)'
@echo ' '
@echo ' $(GREEN)$(BOLD)Production objects$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(PRODUCTION_ENTRY_OBJECT)'
@echo ' $(BOLD)Objects$(RESET): $(PRODUCTION_OBJECTS)'
@echo ' $(GREEN)$(BOLD)Test objects$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(TEST_ENTRY_OBJECT)'