- job:
    name: openembedded-armv8-ilp32
    project-type: matrix
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
                - job-workspace
            everyone-flat:
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 30
    parameters:
        - string:
            name: PUB_DEST
            default: 'openembedded/images/${rootfs}-armv8-ilp32-gcc-${gcc_version}/${MACHINE}/${BUILD_NUMBER}'
        - string:
            name: LAVA_SERVER
            default: 'validation.linaro.org/RPC2/'
    disabled: false
    node: build
    display-name: 'OpenEmbedded ARMv8 ILP32'
    scm:
        - git:
            url: https://git.linaro.org/openembedded/jenkins-setup.git
            refspec: +refs/heads/master:refs/remotes/origin/master
            name: origin
            branches:
                - refs/heads/master
            skip-tag: true
            shallow-clone: true
            wipe-workspace: false
    axes:
        - axis:
            type: slave
            name: label
            values:
                - build
        - axis:
            type: user-defined
            name: gcc_version
            values:
                - 7.1
        - axis:
            type: user-defined
            name: rootfs
            values:
                - minimal
                - minimal-initramfs
                - minimal-ltp
        - axis:
            type: user-defined
            name: MACHINE
            values:
                - 'hikey-ilp32'
    execution-strategy:
        sequential: true
        touchstone:
            expr: 'rootfs=="minimal"'
    wrappers:
        - timestamps
        - matrix-tie-parent:
            node: build
    builders:
        - shell: |
            #!/bin/bash

            set -ex

            export PATH="$HOME/bin:$PATH"

            # we clean build and populate it from cache
            bash -x pre-build-do-cleanup.sh

            if [ `date +%u` = "6" ] && [ "${rootfs}" = "minimal" ]
            then
              echo "manual hard cleanup on day 6"
              bash periodic-cleanup.sh hard
            fi

            # temporary clean-up for glibc
            find /mnt/ci_build/workspace/sstate-cache -type f -name "sstate:*:*:*:*:aarch64_ilp32:*" -exec rm -f {} + || true

            # verbose kernel, add resize-helper
            cat << EOF >> local-init-env.sh
            init_env
            echo 'CMDLINE_remove = "quiet"' >> conf/local.conf
            echo 'IMAGE_INSTALL_append = " 96boards-tools"' >> conf/local.conf
            EOF

            # do a build
            bash -x init-and-build.sh -a armv8 -g ${gcc_version} -i "source ./local-init-env.sh" linaro-image-${rootfs}

            cd ${WORKSPACE}

            bash -x post-build-create-image-manifest.sh
            bash -x post-build-sort-out-downloads.sh

            # Capture what we're building in the build output.
            repo manifest -r
        - linaro-publish-token
        - shell: |
            #!/bin/bash

            set -ex
            # Create sparse image
            # Only the LTP image is sent to LAVA, hence it is the only one
            # needing a sparse image to be published.
            if [ "${rootfs}" = "minimal-ltp" ]; then
              cd out/
              rootfsext=$(ls -1 linaro-image-${rootfs}-${MACHINE}-*.rootfs.ext4.gz)
              gunzip -k ${rootfsext}
              sudo ext2simg -v ${rootfsext%.gz} ${rootfsext%.ext4.gz}.img
              rm -f ${rootfsext%.gz}
              gzip -9 ${rootfsext%.ext4.gz}.img
              cd -
            fi

            # Publish
            test -d ${HOME}/bin || mkdir ${HOME}/bin
            wget -q https://git.linaro.org/ci/publishing-api.git/blob_plain/HEAD:/linaro-cp.py -O ${HOME}/bin/linaro-cp.py
            time python3 ${HOME}/bin/linaro-cp.py \
              --api_version 3 \
              --link-latest \
              out/ ${PUB_DEST}
        - conditional-step:
            condition-kind: strings-match
            condition-string1: '${ENV,var="rootfs"}'
            condition-string2: 'minimal-ltp'
            steps:
                - shell: |
                    #!/bin/bash -x

                    rm -rf configs
                    git clone --depth 1 http://git.linaro.org/ci/job/configs.git

                    export DEVICE_TYPE=hi6220-hikey
                    export BOOT_URL=${PUBLISH_SERVER}/${PUB_DEST}/$(ls out/boot-0.0*.img | xargs basename)
                    export SYSTEM_URL=${PUBLISH_SERVER}/${PUB_DEST}/$(ls out/linaro-image-${rootfs}-${MACHINE}-*.rootfs.img.gz | xargs basename)
                    export TESTNAME=syscalls
                    cat << EOF | python > custom_lava_job_definition.yaml
                    import os
                    fh = open("configs/openembedded-armv8/lava-job-definitions/${DEVICE_TYPE}/master-template-ltp.yaml", "r")
                    print os.path.expandvars(fh.read())
                    fh.close()
                    EOF

                    cat << EOF > post_build_lava_parameters
                    DEVICE_TYPE=${DEVICE_TYPE}
                    CUSTOM_YAML_URL=${JOB_URL}/ws/custom_lava_job_definition.yaml
                    LAVA_SERVER=${LAVA_SERVER}
                    EOF
                - trigger-builds:
                    - project: 'post-build-lava'
                      property-file: post_build_lava_parameters
                      block: true
    publishers:
        - email:
            recipients: 'fathi.boudra@linaro.org daniel.diaz@linaro.org'
