1#!/bin/sh
2#
3# Copyright (C) 2011 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# gen-platforms.sh
18#
19# This tool is used when packaging a new release, or when developing
20# the NDK itself. It will populate DST ($NDK/platforms by default)
21# with the content of SRC ($NDK/../development/ndk/platforms/ by default).
22#
23# The idea is that the content of $SRC/android-N/ only contains stuff
24# that is relevant to API level N, and not contain anything that is already
25# provided by API level N-1, N-2, etc..
26#
27# More precisely, for each architecture A:
28#  $SRC/android-N/include        --> $DST/android-N/arch-A/usr/include
29#  $SRC/android-N/arch-A/include --> $DST/android-N/arch-A/usr/include
30#  $SRC/android-N/arch-A/lib     --> $DST/android-N/arch-A/usr/lib
31#
32# Also, we generate on-the-fly shared dynamic libraries from list of symbols:
33#
34#  $SRC/android-N/arch-A/symbols --> $DST/android-N/arch-A/usr/lib
35#
36# Repeat after that for N+1, N+2, etc..
37#
38
39PROGDIR=$(dirname "$0")
40. "$PROGDIR/prebuilt-common.sh"
41
42# Return the list of platform supported from $1/platforms
43# as a single space-separated sorted list of levels. (e.g. "3 4 5 8 9 14")
44# $1: source directory
45extract_platforms_from ()
46{
47    if [ -d "$1" ] ; then
48        (cd "$1/platforms" && ls -d android-*) | sed -e "s!android-!!" | sort -g | tr '\n' ' '
49    else
50        echo ""
51    fi
52}
53
54SRCDIR="../development/ndk"
55DSTDIR="$ANDROID_NDK_ROOT"
56
57ARCHS="$DEFAULT_ARCHS"
58PLATFORMS=`extract_platforms_from "$SRCDIR"`
59NDK_DIR=$ANDROID_NDK_ROOT
60
61OPTION_HELP=no
62OPTION_PLATFORMS=
63OPTION_SRCDIR=
64OPTION_DSTDIR=
65OPTION_SAMPLES=
66OPTION_FAST_COPY=
67OPTION_MINIMAL=
68OPTION_ARCH=
69OPTION_ABI=
70OPTION_DEBUG_LIBS=
71OPTION_OVERLAY=
72PACKAGE_DIR=
73
74VERBOSE=no
75VERBOSE2=no
76
77for opt do
78  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
79  case "$opt" in
80  --help|-h|-\?) OPTION_HELP=yes
81  ;;
82  --verbose)
83    if [ "$VERBOSE" = "yes" ] ; then
84        VERBOSE2=yes
85    else
86        VERBOSE=yes
87    fi
88    ;;
89  --src-dir=*)
90    OPTION_SRCDIR="$optarg"
91    ;;
92  --dst-dir=*)
93    OPTION_DSTDIR="$optarg"
94    ;;
95  --ndk-dir=*)
96    NDK_DIR=$optarg
97    ;;
98  --platform=*)
99    OPTION_PLATFORM=$optarg
100    ;;
101  --arch=*)
102    OPTION_ARCH=$optarg
103    ;;
104  --abi=*)  # We still support this for backwards-compatibility
105    OPTION_ABI=$optarg
106    ;;
107  --samples)
108    OPTION_SAMPLES=yes
109    ;;
110  --fast-copy)
111    OPTION_FAST_COPY=yes
112    ;;
113  --minimal)
114    OPTION_MINIMAL=yes
115    ;;
116  --package-dir=*)
117    PACKAGE_DIR=$optarg
118    ;;
119  --debug-libs)
120    OPTION_DEBUG_LIBS=true
121    ;;
122  --overlay)
123    OPTION_OVERLAY=true
124    ;;
125  *)
126    echo "unknown option '$opt', use --help"
127    exit 1
128  esac
129done
130
131if [ $OPTION_HELP = "yes" ] ; then
132    echo "Collect files from an Android NDK development tree and assemble"
133    echo "the platform files appropriately into a final release structure."
134    echo ""
135    echo "options:"
136    echo ""
137    echo "  --help                Print this message"
138    echo "  --verbose             Enable verbose messages"
139    echo "  --src-dir=<path>      Source directory for development platform files [$SRCDIR]"
140    echo "  --dst-dir=<path>      Destination directory [$DSTDIR]"
141    echo "  --ndk-dir=<path>      Use toolchains from this NDK directory [$NDK_DIR]"
142    echo "  --platform=<list>     List of API levels [$PLATFORMS]"
143    echo "  --arch=<list>         List of CPU architectures [$ARCHS]"
144    echo "  --minimal             Ignore samples, symlinks and generated shared libs."
145    echo "  --fast-copy           Don't create symlinks, copy files instead"
146    echo "  --samples             Also generate samples directories."
147    echo "  --package-dir=<path>  Package platforms archive in specific path."
148    echo "  --debug-libs          Also generate C source file for generated libraries."
149    echo ""
150    echo "Use the --minimal flag if you want to generate minimal sysroot directories"
151    echo "that will be used to generate prebuilt toolchains. Otherwise, the script"
152    echo "will require these toolchains to be pre-installed and will use them to"
153    echo "generate shared system shared libraries from the symbol list files."
154    exit 0
155fi
156
157if [ -n "$OPTION_SRCDIR" ] ; then
158    SRCDIR="$OPTION_SRCDIR";
159    if [ ! -d "$SRCDIR" ] ; then
160        echo "ERROR: Source directory $SRCDIR does not exist !"
161        exit 1
162    fi
163    if [ ! -d "$SRCDIR/platforms/android-3" ] ; then
164        echo "ERROR: Invalid source directory: $SRCDIR"
165        echo "Please make sure it contains platforms/android-3 etc..."
166        exit 1
167    fi
168else
169    SRCDIR=`dirname $ANDROID_NDK_ROOT`/development/ndk
170    log "Using source directory: $SRCDIR"
171fi
172
173if [ -n "$OPTION_PLATFORM" ] ; then
174    PLATFORMS=$(commas_to_spaces $OPTION_PLATFORM)
175else
176    # Build the list from the content of SRCDIR
177    PLATFORMS=`extract_platforms_from "$SRCDIR"`
178    log "Using platforms: $PLATFORMS"
179fi
180
181# Remove the android- prefix of any platform name
182PLATFORMS=$(echo $PLATFORMS | tr ' ' '\n' | sed -e 's!^android-!!g' | tr '\n' ' ')
183
184if [ -n "$OPTION_DSTDIR" ] ; then
185    DSTDIR="$OPTION_DSTDIR"
186else
187    log "Using destination directory: $DSTDIR"
188fi
189
190# Handle architecture list
191#
192# We support both --arch and --abi for backwards compatibility reasons
193# --arch is the new hotness, --abi is deprecated.
194#
195if [ -n "$OPTION_ARCH" ]; then
196    OPTION_ARCH=$(commas_to_spaces $OPTION_ARCH)
197fi
198
199if [ -n "$OPTION_ABI" ] ; then
200    echo "WARNING: --abi=<names> is deprecated. Use --arch=<names> instead!"
201    OPTION_ABI=$(commas_to_spaces $OPTION_ABI)
202    if [ -n "$OPTION_ARCH" -a "$OPTION_ARCH" != "$OPTION_ABI" ]; then
203        echo "ERROR: You can't use both --abi and --arch with different values!"
204        exit 1
205    fi
206    OPTION_ARCH=$OPTION_ABI
207fi
208
209if [ -n "$OPTION_ARCH" ] ; then
210    ARCHS="$OPTION_ARCH"
211fi
212log "Using architectures: $(commas_to_spaces $ARCHS)"
213
214log "Checking source platforms."
215for PLATFORM in $PLATFORMS; do
216    DIR="$SRCDIR/platforms/android-$PLATFORM"
217    if [ ! -d $DIR ] ; then
218        echo "ERROR: Directory missing: $DIR"
219        echo "Please check your --platform=<list> option and try again."
220        exit 2
221    else
222        log "  $DIR"
223    fi
224done
225
226log "Checking source platform architectures."
227BAD_ARCHS=
228for ARCH in $ARCHS; do
229    eval CHECK_$ARCH=no
230done
231for PLATFORM in $PLATFORMS; do
232    for ARCH in $ARCHS; do
233        DIR="$SRCDIR/platforms/android-$PLATFORM/arch-$ARCH"
234        if [ -d $DIR ] ; then
235            log "  $DIR"
236            eval CHECK_$ARCH=yes
237        fi
238    done
239done
240
241if [ "$OPTION_MINIMAL" ]; then
242    OPTION_SAMPLES=
243    OPTION_FAST_COPY=yes
244fi
245
246BAD_ARCHS=
247for ARCH in $ARCHS; do
248    CHECK=`var_value CHECK_$ARCH`
249    log "  $ARCH check: $CHECK"
250    if [ "$CHECK" = no ] ; then
251        if [ -z "$BAD_ARCHS" ] ; then
252            BAD_ARCHS=$ARCH
253        else
254            BAD_ARCHS="$BAD_ARCHS $ARCH"
255        fi
256    fi
257done
258
259if [ -n "$BAD_ARCHS" ] ; then
260    echo "ERROR: Source directory doesn't support these ARCHs: $BAD_ARCHS"
261    exit 3
262fi
263
264# $1: source directory (relative to $SRCDIR)
265# $2: destination directory (relative to $DSTDIR)
266# $3: description of directory contents (e.g. "sysroot" or "samples")
267copy_src_directory ()
268{
269    local SDIR="$SRCDIR/$1"
270    local DDIR="$DSTDIR/$2"
271    if [ -d "$SDIR" ] ; then
272        log "Copying $3 from \$SRC/$1 to \$DST/$2."
273        mkdir -p "$DDIR" && (cd "$SDIR" && 2>/dev/null tar chf - *) | (tar xf - -C "$DDIR")
274        if [ $? != 0 ] ; then
275            echo "ERROR: Could not copy $3 directory $SDIR into $DDIR !"
276            exit 5
277        fi
278    fi
279}
280
281# $1: source dir
282# $2: destination dir
283# $3: reverse path
284#
285symlink_src_directory_inner ()
286{
287    local files file subdir rev
288    mkdir -p "$DSTDIR/$2"
289    rev=$3
290    files=$(cd $DSTDIR/$1 && ls -1p)
291    for file in $files; do
292        if [ "$file" = "${file%%/}" ]; then
293            log2 "Link \$DST/$2/$file --> $rev/$1/$file"
294            ln -s $rev/$1/$file $DSTDIR/$2/$file
295        else
296            file=${file%%/}
297            symlink_src_directory_inner "$1/$file" "$2/$file" "$rev/.."
298        fi
299    done
300}
301# Create a symlink-copy of directory $1 into $2
302# This function is recursive.
303#
304# $1: source directory (relative to $SRCDIR)
305# $2: destination directory (relative to $DSTDIR)
306symlink_src_directory ()
307{
308    symlink_src_directory_inner "$1" "$2" "$(reverse_path $1)"
309}
310
311# Remove unwanted symbols
312# $1: symbol file (one symbol per line)
313# $2+: Input symbol list
314# Out: Input symbol file, without any unwanted symbol listed by $1
315remove_unwanted_symbols_from ()
316{
317  local SYMBOL_FILE="$1"
318  shift
319  if [ -f "$SYMBOL_FILE" ]; then
320    echo "$@" | tr ' ' '\n' | grep -v -F -x -f $SYMBOL_FILE | tr '\n' ' '
321  else
322    echo "$@"
323  fi
324}
325
326# Remove unwanted symbols from a library's functions list.
327# $1: Architecture name
328# $2: Library name (e.g. libc.so)
329# $3+: Input symbol list
330# Out: Input symbol list without any unwanted symbols.
331remove_unwanted_function_symbols ()
332{
333  local ARCH LIBRARY SYMBOL_FILE
334  ARCH=$1
335  LIBRARY=$2
336  shift; shift
337  SYMBOL_FILE=$PROGDIR/unwanted-symbols/$ARCH/$LIBRARY.functions.txt
338  remove_unwanted_symbols_from $SYMBOL_FILE "$@"
339}
340
341# Same as remove_unwanted_functions_symbols, but for variable names.
342#
343remove_unwanted_variable_symbols ()
344{
345  local ARCH LIBRARY SYMBOL_FILE
346  ARCH=$1
347  LIBRARY=$2
348  shift; shift
349  SYMBOL_FILE=$PROGDIR/unwanted-symbols/$ARCH/$LIBRARY.variables.txt
350  remove_unwanted_symbols_from $SYMBOL_FILE "$@"
351}
352
353# $1: Architecture
354# Out: compiler command
355get_default_compiler_for_arch()
356{
357    local ARCH=$1
358    local TOOLCHAIN_PREFIX EXTRA_CFLAGS CC
359
360    if [ "$(arch_in_unknown_archs $ARCH)" = "yes" ]; then
361        TOOLCHAIN_PREFIX="$NDK_DIR/$(get_llvm_toolchain_binprefix $DEFAULT_LLVM_VERSION)"
362        CC="$TOOLCHAIN_PREFIX/clang"
363        EXTRA_CFLAGS="-emit-llvm -target le32-none-ndk"
364    else
365        TOOLCHAIN_PREFIX="$NDK_DIR/$(get_default_toolchain_binprefix_for_arch $1)"
366        TOOLCHAIN_PREFIX=${TOOLCHAIN_PREFIX%-}
367        CC="$TOOLCHAIN_PREFIX-gcc"
368        EXTRA_CFLAGS=
369    fi
370
371    if [ ! -f "$CC" ]; then
372        dump "ERROR: $ARCH toolchain not installed: $CC"
373        dump "Important: Use the --minimal flag to use this script without generated system shared libraries."
374        dump "This is generally useful when you want to generate the host cross-toolchain programs."
375        exit 1
376    fi
377    echo "$CC $EXTRA_CFLAGS"
378}
379
380# $1: library name
381# $2: functions list
382# $3: variables list
383# $4: destination file
384# $5: compiler command
385gen_shared_lib ()
386{
387    local LIBRARY=$1
388    local FUNCS="$2"
389    local VARS="$3"
390    local DSTFILE="$4"
391    local CC="$5"
392
393    # Now generate a small C source file that contains similarly-named stubs
394    echo "/* Auto-generated file, do not edit */" > $TMPC
395    local func var
396    for func in $FUNCS; do
397        echo "void $func(void) {}" >> $TMPC
398    done
399    for var in $VARS; do
400        echo "int $var = 0;" >> $TMPC
401    done
402
403    # Build it with our cross-compiler. It will complain about conflicting
404    # types for built-in functions, so just shut it up.
405    COMMAND="$CC -Wl,-shared,-Bsymbolic -Wl,-soname,$LIBRARY -nostdlib -o $TMPO $TMPC"
406    echo "## COMMAND: $COMMAND" > $TMPL
407    $COMMAND 1>>$TMPL 2>&1
408    if [ $? != 0 ] ; then
409        dump "ERROR: Can't generate shared library for: $LIBNAME"
410        dump "See the content of $TMPC and $TMPL for details."
411        cat $TMPL | tail -10
412        exit 1
413    fi
414
415    # Copy to our destination now
416    local libdir=$(dirname "$DSTFILE")
417    mkdir -p "$libdir" && cp -f $TMPO "$DSTFILE"
418    if [ $? != 0 ] ; then
419        dump "ERROR: Can't copy shared library for: $LIBNAME"
420        dump "target location is: $DSTFILE"
421        exit 1
422    fi
423
424    if [ "$OPTION_DEBUG_LIBS" ]; then
425      cp $TMPC $DSTFILE.c
426      echo "$FUNCS" > $DSTFILE.functions.txt
427      echo "$VARS" > $DSTFILE.variables.txt
428    fi
429}
430
431# $1: Architecture
432# $2: symbol source directory (relative to $SRCDIR)
433# $3: destination directory for generated libs (relative to $DSTDIR)
434# $4: compiler flags (optional)
435gen_shared_libraries ()
436{
437    local ARCH=$1
438    local SYMDIR="$SRCDIR/$2"
439    local DSTDIR="$DSTDIR/$3"
440    local FLAGS="$4"
441    local CC funcs vars numfuncs numvars
442
443    # Let's locate the toolchain we're going to use
444    CC=$(get_default_compiler_for_arch $ARCH)" $FLAGS"
445
446    # In certain cases, the symbols directory doesn't exist,
447    # e.g. on x86 for PLATFORM < 9
448    if [ ! -d "$SYMDIR" ]; then
449        return
450    fi
451
452    # Let's list the libraries we're going to generate
453    LIBS=$( (cd $SYMDIR && 2>/dev/null ls *.functions.txt) | sort -u | sed -e 's!\.functions\.txt$!!g')
454
455    for LIB in $LIBS; do
456        funcs=$(cat "$SYMDIR/$LIB.functions.txt" 2>/dev/null)
457        vars=$(cat "$SYMDIR/$LIB.variables.txt" 2>/dev/null)
458        funcs=$(remove_unwanted_function_symbols $ARCH libgcc.a $funcs)
459        funcs=$(remove_unwanted_function_symbols $ARCH $LIB $funcs)
460        vars=$(remove_unwanted_variable_symbols $ARCH $LIB $vars)
461        numfuncs=$(echo $funcs | wc -w)
462        numvars=$(echo $vars | wc -w)
463        log "Generating $ARCH shared library for $LIB ($numfuncs functions + $numvars variables)"
464
465        gen_shared_lib $LIB "$funcs" "$vars" "$DSTDIR/$LIB" "$CC"
466    done
467}
468
469# $1: platform number
470# $2: architecture name
471# $3: common source directory (for crtbrand.c, etc)
472# $4: source directory (for *.S files)
473# $5: destination directory
474# $6: flags for compiler (optional)
475gen_crt_objects ()
476{
477    local API=$1
478    local ARCH=$2
479    local COMMON_SRC_DIR="$SRCDIR/$3"
480    local SRC_DIR="$SRCDIR/$4"
481    local DST_DIR="$DSTDIR/$5"
482    local FLAGS="$6"
483    local SRC_FILE DST_FILE
484    local CC
485
486    if [ ! -d "$SRC_DIR" ]; then
487        return
488    fi
489
490    # Let's locate the toolchain we're going to use
491    CC=$(get_default_compiler_for_arch $ARCH)" $FLAGS"
492
493    CRTBRAND_S=$DST_DIR/crtbrand.s
494    log "Generating platform $API crtbrand assembly code: $CRTBRAND_S"
495    (cd "$COMMON_SRC_DIR" && mkdir -p `dirname $CRTBRAND_S` && $CC -DPLATFORM_SDK_VERSION=$API -fpic -S -o - crtbrand.c | \
496        sed -e '/\.note\.ABI-tag/s/progbits/note/' > "$CRTBRAND_S") 1>>$TMPL 2>&1
497    if [ $? != 0 ]; then
498        dump "ERROR: Could not generate $CRTBRAND_S from $COMMON_SRC_DIR/crtbrand.c"
499        dump "Please see the content of $TMPL for details!"
500        cat $TMPL | tail -10
501        exit 1
502    fi
503
504    for SRC_FILE in $(cd "$SRC_DIR" && ls crt*.[cS]); do
505        DST_FILE=${SRC_FILE%%.c}
506        DST_FILE=${DST_FILE%%.S}.o
507
508        case "$DST_FILE" in
509            "crtend.o")
510                # Special case: crtend.S must be compiled as crtend_android.o
511                # This is for long historical reasons, i.e. to avoid name conflicts
512                # in the past with other crtend.o files. This is hard-coded in the
513                # Android toolchain configuration, so switch the name here.
514                DST_FILE=crtend_android.o
515                ;;
516            "crtbegin_dynamic.o"|"crtbegin_static.o")
517                # Add .note.ABI-tag section
518                SRC_FILE=$SRC_FILE" $CRTBRAND_S"
519                ;;
520            "crtbegin.o")
521                # If we have a single source for both crtbegin_static.o and
522                # crtbegin_dynamic.o we generate one and make a copy later.
523                DST_FILE=crtbegin_dynamic.o
524                # Add .note.ABI-tag section
525                SRC_FILE=$SRC_FILE" $CRTBRAND_S"
526                ;;
527        esac
528
529        log "Generating $ARCH C runtime object: $DST_FILE"
530        (cd "$SRC_DIR" && $CC \
531                 -I$SRCDIR/../../bionic/libc/include \
532                 -I$SRCDIR/../../bionic/libc/private \
533                 -I$SRCDIR/../../bionic/libc/arch-$ARCH/include \
534                 -isystem $SRCDIR/../../bionic/libc/kernel/common \
535                 -isystem $SRCDIR/../../bionic/libc/kernel/common/linux \
536                 -O2 -fpic -Wl,-r -nostdlib -nostdinc -o "$DST_DIR/$DST_FILE" $SRC_FILE) 1>>$TMPL 2>&1
537        if [ $? != 0 ]; then
538            dump "ERROR: Could not generate $DST_FILE from $SRC_DIR/$SRC_FILE"
539            dump "Please see the content of $TMPL for details!"
540            cat $TMPL | tail -10
541            exit 1
542        fi
543        if [ ! -s "$DST_DIR/crtbegin_static.o" ]; then
544            cp "$DST_DIR/crtbegin_dynamic.o" "$DST_DIR/crtbegin_static.o"
545        fi
546    done
547    rm -f "$CRTBRAND_S"
548}
549
550# $1: platform number
551# $2: architecture
552# $3: target NDK directory
553generate_api_level ()
554{
555    local API=$1
556    local ARCH=$2
557    local HEADER="platforms/android-$API/arch-$ARCH/usr/include/android/api-level.h"
558    log "Generating: $HEADER"
559    rm -f "$3/$HEADER"  # Remove symlink if any.
560    cat > "$3/$HEADER" <<EOF
561/*
562 * Copyright (C) 2008 The Android Open Source Project
563 * All rights reserved.
564 *
565 * Redistribution and use in source and binary forms, with or without
566 * modification, are permitted provided that the following conditions
567 * are met:
568 *  * Redistributions of source code must retain the above copyright
569 *    notice, this list of conditions and the following disclaimer.
570 *  * Redistributions in binary form must reproduce the above copyright
571 *    notice, this list of conditions and the following disclaimer in
572 *    the documentation and/or other materials provided with the
573 *    distribution.
574 *
575 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
576 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
577 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
578 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
579 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
580 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
581 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
582 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
583 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
584 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
585 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
586 * SUCH DAMAGE.
587 */
588#ifndef ANDROID_API_LEVEL_H
589#define ANDROID_API_LEVEL_H
590
591#define __ANDROID_API__ $API
592
593#endif /* ANDROID_API_LEVEL_H */
594EOF
595}
596
597# Copy platform sysroot and samples into your destination
598#
599
600# if $SRC/android-$PLATFORM/arch-$ARCH exists
601#   $SRC/android-$PLATFORM/include --> $DST/android-$PLATFORM/arch-$ARCH/usr/include
602#   $SRC/android-$PLATFORM/arch-$ARCH/include --> $DST/android-$PLATFORM/arch-$ARCH/usr/include
603#   $SRC/android-$PLATFORM/arch-$ARCH/lib --> $DST/android-$PLATFORM/arch-$ARCH/usr/lib
604#
605if [ -z "$OPTION_OVERLAY" ]; then
606    rm -rf $DSTDIR/platforms && mkdir -p $DSTDIR/platforms
607fi
608for ARCH in $ARCHS; do
609    # Find first platform for this arch
610    PREV_SYSROOT_DST=
611    for PLATFORM in $PLATFORMS; do
612        PLATFORM_DST=platforms/android-$PLATFORM   # Relative to $DSTDIR
613        PLATFORM_SRC=$PLATFORM_DST                 # Relative to $SRCDIR
614        SYSROOT_DST=$PLATFORM_DST/arch-$ARCH/usr
615        # Skip over if there is no arch-specific file for this platform
616        # and no destination platform directory was created. This is needed
617        # because x86 and MIPS don't have files for API levels 3-8.
618        if [ -z "$PREV_SYSROOT_DST" -a \
619           ! -d "$SRCDIR/$PLATFORM_SRC/arch-$ARCH" ]; then
620            log "Skipping: \$SRC/$PLATFORM_SRC/arch-$ARCH"
621            continue
622        fi
623
624        log "Populating \$DST/platforms/android-$PLATFORM/arch-$ARCH"
625
626        # If this is not the first destination directory, copy over, or
627        # symlink the files from the previous one now.
628        if [ "$PREV_SYSROOT_DST" ]; then
629            if [ "$OPTION_FAST_COPY" ]; then
630                log "Copying \$DST/$PREV_SYSROOT_DST to \$DST/$SYSROOT_DST"
631                copy_directory "$DSTDIR/$PREV_SYSROOT_DST" "$DSTDIR/$SYSROOT_DST"
632            else
633                log "Symlink-copying \$DST/$PREV_SYSROOT_DST to \$DST/$SYSROOT_DST"
634                symlink_src_directory $PREV_SYSROOT_DST $SYSROOT_DST
635            fi
636        fi
637
638        # If this is the first destination directory, copy the common
639        # files from previous platform directories into this one.
640        # This helps copy the common headers from android-3 to android-8
641        # into the x86 and mips android-9 directories.
642        if [ -z "$PREV_SYSROOT_DST" ]; then
643            for OLD_PLATFORM in $PLATFORMS; do
644                if [ "$OLD_PLATFORM" -eq "$PLATFORM" ]; then
645                    break
646                fi
647                copy_src_directory platforms/android-$OLD_PLATFORM/include \
648                                   $SYSROOT_DST/include \
649                                   "common android-$OLD_PLATFORM headers"
650            done
651        fi
652
653        # Now copy over all non-arch specific include files
654        copy_src_directory $PLATFORM_SRC/include $SYSROOT_DST/include "common system headers"
655        copy_src_directory $PLATFORM_SRC/arch-$ARCH/include $SYSROOT_DST/include "$ARCH system headers"
656
657        generate_api_level "$PLATFORM" "$ARCH" "$DSTDIR"
658
659        # If --minimal is not used, copy or generate binary files.
660        if [ -z "$OPTION_MINIMAL" ]; then
661            # Copy the prebuilt static libraries.
662            if [ "$ARCH" = "x86_64" ]; then
663            # We need full set for multilib compiler
664                copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib $SYSROOT_DST/lib "x86 sysroot libs"
665                copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib64 $SYSROOT_DST/lib64 "x86_64 sysroot libs"
666                copy_src_directory $PLATFORM_SRC/arch-$ARCH/libx32 $SYSROOT_DST/libx32 "x32 sysroot libs"
667            else
668                copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib $SYSROOT_DST/lib "$ARCH sysroot libs"
669            fi
670
671            # Generate C runtime object files when available
672            PLATFORM_SRC_ARCH=$PLATFORM_SRC/arch-$ARCH/src
673            if [ ! -d "$SRCDIR/$PLATFORM_SRC_ARCH" ]; then
674                PLATFORM_SRC_ARCH=$PREV_PLATFORM_SRC_ARCH
675            else
676                PREV_PLATFORM_SRC_ARCH=$PLATFORM_SRC_ARCH
677            fi
678
679            # Genreate crt objects for known archs
680            if [ "$(arch_in_unknown_archs $ARCH)" != "yes" ]; then
681               if [ "$ARCH" = "x86_64" ]; then
682               # We need full set for multilib compiler
683                 gen_crt_objects $PLATFORM $ARCH platforms/common/src $PLATFORM_SRC_ARCH $SYSROOT_DST/lib "-m32"
684                 gen_crt_objects $PLATFORM $ARCH platforms/common/src $PLATFORM_SRC_ARCH $SYSROOT_DST/lib64 "-m64"
685                 gen_crt_objects $PLATFORM $ARCH platforms/common/src $PLATFORM_SRC_ARCH $SYSROOT_DST/libx32 "-mx32"
686               else
687                 gen_crt_objects $PLATFORM $ARCH platforms/common/src $PLATFORM_SRC_ARCH $SYSROOT_DST/lib
688               fi
689            fi
690
691            # Generate shared libraries from symbol files
692            if [ "$ARCH" = "x86_64" ]; then
693               # We need full set for multilib compiler
694               gen_shared_libraries $ARCH $PLATFORM_SRC/arch-$ARCH/symbols $SYSROOT_DST/lib "-m32"
695               gen_shared_libraries $ARCH $PLATFORM_SRC/arch-$ARCH/symbols $SYSROOT_DST/lib64 "-m64"
696               gen_shared_libraries $ARCH $PLATFORM_SRC/arch-$ARCH/symbols $SYSROOT_DST/libx32 "-mx32"
697            else
698               gen_shared_libraries $ARCH $PLATFORM_SRC/arch-$ARCH/symbols $SYSROOT_DST/lib
699            fi
700        else
701            # Copy the prebuilt binaries to bootstrap GCC
702            if [ "$ARCH" = "x86_64" ]; then
703               # We need full set for multilib compiler
704               copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib-bootstrap/lib $SYSROOT_DST/lib "x86 sysroot libs (boostrap)"
705               copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib-bootstrap/lib64 $SYSROOT_DST/lib64 "x86_64 sysroot libs (boostrap)"
706               copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib-bootstrap/libx32 $SYSROOT_DST/libx32 "x32 sysroot libs (boostrap)"
707            else
708               copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib-bootstrap $SYSROOT_DST/lib "$ARCH sysroot libs (boostrap)"
709            fi
710        fi
711        PREV_SYSROOT_DST=$SYSROOT_DST
712    done
713done
714
715#
716# $SRC/android-$PLATFORM/samples --> $DST/samples
717#
718if [ "$OPTION_SAMPLES" ] ; then
719    # Copy platform samples and generic samples into your destination
720    #
721    # $SRC/samples/ --> $DST/samples/
722    # $SRC/android-$PLATFORM/samples/ --> $DST/samples
723    #
724    dump "Copying generic samples"
725    if [ -z "$OPTION_OVERLAY" ]; then
726        rm -rf $DSTDIR/samples && mkdir -p $DSTDIR/samples
727    fi
728    copy_src_directory  samples samples samples
729
730    for PLATFORM in $PLATFORMS; do
731        dump "Copy android-$PLATFORM samples"
732        # $SRC/platform-$PLATFORM/samples --> $DST/samples
733        copy_src_directory platforms/android-$PLATFORM/samples samples samples
734    done
735
736    # Cleanup generated files in samples
737    rm -rf "$DSTDIR/samples/*/obj"
738    rm -rf "$DSTDIR/samples/*/libs"
739fi
740
741if [ "$PACKAGE_DIR" ]; then
742    mkdir -p "$PACKAGE_DIR"
743    fail_panic "Could not create package directory: $PACKAGE_DIR"
744    ARCHIVE=platforms.tar.bz2
745    dump "Packaging $ARCHIVE"
746    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$DSTDIR" "platforms"
747    fail_panic "Could not package platforms"
748    if [ "$OPTION_SAMPLES" ]; then
749        ARCHIVE=samples.tar.bz2
750        dump "Packaging $ARCHIVE"
751        pack_archive "$PACKAGE_DIR/$ARCHIVE" "$DSTDIR" "samples"
752        fail_panic "Could not package samples"
753    fi
754fi
755
756log "Done !"
757