aboutsummaryrefslogtreecommitdiff
path: root/build-scripts/build-android
blob: f1cd367f24c9c727a5507c56838574609824ba6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
###############################################################################
# Copyright (c) 2014 Linaro
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
###############################################################################

export PATH=$PATH:~/bin/
set -o errtrace

git config --global user.email "android-build-bot@fake-email.com"
git config --global user.name "android-build-bot"

source "${BUILD_SCRIPT_ROOT}"/helpers

trap infrastructure_error ERR

if [ -z "${TARGET_OUT_DIR}" ]; then
    # only set TARGET_OUT_DIR when it is not set
    # Set TARGET_OUT_DIR since few TARGET_PRODUCT include "full_" keyword as well
    export TARGET_OUT_DIR=`echo $TARGET_PRODUCT | sed 's/_/\n/;s/.*\n//'`
fi

if [ -n "$BUILD_COPYCAT" ]; then

    download_another_build "$BUILD_COPYCAT"

else

    # Download overlays ASAP for fail-fast approach
    if [ -n "$SOURCE_OVERLAY" ]; then
        download_overlays "$SOURCE_OVERLAY"
    fi

    # Select JAVA version based on Android version. Android 4.4 < builds with JAVA6
    # AOSP master and future build will use java 1.7 openjdk.
    # Step 1/2 (fail fast)
    if [ -n "$USE_JAVA_6" ]; then
        sudo update-java-alternatives --set java-6-sun || true
    elif [ -n "$USE_JAVA_7" ]; then
        sudo update-java-alternatives --set java-1.7.0-openjdk-amd64 || true
    fi

    java -version

    # Download toolchain, fail-fast
    if test -n "$TOOLCHAIN_URL"; then
        wget -nv --no-check-certificate $TOOLCHAIN_URL
    fi

    # Remove any old local manifest and manifest which can break repo init if
    # duplicate repository are found or forceful manifest update
    rm -rf .repo/local_manifests .repo/manifest*

    if [ -n "$REPO_SEED_URL" ]; then
        repo-sync-from-seed "${1}"
    else
        repo-sync-from-mirror "${1}"
    fi

    if [ -n "$GERRIT_PROJECT" ] && [ $GERRIT_PROJECT != "android-build-configs" ] && [ $GERRIT_EVENT_TYPE == "comment-added" ]; then
        if [ ${GERRIT_HOST} == "dev-private-review.linaro.org" ]; then
            GERRIT_URL="ssh://git@${GERRIT_HOST}/${GERRIT_PROJECT}"
        else
            GERRIT_URL="http://${GERRIT_HOST}/${GERRIT_PROJECT}"
        fi
        if [ "$GERRIT_PROJECT" == "platform/manifest" ] || [ "$GERRIT_PROJECT" == "linaro-art/platform/manifest" ]; then
            cd .repo/local_manifests;
            git pull ${GERRIT_URL} $GERRIT_REFSPEC
            cd -
            repo sync -j16
        else
            cd `grep -rni $GERRIT_PROJECT\" .repo/local_manifests | grep -Po 'path="\K[^"]*'`
            if git pull ${GERRIT_URL} ${GERRIT_REFSPEC} | grep -q "Automatic merge failed"; then
                git reset --hard
                echo "Error: *** Error patch merge failed"
                exit 1
            fi
            cd -
        fi
    fi

    if [ -n "$PATCHSETS" ]; then
        for i in $PATCHSETS; do
            sh ./android-patchsets/$i
        done
    fi

    if [ -n "$POST_CHECKOUT_HOOK" ]; then
    #    if echo "$POST_CHECKOUT_HOOK" | grep -F / >/dev/null; then
    #        echo "Invalid POST_CHECKOUT_HOOK: $POST_CHECKOUT_HOOK"
    #        exit 1
    #    fi
        $BUILD_SCRIPT_ROOT/$POST_CHECKOUT_HOOK
    fi

    # Generate a Changelog describing additional patches applied to AOSP codebase
    export android_version=`grep -rni "refs/tags" .repo/manifest.xml  | sed -e "s/.*tags\///" -e "s/\"//g"`
    repo forall -c 'if git rev-parse $android_version >/dev/null 2>&1
        then
        patches=`git log --oneline --no-merges $android_version..HEAD | wc -l`
        if [ $patches -ne 0 ]; then
             git_remote=`git remote -v | grep "fetch" | cut -f2 | cut -d " " -f1`
             echo "====================================================================="
             echo "Patchsets applied to $git_remote on top of $android_version"
             git diff --stat $android_version HEAD
             git log --oneline --no-merges $android_version..HEAD
             echo ""
       fi;
    fi' | cat > temp_changelog.txt
    files_changed=`add_changes \`grep -e "file changed" -e "files changed" temp_changelog.txt | cut -d " " -f2\``
    insertions=`add_changes \`grep -e "file changed" -e "files changed" temp_changelog.txt | cut -d " " -f5\``
    deletions=`add_changes \`grep -e "file changed" -e "files changed" temp_changelog.txt | cut -d " " -f7\``
    echo "=====================================================================" > changelog.txt
    echo "=====================================================================" >> changelog.txt
    echo "$files_changed files changed, $insertions insertions(+), $deletions deletions(-)" >> changelog.txt
    echo "=====================================================================" >> changelog.txt
    echo "=====================================================================" >> changelog.txt
    echo "" >> changelog.txt
    cat temp_changelog.txt >> changelog.txt
    trap - ERR
    . build/envsetup.sh
    trap infrastructure_error ERR

    [ -n "$LUNCH" ] && lunch "$LUNCH"

    # We evaluate the configuration again because envsetup.sh might well
    # have stomped on some of the values in it.

    set -a
    eval $CONFIGURATION
    set +a

#    export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.22/
#    export ANDROID_JAVA_HOME=$JAVA_HOME

    # Select JAVA version based on Android version. Android 4.4 < builds with JAVA6
    # AOSP master and future build will use java 1.7 openjdk.
    # Step 2/2
#    if [ -n "$USE_JAVA_6" ]; then
#        export JAVA_HOME=/usr/lib/jvm/java-6-sun/
#        export ANDROID_JAVA_HOME=$JAVA_HOME
#    elif [ -n "$USE_JAVA_7" ]; then
#        export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64/
#        export ANDROID_JAVA_HOME=$JAVA_HOME
#    fi

    MAKE_TARGETS="${MAKE_TARGETS-boottarball systemtarball userdatatarball}"
    calc_make_jobs

    if test -n "$TOOLCHAIN_URL"; then
        toolchain_filename=`echo $TOOLCHAIN_URL | sed -e 's/.*\/\([^/]*\)$/\1/'`
        mkdir toolchain
        tar -C toolchain --strip-components 1 -xf $toolchain_filename
        if test -z "$TOOLCHAIN_TRIPLET"; then
                # Android 2.x usually needs to be built with arm-eabi,
                # Android 4.x usually needs to be built with arm-linux-androideabi...
                if echo $MANIFEST_BRANCH |grep -qE 'android.2\.3'; then
                        p=`find $PWD/toolchain/bin/arm-eabi-gcc 2>/dev/null |head -n1`
                else
                        p=`find $PWD/toolchain/bin/arm-linux-androideabi-gcc 2>/dev/null |head -n1`
                fi
                [ -z "$p" ] && p=`find $PWD/toolchain/bin/*-gcc |head -n1`
        else
                p=`find $PWD/toolchain/bin/$TOOLCHAIN_TRIPLET-gcc |head -n1`
        fi
        export TARGET_TOOLS_PREFIX=`echo $p | sed -e 's/-gcc$/-/'`
    fi

    if [ "$TOOLCHAIN_FINGERPRINT" != "0" -a -n "$toolchain_filename" ]; then
        mkdir -p fingerprints
        cp $toolchain_filename fingerprints/
    fi

    if test -z "$TARGET_ARCH_VARIANT" -a -z "$LUNCH"; then
        TARGET_ARCH_VARIANT=armv7-a-neon
    fi

    SHOWCOMMANDS=
    if ! test -z "$QUIET" && test "$QUIET" = "1"; then
        SHOWCOMMANDS=showcommands
    fi

    if [ -n "$SOURCE_OVERLAY" ]; then
        unpack_overlays "$SOURCE_OVERLAY"
    fi

    if [ -n "$EXTERNAL_TARBALL" ]; then
        unpack_external_tarball "$EXTERNAL_TARBALL"
    fi

    if [ -n "$CLANG_TOOLCHAIN_URL" ]; then
        unpack_external_tarball "$CLANG_TOOLCHAIN_URL"
        # Adapt to the aosp toolchain directory hierarchy - compile error otherwise
        # ANDROID_CLANGVER=$(gawk '{ if ($1 == "LLVM_PREBUILTS_VERSION") print $3 }' $PWD/build/core/clang/versions.mk)
        ANDROID_CLANGVER=$(gawk '{ if ($1 == "ClangDefaultVersion") print $3 }' $PWD/build/soong/cc/config/global.go)
        ANDROID_CLANGVER=$(echo ${ANDROID_CLANGVER//'"'})
        [ -z $ANDROID_CLANGVER ] && ANDROID_CLANGVER=clang-4053586 # aosp prebuilt build number at 2017/10/11 master
        mkdir -p $PWD/prebuilts/clang/host/linux-x86/clang-master
        cp -r external_tarballs/$(basename ${CLANG_TOOLCHAIN_URL} .tar.xz)/* $PWD/prebuilts/clang/host/linux-x86/clang-master/

        export LLVM_PREBUILTS_VERSION=clang-master
	export LLVM_RELEASE_VERSION=$(ls $PWD/prebuilts/clang/host/linux-x86/clang-master/lib64/clang/)

        # ********** FIXME **********
        # All the files being copied from the AOSP clang build
        # should really be part of the toolchain tarball.
        # Not done currently because they aren't generated by upstream
        # clang's build process.
        for i in test; do
          ln -s $PWD/prebuilts/clang/host/linux-x86/$ANDROID_CLANGVER/${i} $PWD/prebuilts/clang/host/linux-x86/clang-master/${i}
        done
        # This catches crosscompiled compiler-rt versions, libFuzzer,
        # as well as symbol versioning files
        for i in $PWD/prebuilts/clang/host/linux-x86/$ANDROID_CLANGVER/lib64/clang/*/lib/linux/*; do
          [ -e "$PWD/prebuilts/clang/host/linux-x86/clang-master/lib64/clang/$LLVM_RELEASE_VERSION/lib/linux/$(basename $i)" ] || cp -a $i $PWD/prebuilts/clang/host/linux-x86/clang-master/lib64/clang/$LLVM_RELEASE_VERSION/lib/linux/
        done
    fi

    if [ -x "${TARGET_TOOLS_PREFIX}gcc" ]; then
        ${TARGET_TOOLS_PREFIX}gcc -v
    fi

    if [ -z "$MAKE" ]; then
        MAKE=make
    fi

#    if ! is_on_ec2; then
#        export USE_CCACHE=1
#        export CCACHE_DIR=$PWD/.ccache
#        if [ ! -e ".ccache" ]; then
#            ./prebuilts/misc/linux-x86/ccache/ccache -M 25G
#        fi
#    fi

    trap - ERR
    export TIMEFORMAT="TIME: Compilation: %lR"
    time $MAKE $MAKE_OPTS -j$MAKE_JOBS $MAKE_TARGETS $SHOWCOMMANDS USE_CLANG_PLATFORM_BUILD="$USE_CLANG_PLATFORM_BUILD"
    unset TIMEFORMAT
    trap infrastructure_error ERR

    # Show remaining disk space to catch early if
    # we're approaching ramdisk limit again
    echo "Remaining disk space after build:"
    df

    if [ -n "$BUILD_SYSROOT" ]; then
        git clone git://git.linaro.org/android/toolchain/build.git toolchain-build
        rm -rf /tmp/sysroot
        toolchain-build/build-sysroot.sh out/target/product/`ls -1 out/target/product | head -n1` /tmp/sysroot
        tar -cj -C /tmp/sysroot -f out/sysroot.tar.bz2 .
    fi

fi

lamc_system="--systemimage $PWD/out/target/product/$TARGET_OUT_DIR/system.img"
if [ -f " $PWD/out/target/product/$TARGET_OUT_DIR/system.tar.bz2" ]; then
    lamc_system="--system $PWD/out/target/product/$TARGET_OUT_DIR/system.tar.bz2"
fi
lamc_userdata="--userdataimage $PWD/out/target/product/$TARGET_OUT_DIR/userdata.img"
if [ -f " $PWD/out/target/product/$TARGET_OUT_DIR/system.tar.bz2" ]; then
    lamc_system="--userdata $PWD/out/target/product/$TARGET_OUT_DIR/userdata.tar.bz2"
fi

# Create filesystem images
if [ -n "$BUILD_FS_IMAGE" ]; then
    FS_IMAGE_SIZE=${FS_IMAGE_SIZE-2G}
    sudo linaro-android-media-create \
        --dev `product2lamc_dev $TARGET_PRODUCT` \
        ${lamc_system} \
        ${lamc_userdata} \
        --boot $PWD/out/target/product/$TARGET_OUT_DIR/boot.tar.bz2 \
        --image-size "$FS_IMAGE_SIZE" \
        --image-file out/target/product/$TARGET_OUT_DIR/$TARGET_PRODUCT.img
    bzip2 -9 out/target/product/$TARGET_PRODUCT/$TARGET_PRODUCT.img
fi

# Store kernel .config as a build artifact
if [ -f kernel/.config ]; then
    cp kernel/.config out/kernel_config
elif [ -f "out/target/product/$TARGET_OUT_DIR/obj/kernel/.config" ]; then
    cp "out/target/product/$TARGET_OUT_DIR/obj/kernel/.config" out/kernel_config
fi
# Create md5sums file for platform artifacts
(cd out/target/product/${TARGET_OUT_DIR}/; md5sum *.tar.bz2 *.img *.img.bz2 >MD5SUMS) || true

(cd out/; ${BUILD_SCRIPT_ROOT}/create-user-build-script)
if  [ -f  out/kernel_config ]; then
    (cd out/; ${BUILD_SCRIPT_ROOT}/create-user-kernel-script) || true
fi

if [ ! -n "$SKIP_LICENSE_CHECK" ]; then
    ${BUILD_SCRIPT_ROOT}/license-verification.sh out/pinned-manifest.xml  out/target/product/$TARGET_PRODUCT/BUILD-INFO.txt
fi

if [ -n "$PUBLISH_DESCRIPTIONS" ]; then
    ${BUILD_SCRIPT_ROOT}/../utils/manifest-descriptions/make.sh out/source-manifest.xml out/pinned-manifest.xml
fi

# Create build_config.json as a build artifact
target_kernel_src=`grep '^TARGET_KERNEL_SOURCE' device/linaro/${TARGET_PRODUCT}/BoardConfig.mk|cut -d'=' -f2|tr -d '[[:space:]]'`
if [ -n "${target_kernel_src}" ]; then
    kernel_proj_str=`grep "path=\"${target_kernel_src}\"" out/pinned-manifest.xml`
    kernel_rev=`echo ${kernel_proj_str} | grep -o "revision[^ ]*" | cut -d"\"" -f2`
    kernel_branch=`echo ${kernel_proj_str} | grep -o "upstream[^ ]*" | cut -d"\"" -f2`
    kernel_repo_path=`echo ${kernel_proj_str} | grep -o "path[^ ]*" | cut -d"\"" -f2`
    cd $kernel_repo_path
    kernel_repo=`git remote -v | grep "fetch" | cut -f2 | cut -d " " -f1`
    cd -
fi
android_ver=`grep "ro\.build\.version\.release" out/target/product/${TARGET_OUT_DIR}/system/build.prop|cut -d"=" -f2`
toolchain=`echo ${ANDROID_TOOLCHAIN}|awk -F'/' '{print $(NF-1)}'`
build_arch=`echo ${toolchain} | cut -d"-" -f1`
compiler=`$(find ${ANDROID_TOOLCHAIN} -name "*-gcc"|head -n1) --version | head -n1`
dtbs=`find out -iname *.dtb | rev | cut -d"/" -f1 | rev | sort | uniq`
dtbfs=""
for f in $dtbs;
do
  dtbfs="\"$f\", $dtbfs"
done
dtb="[${dtbfs%,*}]"
cat > out/build_config.json <<EOF
{
  "kernel_repo" : "${kernel_repo}",
  "kernel_commit_id" : "${kernel_rev}",
  "kernel_branch" : "${kernel_branch}",
  "android_version" : "${android_ver}",
  "android_toolchain" : "${toolchain}",
  "build_arch" : "${build_arch}",
  "compiler" : "${compiler}",
  "dtb" : ${dtb}
}
EOF
cp changelog.txt out/

flatten_artifacts