-
Notifications
You must be signed in to change notification settings - Fork 8
/
func
221 lines (201 loc) · 5.67 KB
/
func
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
#!/bin/sh
# functions library
# this is sourced by pkg/<app>_static/<app>_static.petbuild
[ -f ./build.conf ] && build_conf='./build.conf'
[ -f ../build.conf ] && build_conf='../build.conf'
[ -f ../../build.conf ] && build_conf='../../build.conf' #*
[ -f ../../../build.conf ] && build_conf='../../../build.conf'
[ -f ../../../../build.conf ] && build_conf='../../../../build.conf'
[ ! -f "$build_conf" ] && exit 1
. ${build_conf}
if [ -d "$MWD" -a "$DLD_ONLY" = "no" ] ; then
echo -e "\n* main dir is $MWD\n" #MWD is set in build.sh
fi
function set_arch() {
ARCH=`uname -m`
if [ "$OVERRIDE_ARCH" ] ; then
ARCH=${OVERRIDE_ARCH}
fi
case $ARCH in
*64) LIBDIR=lib64 ;;
*) LIBDIR=lib ;;
esac
if [ "$TARGET_TRIPLET" != "" ] ; then
XCOMPILER=${TARGET_TRIPLET} #this is exported by build.sh
fi
echo "$XCOMPILER"
}
set_arch
#if [ -d "${CC_INSTALL_DIR}" ] ; then
# export PATH=${CC_INSTALL_DIR}/bin:$PATH # _configure _make _install
#fi
if [ -d "$XPATH" ] ; then #set in build.sh
export PATH=${XPATH}/bin:$PATH # _configure _make _install
export LD_LIBRARY_PATH=${XPATH}/lib:${LD_LIBRARY_PATH}
export PKG_CONFIG_PATH=${XPATH}/lib/pkgconfig
export C_INCLUDE_PATH=${XPATH}/include
export CPLUS_INCLUDE_PATH=${XPATH}/include
fi
#---------------------------------------------------------
function exit_error() {
echo -n > $MWD/.fatal
exit $@
}
function retrieve() {
local SOURCE=${1}
local FILE=$SOURCE
case $SOURCE in http*|ftp*)
local URL=${SOURCE%/*}
local FILE=${SOURCE##*/}
esac
#--
ZZDL=1
if [ "$2" != "" ] ; then
[ -f ${MWD}/0sources/${2} ] && ZZDL=0
zzoutfile="${MWD}/0sources/${2}"
else
[ -f ${MWD}/0sources/${FILE} ] && ZZDL=0
zzoutfile="${MWD}/0sources/${FILE}"
fi
if [ -f "$zzoutfile" ] && [ ! -s "$zzoutfile" ] ; then
ZZDL=1 # empty file , must redownload
rm -f "$zzoutfile"
fi
#--
if [ $ZZDL -eq 0 ] ; then
[ "$DLD_ONLY" = "yes" ] && echo "Already downloaded ${FILE}"
else
echo "Downloading ${URL}/${FILE}"
wget -O ${zzoutfile} -c --no-check-certificate "${URL}/${FILE}"
if [ $? -ne 0 ] ; then
rm -f ${FILE}
exit_error 1
fi
fi
}
function extract() {
tfile="$@"
[ -f "${MWD}/0sources/$tfile" ] || { echo "No such file: $tfile"; exit_error 1; }
tar -axf ${MWD}/0sources/${tfile} || { rm -f ${tfile}; exit_error 1; }
}
function abort_if_file_not_found() {
[ "$1" ] || exit_error 1
for i in "$@" ; do
if [ ! -f "$i" ] ; then
echo "$i: file does no exist!"
exit_error 1
fi
done
}
#---------------------------------------------------------
function hl_echo() {
echo
echo '------------------------------'
echo "$@"
echo '------------------------------'
echo
}
function _cflags() {
echo "-g -Os" #-fmerge-all-constants -fno-unroll-loops -fomit-frame-pointer -ffunction-sections -fdata-sections"
}
function make_sh() {
# _make() creates make.sh in the current dir (calls make_sh())
# You can manually run make.sh and it will source this script (func)
# and will call _make() again...
# ... but make.sh must not overwrite itself
# that's why it exports MAKE_SCRIPT=yes
if [ "$MAKE_SCRIPT" != "yes" ] ; then
echo "#!/bin/sh" > make.sh
echo "export MAKE_SCRIPT=yes" >> make.sh
[ -d "$XPATH" ] && echo "export XPATH=$XPATH" >> make.sh
echo ". $MWD/func" >> make.sh
echo "_make $@" >> make.sh
chmod +x make.sh
fi
}
#this is inside the source dir
function _configure() {
unset host
if [ -f configure ] ; then
#this is to avoid possible problems with the triplet
sed -i -e 's|^build_os=.*|build_os=linux|' \
-e 's|^host_os=.*|host_os=linux|' \
-e 's|^target_os=.*|target_os=linux|' \
-e 's|linux_os=no|linux_os=yes|' \
configure
fi
if [ -d "$XPATH" ] ; then
echo "PATH: $PATH"
host="--host=${XCOMPILER}"
fi
hl_echo "./configure ${opts} ${host}"
./configure ${opts} ${host} #opts is set in the calling script
[ $? -ne 0 ] && exit_error 1
}
#this is inside the source dir
function _config_sh() {
unset host
if [ -f config.sh ] ; then
#this is to avoid possible problems with the triplet
sed -i -e 's|^build_os=.*|build_os=linux|' \
-e 's|^host_os=.*|host_os=linux|' \
-e 's|^target_os=.*|target_os=linux|' \
-e 's|linux_os=no|linux_os=yes|' \
config.sh
fi
if [ -d "$XPATH" ] ; then
echo "PATH: $PATH"
host="--host=${XCOMPILER}"
fi
hl_echo "./config.sh ${opts} ${host}"
./config.sh ${opts} ${host} #opts is set in the calling script
[ $? -ne 0 ] && exit_error 1
}
#this is inside the source dir
function _make() {
cf=$(_cflags)
if [ -f Makefile ] ; then
makefiles=$(find . -type f -name Makefile | tr '\n' ' ')
sed -i "s|\(^CFLAGS =.*\)|\1 $cf|g" ${makefiles}
hl_echo "$(grep '^CFLAGS = ' $makefiles)"
else
export CFLAGS="$cf"
fi
[ "$1" = "make" ] && shift
if [ "$1" = "" ] ; then #no args
if [ -d "$XPATH" ] ; then #musl
echo "PATH: $PATH"
mkstr="${MKFLG} CC=${XCOMPILER}-gcc LD=${XCOMPILER}-ld AR=${XCOMPILER}-ar RANLIB=${XCOMPILER}-ranlib NM=${XCOMPILER}-nm STRIP=${XCOMPILER}-strip LDFLAGS=-static"
hl_echo "make $mkstr"
make_sh "make $mkstr"
make $mkstr
else # system gcc
mkstr "${MKFLG} LDFLAGS=-static CFLAGS=-static"
hl_echo "make $mkstr"
make_sh "make $mkstr"
eval make $mkstr
fi
else
hl_echo "make $@"
make_sh "make $@"
eval make $@
fi
return $?
}
#this is inside the source dir
function _install() {
local file="$1" dest="$2"
if [ -d "$XPATH" ] ; then
${XCOMPILER}-strip --strip-all "$file"
else
strip --strip-all "$file"
fi
chmod +x "$file"
cp "$file" "$dest"
file $file | grep -v 'pie executable' | grep -E 'dynamically|shared' && {
echo "$file is not static..!"
# bullseye says busybox is an 'ELF 32-bit LSB pie executable', focal says it's an 'ELF 32-bit LSB shared object'
[ -z "$GITHUB_ACTIONS" ] && exit_error 1
}
}
### END ###