1#!/bin/bash -e
2
3# List of files required in output. Every other file generated will be skipped.
4OUTFILES=(
5  com.android.apex.test.bar_stripped.v1.libvX.apex
6  com.android.apex.test.bar_stripped.v2.libvY.apex
7  com.android.apex.test.bar.v1.libvX.apex
8  com.android.apex.test.bar.v2.libvY.apex
9  com.android.apex.test.baz_stripped.v1.libvX.apex
10  com.android.apex.test.foo_stripped.v1.libvX.apex
11  com.android.apex.test.foo_stripped.v2.libvY.apex
12  com.android.apex.test.foo.v1.libvX.apex
13  com.android.apex.test.foo.v2.libvY.apex
14  com.android.apex.test.pony_stripped.v1.libvZ.apex
15  com.android.apex.test.pony.v1.libvZ.apex
16  com.android.apex.test.sharedlibs_generated.v1.libvX.apex
17  com.android.apex.test.sharedlibs_generated.v2.libvY.apex
18  com.android.apex.test.sharedlibs_secondary_generated.v1.libvZ.apex
19)
20
21# "apex" type build targets to build.
22APEX_TARGETS=(
23  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.bar:com.android.apex.test.bar
24  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.foo:com.android.apex.test.foo
25  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.pony:com.android.apex.test.pony
26)
27
28# "genrule" type build targets to build, and directory they are built from.
29GENRULE_TARGETS=(
30  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.bar:com.android.apex.test.bar_stripped
31  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.baz:com.android.apex.test.baz_stripped
32  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.foo:com.android.apex.test.foo_stripped
33  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.pony:com.android.apex.test.pony_stripped
34  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.sharedlibs:com.android.apex.test.sharedlibs_generated
35  system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.sharedlibs_secondary:com.android.apex.test.sharedlibs_secondary_generated
36)
37
38if [ ! -e "build/make/core/Makefile" ]; then
39    echo "$0 must be run from the top of the tree"
40    exit 1
41fi
42
43OUT_DIR=$(source build/envsetup.sh > /dev/null; TARGET_PRODUCT= get_build_var OUT_DIR)
44DIST_DIR=$(source build/envsetup.sh > /dev/null; TARGET_PRODUCT= get_build_var DIST_DIR)
45TMPDIR=$(source build/envsetup.sh > /dev/null; TARGET_PRODUCT= get_build_var TMPDIR)
46
47manifestdirs=()
48
49for t in "${APEX_TARGETS[@]}" "${GENRULE_TARGETS[@]}"; do
50    IFS=: read -a ar <<< "${t}"
51    manifestdirs+=( ${ar[0]})
52done
53
54manifestdirs=($(printf "%s\n" "${manifestdirs[@]}" | sort -u))
55
56generated_artifacts=()
57
58archs=(
59  arm
60  arm64
61  x86
62  x86_64
63)
64
65apexversions=(
66  1
67  2
68)
69
70libversions=(
71  X
72  Y
73  Z
74)
75
76for arch in "${archs[@]}"; do
77    for apexversion in "${apexversions[@]}"; do
78        apexfingerprint="VERSION_${apexversion}"
79        sed -i "s/#define FINGERPRINT .*/#define FINGERPRINT \"${apexfingerprint}\"/g" \
80        system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.bar/bar_test.cc \
81        system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.baz/baz_test.cc \
82        system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.foo/foo_test.cc \
83        system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.pony/pony_test.cc
84
85        for d in "${manifestdirs[@]}"; do
86            sed -i "s/  \"version\": .*/  \"version\": ${apexversion}/g" \
87            ${d}/manifest.json
88        done
89        for libversion in "${libversions[@]}"; do
90            # Check if we need to build this combination of versions.
91            found=n
92            for t in "${APEX_TARGETS[@]}" "${GENRULE_TARGETS[@]}"; do
93                IFS=: read -a ar <<< "${t}"
94                outfile=${ar[1]}.v${apexversion}.libv${libversion}.apex
95                if printf '%s\n' "${OUTFILES[@]}" | grep -q -F "${outfile}"; then
96                    found=y
97                    break
98                fi
99            done
100            if [ "${found}" != "y" ]; then
101                # Skipping this combination.
102                continue
103            fi
104
105            echo "Building combination arch: ${arch}, apexversion: ${apexversion}, libversion: ${libversion}"
106            libfingerprint="VERSION_${libversion}"
107            sed -i "s/#define FINGERPRINT .*/#define FINGERPRINT \"${libfingerprint}\"/g" \
108            system/apex/tests/testdata/sharedlibs/build/sharedlibstest.cpp
109
110            build/soong/soong_ui.bash \
111                --make-mode \
112                TARGET_PRODUCT=aosp_${arch} \
113                dist sharedlibs_test
114
115            for t in "${APEX_TARGETS[@]}" "${GENRULE_TARGETS[@]}"; do
116                IFS=: read -a ar <<< "${t}"
117                outfile=${ar[1]}.v${apexversion}.libv${libversion}.apex
118                if printf '%s\n' "${OUTFILES[@]}" | grep -q -P "^${outfile}\$"; then
119                    cp -v \
120                    "${DIST_DIR}"/"${ar[1]}".apex \
121                    system/apex/tests/testdata/sharedlibs/prebuilts/${arch}/${outfile}
122                    generated_artifacts+=(system/apex/tests/testdata/sharedlibs/prebuilts/${arch}/${outfile})
123                fi
124            done
125        done
126    done
127done
128
129# Generate the Android.bp file for the prebuilts.
130tmpfile=$(mktemp)
131
132cat > "${tmpfile}" << EOF
133// Copyright (C) 2020 The Android Open Source Project
134//
135// Licensed under the Apache License, Version 2.0 (the "License");
136// you may not use this file except in compliance with the License.
137// You may obtain a copy of the License at
138//
139//     http://www.apache.org/licenses/LICENSE-2.0
140//
141// Unless required by applicable law or agreed to in writing, software
142// distributed under the License is distributed on an "AS IS" BASIS,
143// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144// See the License for the specific language governing permissions and
145// limitations under the License.
146
147// This file is auto-generated by
148// ./system/apex/tests/testdata/sharedlibs/build/build_artifacts.sh
149// Do NOT edit manually.
150
151package {
152    default_applicable_licenses: ["Android-Apache-2.0"],
153}
154EOF
155
156artifacts_filenames=()
157for artifact in "${generated_artifacts[@]}"; do
158    artifacts_filenames+=($(basename ${artifact}))
159done
160
161artifacts_filenames=($(printf '%s\n' "${artifacts_filenames[@]}" | sort -u))
162
163for artifact in "${artifacts_filenames[@]}"; do
164    outfile=$(basename "${artifact}")
165    # remove .apex suffix
166    rulename=${outfile%.apex}
167
168    cat >> "${tmpfile}" << EOF
169
170prebuilt_apex {
171  name: "${rulename}_prebuilt",
172  arch: {
173EOF
174
175    for arch in "${archs[@]}"; do
176        cat >> "${tmpfile}" << EOF
177    ${arch}: {
178      src: "${arch}/${outfile}",
179    },
180EOF
181    done
182
183    cat >> "${tmpfile}" << EOF
184  },
185  filename: "${outfile}",
186  installable: false,
187}
188EOF
189done
190
191mv "${tmpfile}" system/apex/tests/testdata/sharedlibs/prebuilts/Android.bp
192
193# Restore the default version string to avoid bogus diffs.
194sed -i "s/#define FINGERPRINT .*/#define FINGERPRINT \"VERSION_XXX\"/g" \
195system/apex/tests/testdata/sharedlibs/build/sharedlibstest.cpp \
196system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.bar/bar_test.cc \
197system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.baz/baz_test.cc \
198system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.foo/foo_test.cc \
199system/apex/tests/testdata/sharedlibs/build/com.android.apex.test.pony/pony_test.cc
200
201for d in "${manifestdirs[@]}"; do
202    sed -i "s/  \"version\": .*/  \"version\": 1/g" \
203    ${d}/manifest.json
204done
205
206ls -l "${generated_artifacts[@]}"
207