-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.sh
More file actions
226 lines (201 loc) · 6.09 KB
/
compile.sh
File metadata and controls
226 lines (201 loc) · 6.09 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#! /bin/bash
RED="\033[31m"
NORMAL="\033[0m"
# Variables
library_dir=library
lib_dir=lib
examples_dir=examples
example_bin_dir=bin/bash
example_obj_dir=examples/obj
dynamic_bin_dir=$lib_dir/bash/dynamic/bin
dynamic_obj_dir=$lib_dir/bash/dynamic/bin
static_obj_dir=$lib_dir/bash/static/obj
static_bin_dir=$lib_dir/bash/static/bin
dependencies=(src/mystring.cpp include/mystring.h)
# Compiler
cxx=g++
cxx_flags=-O3
# Flags
Help() {
echo "Usage: compile.sh [options]"
echo "Options:"
echo " -h, --help"
echo " -cr, --compile-run: Compile and run the example"
echo " -c, --compile: Compile only"
echo " -r, --run: Run only"
echo " --clean: Clean compiled files and build directory"
echo " --build_type=Debug/Release: Set build type. Defaults to Debug"
}
DebugMsg() {
echo "$1"
}
# Compile static and shared libraries
CompileLibraries() {
mkdir -p $static_bin_dir && mkdir -p $static_obj_dir
mkdir -p $dynamic_bin_dir && mkdir -p $dynamic_obj_dir
DebugMsg "Compiling static library..."
$cxx $cxx_flags -c $library_dir/src/mystring.cpp -o $static_obj_dir/mystring.o
ar rcs $static_bin_dir/mystring.a $static_obj_dir/mystring.o
DebugMsg "Compiled successfully. Written to $static_bin_dir/mystring.a"
DebugMsg "Compiling dynamic library..."
$cxx $cxx_flags -c $library_dir/src/mystring.cpp -fPIC -o $dynamic_obj_dir/mystring.o
$cxx $cxx_flags -shared -fPIC -o $dynamic_bin_dir/libmystring.so $dynamic_obj_dir/mystring.o
DebugMsg "Compiled successfully. Written to $dynamic_bin_dir/libmystring.so"
}
# Compile the example
CompileExample() {
DebugMsg "Compiling example..."
mkdir -p $example_bin_dir && mkdir -p $example_obj_dir
$cxx $cxx_flags -c $examples_dir/example.cpp -o $example_obj_dir/example.o -I$library_dir
DebugMsg "Linking example with static library..."
$cxx $cxx_flags -fPIC $example_obj_dir/example.o $static_bin_dir/mystring.a -o $example_bin_dir/static_example
DebugMsg "Successfully linked with static library. Written to $example_bin_dir/static_example"
DebugMsg "Linking example with dynamic library..."
$cxx $cxx_flags -fPIC $example_obj_dir/example.o $dynamic_bin_dir/libmystring.so -o $example_bin_dir/dynamic_example
DebugMsg "Successfully linked with dynamic library. Written to $example_bin_dir/dynamic_example"
}
# Check state and compile libraries and example if needed
Compile() {
if CheckUpToDate; then
echo -e "${RED}Libraries are up to date. Nothing to compile${NORMAL}"
echo "Do you want to clean-compile? (y/n)"
read -r answer
if [ "$answer" == "n" ]; then
exit 0
fi
if Clean; then
CompileLibraries
CompileExample
else
exit 0
fi
else
CompileLibraries
CompileExample
fi
}
# Check if libraries are up to date
CheckUpToDate() {
if ! CheckCompiled; then
return 1
fi
for file in "${dependencies[@]}"; do
if [ $library_dir/"$file" -nt $dynamic_bin_dir/libmystring.so ]; then
return 1
elif [ $library_dir/"$file" -nt $static_bin_dir/mystring.a ]; then
return 1
else
return 0
fi
done
}
# Check if libraries and example are compiled
CheckCompiled() {
if [[ -f $example_bin_dir/static_example && -f $static_bin_dir/mystring.a ]]; then
return 0
elif [[ -f $example_bin_dir/dynamic_example && -f $dynamic_bin_dir/libmystring.so ]]; then
return 0
else
return 1
fi
}
# Run the example
Run() {
if CheckCompiled; then
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "Linux detected. Setting LD_LIBRARY_PATH..."
echo ""
echo "With which type of library you want compile your example? (s/d)"
read -r answer
if [ "$answer" == "s" ] || [ "$answer" == "S" ]; then
LIBRARY_PATH="$static_bin_dir" $example_bin_dir/static_example
elif [ "$answer" == "d" ] || [ "$answer" == "D" ]; then
LD_LIBRARY_PATH="$dynamic_bin_dir" $example_bin_dir/dynamic_example
else
echo -e "${RED}Error: You input incorrect data. Restart..."
Run
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "MacOS detected. Setting DYLD_LIBRARY_PATH..."
echo ""
echo "With which type of library you want compile your example? (s/d)"
read -r answer
if [ "$answer" == "s" ] || [ "$answer" == "S" ]; then
LIBRARY_PATH="$static_bin_dir" $example_bin_dir/static_example
elif [ "$answer" == "d" ] || [ "$answer" == "D" ]; then
DYLD_LIBRARY_PATH="$dynamic_bin_dir" $example_bin_dir/dynamic_example
else
echo -e "${RED}Error: You input incorrect data. Restart..."
Run
fi
fi
else
echo -e "${RED}Error: nothing to run. Necessary files are not compiled. Run with -c or --compile option to compile.${NORMAL}
Do you want to automatically compile libraries and run the example? (y/n)"
read -r answer
if [[ "$answer" == "y" ]]; then
Compile
Run
else
echo "Exiting..."
exit 1
fi
fi
}
# Clean compiled files
Clean() {
echo "This will remove all compiled files along with the library. Are you sure? (y/n)"
read -r answer
if [[ "$answer" == "y" ]]; then
rm -rf $lib_dir/bash
rm -rf $example_bin_dir
echo "Cleaned successfully."
return 0
else
echo -e "${RED}Aborted${NORMAL}"
return 1
fi
}
# Check if there is no argument
if [ $# -eq 0 ]; then
Help
exit 1
fi
# Check if there is more than one argument
if [ $# -gt 1 ]; then
echo -e "${RED}Error: Too many arguments${NORMAL}"
Help
exit 1
fi
# Check if the argument is -h or --help
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
Help
exit 0
fi
# Check if the argument is -r or --run
if [ "$1" = "-r" ] || [ "$1" = "--run" ]; then
Run
exit 0
fi
# Check if the argument is -c or --compile
if [ "$1" = "-c" ] || [ "$1" = "--compile" ]; then
Compile
exit 0
fi
# Check if the argument is -cr or --compile-run
if [ "$1" = "-cr" ] || [ "$1" = "--compile-run" ]; then
Compile
Run
exit 0
fi
# Check if the argument is clean
if [ "$1" = "--clean" ]; then
Clean
exit 0
fi
# Check if the argument is not valid
if [[ ! " ${valid_args[*]} " == *" $1 "* ]]; then
echo -e "${RED}Error: Invalid argument${NORMAL}"
Help
exit 1
fi