• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# Copyright (C) 2009 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# build-ndk-sysroot.sh
18#
19# collect files from an Android tree to assemble a sysroot suitable for
20# building a standable toolchain.
21#
22# after that, you may use build/tools/package-ndk-sysroot.sh to package
23# the resulting files for distribution.
24#
25# NOTE: this is different from the Android toolchain original build-sysroot.sh
26#       script because we place target files differently.
27#
28# WARNING: For now, only a single target ABI/Architecture us supported
29#
30
31. `dirname $0`/ndk-common.sh
32
33# PLATFORM is the name of the current Android system platform
34PLATFORM=android-3
35
36# ABI is the target ABI name for the NDK
37ABI=arm
38
39OPTION_HELP=no
40OPTION_BUILD_OUT=
41OPTION_PLATFORM=
42OPTION_PACKAGE=no
43OPTION_ABI=
44
45for opt do
46  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
47  case "$opt" in
48  --help|-h|-\?) OPTION_HELP=yes
49  ;;
50  --verbose)
51    VERBOSE=yes
52    ;;
53  --platform=*)
54    OPTION_PLATFORM=$optarg
55    ;;
56  --build-out=*)
57    OPTION_BUILD_OUT=$optarg
58    ;;
59  --package)
60    OPTION_PACKAGE=yes
61    ;;
62  --abi=*)
63    OPTION_ABI=$optarg
64    ;;
65  *)
66    echo "unknown option '$opt', use --help"
67    exit 1
68  esac
69done
70
71if [ $OPTION_HELP = "yes" ] ; then
72    echo "Collect files from an Android build tree and assembles a sysroot"
73    echo "suitable for building a standalone toolchain or be used by the"
74    echo "Android NDK."
75    echo ""
76    echo "options:"
77    echo ""
78    echo "  --help             print this message"
79    echo "  --verbose          enable verbose messages"
80    echo "  --platform=<name>  generate sysroot for platform <name> (default is $PLATFORM)"
81    echo "  --abi=<name>       generate sysroot for abi <name> (default is $ABI)"
82    echo "  --build-out=<path> set Android build out directory"
83    echo "  --package          generate sysroot package tarball"
84    echo ""
85    exit 0
86fi
87
88if [ -n "$OPTION_PLATFORM" ] ; then
89    PLATFORM=$OPTION_PLATFORM
90fi
91
92if [ -n "$OPTION_ABI" ] ; then
93    ABI=$OPTION_ABI
94fi
95
96case "$ABI" in
97arm*)
98    ARCH=arm
99    ;;
100*)
101    ARCH=$ABI
102    ;;
103esac
104
105
106# Get the root of the NDK from the current program location
107NDK_ROOT=`dirname $0`
108NDK_ROOT=`dirname $NDK_ROOT`
109NDK_ROOT=`dirname $NDK_ROOT`
110
111# Get the Android out directory
112if [ -z "$OPTION_BUILD_OUT" ] ; then
113    if [ -z "$ANDROID_PRODUCT_OUT" ] ; then
114        echo "ANDROID_PRODUCT_OUT is not defined in your environment. Aborting"
115        exit 1
116    fi
117    if [ ! -d $ANDROID_PRODUCT_OUT ] ; then
118        echo "ANDROID_PRODUCT_OUT does not point to a valid directory. Aborting"
119        exit 1
120    fi
121else
122    ANDROID_PRODUCT_OUT=$OPTION_BUILD_OUT
123    if [ ! -d $ANDROID_PRODUCT_OUT ] ; then
124        echo "The build out path is not a valid directory: $OPTION_BUILD_OUT"
125        exit 1
126    fi
127fi
128
129PRODUCT_DIR=$ANDROID_PRODUCT_OUT
130SYSROOT=$NDK_ROOT/build/platforms/$PLATFORM/arch-$ABI
131COMMON_ROOT=$NDK_ROOT/build/platforms/$PLATFORM/common
132
133# clean up everything in existing sysroot
134rm -rf $SYSROOT
135mkdir -p $SYSROOT
136
137rm -rf $COMMON_ROOT
138mkdir -p $COMMON_ROOT
139
140LIB_ROOT=$SYSROOT/usr/lib
141
142install_file ()
143{
144    mkdir -p $2/`dirname $1`
145    cp -fp $1 $2/$1
146}
147
148install_helper ()
149{
150  (cd $1 && find . -type f | while read ff; do install_file $ff $2; done)
151}
152
153TOP=$PRODUCT_DIR/../../../..
154
155# CRT objects that need to be copied
156CRT_OBJS_DIR=$PRODUCT_DIR/obj/lib
157CRT_OBJS="$CRT_OBJS_DIR/crtbegin_static.o \
158$CRT_OBJS_DIR/crtbegin_dynamic.o \
159$CRT_OBJS_DIR/crtend_android.o"
160
161# static libraries that need to be copied.
162STATIC_LIBS_DIR=$PRODUCT_DIR/obj/STATIC_LIBRARIES
163STATIC_LIBS="$STATIC_LIBS_DIR/libc_intermediates/libc.a \
164$STATIC_LIBS_DIR/libm_intermediates/libm.a \
165$STATIC_LIBS_DIR/libstdc++_intermediates/libstdc++.a
166$STATIC_LIBS_DIR/libthread_db_intermediates/libthread_db.a"
167
168# dynamic libraries that need to be copied.
169DYNAMIC_LIBS_DIR=$PRODUCT_DIR/symbols/system/lib
170DYNAMIC_LIBS="$DYNAMIC_LIBS_DIR/libdl.so \
171$DYNAMIC_LIBS_DIR/libc.so \
172$DYNAMIC_LIBS_DIR/libm.so \
173$DYNAMIC_LIBS_DIR/libstdc++.so \
174$DYNAMIC_LIBS_DIR/libthread_db.so"
175
176# Copy all CRT objects and libraries
177rm -rf $LIB_ROOT
178mkdir -p $LIB_ROOT
179cp -f $CRT_OBJS $STATIC_LIBS $DYNAMIC_LIBS $LIB_ROOT
180
181# Check $TOP/bionic to see if this is new source layout.
182if [ -d $TOP/bionic ] ;then
183  BIONIC_ROOT=$TOP/bionic
184  LIBC_ROOT=$BIONIC_ROOT/libc
185else
186  BIONIC_ROOT=$TOP/system
187  LIBC_ROOT=$BIONIC_ROOT/bionic
188fi
189
190# Copy headers.  This need to be done in the reverse order of inclusion
191# in case there are different headers with the same name.
192ARCH_INCLUDE=$SYSROOT/usr/include
193rm -rf $ARCH_INCLUDE
194mkdir -p $ARCH_INCLUDE
195
196COMMON_INCLUDE=$COMMON_ROOT/include
197rm -rf $COMMON_INCLUDE
198mkdir -p $COMMON_INCLUDE
199
200# Install a common header and create the appropriate arch-specific
201# directory for it.
202#
203# $1: source directory
204# $2: header path, relative to source directory
205#
206common_header ()
207{
208    echo "Copy: $COMMON_INCLUDE/$2"
209    mkdir -p `dirname $COMMON_INCLUDE/$2`
210    install $1/$2 $COMMON_INCLUDE/$2
211    # just to be safe
212    chmod a-x $COMMON_INCLUDE/$2
213
214    # the link prefix, used to point to common/
215    # from arch-$ARCH/usr/
216    link_prefix=../../common/include
217
218    # we need to count the number of directory separators in $2
219    # for each one of them, we're going to prepend ../ to the
220    # link prefix
221    for item in `echo $2 | tr '/' ' '`; do
222        link_prefix=../$link_prefix
223    done
224
225    echo "Link: $ARCH_INCLUDE/$2"
226    mkdir -p `dirname $ARCH_INCLUDE/$2`
227    ln -s $link_prefix/$2 $ARCH_INCLUDE/$2
228}
229
230common_headers ()
231{
232    srcs=`cd $1 && find . -type f`
233    # remove leading ./
234    srcs=`echo $srcs | sed -e "s%\./%%g"`
235
236    for src in $srcs; do
237        common_header $1 $src
238    done
239}
240
241arch_header ()
242{
243    echo "Copy: $ARCH_INCLUDE/$2"
244    mkdir -p `dirname $ARCH_INCLUDE/$2`
245    install $1/$2 $ARCH_INCLUDE/$2
246    # just to be safe
247    chmod a-x $ARCH_INCLUDE/$2
248}
249
250arch_headers ()
251{
252    srcs=`cd $1 && find . -type f`
253    # remove leading ./
254    srcs=`echo $srcs | sed -e "s%\./%%g"`
255
256    for src in $srcs; do
257        arch_header $1 $src
258    done
259}
260
261# ZLib headers
262common_header  $TOP/external/zlib zlib.h
263common_header  $TOP/external/zlib zconf.h
264
265# Jni header
266common_header  $TOP/libnativehelper/include/nativehelper jni.h
267
268# libthread_db headers, not sure if this is needed for the NDK
269common_headers $BIONIC_ROOT/libthread_db/include
270
271# for libm, just copy math.h and fenv.h
272common_header $BIONIC_ROOT/libm/include math.h
273if [ "$ARCH" = "x86" ]; then
274    arch_header   $BIONIC_ROOT/libm/include i387/fenv.h
275else
276    arch_header   $BIONIC_ROOT/libm/include $ARCH/fenv.h
277fi
278
279# our tiny C++ standard library
280common_headers $BIONIC_ROOT/libstdc++/include
281
282# C library kernel headers
283common_headers $LIBC_ROOT/kernel/common
284arch_headers   $LIBC_ROOT/kernel/arch-$ARCH
285
286# C library headers
287common_headers $LIBC_ROOT/include
288arch_headers   $LIBC_ROOT/arch-$ARCH/include
289
290# Do we need to package the result
291if [ $OPTION_PACKAGE = yes ] ; then
292    DATE=`date +%Y%m%d`
293    PKGFILE=$TMPDIR/android-ndk-sysroot-$DATE.tar.bz2
294    tar cjf $PKGFILE build/platforms/$PLATFORM/arch-$ARCH
295    echo "Packaged in $PKGFILE"
296fi
297