-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminimal_erlang.sh
executable file
·221 lines (196 loc) · 6.22 KB
/
minimal_erlang.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
#!/bin/bash
#
# All variables used in the script should be prefixed with "SE_"
# Some of these are changed while parsing getopts.
SE_XCOMP_FILE=unset
SE_DO_BUILD=unset
SE_ERLANG_SOURCE_FILE=unset
SE_SMP_ENABLED=unset
SE_RELEASE_DIR=stripped_release
# Internal functions
usage()
{
cat<<EOF
Usage: [options]
Available options:
-a <xcomp file> Analyze xcomp file. If set no other options are needed.
-b <xcomp file> Build Erlang with a xcomp file (not supported yet)
-e <Erlang src> Set the Erlang source tarball used for building
-d Optional, keeps the temp build directory.
Useful for debugging the script.
-s Enable Erlang SMP. If not set beam.smp will be removed
Even if it was built. If enabled the non-smp beam will
be removed.
EOF
exit
}
analyse_xcomp()
{
if [ `grep "CFLAGS=" $SE_XCOMP_FILE | grep -c "\-DSMALL_MEMORY"` == 1 ]
then
echo "GCC optimisation flag -DSMALL_MEMORY enabled"
else
echo "GCC optimisation flag -DSMALL_MEMORY disabled"
echo " Recommended for limited memory systems."
echo " You can enable it by editing the 'CFLAGS=' entry in"
echo " ${SE_XCOMP_FILE}"
fi
SE_O_LEVEL=`grep "CFLAGS=" $SE_XCOMP_FILE | grep -o "\ -O. " `
if [ `grep "CFLAGS=" $SE_XCOMP_FILE | grep -c "\-O. "` != 0 ]
then
echo "GCC optimisation flag${SE_O_LEVEL} enabled"
if [ `echo ${SE_O_LEVEL} | grep -c "s"` == 0 ]
then
echo " -0s is recommended when optimising for limited disk size"
fi
else
echo "GCC optimisation flag -Ox disabled"
echo " -Os is recommended to optimise for small disk size."
echo " You can enable it by editing the 'CFLAGS=' entry in"
echo " ${SE_XCOMP_FILE}"
fi
}
# End of internal functions
# Begin script execution
# 1. Parse options
if ( ! getopts ":a:" opt); then
usage
exit 1
fi
while getopts ":a:b:e:sd" Option
do
case $Option in
a )
SE_XCOMP_FILE=${OPTARG}
analyse_xcomp
exit 0
;;
b )
SE_XCOMP_FILE=${OPTARG}
SE_DO_BUILD=yes
;;
e )
SE_ERLANG_SOURCE_FILE=${OPTARG}
SE_DO_BUILD=yes
;;
s )
SE_SMP_ENABLED=true
;;
d )
SE_DEBUG_ENABLED=true
;;
* ) usage
exit 1
;;
esac
done
# 2. Create a directory for building and configuration of Erlang release and unpack source.
if [ "$SE_DO_BUILD" == yes ]
then
if [ "$SE_ERLANG_SOURCE_FILE" == unset ]
then
echo "A build requires an Erlang source tarball, set it with -e <file>"
echo "Download one with 'wget http://www.erlang.org/download/otp_src_R14B04.tar.gz'"
exit 1
fi
SE_TIMESTAMP=`date +%F_%H%M%S`
SE_BUILD_DIR=stripped_erlang_$SE_TIMESTAMP
echo "Making build dir ${SE_BUILD_DIR}"
mkdir $SE_BUILD_DIR
echo "Unpacking source and moving into dir.."
tar xfz $SE_ERLANG_SOURCE_FILE -C $SE_BUILD_DIR
fi
# 3. Enter directory and build the source.
SE_OTP_SOURCE_DIR_NAME=`tar -tf ${SE_ERLANG_SOURCE_FILE} | grep -o '^[^/]\+' | sort -u`
cd $SE_BUILD_DIR
mkdir $SE_RELEASE_DIR
SE_BUILD_DIR_ABS=$PWD
SE_OTP_SOURCE_DIR_ABS=$SE_BUILD_DIR_ABS/$SE_OTP_SOURCE_DIR_NAME
cd $SE_OTP_SOURCE_DIR_ABS
export ERL_TOP=$SE_OTP_SOURCE_DIR_ABS
./configure
make
make RELEASE_ROOT=$SE_BUILD_DIR_ABS/$SE_RELEASE_DIR release
# 4. Strip beams
erl -eval "beam_lib:strip_release('$SE_BUILD_DIR_ABS/$SE_RELEASE_DIR')" -s init stop > /dev/null
cd $SE_BUILD_DIR_ABS/$SE_RELEASE_DIR
# 5. Strip binaries (using correct tool chain)
#TODO: use toolchain specified in xcomp.conf or from CLI option
strip erts-*/bin/*
# 6. Remove crap (src, include, doc etc.)
echo "Removing unnecessary files and directories"
rm -rf usr/
rm -rf misc/
for SE_DIR in erts* lib/*; do
rm -rf ${SE_DIR}/src
rm -rf ${SE_DIR}/include
rm -rf ${SE_DIR}/doc
rm -rf ${SE_DIR}/man
rm -rf ${SE_DIR}/examples
rm -rf ${SE_DIR}/emacs
rm -rf ${SE_DIR}/c_src
done
rm -rf erts-*/lib/
rm erts-*/bin/dialyzer
rm erts-*/bin/erlc
rm erts-*/bin/typer
rm erts-*/bin/ct_run
if [ "$SE_SMP_ENABLED" == yes ]
then
echo "SMP enabled, removing non-SMP BEAM binary"
rm erts-*/bin/beam
else
echo "SMP disabled, removing SMP BEAM binary"
rm erts-*/bin/beam.smp
fi
# 7. Compress .ez archives (without priv!)
cd lib
SE_LIBS=$(ls)
for SE_APP in $SE_LIBS; do
if [ "${SE_APP##*.}" == "ez" ]; then
echo "Skipping $SE_APP, already compressed"
else
echo "Compressing $SE_APP"
# Skip priv directories, they need to exist on disk
zip -0 -q -r ${SE_APP}.zip $SE_APP -x $SE_APP/priv/\*
mv ${SE_APP}.zip ${SE_APP}.ez
if [ ! -d $SE_APP/priv ]; then
rm -f -r $SE_APP
else
mv ${SE_APP}/priv ${SE_APP}_priv
rm -f -r $SE_APP
mkdir -p $SE_APP
mv ${SE_APP}_priv ${SE_APP}/priv
fi
fi
done
cd ..
# 8. A script file; 'Install', was generated by the release making. Editing out some things from it:
# - delete everything that is copied from erts-*/bin
# - delete run_test link
sed -i 's/cp -p $ERL_ROOT\/erts-.*\/bin\/erl .//' Install
sed -i 's/cp -p $ERL_ROOT\/erts-.*\/bin\/erlc .//' Install
sed -i 's/cp -p $ERL_ROOT\/erts-.*\/bin\/dialyzer .//' Install
sed -i 's/cp -p $ERL_ROOT\/erts-.*\/bin\/typer .//' Install
sed -i 's/cp -p $ERL_ROOT\/erts-.*\/bin\/ct_run .//' Install
sed -i 's/cp -p $ERL_ROOT\/erts-.*\/bin\/escript .//' Install
sed -i 's/ln -s ct_run run_test//' Install
# 9. Create installation archive
cd ../..
SE_TARBALL_PREFIX=minimal_erlang_$SE_OTP_SOURCE_DIR_NAME_$SE_TIMESTAMP
mkdir $SE_TARBALL_PREFIX
cp -r $SE_BUILD_DIR/$SE_RELEASE_DIR $SE_TARBALL_PREFIX/
echo "
Installation:
1. Unpack archive.
2. ./Install -minimal
" >> $SE_TARBALL_PREFIX/README
tar czf $SE_TARBALL_PREFIX.tar.gz $SE_TARBALL_PREFIX
if [ "$SE_DEBUG_ENABLED" != yes ]
then
rm -rf $SE_BUILD_DIR
fi
echo "Done. Check README in $SE_TARBALL_PREFIX folder/tarball."
# TODO (on target system):
# 1. Unpack archive at preffered location (this will be $ERL_ROOT)
# 2. Run $ERL_ROOT/Install -minimal