-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_wrapper.sh
executable file
·79 lines (68 loc) · 2.11 KB
/
build_wrapper.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
#!/bin/bash
# Script for embedding a lua script with it's lua dependencies, into a launcher
# for the script, in order to allow to build a fully autonomous, statically
# "compiled" script. Dependencies don't need to be ordered, they are loaded on
# the fly.
#
# Objcopy is used to add sections containing the lua scripts. The sections names
# are the corresponding module name, prefixed by lw_.
#set -x
set -e
objcopy=${TARGET_OBJCOPY:-objcopy}
function usage {
echo "usage : $(basename ${command}) -h"
echo -e "\t\tDisplays this help."
echo " $(basename ${command}) [-o OUTPUT] LAUNCHER [LUA_DEP1 \
[LUA_DEP2 ... ]] SCRIPT"
echo -e "\t\tCreates an autonomous executable from a lua script and it's \
dependencies."
echo -e "\t\tOUTPUT: resulting executable's path, default is to modify \
LAUNCHER in place."
echo -e "\t\tLAUNCHER: path to the precompiled luawrapper."
echo -e "\t\tLUA_DEPX and SCRIPT are paths to the lua scripts to embed, \
SCRIPT being the main script which will be directly executed. To choose the \
module name associated to a script (the parameter to _require_) one can use the\
syntax name:path. If no name is given, then the script's basename is used as \
the module name. The module name can't contain '/' or ':'."
exit $1
}
if [ "$1" = "-h" ]; then
usage 0
fi
if [ "$1" = "-o" ]; then
shift
output=$1
shift
else
output=$1
fi
echo "output to file ${output}, using ${objcopy}"
command=$0
launcher=$1
first_lua_file=$2
if [ ! -f "${launcher}" ]; then
echo "launcher '${launcher}' not found"
usage 1
fi
if [ ${launcher} != ${output} ]; then
cp -f ${launcher} ${output}
fi
shift
for script in "$@" ; do
# try to match the pattern name:path, if no match, use basename as the name
pattern='^([^:/]+):(.*)$'
if [[ ${script} =~ ${pattern} ]]; then
name=lw_${BASH_REMATCH[1]}
script=${BASH_REMATCH[2]}
else
pattern='^.*/([^/]+)\.lua$'
[[ ${script} =~ ${pattern} ]]
name="lw_${BASH_REMATCH[1]}"
fi
if [ ! -f "${script}" ]; then
echo "script '${script}' not found"
usage 1
fi
echo "add section ${name} for ${script}"
${objcopy} --add-section ${name}=${script} ${output} ${output}
done