-
Notifications
You must be signed in to change notification settings - Fork 2
/
make.sh
277 lines (226 loc) · 8.85 KB
/
make.sh
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
################################################################################
# LICENSE #
################################################################################
# This file is part of rss_ringoccs. #
# #
# rss_ringoccs is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# rss_ringoccs is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with rss_ringoccs. If not, see <https://www.gnu.org/licenses/>. #
################################################################################
# Author: Ryan Maguire #
# Date: March 10, 2021 #
################################################################################
# Choose whatever C compiler you want. Tested with gcc, clang, tcc, and pcc
# on GNU/Linux (Debian, Ubuntu, Fedora, and more) and FreeBSD 12.1.
CC=cc
# Version of C to be used. Change this with -std=cxx.
STDVER="-std=c89"
# Use of OpenMP, the multiprocessing API. Enable this with -omp.
USEOMP=0
# Whether or not to install the library to system folders.
INPLACE=0
# Compile the entire library by #include'ing all files into one translation
# unit. The compiler get's to the see the entire library at once and make many
# optimizations.
MAKEMONSTER=0
# Files to be excluded (added later based on USEINLINE and USEMATH.
Exclude=""
# You can pass extra arguments. Just add -MyArgument.
ExtraArgs=""
# Parse the inputs.
for arg in "$@"; do
# If there are no more arguments, break out of this loop.
if [ "$arg" == "" ]; then
break
# Check if the user wants to use a different compiler.
elif [[ "$arg" == *"-cc"* ]]; then
CC=${arg#*=}
# Check if the user wants to use a different version of the C language.
elif [[ "$arg" == *"-std"* ]]; then
STDVER=$arg
# Check if OpenMP support is requested.
elif [ "$arg" == "-omp" ]; then
USEOMP=1
# Check if the user wants to build rss_ringoccs in rss_ringoccs/ directory.
elif [ "$arg" == "-inplace" ]; then
INPLACE=1
elif [ "$arg" == "-monster" ]; then
MAKEMONSTER=1
elif [ "$arg" == "-remove" ]; then
SONAME="librssringoccs.so"
SODIR="/usr/local/lib"
INCLUDE_TARGET=/usr/local/include/rss_ringoccs/
echo "Removing rss_ringoccs:"
echo " Clearing older files..."
rm -f *.so *.o *.obj *.lib
echo -e " Removing include directory if it exists..."
if [ -d "$INCLUDE_TARGET" ]; then
rm -rf "$INCLUDE_TARGET";
fi
echo " Removing shared object file if it exists..."
if [ -e "$SODIR/$SONAME" ]; then
rm -f "$SODIR/$SONAME";
fi
echo "rss_ringoccs removed."
exit 1
# Check for any extra arguments.
else
ExtraArgs="$ExtraArgs ${arg#*=}"
fi
done
# If OpenMP support is requested, add this to the extra arguments.
if [ $USEOMP == 1 ]; then
ExtraArgs="$ExtraArgs -fopenmp"
fi
# LLVM's clang has the -Weverything warning which enables every possible
# warning. Padding warning's are unnecessary, as are warnings about comparing
# floats for equality. All other warnings should be enabled.
if [ "$CC" == "clang" ]; then
ExtraArgs="$ExtraArgs -Weverything -Wno-padded -Wno-float-equal"
fi
# Name of the created Shared Object file (.so).
SONAME="librssringoccs.so"
# Location to store SONAME at the end of building.
SODIR="/usr/local/lib"
# Compiler arguments. All of these are supported by gcc, tcc, pcc, and clang.
CArgs1="-pedantic -Wall -Wextra -Wpedantic -Wmissing-field-initializers"
CArgs2="-Wconversion -Wmissing-prototypes -Wmissing-declarations"
CArgs3="-Winit-self -Wnull-dereference -Wwrite-strings -Wdouble-promotion"
CArgs4="-Wfloat-conversion -Wstrict-prototypes -Wold-style-definition"
CArgs5="-I../ -DNDEBUG -g -fPIC -O3 -flto -c"
CompilerArgs="$STDVER $ExtraArgs $CArgs1 $CArgs2 $CArgs3 $CArgs4 $CArgs5"
# Linking arguments.
# -O3 is optimization level 3.
# -I../ means include the parent directory so rss_ringoccs/ is in the path.
# -flto is link time optimization.
# -lm means link against the standard math library.
# -o means create an output.
# -shared means the output is a shared object, like a library file.
if [ $USEOMP == 1 ]; then
LinkerArgs="-O3 -flto -fopenmp -shared -o $SONAME -lm"
else
LinkerArgs="-O3 -flto -shared -o $SONAME -lm"
fi
# Location where the .h files will be stored.
INCLUDE_TARGET=/usr/local/include/rss_ringoccs/
# There may be left-over .so and .o files from a previous build. Remove those
# to avoid a faulty build.
echo "Clearing older files"
rm -f *.so *.o *.obj *.lib
# If we're not building rss_ringoccs into rss_ringoccs/, clear older files.
if [ $INPLACE == 0 ]; then
# Clear older header files.
if [ -d "$INCLUDE_TARGET" ]; then
echo "Clearing $INCLUDE_TARGET"
rm -rf "$INCLUDE_TARGET";
fi
# Clear older library files.
if [ -e "$SODIR/$SONAME" ]; then
echo "Erasing $SODIR/$SONAME"
rm -f "$SODIR/$SONAME";
fi
fi
# Build
echo ""
echo "Compiling rss_ringoccs"
echo " Compiler:"
echo " $CC"
echo " Version:"
echo " $STDVER"
echo " Compiler Options:"
echo " $CArgs1"
echo " $CArgs2"
echo " $CArgs3"
echo " $CArgs4"
echo " $CArgs5"
echo " Extra Compiler Arguments:"
echo " $ExtraArgs"
if [ $MAKEMONSTER == 1 ]; then
if [ -e "monster.c" ]; then
rm -f monster.c;
fi
touch monster.c
for file in include/*.h; do
echo "#include \"$file\"" >> monster.c;
done
for dir in src/*/; do
for filename in $dir*.c; do
filename_without_path=$(basename -- $filename)
if [[ $Exclude == *"$filename_without_path"* ]]; then
continue;
fi
echo "#include \"$filename\"" >> monster.c;
done
done
echo "Compiling rss_ringoccs..."
if !($CC $CompilerArgs monster.c); then
exit 1
fi
echo "Building rss_ringoccs Shared Object (.so file)"
if !($CC ./*.o $LinkerArgs); then
exit 1
fi
rm -f *.o
rm -f monster.c
else
# Loop over all directories in src/ and compile all .c files.
for dir in src/*; do
echo ""
echo " Compiling $dir"
for filename in $dir/*.c; do
filename_without_path=$(basename -- $filename)
if [[ $Exclude == *"$filename_without_path"* ]]; then
continue;
fi
echo " Compiling: $filename"
if !($CC $CompilerArgs $filename); then
exit 1
fi
done
done
echo ""
echo "Building rss_ringoccs Shared Object (.so file)"
if !($CC ./*.o $LinkerArgs); then
exit 1
fi
fi
# If inplace is set, we can't use sudo.
if [ $INPLACE == 0 ]; then
# Copy the header files to the appropriate directory.
echo "Copying include/ directory to $INCLUDE_TARGET"
mkdir -p /usr/local/lib/
mkdir -p "$INCLUDE_TARGET"
cp -r ./include "$INCLUDE_TARGET"
# Move the shared object file to the appropriate directory.
echo "Moving $SONAME to $SODIR"
mv $SONAME $SODIR
fi
echo "Cleaning up"
rm -f *.o
echo "Done"
if [ $INPLACE == 1 ]; then
echo "PLEASE NOTE:"
echo " You used the in-place option."
echo " rss_ringoccs was only installed in this directory:"
echo " $(pwd)"
echo " To use rss_ringoccs you must have this in your path."
echo " The header files are located in:"
echo " $(pwd)/include/"
echo " and have NOT been placed in /usr/local/include/"
echo " Your compiler will not see these if you"
echo " do not pass the correct options."
echo " For most compilers (GCC, Clang, PCC, TCC) you can link"
echo " rss_ringoccs to you programs with the proper include"
echo " directories using the -I and -L option as follows:"
echo " gcc -I$(pwd)/../ -L$(pwd)/ file.c -o file.out -lrssringoccs"
fi