1#! /bin/bash
2# Copyright (c) 2016, Intel Corporation
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without modification,
6# are permitted provided that the following conditions are met:
7#
8# 1. Redistributions of source code must retain the above copyright notice, this
9# list of conditions and the following disclaimer.
10#
11# 2. Redistributions in binary form must reproduce the above copyright notice,
12# this list of conditions and the following disclaimer in the documentation and/or
13# other materials provided with the distribution.
14#
15# 3. Neither the name of the copyright holder nor the names of its contributors
16# may be used to endorse or promote products derived from this software without
17# specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30cd "$ANDROID_BUILD_TOP"
31. build/envsetup.sh
32cd -
33
34set -eo pipefail
35
36# INSTRUCTIONS
37#
38# Prior to running this script you should git-rm the previous content of the
39# directory pointed to by the "asio" symbolic link, extract the version you
40# want to integrate and finally, make the "asio" symbolic link point to the
41# extracted directory. Additionaly, the build environment must be ready; i.e.
42# you must have sourced "build/envestup.sh" and lunched any target.
43#
44# Then, run this script by passing it a list of lunch targets. ASIO will be
45# minified based on which files were used when building the Parameter Framework
46# on those lunch targets. As results may very among targets, it is advised to
47# pass lunch targets of various architectures (ideally, all lunch targets).
48
49# USAGE
50# See the usage() function below
51
52usage () {
53    echo "run from the Parameter Framework's git root"
54    echo "\tsupport/android/asio/asio_shrinker.sh <lunch target> [more lunch targets]"
55    exit 1
56}
57
58fail () {
59    code=$?
60    echo "Asio shrinker error: $1"
61    exit $code
62}
63
64list_compiled_files () {
65    directory=$1
66    output=$2
67
68    # list all files used during compilation. Ignore "grep" errors: some .P
69    # files may well not contain any "asio" line.
70    find "$directory/obj" -name "*.P" | \
71        xargs grep --no-filename 'external/parameter-framework/asio' >> "$output" || true
72}
73
74if [ $# -eq 0 ]; then
75    echo "Not enough arguments."
76    usage
77fi
78
79# This script must be run from the Parameter Framework's git root.
80cd asio
81
82asio_includes=$(mktemp)
83find include -type f -name '*.[ih]pp' > "$asio_includes"
84
85# unifdef can't parse multi-line macros. Help him out by removing the wrapping
86xargs sed -i -e :a -e '/\\$/N' -e 's@\\ *\n@ @' -e ta < "$asio_includes"
87
88# apply macro definitions
89# "-x 2" means: only exit with a code != 0 if an error happened
90# "-m" means: work on multiple files
91# -f" means: read define and undef directives from the following file
92xargs unifdef -x 2 -m -f ../support/android/asio/asio_defines.txt < "$asio_includes"
93rm "$asio_includes"
94
95
96# Take a list of lunch targets as argument and run "mma" on all of them.
97# Why? because it seems that different target architectures require different
98# sets of files
99pushd ..
100tmpfile=$(mktemp)
101for target in "$@"; do
102    lunch $target || fail "Failed to lunch $target"
103
104    # compile and list the source files that actually have been used
105    mma -j$(nproc) || fail "Failed to build $target"
106    list_compiled_files "$ANDROID_PRODUCT_OUT" $tmpfile
107done
108popd
109list_compiled_files "$ANDROID_HOST_OUT" $tmpfile
110
111# In .P files, a line may contain several entries and may be terminated by " \"
112# or " :" or nothing. Let's make sure we have one entry per line.
113sed -r -e 's@^ *([^:\\]*)( [:\\])?$@\1@' \
114    -e 's@^external/parameter-framework/asio/@@g' \
115    -e 's@ @\n@' $tmpfile | \
116        sort -u | \
117        xargs git add || fail "Failed to git-add some necessary ASIO headers"
118rm $tmpfile
119
120# Add copyright mentions and readmes
121git add COPYING LICENSE_1_0.txt README
122# Remove asio.hpp because we override it
123git rm -f include/asio.hpp || true # it may already have been removed
124
125cat > README.parameter-framework << __EOF__
126This version of ASIO has been minified for lower disk footprint for Android
127integraton by support/android/asio/asio_shrinker.sh.
128
129Although unlikely (thanks to having been tested on all AOSP lunch targets), if
130the Parameter Framework fails to compile on your lunch target because of a
131missing ASIO header, you may download a vanilla ASIO version from
132'http://sourceforge.net/projects/asio/files/asio/1.10.6%20%28Stable%29/' and
133run that script anew.
134__EOF__
135git add README.parameter-framework
136