summaryrefslogtreecommitdiff
path: root/hints/linux-android.sh
blob: cd22876662826a44a4ea00b164a14c19aea72715 (plain)
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
# set -x

# Install the perl and its libraries anywhere:
case "$userelocatableinc" in
'') userelocatableinc='define' ;;
esac

# The Android linker has some unusual behavior: No matter what
# path is passed in to dlopen(), it'll only use the path's
# basename when trying to find a cached library.
# Unfortunately, this is quite problematic for us, since for example,
# Hash::Util and List::Util both end up creating a Util.so --
# So if you load List::Util and then Hash::Util, the dlopen() for
# the latter will return the handle for the former.
# See the implementation for details:
# https://code.google.com/p/android-source-browsing/source/browse/linker/linker.c?repo=platform--bionic&r=9ec0f03a0d0b17bbb94ac0b9fef6add28a133c3a#1231
# What d_libname_unique does is inform MakeMaker that, rather than
# creating Hash/Util/Util.so, it needs to make Hash/Util/Perl_Hash_Util.so
d_libname_unique='define'

# On Android the shell is /system/bin/sh:
targetsh='/system/bin/sh'
case "$usecrosscompile" in
define) ;;
   # If we aren't cross-compiling, then sh should also point
   # to /system/bin/sh.
*) sh=$targetsh ;;
esac

# Make sure that we look for libm
libswanted="$libswanted m log"

# Older Androids lack locale support and may need the following undefs
# uncommenting. This isn't necessary from at least Android 8.1 (Oreo)
# https://github.com/android/platform_bionic/blob/master/libc/CAVEATS
#d_locconv='undef'
#d_setlocale='undef'
#d_setlocale_r='undef'
#d_lc_monetary_2008='undef'
#i_locale='undef'
#d_newlocale='undef'

# https://code.google.com/p/android-source-browsing/source/browse/libc/netbsd/net/getservent_r.c?repo=platform--bionic&r=ca6fe7bebe3cc6ed7e2db5a3ede2de0fcddf411d#95
d_getservent_r='undef'

# Bionic defines several stubs that warn (in older releases) and return NULL
# https://gitorious.org/0xdroid/bionic/blobs/70b2ef0ec89a9c9d4c2d4bcab728a0e72bafb18e/libc/bionic/stubs.c
# https://android.googlesource.com/platform/bionic/+/master/libc/bionic/stubs.cpp

# These tests originally looked for 'FIX' or 'Android' warnings, as they
# indicated stubs to avoid. At some point, Android stopped emitting
# those warnings; the tests were adapted to check function return values
# and hopefully now detect stubs on both older and newer Androids.

# These are all stubs as well, but the core doesn't use them:
# getusershell setusershell endusershell

# This script UU/archname.cbu will get 'called-back' by Configure.
$cat > UU/archname.cbu <<'EOCBU'
# original egrep pattern to detect a stub warning on Android.
# Right now we're checking for:
# Android 2.x: FIX ME! implement FUNC
# Android 4.x: FUNC is not implemented on Android
# Android 8.x: <no warnings; tests now printf a compatible warning>
android_stub='FIX|Android'

$cat > try.c << 'EOM'
#include <netdb.h>
#include <stdio.h>
int main() {
  struct netent* test = getnetbyname("loopback");
  if (test == NULL) {
    printf("getnetbyname is still a stub function on Android");
  }
  return(0);
}
EOM
$cc $ccflags try.c -o try
android_warn=`$run ./try 2>&1 | $egrep "$android_stub"`
if test "X$android_warn" != X; then
   d_getnbyname="$undef"
fi

$cat > try.c << 'EOM'
#include <netdb.h>
#include <stdio.h>
int main() {
  struct netent* test = getnetbyaddr(127, AF_INET);
  if (test == NULL) {
    printf("getnetbyaddr is still a stub function on Android");
  }
  return(0);
}
EOM
$cc $ccflags try.c -o try
android_warn=`$run ./try 2>&1 | $egrep "$android_stub"`
if test "X$android_warn" != X; then
   d_getnbyaddr="$undef"
fi

$cat > try.c << 'EOM'
#include <stdio.h>
#include <mntent.h>
#include <unistd.h>
int main() { (void) getmntent(stdout); return(0); }
EOM
$cc $ccflags try.c -o try
android_warn=`$run ./try 2>&1 | $egrep "$android_stub"`
if test "X$android_warn" != X; then
   d_getmntent="$undef"
fi

$cat > try.c << 'EOM'
#include <netdb.h>
#include <stdio.h>
int main() {
  struct protoent* test = getprotobyname("tcp");
  if (test == NULL) {
    printf("getprotobyname is still a stub function on Android");
  }
  return(0);
}
EOM
$cc $ccflags try.c -o try
android_warn=`$run ./try 2>&1 | $egrep "$android_stub"`
if test "X$android_warn" != X; then
   d_getpbyname="$undef"
fi

$cat > try.c << 'EOM'
#include <netdb.h>
#include <stdio.h>
int main() {
  struct protoent* test = getprotobynumber(1);
  if (test == NULL) {
    printf("getprotobynumber is still a stub function on Android");
  }
  return(0);
}
EOM
$cc $ccflags try.c -o try
android_warn=`$run ./try 2>&1 | $egrep "$android_stub"`
if test "X$android_warn" != X; then
   d_getpbynumber="$undef"
fi

$cat > try.c << 'EOM'
#include <sys/types.h>
#include <pwd.h>
int main() { endpwent(); return(0); }
EOM
$cc $ccflags try.c -o try
android_warn=`$run ./try 2>&1 | $egrep "$android_stub"`
if test "X$android_warn" != X; then
   d_endpwent="$undef"
fi

$cat > try.c << 'EOM'
#include <unistd.h>
#include <stdio.h>
int main() {
  char *tty = ttyname(STDIN_FILENO);
  if (tty == NULL) {
    printf("ttyname is still a stub function on Android");
  }
  return(0);
}
EOM
$cc $ccflags try.c -o try
android_warn=`$run ./try 2>&1 | $egrep "$android_stub"`
if test "X$android_warn" != X; then
   d_ttyname="$undef"
fi

EOCBU

if $test "X$targetrun" = "Xadb"; then

$rm $run $to $from $targetmkdir

case "$src" in
    /*) run=$src/Cross/run
            targetmkdir=$src/Cross/mkdir
            to=$src/Cross/to
            from=$src/Cross/from
            ;;
    *)  pwd=`test -f ../Configure && cd ..; pwd`
            run=$pwd/Cross/run
            targetmkdir=$pwd/Cross/mkdir
            to=$pwd/Cross/to
            from=$pwd/Cross/from
               ;;
esac

targetrun=adb-shell
targetto=adb-push
targetfrom=adb-pull
run=$run-$targetrun
to=$to-$targetto
from=$from-$targetfrom

$cat >$run <<EOF
#!/bin/sh
doexit="echo \\\$? >$targetdir/output.status"
env=''
case "\$1" in
-cwd)
  shift
  cwd=\$1
  shift
  ;;
esac
case "\$1" in
-env)
  shift
  env=\$1
  shift
  ;;
esac
case "\$cwd" in
'') cwd=$targetdir ;;
esac
case "\$env" in
'') env="echo "
esac
exe=\$1
shift
args=\$@
$to \$exe > /dev/null 2>&1

# send copy results to /dev/null as otherwise it outputs speed stats which gets in our way.
# sometimes there is no $?, I dunno why? we then get Cross/run-adb-shell: line 39: exit: XX: numeric argument required
adb -s $targethost shell "sh -c '(cd \$cwd && \$env ; \$exe \$args > $targetdir/output.stdout 2>$targetdir/output.stderr) ; \$doexit '" > /dev/null

rm output.stdout output.stderr output.status 2>/dev/null

$from output.stdout
$from output.stderr
$from output.status

# We get back Ok\r\n on android for some reason, grrr:
$cat output.stdout | $tr -d '\r'
if test -s output.stderr; then
    $cat output.stderr | $tr -d '\r' >&2
fi

result_status=\`$cat output.status | $tr -d '\r'\`

rm output.stdout output.stderr output.status

# Also, adb doesn't exit with the commands exit code, like ssh does, double-grr
exit \$result_status

EOF
$chmod a+rx $run

$cat >$targetmkdir <<EOF
#!/bin/sh
adb -s $targethost shell "mkdir -p \$@"
EOF
$chmod a+rx $targetmkdir

$cat >$to <<EOF
#!/bin/sh
for f in \$@
do
  case "\$f" in
  /*)
    adb -s $targethost push \$f \$f            || exit 1
    ;;
  *)
    (adb -s $targethost push \$f $targetdir/\$f < /dev/null 2>&1) || exit 1
    ;;
  esac
done
exit 0
EOF
$chmod a+rx $to

$cat >$from <<EOF
#!/bin/sh
for f in \$@
do
  $rm -f \$f
  (adb -s $targethost pull $targetdir/\$f . > /dev/null 2>&1) || exit 1
done
exit 0
EOF
$chmod a+rx $from

fi # Cross-compiling with adb

case "$usecrosscompile" in
define)
# The tests for this in Configure doesn't play nicely with
# cross-compiling
d_procselfexe="define"
if $test "X$hostosname" = "Xdarwin"; then
  firstmakefile=GNUmakefile;
fi

# When cross-compiling, full_csh and d_csh will get the
# host's values, which is all sorts of wrong.  So unless
# full_csh has been set on the command line, set d_csh to
# undef.
case "$full_csh" in
'') d_csh="$undef"
;;
esac

;;
*)
ldflags="$ldflags -L/system/lib"
;;
esac

osvers="`$run getprop ro.build.version.release`"

# We want osname to be linux-android during Configure,
# but plain 'android' afterwards.
case "$src" in
    /*) pwd="$src";;
    *)  pwd=`test -f ../Configure && cd ..; pwd`
        ;;
esac

$cat <<'EOO' >> $pwd/config.arch

osname='android'
eval "libpth='$libpth /system/lib /vendor/lib'"

if $test "X$procselfexe" = X; then
    case "$d_procselfexe" in
        define) procselfexe='"/proc/self/exe"';;
    esac
fi
EOO

# Android is a linux variant, so run those hints.
. ./hints/linux.sh