1bf215546Sopenharmony_ci#!/bin/bash
2bf215546Sopenharmony_ci
3bf215546Sopenharmony_ciset -ex
4bf215546Sopenharmony_ci
5bf215546Sopenharmony_ciEPHEMERAL="\
6bf215546Sopenharmony_ci         autoconf \
7bf215546Sopenharmony_ci         rdfind \
8bf215546Sopenharmony_ci         unzip \
9bf215546Sopenharmony_ci         "
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_ciapt-get install -y --no-remove $EPHEMERAL
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ci# Fetch the NDK and extract just the toolchain we want.
14bf215546Sopenharmony_cindk=android-ndk-r21d
15bf215546Sopenharmony_ciwget -O $ndk.zip https://dl.google.com/android/repository/$ndk-linux-x86_64.zip
16bf215546Sopenharmony_ciunzip -d / $ndk.zip "$ndk/toolchains/llvm/*"
17bf215546Sopenharmony_cirm $ndk.zip
18bf215546Sopenharmony_ci# Since it was packed as a zip file, symlinks/hardlinks got turned into
19bf215546Sopenharmony_ci# duplicate files.  Turn them into hardlinks to save on container space.
20bf215546Sopenharmony_cirdfind -makehardlinks true -makeresultsfile false /android-ndk-r21d/
21bf215546Sopenharmony_ci# Drop some large tools we won't use in this build.
22bf215546Sopenharmony_cifind /android-ndk-r21d/ -type f | egrep -i "clang-check|clang-tidy|lldb" | xargs rm -f
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_cish .gitlab-ci/container/create-android-ndk-pc.sh /$ndk zlib.pc "" "-lz" "1.2.3"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_cish .gitlab-ci/container/create-android-cross-file.sh /$ndk x86_64-linux-android x86_64 x86_64
27bf215546Sopenharmony_cish .gitlab-ci/container/create-android-cross-file.sh /$ndk i686-linux-android x86 x86
28bf215546Sopenharmony_cish .gitlab-ci/container/create-android-cross-file.sh /$ndk aarch64-linux-android arm armv8
29bf215546Sopenharmony_cish .gitlab-ci/container/create-android-cross-file.sh /$ndk arm-linux-androideabi arm armv7hl armv7a-linux-androideabi
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci# Not using build-libdrm.sh because we don't want its cleanup after building
32bf215546Sopenharmony_ci# each arch.  Fetch and extract now.
33bf215546Sopenharmony_ciexport LIBDRM_VERSION=libdrm-2.4.110
34bf215546Sopenharmony_ciwget https://dri.freedesktop.org/libdrm/$LIBDRM_VERSION.tar.xz
35bf215546Sopenharmony_citar -xf $LIBDRM_VERSION.tar.xz && rm $LIBDRM_VERSION.tar.xz
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cifor arch in \
38bf215546Sopenharmony_ci        x86_64-linux-android \
39bf215546Sopenharmony_ci        i686-linux-android \
40bf215546Sopenharmony_ci        aarch64-linux-android \
41bf215546Sopenharmony_ci        arm-linux-androideabi ; do
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci    cd $LIBDRM_VERSION
44bf215546Sopenharmony_ci    rm -rf build-$arch
45bf215546Sopenharmony_ci    meson build-$arch \
46bf215546Sopenharmony_ci          --cross-file=/cross_file-$arch.txt \
47bf215546Sopenharmony_ci          --libdir=lib/$arch \
48bf215546Sopenharmony_ci          -Dlibkms=false \
49bf215546Sopenharmony_ci          -Dnouveau=false \
50bf215546Sopenharmony_ci          -Dvc4=false \
51bf215546Sopenharmony_ci          -Detnaviv=false \
52bf215546Sopenharmony_ci          -Dfreedreno=false \
53bf215546Sopenharmony_ci          -Dintel=false \
54bf215546Sopenharmony_ci          -Dcairo-tests=false \
55bf215546Sopenharmony_ci          -Dvalgrind=false
56bf215546Sopenharmony_ci    ninja -C build-$arch install
57bf215546Sopenharmony_ci    cd ..
58bf215546Sopenharmony_cidone
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cirm -rf $LIBDRM_VERSION
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ciexport LIBELF_VERSION=libelf-0.8.13
63bf215546Sopenharmony_ciwget https://fossies.org/linux/misc/old/$LIBELF_VERSION.tar.gz
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci# Not 100% sure who runs the mirror above so be extra careful
66bf215546Sopenharmony_ciif ! echo "4136d7b4c04df68b686570afa26988ac ${LIBELF_VERSION}.tar.gz" | md5sum -c -; then
67bf215546Sopenharmony_ci    echo "Checksum failed"
68bf215546Sopenharmony_ci    exit 1
69bf215546Sopenharmony_cifi
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_citar -xf ${LIBELF_VERSION}.tar.gz
72bf215546Sopenharmony_cicd $LIBELF_VERSION
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci# Work around a bug in the original configure not enabling __LIBELF64.
75bf215546Sopenharmony_ciautoreconf
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cifor arch in \
78bf215546Sopenharmony_ci        x86_64-linux-android \
79bf215546Sopenharmony_ci        i686-linux-android \
80bf215546Sopenharmony_ci        aarch64-linux-android \
81bf215546Sopenharmony_ci        arm-linux-androideabi ; do
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci    ccarch=${arch}
84bf215546Sopenharmony_ci    if [ "${arch}" ==  'arm-linux-androideabi' ]
85bf215546Sopenharmony_ci    then
86bf215546Sopenharmony_ci       ccarch=armv7a-linux-androideabi
87bf215546Sopenharmony_ci    fi
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci    export CC=/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/${arch}-ar
90bf215546Sopenharmony_ci    export CC=/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/${ccarch}29-clang
91bf215546Sopenharmony_ci    export CXX=/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/${ccarch}29-clang++
92bf215546Sopenharmony_ci    export LD=/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/${arch}-ld
93bf215546Sopenharmony_ci    export RANLIB=/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/${arch}-ranlib
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci    # The configure script doesn't know about android, but doesn't really use the host anyway it
96bf215546Sopenharmony_ci    # seems
97bf215546Sopenharmony_ci    ./configure --host=x86_64-linux-gnu  --disable-nls --disable-shared \
98bf215546Sopenharmony_ci                --libdir=/usr/local/lib/${arch}
99bf215546Sopenharmony_ci    make install
100bf215546Sopenharmony_ci    make distclean
101bf215546Sopenharmony_cidone
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_cicd ..
104bf215546Sopenharmony_cirm -rf $LIBELF_VERSION
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ciapt-get purge -y $EPHEMERAL
107