1bf215546Sopenharmony_ci#!/bin/sh
2bf215546Sopenharmony_ci#
3bf215546Sopenharmony_ci# Copyright (C) 2022 Collabora Limited
4bf215546Sopenharmony_ci# Author: Guilherme Gallo <guilherme.gallo@collabora.com>
5bf215546Sopenharmony_ci#
6bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci#
13bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next
14bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
15bf215546Sopenharmony_ci# Software.
16bf215546Sopenharmony_ci#
17bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22bf215546Sopenharmony_ci# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23bf215546Sopenharmony_ci# SOFTWARE.
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_cicopy_tests_files() (
27bf215546Sopenharmony_ci    # Copy either unit test or render test files from a specific driver given by
28bf215546Sopenharmony_ci    # GPU VERSION variable.
29bf215546Sopenharmony_ci    # If there is no test file at the expected location, this function will
30bf215546Sopenharmony_ci    # return error_code 1
31bf215546Sopenharmony_ci    SKQP_BACKEND="${1}"
32bf215546Sopenharmony_ci    SKQP_FILE_PREFIX="${INSTALL}/${GPU_VERSION}-skqp"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci    if echo "${SKQP_BACKEND}" | grep -qE 'vk|gl(es)?'
35bf215546Sopenharmony_ci    then
36bf215546Sopenharmony_ci        SKQP_RENDER_TESTS_FILE="${SKQP_FILE_PREFIX}-${SKQP_BACKEND}_rendertests.txt"
37bf215546Sopenharmony_ci        [ -f "${SKQP_RENDER_TESTS_FILE}" ] || return 1
38bf215546Sopenharmony_ci        cp "${SKQP_RENDER_TESTS_FILE}" "${SKQP_ASSETS_DIR}"/skqp/rendertests.txt
39bf215546Sopenharmony_ci        return 0
40bf215546Sopenharmony_ci    fi
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci    # The unittests.txt path is hardcoded inside assets directory,
43bf215546Sopenharmony_ci    # that is why it needs to be a special case.
44bf215546Sopenharmony_ci    if echo "${SKQP_BACKEND}" | grep -qE "unitTest"
45bf215546Sopenharmony_ci    then
46bf215546Sopenharmony_ci        SKQP_UNIT_TESTS_FILE="${SKQP_FILE_PREFIX}_unittests.txt"
47bf215546Sopenharmony_ci        [ -f "${SKQP_UNIT_TESTS_FILE}" ] || return 1
48bf215546Sopenharmony_ci        cp "${SKQP_UNIT_TESTS_FILE}" "${SKQP_ASSETS_DIR}"/skqp/unittests.txt
49bf215546Sopenharmony_ci    fi
50bf215546Sopenharmony_ci)
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_citest_vk_backend() {
53bf215546Sopenharmony_ci    if echo "${SKQP_BACKENDS}" | grep -qE 'vk'
54bf215546Sopenharmony_ci    then
55bf215546Sopenharmony_ci        if [ -n "$VK_DRIVER" ]; then
56bf215546Sopenharmony_ci            return 0
57bf215546Sopenharmony_ci        fi
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci        echo "VK_DRIVER environment variable is missing."
60bf215546Sopenharmony_ci        VK_DRIVERS=$(ls "$INSTALL"/share/vulkan/icd.d/ | cut -f 1 -d '_')
61bf215546Sopenharmony_ci        if [ -n "${VK_DRIVERS}" ]
62bf215546Sopenharmony_ci        then
63bf215546Sopenharmony_ci            echo "Please set VK_DRIVER to the correct driver from the list:"
64bf215546Sopenharmony_ci            echo "${VK_DRIVERS}"
65bf215546Sopenharmony_ci        fi
66bf215546Sopenharmony_ci        echo "No Vulkan tests will be executed, but it was requested in SKQP_BACKENDS variable. Exiting."
67bf215546Sopenharmony_ci        exit 2
68bf215546Sopenharmony_ci    fi
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci    # Vulkan environment is not configured, but it was not requested by the job
71bf215546Sopenharmony_ci    return 1
72bf215546Sopenharmony_ci}
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_cisetup_backends() {
75bf215546Sopenharmony_ci    if test_vk_backend
76bf215546Sopenharmony_ci    then
77bf215546Sopenharmony_ci        export VK_ICD_FILENAMES="$INSTALL"/share/vulkan/icd.d/"$VK_DRIVER"_icd."${VK_CPU:-$(uname -m)}".json
78bf215546Sopenharmony_ci    fi
79bf215546Sopenharmony_ci}
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ciset -ex
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci# Needed so configuration files can contain paths to files in /install
84bf215546Sopenharmony_ciln -sf "$CI_PROJECT_DIR"/install /install
85bf215546Sopenharmony_ciINSTALL=${PWD}/install
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ciif [ -z "$GPU_VERSION" ]; then
88bf215546Sopenharmony_ci    echo 'GPU_VERSION must be set to something like "llvmpipe" or
89bf215546Sopenharmony_ci"freedreno-a630" (it will serve as a component to find the path for files
90bf215546Sopenharmony_ciresiding in src/**/ci/*.txt)'
91bf215546Sopenharmony_ci    exit 1
92bf215546Sopenharmony_cifi
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ciLD_LIBRARY_PATH=$INSTALL:$LD_LIBRARY_PATH
95bf215546Sopenharmony_cisetup_backends
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ciSKQP_ASSETS_DIR=/skqp/assets
98bf215546Sopenharmony_ciSKQP_RESULTS_DIR="${SKQP_RESULTS_DIR:-$PWD/results}"
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_cimkdir -p "${SKQP_ASSETS_DIR}"/skqp
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ciSKQP_EXITCODE=0
103bf215546Sopenharmony_cifor SKQP_BACKEND in ${SKQP_BACKENDS}
104bf215546Sopenharmony_cido
105bf215546Sopenharmony_ci    set -e
106bf215546Sopenharmony_ci    if !  copy_tests_files "${SKQP_BACKEND}"
107bf215546Sopenharmony_ci    then
108bf215546Sopenharmony_ci        echo "No override test file found for ${SKQP_BACKEND}. Using the default one."
109bf215546Sopenharmony_ci    fi
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci    set +e
112bf215546Sopenharmony_ci    SKQP_BACKEND_RESULTS_DIR="${SKQP_RESULTS_DIR}"/"${SKQP_BACKEND}"
113bf215546Sopenharmony_ci    mkdir -p "${SKQP_BACKEND_RESULTS_DIR}"
114bf215546Sopenharmony_ci    /skqp/skqp "${SKQP_ASSETS_DIR}" "${SKQP_BACKEND_RESULTS_DIR}" "${SKQP_BACKEND}_"
115bf215546Sopenharmony_ci    BACKEND_EXITCODE=$?
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci    if [ ! $BACKEND_EXITCODE -eq 0 ]
118bf215546Sopenharmony_ci    then
119bf215546Sopenharmony_ci        echo "skqp failed on ${SKQP_BACKEND} tests with ${BACKEND_EXITCODE} exit code."
120bf215546Sopenharmony_ci    fi
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci    # Propagate error codes to leverage the final job result
123bf215546Sopenharmony_ci    SKQP_EXITCODE=$(( SKQP_EXITCODE | BACKEND_EXITCODE ))
124bf215546Sopenharmony_cidone
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ciset +x
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci# Unit tests produce empty HTML reports, guide the user to check the TXT file.
129bf215546Sopenharmony_ciif echo "${SKQP_BACKENDS}" | grep -qE "unitTest"
130bf215546Sopenharmony_cithen
131bf215546Sopenharmony_ci    # Remove the empty HTML report to avoid confusion
132bf215546Sopenharmony_ci    rm -f "${SKQP_RESULTS_DIR}"/unitTest/report.html
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci    echo "See skqp unit test results at:"
135bf215546Sopenharmony_ci    echo "https://$CI_PROJECT_ROOT_NAMESPACE.pages.freedesktop.org/-/$CI_PROJECT_NAME/-/jobs/$CI_JOB_ID/artifacts/${SKQP_RESULTS_DIR}/unitTest/unit_tests.txt"
136bf215546Sopenharmony_cifi
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ciREPORT_FILES=$(mktemp)
139bf215546Sopenharmony_cifind "${SKQP_RESULTS_DIR}"/**/report.html -type f > "${REPORT_FILES}"
140bf215546Sopenharmony_ciwhile read -r REPORT
141bf215546Sopenharmony_cido
142bf215546Sopenharmony_ci    BACKEND_NAME=$(echo "${REPORT}" | sed  's@.*/\([^/]*\)/report.html@\1@')
143bf215546Sopenharmony_ci    echo "See skqp ${BACKEND_NAME} render tests report at:"
144bf215546Sopenharmony_ci    echo "https://$CI_PROJECT_ROOT_NAMESPACE.pages.freedesktop.org/-/$CI_PROJECT_NAME/-/jobs/$CI_JOB_ID/artifacts/${REPORT}"
145bf215546Sopenharmony_cidone < "${REPORT_FILES}"
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci# If there is no report available, tell the user that something is wrong.
148bf215546Sopenharmony_ciif [ ! -s "${REPORT_FILES}" ]
149bf215546Sopenharmony_cithen
150bf215546Sopenharmony_ci    echo "No skqp report available. Probably some fatal error has occured during the skqp execution."
151bf215546Sopenharmony_cifi
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ciexit $SKQP_EXITCODE
154