-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·200 lines (152 loc) · 4.85 KB
/
build.sh
File metadata and controls
executable file
·200 lines (152 loc) · 4.85 KB
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
#!/bin/bash
# =============================================================================================
# -- LINUX BUILD SCRIPT FOR C PROJECTS --
# =============================================================================================
SRC_PATH="./src/main.c"
EXE_NAME="GetBack"
CC="clang"
FLAGS="-std=c11 -g -g3 -O0 -DDEBUG -W -Wall -Wextra -Wno-missing-braces -Wincompatible-pointer-types-discards-qualifiers -Wno-variadic-macros -rdynamic -Werror=incompatible-pointer-types -Wno-int-conversion"
LINKERS="-lfreetype -lSDL2 -lGLEW -lGLU -lGL -lm -lassimp -pthread"
INCLUDES="-I/usr/include/freetype2 -I./lib/ -I/usr/include/SDL2"
# =============================================================================================
# -- IMPLEMENTATION --
# =============================================================================================
red=$(tput setaf 1)
green=$(tput bold; tput setaf 2)
blue=$(tput bold; tput setaf 4)
reset=$(tput sgr0)
function setup_envirnoment {
rm -rf core
ulimit -c unlimited
}
function cleanup_envirnoment {
ulimit -c 0
}
function compile_in_linux {
local FILE_PATH="$1"
# Compose the compile command
local CMD="$CC $FILE_PATH $FLAGS $INCLUDES $LINKERS -o ./bin/$EXE_NAME"
# Run the actual compile command
eval bear -- $CMD 2>&1 | grep ": error:"
# Check if the compilation actually failed
if [ ${PIPESTATUS[0]} -ne 0 ]; then
echo "Build failed with errors."
return 1
fi
}
function run_profiler {
time ./bin/$EXE_NAME
}
help_message=$(cat <<EOF
Usage: ./build.sh <tag>
Builds the project using gcc or specified compiler
Types of tags
-------------
help - prints the usage
build - only compiles program
run - compiles and runs the program
debug - runs the executable in a debugger after compilation (default)
EOF
)
function clear_terminal {
clear &&
if [ -n "$TMUX" ]; then
tmux clear-history
fi
}
function main {
clear_terminal
if [ "$1" == "help" ]
then
echo -e "$help_message"
exit 0
fi
local BIN_DIR="./bin"
local LIB_DIR="./lib"
# Cleaning bin directory
if [ "$1" == "clean" ]
then
echo -e "[!] ${green}Cleaning $BIN_DIR directory${reset}"
rm -rf $BIN_DIR 2> /dev/null
echo -e "[!] ${green}Removing coredumps${reset}"
rm -f core
echo " "
exit 0
fi
# if [ "$1" != "build" ] && [ "$1" != "run" ] && [ "$1" != "debug" ]
# then
# echo -e "$help_message"
# exit 0
# fi
if [ ! -d .git ]
then
echo "[!] Run within a C project (coudnt find .git folder here)"
exit 0
fi
# Set environment
echo -e "[!] ${green}Setting up environment${reset}"
setup_envirnoment
# Checking if bin directory is made
if [ ! -d "$BIN_DIR" ]
then
echo -e "[!] ${green}Creating directory ${reset}\`$BIN_DIR\`"
mkdir bin/
else
echo -e "[!] ${green}Found directory ${reset}\`$BIN_DIR\`"
fi
# Checking if lib directory is made
if [ ! -d "$LIB_DIR" ]
then
echo -e "[!] ${green}Creating directory ${reset}\`$LIB_DIR\` (poglib)"
if [ $(whoami) == "gokul" ]
then
ln -s /mnt/c/Users/User/OneDrive/Documents/projects/dev-libs lib
else
mkdir "$LIB_DIR"
git clone https://github.com/gimploo/poglib.git $LIB_DIR/poglib > /dev/null
fi
else
echo -e "[!] ${green}Found directory ${reset}\`$LIB_DIR\`"
fi
# Compiling source files
echo -e "[*] ${blue}Compiling source file ...${reset}"
if ! compile_in_linux $SRC_PATH ;
then
echo -e "[!] ${red}Compilation Failed ${reset}"
exit $LINENO
else
echo -e "[!] ${green}Compilation Successfull ${reset}"
fi
# Running it through the debugger
if [ "$1" == "debug" ] || [ "$1" == "" ]
then
echo -e "\n[*] ${blue}Running executable through debugger ...${reset}"
gdb_debug &
echo -e "[!] ${green}Exiting debugger ${reset}"
exit 0
fi
# Running executable
if [ "$1" == "run" ]
then
echo -e "[*] ${blue}Running executable ...\n${reset}"
run_profiler
if [ $? -eq 139 ]
then
echo -e "\n[!] ${red} Segmentation Fault Occurred ${reset}"
echo -e "\n[*] ${blue}Running executable through debugger ...${reset}"
gdb_debug
echo -e "[!] ${green}Exiting debugger ${reset}"
fi
fi
# Reseting environment
echo -e "[!] ${green}Cleaning up environment ${reset}"
cleanup_envirnoment
echo " "
exit 0
}
function gdb_debug {
#gdb --tui ./bin/GetBack
#gdbgui ./bin/GetBack
gf2 -ex run ./bin/GetBack
}
main "$1"