-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.linux.sh
executable file
·353 lines (254 loc) · 9.21 KB
/
build.linux.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env bash
# MIT License
#
# Copyright (c) 2017-2021 Cody Tilkins
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# - Basic Variables --------------------------------------------------
[[ -z "${debug}" ]] && debug="0"
[[ -z "${debug_coverage}" ]] && debug_coverage="0"
[[ -z "${GCC}" ]] && GCC="gcc"
[[ -z "${OBJCOPY}" ]] && OBJCOPY="objcopy"
[[ -z "${AR}" ]] && AR="ar"
[[ -z "${MAKE}" ]] && MAKE="make"
[[ -z "${GCC_VER}" ]] && GCC_VER="gnu99"
# - Basic Functions --------------------------------------------------
error() {
printf "\n\n$0 : ${2} : Error: ${1}\n" 1>&2
exit 1
}
# TODO: translate, 'cat build.espanol.linux.help'
help_message() {
printf """
Usage:
build build lua-x.x.x Builds the driver with a default package
build package lua-x.x.x Creates packages for the driver
build clean Cleans the environment of built files
build install {directory} Installs to a pre-created directory
build -? /? --help Shows this help message
build and package accepts:
luajit, lua-5.1, lua-5.1.5, lua-5.2.0, etc
Listens to these variables:
debug, debug_coverage, GCC, OBJCOPY, AR, MAKE, GCC_VER
debug - 0, 1 Default: 0
debug_coverage - 0, 1 Default: 0
GCC - gcc binary Default: gcc
OBJCOPY - objcopy binary Default: objcopy
AR - ar binary Default: ar
MAKE - make binary Default: make
GCC_VER - stdlib version Default: gnu99
Notes:
After building, you may need to configure LD_LIBRARY_PATH to bin/Release/* to test
"""
exit 0
}
# - Basic Switches ---------------------------------------------------
[[ -z "${1}" || "${1}" == "/?" || "${1}" == "-?" || "${1}" == "--help" ]] && help_message
# - Basic Dependencies Checking --------------------------------------
which "${GCC}" || error "GCC not found suggested: apt install gcc build-essential" $LINENO
which "${OBJCOPY}" || error "OBJCOPY not found suggested: apt install gcc build-essential" $LINENO
which "${AR}" || error "AR not found suggested: apt install gcc build-essential" $LINENO
which "${MAKE}" || error "MAKE not found suggested: apt install gcc build-essential" $LINENO
# - Basic Innerworking Variables -------------------------------------
CWD="$(pwd)"
# - Basic Cleaner ----------------------------------------------------
if [[ "${1}" == "clean" ]]; then
printf "Cleaning build directory (${CWD})...\n"
printf "Remove ${CWD}/include/*.h\n"
rm -f -I ${CWD}/include/*.h
printf "Remove ${CWD}/dll/*.so\n"
rm -f -I ${CWD}/dll/*.so*
printf "Remove ${CWD}/obj/*.o\n"
rm -f -I ${CWD}/obj/*.o
printf "Remove ${CWD}/lib/*.a\n"
rm -f -I ${CWD}/lib/*.a
printf "Remove ${CWD}/bin\n"
rm -f -I -r -d ${CWD}/bin
printf "Done.\n"
exit 0
fi
# - Basic Installer --------------------------------------------------
if [[ "${1}" == "install" ]]; then
[[ -z "${2}" ]] && error "please specify where to install to" $LINENO
[[ -d "${2}" ]] || error "please create the destination folder first" $LINENO
printf "Installing to directory (${2})...\n"
cp -r -i ${CWD}/bin/Release/* "${2}"
printf "Done.\n"
exit 0
fi
# - Basic GCC Setup --------------------------------------------------
if [[ "${debug}" == "0" ]]; then
attrib="-std=gnu11 -Wall -O2"
root="${CWD}/bin/Release"
else
attrib="-std=gnu11 -Wall -g -O0"
root="${CWD}/bin/Debug"
[[ "${debug_coverage}" == "1" ]] && attrib="${attrib} -coverage"
fi
objdir="${CWD}/obj"
libdir="${CWD}/lib"
resdir="${CWD}/res"
rootdir="${CWD}/root"
dlldir="${CWD}/dll"
srcdir="${CWD}/src"
incdir="${CWD}/include"
dirs="-L${srcdir} -L${libdir} -L${dlldir} -I${srcdir} -I${incdir}"
# - Basic Cache Checking ---------------------------------------------
[[ -d "${CWD}/lua-all" ]] || error "please run 'prereqs.sh download' to get lua-all" $LINENO
[[ -d "${CWD}/luajit-2.0" ]] || error "please run prereqs.sh downliad' to get luajit" $LINENO
# --------------------------------------------------------------------
build_luajit() {
printf "Locally building luajit (${CWD}/luajit-2.0)...\n"
pushd "${CWD}/luajit-2.0/src"
if [[ -f "libluajit.so" ]]; then
printf "libluajit.so already cached\n"
else
$MAKE "-j$(nproc)"
fi
printf "Locally installing luajit (${CWD})...\n"
cp lua.h "${incdir}"
cp luaconf.h "${incdir}"
cp lualib.h "${incdir}"
cp lauxlib.h "${incdir}"
cp luajit.h "${incdir}"
ln libluajit.so libluajit-5.1.so.2
cp libluajit.so "${dlldir}"
cp libluajit-5.1.so.2 "${dlldir}"
popd
printf "Finished locally building & installing luajit.\n"
}
build_lua() {
printf "Locally building lua (${CWD}/lua-all/${1})...\n"
pushd "${CWD}/lua-all/${1}"
if [[ -f "lib${1}.so" ]]; then
printf "lib${1}.so already cached\n"
else
printf "Compiling (${1})...\n"
$GCC "-std=${GCC_VER}" -g0 -O2 -Wall -fPIC $luaverdef -DLUA_USE_POSIX -c *.c
rm lua.o
$OBJCOPY --redefine-sym "main=luac_main" luac.o
printf "Linking (lib${1}.so)...\n"
$GCC "-std=${GCC_VER}" -g0 -O2 -Wall -fPIC -shared -Wl,-E -o "lib${1}.so" *.o -lm -ldl
printf "Archiving (lib${1}.a)...\n"
$AR rcs "lib${1}.a" *.o
fi
printf "Locally installing (${CWD})...\n"
cp lua.h "${incdir}"
cp luaconf.h "${incdir}"
cp lualib.h "${incdir}"
cp lauxlib.h "${incdir}"
cp "lib${1}.so" "${dlldir}"
popd
printf "Finished locally building & installing ${1}.\n"
}
# TODO: very misleading
build_install() {
printf "Migrating ${1} to ${root}...\n"
mv *.o "${objdir}"
if [[ "${1}" == "luajit" ]]; then
cp -r ${dlldir}/* "${root}"
else
cp "${dlldir}/lib${1}.so" "${root}"
fi
mv liblc*.so "${root}"
printf "Finished migrating ${1} to ${root}.\n"
}
build_package() {
if [[ "${1}" == "luajit" ]]; then
luaverdef="-DLUA_JIT_51"
luaverout="-lluajit"
else
luaverout="-l${1}"
fi
printf "Compiling luaw driver package ${1}...\n"
$GCC $attrib $dirs -fPIC -DLC_LD_DLL $luaverdef -c "${srcdir}/ldata.c" "${srcdir}/jitsupport.c"
printf "Linking luaw driver package liblc${1}.so...\n"
$GCC $attrib $dirs -fPIC -shared -Wl,-E -o "liblc${1}.so" ldata.o jitsupport.o "${luaverout}"
printf "Finished building driver package ${1}.\n"
}
build_driver() {
printf "Compiling luaw driver...\n"
$GCC $attrib $dirs "-DDEFAULT_LUA=\"liblc${1}.so\"" -c "${srcdir}/luadriver.c"
printf "Linking luaw driver ${1}...\n"
$GCC $attrib $dirs -o luaw luadriver.o -ldl
printf "Building default lua package ${1}...\n"
build_package "${1}"
if [[ "${debug}" == "0" ]]; then
printf "Stripping driver...\n"
strip --strip-all luaw
fi
printf "Finished building driver ${1}.\n"
}
# --------------------------------------------------------------------
if [[ "$1" == "driver" ]]; then
printf "Cleaning workspace (${CWD})...\n"
# Resets bin
[[ -d "${root}" ]] && rm -r --one-file-system -d "${root}"
# Resets dll
rm -r ${dlldir}/*.so
# Create build structure
mkdir -p "${resdir}"
mkdir -p "${dlldir}"
mkdir -p "${root}/lang"
# Build dependencies
if [[ "${2}" == "luajit" ]]; then
build_luajit
if [[ ! -d "${root}/jit" ]]; then
mkdir -p "${root}/jit"
cp -r $CWD/luajit-2.0/src/jit/* "${root}/jit"
fi
else
[[ -d "${CWD}/lua-all/${2}" ]] || error "supplied argument ${2} is not a valid lua version in lua-all" $LINENO
build_lua "${2}"
fi
# Build driver
build_driver "${2}"
chmod +x luaw
# Build install
mv luaw "${root}"
cp -r ${resdir} "${root}/res"
cp -r ${rootdir}/* "${root}"
build_install "${2}"
printf "Finished.\n"
exit 0
fi
# --------------------------------------------------------------------
if [[ "$1" == "package" ]]; then
# Force driver to be built
[[ -f "${root}/luaw" ]] || error "please run ${0} driver lua-5.x.x first" $LINENO
printf "Cleaning workspace...\n"
# Resets dll
rm -r ${dlldir}/*.so
if [[ "${2}" == "luajit" ]]; then
build_luajit
if [[ ! -d "${root}/jit" ]]; then
mkdir -p "${root}/jit"
cp -r $CWD/luajit-2.0/src/jit/* "${root}/jit"
fi
else
[[ -d "${CWD}/lua-all/${2}" ]] || error "supplied argument ${2} is not a valid lua version in lua-all" $LINENO
build_lua "${2}"
fi
# Build package and install
build_package "${2}"
build_install "${2}"
printf "Finished.\n"
exit 0
fi
error "This shouldn't be reached!\n" $LINENO