aboutsummaryrefslogtreecommitdiff
path: root/build/tools/build-ndk-sysroot.sh
blob: 13acda0a8fa8bc122b8a5516b81d8dcb1fde0a6f (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
#!/bin/sh
#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# build-ndk-sysroot.sh
#
# collect files from an Android tree to assemble a sysroot suitable for
# building a standable toolchain.
#
# after that, you may use build/tools/package-ndk-sysroot.sh to package
# the resulting files for distribution.
#
# NOTE: this is different from the Android toolchain original build-sysroot.sh
#       script because we place target files differently.
#
# WARNING: For now, only a single target ABI/Architecture us supported
#

. `dirname $0`/ndk-common.sh

# PLATFORM is the name of the current Android system platform
PLATFORM=android-3

# ABI is the target ABI name for the NDK
ABI=arm

OPTION_HELP=no
OPTION_BUILD_OUT=
OPTION_PLATFORM=
OPTION_PACKAGE=no
OPTION_ABI=

for opt do
  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  case "$opt" in
  --help|-h|-\?) OPTION_HELP=yes
  ;;
  --verbose)
    if [ "$VERBOSE" = "yes" ] ; then
        VERBOSE2=yes
    else
        VERBOSE=yes
    fi
    ;;
  --platform=*)
    OPTION_PLATFORM=$optarg
    ;;
  --build-out=*)
    OPTION_BUILD_OUT=$optarg
    ;;
  --package)
    OPTION_PACKAGE=yes
    ;;
  --abi=*)
    OPTION_ABI=$optarg
    ;;
  *)
    echo "unknown option '$opt', use --help"
    exit 1
  esac
done

if [ $OPTION_HELP = "yes" ] ; then
    echo "Collect files from an Android build tree and assembles a sysroot"
    echo "suitable for building a standalone toolchain or be used by the"
    echo "Android NDK."
    echo ""
    echo "options:"
    echo ""
    echo "  --help             print this message"
    echo "  --verbose          enable verbose messages"
    echo "  --platform=<name>  generate sysroot for platform <name> (default is $PLATFORM)"
    echo "  --abi=<name>       generate sysroot for abi <name> (default is $ABI)"
    echo "  --build-out=<path> set Android build out directory"
    echo "  --package          generate sysroot package tarball"
    echo ""
    exit 0
fi

if [ -n "$OPTION_PLATFORM" ] ; then
    PLATFORM=$OPTION_PLATFORM
fi

if [ -n "$OPTION_ABI" ] ; then
    ABI=$OPTION_ABI
fi

case "$ABI" in
arm*)
    ARCH=arm
    ;;
*)
    ARCH=$ABI
    ;;
esac


# Get the root of the NDK from the current program location
NDK_ROOT=`dirname $0`
NDK_ROOT=`dirname $NDK_ROOT`
NDK_ROOT=`dirname $NDK_ROOT`

# Get the Android out directory
if [ -z "$OPTION_BUILD_OUT" ] ; then
    if [ -z "$ANDROID_PRODUCT_OUT" ] ; then
        echo "ANDROID_PRODUCT_OUT is not defined in your environment. Aborting"
        exit 1
    fi
    if [ ! -d $ANDROID_PRODUCT_OUT ] ; then
        echo "ANDROID_PRODUCT_OUT does not point to a valid directory. Aborting"
        exit 1
    fi
else
    ANDROID_PRODUCT_OUT=$OPTION_BUILD_OUT
    if [ ! -d $ANDROID_PRODUCT_OUT ] ; then
        echo "The build out path is not a valid directory: $OPTION_BUILD_OUT"
        exit 1
    fi
fi

PRODUCT_DIR=$ANDROID_PRODUCT_OUT
SYSROOT=$NDK_ROOT/build/platforms/$PLATFORM/arch-$ABI
COMMON_ROOT=$NDK_ROOT/build/platforms/$PLATFORM/common

# clean up everything in existing sysroot
rm -rf $SYSROOT
mkdir -p $SYSROOT

rm -rf $COMMON_ROOT
mkdir -p $COMMON_ROOT

LIB_ROOT=$SYSROOT/usr/lib
INCLUDE_ROOT=$SYSROOT/usr/include

install_file ()
{
    mkdir -p $2/`dirname $1`
    cp -fp $1 $2/$1
}

install_helper ()
{
  (cd $1 && find . -type f | while read ff; do install_file $ff $2; done)
}

TOP=$PRODUCT_DIR/../../../..

# CRT objects that need to be copied
CRT_OBJS_DIR=$PRODUCT_DIR/obj/lib
CRT_OBJS="$CRT_OBJS_DIR/crtbegin_static.o \
$CRT_OBJS_DIR/crtbegin_dynamic.o \
$CRT_OBJS_DIR/crtend_android.o"

# static libraries that need to be copied.
STATIC_LIBS_DIR=$PRODUCT_DIR/obj/STATIC_LIBRARIES
STATIC_LIBS="$STATIC_LIBS_DIR/libc_intermediates/libc.a \
$STATIC_LIBS_DIR/libm_intermediates/libm.a \
$STATIC_LIBS_DIR/libstdc++_intermediates/libstdc++.a
$STATIC_LIBS_DIR/libthread_db_intermediates/libthread_db.a"

# dynamic libraries that need to be copied.
DYNAMIC_LIBS_DIR=$PRODUCT_DIR/symbols/system/lib
DYNAMIC_LIBS="$DYNAMIC_LIBS_DIR/libdl.so \
$DYNAMIC_LIBS_DIR/libc.so \
$DYNAMIC_LIBS_DIR/libm.so \
$DYNAMIC_LIBS_DIR/libstdc++.so \
$DYNAMIC_LIBS_DIR/libthread_db.so"

# Copy all CRT objects and libraries
rm -rf $LIB_ROOT
mkdir -p $LIB_ROOT
cp -f $CRT_OBJS $STATIC_LIBS $DYNAMIC_LIBS $LIB_ROOT

# Check $TOP/bionic to see if this is new source layout.
if [ -d $TOP/bionic ] ;then
  BIONIC_ROOT=$TOP/bionic
  LIBC_ROOT=$BIONIC_ROOT/libc
else
  BIONIC_ROOT=$TOP/system
  LIBC_ROOT=$BIONIC_ROOT/bionic
fi

# Copy headers.  This need to be done in the reverse order of inclusion
# in case there are different headers with the same name.
ARCH_INCLUDE=$SYSROOT/usr/include
rm -rf $ARCH_INCLUDE
mkdir -p $ARCH_INCLUDE

COMMON_INCLUDE=$COMMON_ROOT/include
rm -rf $COMMON_INCLUDE
mkdir -p $COMMON_INCLUDE

# Install a common header and create the appropriate arch-specific
# directory for it.
#
# $1: source directory
# $2: header path, relative to source directory
#
common_header ()
{
    echo "Copy: $COMMON_INCLUDE/$2"
    mkdir -p `dirname $COMMON_INCLUDE/$2`
    install $1/$2 $COMMON_INCLUDE/$2
    # just to be safe
    chmod a-x $COMMON_INCLUDE/$2

    # the link prefix, used to point to common/
    # from arch-$ARCH/usr/
    link_prefix=../../common/include

    # we need to count the number of directory separators in $2
    # for each one of them, we're going to prepend ../ to the
    # link prefix
    for item in `echo $2 | tr '/' ' '`; do
        link_prefix=../$link_prefix
    done

    echo "Link: $ARCH_INCLUDE/$2"
    mkdir -p `dirname $ARCH_INCLUDE/$2`
    ln -s $link_prefix/$2 $ARCH_INCLUDE/$2
}

common_headers ()
{
    srcs=`cd $1 && find . -type f`
    # remove leading ./
    srcs=`echo $srcs | sed -e "s%\./%%g"`

    for src in $srcs; do
        common_header $1 $src
    done
}

arch_header ()
{
    echo "Copy: $ARCH_INCLUDE/$2"
    mkdir -p `dirname $ARCH_INCLUDE/$2`
    install $1/$2 $ARCH_INCLUDE/$2
    # just to be safe
    chmod a-x $ARCH_INCLUDE/$2
}

arch_headers ()
{
    srcs=`cd $1 && find . -type f`
    # remove leading ./
    srcs=`echo $srcs | sed -e "s%\./%%g"`

    for src in $srcs; do
        arch_header $1 $src
    done
}

# ZLib headers
common_header  $TOP/external/zlib zlib.h
common_header  $TOP/external/zlib zconf.h

# Jni header
common_header  $TOP/libnativehelper/include/nativehelper jni.h

# libthread_db headers, not sure if this is needed for the NDK
common_headers $BIONIC_ROOT/libthread_db/include

# for libm, just copy math.h and fenv.h
common_header $BIONIC_ROOT/libm/include math.h
if [ "$ARCH" = "x86" ]; then
    arch_header   $BIONIC_ROOT/libm/include i387/fenv.h
else
    arch_header   $BIONIC_ROOT/libm/include $ARCH/fenv.h
fi

# our tiny C++ standard library
common_headers $BIONIC_ROOT/libstdc++/include

# C library kernel headers
common_headers $LIBC_ROOT/kernel/common
arch_headers   $LIBC_ROOT/kernel/arch-$ARCH

# C library headers
common_headers $LIBC_ROOT/include
arch_headers   $LIBC_ROOT/arch-$ARCH/include

# Do we need to package the result
if [ $OPTION_PACKAGE = yes ] ; then
    DATE=`date +%Y%m%d`
    PKGFILE=/tmp/ndk-$USER/android-ndk-sysroot-$DATE.tar.bz2
    tar cjf $PKGFILE build/platforms/$PLATFORM/arch-$ARCH
    echo "Packaged in $PKGFILE"
fi