blob: 8741220456890169147757bd1c1e5a91f874cdba [file] [log] [blame]
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +02001#!/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
31source `dirname $0`/../core/ndk-common.sh
32
33# PLATFORM is the name of the current Android system platform
David 'Digit' Turnerfdc5ea22009-07-24 17:56:51 +020034PLATFORM=android-3
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +020035
36# ABI is the target ABI name for the NDK
37ABI=arm
38
39# ARCH is the target ABI name in the Android sources
40ARCH=arm
41
42OPTION_HELP=no
43OPTION_BUILD_OUT=
44OPTION_PLATFORM=
45OPTION_PACKAGE=no
46for opt do
47 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
48 case "$opt" in
49 --help|-h|-\?) OPTION_HELP=yes
50 ;;
51 --verbose)
52 if [ "$VERBOSE" = "yes" ] ; then
53 VERBOSE2=yes
54 else
55 VERBOSE=yes
56 fi
57 ;;
58 --platform=*)
59 OPTION_PLATFORM=$optarg
60 ;;
61 --build-out=*)
62 OPTION_BUILD_OUT=$optarg
63 ;;
64 --package)
65 OPTION_PACKAGE=yes
66 ;;
67 *)
68 echo "unknown option '$opt', use --help"
69 exit 1
70 esac
71done
72
73if [ $OPTION_HELP = "yes" ] ; then
74 echo "Collect files from an Android build tree and assembles a sysroot"
75 echo "suitable for building a standalone toolchain or be used by the"
76 echo "Android NDK."
77 echo ""
78 echo "options:"
79 echo ""
80 echo " --help print this message"
81 echo " --verbose enable verbose messages"
82 echo " --platform=<name> generate sysroot for platform <name> (default is $PLATFORM)"
83 echo " --build-out=<path> set Android build out directory"
84 echo " --package generate sysroot package tarball"
85 echo ""
86 exit 0
87fi
88
89if [ -n "$OPTION_PLATFORM" ] ; then
90 PLATFORM=$OPTION_PLATFORM
91fi
92
93# Get the root of the NDK from the current program location
94NDK_ROOT=`dirname $0`
95NDK_ROOT=`dirname $NDK_ROOT`
96NDK_ROOT=`dirname $NDK_ROOT`
97
98# Get the Android out directory
99if [ -z "$OPTION_BUILD_OUT" ] ; then
100 if [ -z "$ANDROID_PRODUCT_OUT" ] ; then
101 echo "ANDROID_PRODUCT_OUT is not defined in your environment. Aborting"
102 exit 1
103 fi
104 if [ ! -d $ANDROID_PRODUCT_OUT ] ; then
105 echo "ANDROID_PRODUCT_OUT does not point to a valid directory. Aborting"
106 exit 1
107 fi
108else
109 ANDROID_PRODUCT_OUT=$OPTION_BUILD_OUT
110 if [ ! -d $ANDROID_PRODUCT_OUT ] ; then
111 echo "The build out path is not a valid directory: $OPTION_BUILD_OUT"
112 exit 1
113 fi
114fi
115
116PRODUCT_DIR=$ANDROID_PRODUCT_OUT
117SYSROOT=$NDK_ROOT/build/platforms/$PLATFORM/arch-$ABI
118COMMON_ROOT=$NDK_ROOT/build/platforms/$PLATFORM/common
119
120# clean up everything in existing sysroot
121rm -rf $SYSROOT
122mkdir -p $SYSROOT
123
124rm -rf $COMMON_ROOT
125mkdir -p $COMMON_ROOT
126
127LIB_ROOT=$SYSROOT/usr/lib
128INCLUDE_ROOT=$SYSROOT/usr/include
129
130install_file ()
131{
132 mkdir -p $2/`dirname $1`
133 cp -fp $1 $2/$1
134}
135
136install_helper ()
137{
138 (cd $1 && find . -type f | while read ff; do install_file $ff $2; done)
139}
140
141TOP=$PRODUCT_DIR/../../../..
142
143# CRT objects that need to be copied
144CRT_OBJS_DIR=$PRODUCT_DIR/obj/lib
145CRT_OBJS="$CRT_OBJS_DIR/crtbegin_static.o \
146$CRT_OBJS_DIR/crtbegin_dynamic.o \
147$CRT_OBJS_DIR/crtend_android.o"
148
149# static libraries that need to be copied.
150STATIC_LIBS_DIR=$PRODUCT_DIR/obj/STATIC_LIBRARIES
151STATIC_LIBS="$STATIC_LIBS_DIR/libc_intermediates/libc.a \
152$STATIC_LIBS_DIR/libm_intermediates/libm.a \
153$STATIC_LIBS_DIR/libstdc++_intermediates/libstdc++.a
154$STATIC_LIBS_DIR/libthread_db_intermediates/libthread_db.a"
155
156# dynamic libraries that need to be copied.
157DYNAMIC_LIBS_DIR=$PRODUCT_DIR/symbols/system/lib
158DYNAMIC_LIBS="$DYNAMIC_LIBS_DIR/libdl.so \
159$DYNAMIC_LIBS_DIR/libc.so \
160$DYNAMIC_LIBS_DIR/libm.so \
161$DYNAMIC_LIBS_DIR/libstdc++.so \
162$DYNAMIC_LIBS_DIR/libthread_db.so"
163
164# Copy all CRT objects and libraries
165rm -rf $LIB_ROOT
166mkdir -p $LIB_ROOT
167cp -f $CRT_OBJS $STATIC_LIBS $DYNAMIC_LIBS $LIB_ROOT
168
David 'Digit' Turnerb20725d2009-05-11 16:37:42 +0200169# Check $TOP/bionic to see if this is new source layout.
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +0200170if [ -d $TOP/bionic ] ;then
171 BIONIC_ROOT=$TOP/bionic
172 LIBC_ROOT=$BIONIC_ROOT/libc
173else
174 BIONIC_ROOT=$TOP/system
175 LIBC_ROOT=$BIONIC_ROOT/bionic
176fi
177
178# Copy headers. This need to be done in the reverse order of inclusion
179# in case there are different headers with the same name.
180ARCH_INCLUDE=$SYSROOT/usr/include
181rm -rf $ARCH_INCLUDE
182mkdir -p $ARCH_INCLUDE
183
184COMMON_INCLUDE=$COMMON_ROOT/include
185rm -rf $COMMON_INCLUDE
186mkdir -p $COMMON_INCLUDE
187
188# Install a common header and create the appropriate arch-specific
189# directory for it.
190#
191# $1: source directory
192# $2: header path, relative to source directory
193#
194common_header ()
195{
196 echo "Copy: $COMMON_INCLUDE/$2"
197 mkdir -p `dirname $COMMON_INCLUDE/$2`
198 install $1/$2 $COMMON_INCLUDE/$2
199 # just to be safe
200 chmod a-x $COMMON_INCLUDE/$2
201
202 # the link prefix, used to point to common/
203 # from arch-$ARCH/usr/
204 link_prefix=../../common/include
205
206 # we need to count the number of directory separators in $2
207 # for each one of them, we're going to prepend ../ to the
208 # link prefix
209 for item in `echo $2 | tr '/' ' '`; do
210 link_prefix=../$link_prefix
211 done
212
213 echo "Link: $ARCH_INCLUDE/$2"
214 mkdir -p `dirname $ARCH_INCLUDE/$2`
215 ln -s $link_prefix/$2 $ARCH_INCLUDE/$2
216}
217
218common_headers ()
219{
220 srcs=`cd $1 && find . -type f`
221 # remove leading ./
222 srcs=`echo $srcs | sed -e "s%\./%%g"`
223
224 for src in $srcs; do
225 common_header $1 $src
226 done
227}
228
229arch_header ()
230{
231 echo "Copy: $ARCH_INCLUDE/$2"
232 mkdir -p `dirname $ARCH_INCLUDE/$2`
233 install $1/$2 $ARCH_INCLUDE/$2
234 # just to be safe
235 chmod a-x $ARCH_INCLUDE/$2
236}
237
238arch_headers ()
239{
240 srcs=`cd $1 && find . -type f`
241 # remove leading ./
242 srcs=`echo $srcs | sed -e "s%\./%%g"`
243
244 for src in $srcs; do
245 arch_header $1 $src
246 done
247}
248
David 'Digit' Turner985338c2009-05-19 12:10:09 +0200249# ZLib headers
250common_header $TOP/external/zlib zlib.h
251common_header $TOP/external/zlib zconf.h
252
253# Jni header
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +0200254common_header $TOP/dalvik/libnativehelper/include/nativehelper jni.h
David 'Digit' Turner985338c2009-05-19 12:10:09 +0200255
256# libthread_db headers, not sure if this is needed for the NDK
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +0200257common_headers $BIONIC_ROOT/libthread_db/include
258
259# for libm, just copy math.h and fenv.h
260common_header $BIONIC_ROOT/libm/include math.h
261arch_header $BIONIC_ROOT/libm/include $ARCH/fenv.h
262
David 'Digit' Turner985338c2009-05-19 12:10:09 +0200263# our tiny C++ standard library
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +0200264common_headers $BIONIC_ROOT/libstdc++/include
265
David 'Digit' Turner985338c2009-05-19 12:10:09 +0200266# C library kernel headers
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +0200267common_headers $LIBC_ROOT/kernel/common
268arch_headers $LIBC_ROOT/kernel/arch-arm
269
David 'Digit' Turner985338c2009-05-19 12:10:09 +0200270# C library headers
David 'Digit' Turnerb9a84792009-05-07 20:39:04 +0200271common_headers $LIBC_ROOT/include
272arch_headers $LIBC_ROOT/arch-$ARCH/include
273
274# Do we need to package the result
275if [ $OPTION_PACKAGE = yes ] ; then
276 DATE=`date +%Y%m%d`
277 PKGFILE=/tmp/android-ndk-sysroot-$DATE.tar.bz2
278 tar cjf $PKGFILE build/platforms/$PLATFORM
279 echo "Packaged in $PKGFILE"
280fi