-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_socat_for_android.sh
More file actions
executable file
·263 lines (214 loc) · 6.4 KB
/
build_socat_for_android.sh
File metadata and controls
executable file
·263 lines (214 loc) · 6.4 KB
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
#!/bin/bash
# build socat for android
# g0, 2018
if [ "$1" = "-h" -o "$1" = "-help" ]
then
echo -e "\t $0 arch api_version rmbuild verbose stip"
echo -e "\t example: $0 arm64 24 rmbuild"
echo -e "\t with no arguments, it will attempt to build socat for the android seen over adb"
echo ""
exit 0
fi
COLORS=1
DEFAULT_ARCH='arm'
DEFAULT_SDKV='23'
SDKV=$(adb shell getprop ro.build.version.sdk 2>/dev/null)
[ -z "$SDKV" ] && SDKV=$DEFAULT_SDKV
ARCH=$(adb shell getprop ro.product.cpu.abi 2>/dev/null)
[ -z "$ARCH" ] && ARCH=$DEFAULT_ARCH
[[ "$ARCH" = *"-"* ]] && ARCH=$(echo "$ARCH" |awk -F '-' '{print $1}')
_not_in_archs(){
archs=(arm arm64 mips mips64 x86 x86_64)
for arch in ${archs[*]}
do
[ "$arch" = "$1" ] && return 1
done
echo "$1 is not a supported arch."
return 0
}
if _not_in_archs $ARCH; then
ARCH=$DEFAULT_ARCH
fi
[ "$COLORS" -eq "1" ] && ESC8='\033['
[ "$COLORS" -eq "1" ] && GREEN=${ESC8}"01;32m"
[ "$COLORS" -eq "1" ] && RED=${ESC8}"31;01m"
[ "$COLORS" -eq "1" ] && RESET=${ESC8}"00m"
STATUS=0
_die(){
printf "${RED}%s${RESET}\n" "${1}"
STATUS=$((STATUS+1))
exit $STATUS
}
_say(){
printf "${GREEN}%s${RESET}\n" "${1}"
}
# ARCH='arm64'
# SDKV='26'
# OP3='rmbuild'
# OP4='verbose'
# OP5='strip'
[ $# -ge 1 ] && ARCH=$1
[ $# -ge 2 ] && SDKV=$2
[ $# -ge 3 ] && OP3=$3
[ $# -ge 4 ] && OP4=$4
[ $# -ge 5 ] && OP5=$5
_say "Building socat for Architecture:$ARCH, Android SDK API version:$SDKV"
ROOT=`pwd`
BUILD="${ROOT}/builds/${ARCH}_${SDKV}"
mkdir -p $BUILD
HOST='arm-linux-androideabi'
TCDIR="${BUILD}/toolchain_${ARCH}${SDKV}"
SYSROOT="${BUILD}/toolchain_${ARCH}${SDKV}/sysroot"
[[ "$ARCH" = 'arm' ]] && HOST='arm-linux-androideabi'
[[ "$ARCH" = 'arm64' ]] && HOST='aarch64-linux-android'
[[ "$ARCH" = 'x86' ]] && HOST='i686-linux-android'
[[ "$ARCH" = 'x86_64' ]] && HOST='x86_64-linux-android'
[[ "$ARCH" = 'mips' ]] && HOST='mipsel-linux-android'
[[ "$ARCH" = 'mips64' ]] && HOST='mips64el-linux-android'
CC="$TCDIR/bin/$HOST-clang --sysroot=$SYSROOT"
LD="$TCDIR/bin/$HOST-ld"
AR="$TCDIR/bin/$HOST-ar"
RANLIB="$TCDIR/bin/$HOST-ranlib"
STRIP="$TCDIR/bin/$HOST-strip"
# CLIBS="-lm -lefence"
# LIBS="-static"
CFLAGS="-fPIE -fPIC"
# CFLAGS="-fPIE -fPIC -static -fno-debug-info-for-profiling -fno-debug-macro"
[[ "$OP4" = 'verbose' ]] && CFLAGS="$CFLAGS -v"
LDFLAGS="-fPIE -pie"
# LDFLAGS="-fPIE -pie -static"
[[ "$OP4" = 'verbose' ]] && LDFLAGS="$LDFLAGS -v"
_say "HOST: $HOST"
_say "CC: $CC"
_say "CFLAGS: $CFLAGS"
_say "LD: $LD"
_say "LDFLAGS: $LDFLAGS"
_say "AR: $AR"
_say "RANLIB: $RANLIB"
# _say "CLIBS: $CLIBS"
# _say "LIBS: $LIBS"
export RANLIB="$RANLIB"
export AR="$AR"
# export CLIBS="$CLIBS"
# export LIBS="$LIBS"
rm -rf $TCDIR
V=''
[[ "$OP4" = 'verbose' ]] && V="-v"
make_standalone_toolchain.py $V --arch $ARCH --api $SDKV --install-dir $TCDIR --force
# Create configure script
cd ${ROOT}
autoconf || _die "autoconf failed"
# config.h and Makefile
cd ${BUILD}
${ROOT}/configure \
--disable-openssl \
--host=$HOST \
CC="$CC" \
LD="$LD" \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS" \
RANLIB="$RANLIB" \
AR="$AR" \
|| _die "configure failed"
# Replace misconfigured values in config.h and enable PTY functions
mv config.h config.old
# | sed 's/\/\* #undef WITH_OPENSSL \*\//#define WITH_OPENSSL 1/' \
cat config.old \
| sed 's/CRDLY_SHIFT.*/CRDLY_SHIFT 9/' \
| sed 's/TABDLY_SHIFT.*/TABDLY_SHIFT 11/' \
| sed 's/CSIZE_SHIFT.*/CSIZE_SHIFT 4/' \
| sed 's/\/\* #undef HAVE_OPENPTY \*\//#define HAVE_OPENPTY 1/' \
| sed 's/\/\* #undef HAVE_GRANTPT \*\//#define HAVE_GRANTPT 1/' \
| sed 's/#define HAVE_RESOLV_H 1/\/\* #undef HAVE_RESOLV_H \*\//' \
> config.h
# Enable openpty() in Makefile
mv Makefile Makefile.old
cat Makefile.old | sed 's/error.c/error.c openpty.c/' > Makefile
# Provide openpty.c
cat >openpty.c <<EOF
/* Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#define _PATH_DEVPTMX "/dev/ptmx"
int openpty (int *amaster, int *aslave, char *name, struct termios *termp,
struct winsize *winp)
{
char buf[PATH_MAX];
int master, slave;
master = open(_PATH_DEVPTMX, O_RDWR);
if (master == -1)
return -1;
if (grantpt(master))
goto fail;
if (unlockpt(master))
goto fail;
if (ptsname_r(master, buf, sizeof buf))
goto fail;
slave = open(buf, O_RDWR | O_NOCTTY);
if (slave == -1)
goto fail;
/* XXX Should we ignore errors here? */
if (termp)
tcsetattr(slave, TCSAFLUSH, termp);
if (winp)
ioctl(slave, TIOCSWINSZ, winp);
*amaster = master;
*aslave = slave;
if (name != NULL)
strcpy(name, buf);
return 0;
fail:
close(master);
return -1;
}
EOF
_handle_bin(){
if [ -e "${BUILD}/${1}" ]
then
mkdir -p ${ROOT}/binaries/${ARCH}/${SDKV}
cp ${BUILD}/${1} ${ROOT}/binaries/${ARCH}/${SDKV}/${1}
[[ "$OP5" = 'strip' ]] && $STRIP ${ROOT}/binaries/${ARCH}/${SDKV}/${1}
_say "Build finished, ${1} has been generated successfuly in ${ROOT}/binaries/$ARCH/$SDKV/${1}"
else
STATUS=$((STATUS+1))
printf "${RED}%s${RESET}\n" "${1} was not made"
fi
}
MAKE='make'
J=$(sysctl -n hw.ncpu)
[ "$J" -ge 2 -a "$J" -le 16 ] && MAKE="make -j${J}"
_say "$MAKE"
$MAKE socat || _die "make failed"
_say "Build finished, socat has been generated successfuly in $BUILD/socat"
$MAKE filan
$MAKE procan
_handle_bin "socat"
_handle_bin "filan"
_handle_bin "procan"
if [ "$OP3" = 'rmbuild' -a -d "$BUILD" ]
then
rm -r ${BUILD}
[ ! -d "$BUILD" ] && _say "deleted $BUILD"
fi
exit $STATUS