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# dev-platform-expand.sh
18#
19# This tool is used to expand the content of development/ndk/platforms
20# into a _single_ directory containing all headers / libraries corresponding
21# to a given API level and architecture.
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# If we target level $API, we're going to copy the contents of android-3 to
33# android-$API to the destination directory.
34#
35
36. `dirname $0`/prebuilt-common.sh
37
38# Return the list of platform supported from $1/platforms
39# as a single space-separated list of levels. (e.g. "3 4 5 8 9")
40# $1: source directory
41extract_platforms_from ()
42{
43    local RET
44    if [ -d "$1" ] ; then
45        RET=$((cd "$1/platforms" && ls -d android-*) | sed -e "s!android-!!" | sort -g)
46    else
47        RET=""
48    fi
49    RET=$(echo $RET)  # converts newlines to spaces
50    echo $RET
51}
52
53# The default platform is the last entry in the API_LEVELS default variable
54PLATFORM=android-$(echo $API_LEVELS | tr ' ' '\n' | tail -1)
55register_var_option "--platform=<level>" PLATFORM "Target API level"
56
57# We take by default stuff from $NDK/../development/ndk
58SRCDIR="$(cd $ANDROID_NDK_ROOT/../development/ndk/platforms && pwd)"
59register_var_option "--src-dir=<path>" SRCDIR "Source for compressed platforms"
60
61# The default destination directory is a temporary one
62DSTDIR=$TMPDIR/platforms
63register_var_option "--dst-dir=<path>" DSTDIR "Destination directory"
64
65# Default architecture, note we can have several ones here
66ARCHS="$DEFAULT_ARCHS"
67register_var_option "--arch=<name>" ARCHS "List of target architectures"
68
69PROGRAM_PARAMETERS=""
70
71PROGRAM_DESCRIPTION=\
72"Uncompress the platform files (headers/libraries) correspond to a given
73platform into a single directory. The main idea is that the files are stored
74in a platform-specific way under SRC=$$NDK/../development/ndk, i.e.:
75
76  \$SRC/platforms/android-3/  -> all files corresponding to API level 3
77  \$SRC/platforms/android-4/  -> only new/modified files corresponding to API level 4
78  \$SRC/platforms/android-5/  -> only new/modified files corresponding to API level 5
79  ...
80
81As an example, expanding android-5 would mean:
82
83  1 - copy all files from android-3 to \$DST directory
84
85  2 - copy all files from android-4 to \$DST, eventually overwriting stuff
86      from android-3 that was modified in API level 4
87
88  3 - copy all files from android-5 to \$DST, eventually overwriting stuff
89      from android-4 that was modified in API level 5
90
91The script 'dev-platform-compress.sh' can be used to perform the opposite
92operation, and knows which files are part of which API level.
93"
94
95extract_parameters "$@"
96
97# Check source directory
98if [ ! -d "$SRCDIR" ] ; then
99    echo "ERROR: Source directory doesn't exist: $SRCDIR"
100    exit 1
101fi
102if [ ! -d "$SRCDIR/android-3" ]; then
103    echo "ERROR: Source directory doesn't seem to be valid: $SRCDIR"
104    exit 1
105fi
106log "Using source directory: $SRCDIR"
107
108# Check platform (normalize it, i.e. android-9 -> 9}
109PLATFORM=${PLATFORM##android-}
110if [ ! -d "$SRCDIR/android-$PLATFORM" ]; then
111    echo "ERROR: Platform directory doesn't exist: $SRCDIR/android-$PLATFORM"
112    exit 1
113fi
114log "Using platform: $PLATFORM"
115
116if [ ! -d "$DSTDIR" ]; then
117    mkdir -p "$DSTDIR"
118    if [ $? != 0 ]; then
119        echo "ERROR: Could not create destination directory: $DSTDIR"
120        exit 1
121    fi
122fi
123log "Using destination directory: $DSTDIR"
124
125# Handle architecture list
126#
127# We support both --arch and --abi for backwards compatibility reasons
128# --arch is the new hotness, --abi is deprecated.
129#
130if [ -n "$OPTION_ARCH" ]; then
131    OPTION_ARCH=$(commas_to_spaces $OPTION_ARCH)
132fi
133
134if [ -n "$OPTION_ABI" ] ; then
135    echo "WARNING: --abi=<names> is deprecated. Use --arch=<names> instead!"
136    OPTION_ABI=$(commas_to_spaces $OPTION_ABI)
137    if [ -n "$OPTION_ARCH" -a "$OPTION_ARCH" != "$OPTION_ABI" ]; then
138        echo "ERROR: You can't use both --abi and --arch with different values!"
139        exit 1
140    fi
141    OPTION_ARCH=$OPTION_ABI
142fi
143
144ARCHS=$(commas_to_spaces $ARCHS)
145log "Using architectures: $(commas_to_spaces $ARCHS)"
146
147# log "Checking source platform architectures."
148# BAD_ARCHS=
149# for ARCH in $ARCHS; do
150#     eval CHECK_$ARCH=no
151# done
152# for ARCH in $ARCHS; do
153#     DIR="$SRCDIR/android-$PLATFORM/arch-$ARCH"
154#     if [ -d $DIR ] ; then
155#         log "  $DIR"
156#         eval CHECK_$ARCH=yes
157#     fi
158# done
159#
160# BAD_ARCHS=
161# for ARCH in $ARCHS; do
162#     CHECK=`var_value CHECK_$ARCH`
163#     log "  $ARCH check: $CHECK"
164#     if [ "$CHECK" = no ] ; then
165#         if [ -z "$BAD_ARCHS" ] ; then
166#             BAD_ARCHS=$ARCH
167#         else
168#             BAD_ARCHS="$BAD_ARCHS $ARCH"
169#         fi
170#     fi
171# done
172#
173# if [ -n "$BAD_ARCHS" ] ; then
174#     echo "ERROR: Source directory doesn't support these ARCHs: $BAD_ARCHS"
175#     exit 3
176# fi
177
178copy_optional_directory ()
179{
180    if [ -d "$1" ]; then
181        copy_directory "$1" "$2"
182    fi
183}
184
185# Copy platform sysroot into your destination
186#
187
188# $SRC/android-$PLATFORM/include --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr/include
189# $SRC/android-$PLATFORM/arch-$ARCH/include --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr/include
190# for compatibility:
191# $SRC/android-$PLATFORM/arch-$ARCH/usr/include --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr/include
192
193
194
195# $SRC/android-$PLATFORM/arch-$ARCH/usr --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr
196#
197for LEVEL in $API_LEVELS; do
198    if [ "$LEVEL" -gt "$PLATFORM" ]; then
199        break
200    fi
201    log "Copying android-$LEVEL platform files"
202    for ARCH in $ARCHS; do
203        SDIR="$SRCDIR/android-$LEVEL"
204        DDIR="$DSTDIR/android-$PLATFORM"
205        if [ -d "$SDIR" ]; then
206            copy_directory "$SDIR/include" "$DDIR/include"
207        fi
208        ARCH_SDIR="$SDIR/arch-$ARCH"
209        ARCH_DDIR="$DDIR/arch-$ARCH"
210        if [ -d "$ARCH_SDIR" ]; then
211            copy_optional_directory "$ARCH_SDIR/include" "$ARCH_DDIR/include"
212            copy_optional_directory "$ARCH_SDIR/lib"     "$ARCH_DDIR/lib"
213            rm -f "$ARCH_DDIR"/lib/*.so
214            copy_optional_directory "$ARCH_SDIR/symbols" "$ARCH_DDIR/symbols"
215            rm -f "$ARCH_DDIR"/symbols/*.so.txt
216        fi
217    done
218done
219
220log "Done !"
221exit 0
222