1# Special script used to check that LOCAL_SHORT_COMMANDS works
2# correctly even when using a very large number of source files
3# when building a static or shared library.
4#
5# We're going to auto-generate all the files we need in a
6# temporary directory, because that's how we roll.
7#
8
9PROGDIR=$(dirname $0)
10PROGDIR=$(cd "$PROGDIR" && pwd)
11
12# TODO: Increment this to 1000 for long test runs. For the simple unit test
13#        suite, keep this just over 100
14#
15COUNT=110
16
17# Generate all our source files
18NUM=0
19
20SRCDIR=$PROGDIR/jni
21rm -rf "$SRCDIR" && mkdir -p "$SRCDIR"
22if [ $? != 0 ]; then
23    echo "ERROR: Could not create temporary source directory: $SRCDIR"
24    exit 1
25fi
26
27CLEAN_FILES=
28
29generate_source_files ()
30{
31    # Generate all temporary source files we need
32    local NUM=0
33    while [ $NUM -lt $COUNT ]; do
34        SRCFILE=$SRCDIR/foo$NUM.c
35    cat > $SRCFILE <<EOF
36int foo$NUM (int x)
37{
38    return x + 1;
39}
40EOF
41        NUM=$(( $NUM + 1 ))
42        CLEAN_FILES=$CLEAN_FILES" $SRCFILE"
43    done
44}
45
46generate_main_file ()
47{
48    cat > $SRCDIR/main.c <<EOF
49#include <stdio.h>
50EOF
51    NUM=0
52    while [ $NUM -lt $COUNT ]; do
53        cat >> $SRCDIR/main.c <<EOF
54extern int foo$NUM (int);
55EOF
56        NUM=$(( $NUM + 1 ))
57    done
58    cat >> $SRCDIR/main.c <<EOF
59int main(void)
60{
61    int x = 0;
62EOF
63    NUM=0
64    while [ $NUM -lt $COUNT ]; do
65        cat >> $SRCDIR/main.c <<EOF
66    x = foo$NUM(x);
67EOF
68        NUM=$(( $NUM + 1 ))
69    done
70cat >> $SRCDIR/main.c <<EOF
71    return 0;
72}
73EOF
74    CLEAN_FILES=$CLEAN_FILES" $SRCDIR/main.c"
75}
76
77generate_build_file ()
78{
79    local NUM
80
81    # Generate the Android.mk
82    cat > $SRCDIR/Android.mk <<EOF
83# Auto-generated - do not edit
84LOCAL_PATH := \$(call my-dir)
85EOF
86
87    # First, build a huge static library with all the files
88    cat >> $SRCDIR/Android.mk <<EOF
89include \$(CLEAR_VARS)
90LOCAL_MODULE := libfoo_big
91EOF
92
93    NUM=0
94    while [ $NUM -lt $COUNT ]; do
95        cat >> $SRCDIR/Android.mk <<EOF
96LOCAL_SRC_FILES += foo$NUM.c
97EOF
98        NUM=$(( $NUM + 1 ))
99    done
100
101    cat >> $SRCDIR/Android.mk <<EOF
102LOCAL_SHORT_COMMANDS := true
103include \$(BUILD_SHARED_LIBRARY)
104EOF
105
106    # Second, generate a large number of static libraries
107    # Then an executable that use them all
108    NUM=0
109    while [ $NUM -lt $COUNT ]; do
110        cat >> $SRCDIR/Android.mk <<EOF
111include \$(CLEAR_VARS)
112LOCAL_MODULE := foo$NUM
113LOCAL_SRC_FILES := foo$NUM.c
114include \$(BUILD_STATIC_LIBRARY)
115EOF
116        NUM=$(( $NUM + 1 ))
117    done
118
119        cat >> $SRCDIR/Android.mk <<EOF
120include \$(CLEAR_VARS)
121LOCAL_MODULE := test_linker_options_list
122LOCAL_SRC_FILES := main.c
123EOF
124
125    NUM=0
126    while [ $NUM -lt $COUNT ]; do
127        cat >> $SRCDIR/Android.mk <<EOF
128LOCAL_WHOLE_STATIC_LIBRARIES += foo$NUM
129EOF
130        NUM=$(( $NUM + 1 ))
131    done
132
133    cat >> $SRCDIR/Android.mk <<EOF
134#LOCAL_SHORT_COMMANDS := true
135include \$(BUILD_EXECUTABLE)
136EOF
137    CLEAN_FILES=$CLEAN_FILES" $SRCDIR/Android.mk"
138}
139
140generate_source_files &&
141generate_main_file &&
142generate_build_file
143if [ $? != 0 ]; then
144    echo "ERROR: Could not generate files for this test!"
145    exit 1
146fi
147
148# Now run the build
149$NDK/ndk-build -C "$PROGDIR" "$@"
150RET=$?
151
152# check if linker.list is empty
153ALL_LINKER_LIST=`find obj -name linker.list`
154for LINKER_LIST in $ALL_LINKER_LIST; do
155    if [ ! -s $LINKER_LIST ]; then
156        echo "ERROR: linker list file $LINKER_LIST isn't generated!"
157        exit 1
158    fi
159done
160
161# Clean everything we generated
162rm -f $CLEAN_FILES
163rm -rf "$PROGDIR/obj" "$PROGDIR/libs"
164rm -rf jni
165
166# Exit
167exit $RET
168