blob: 18b56007686652456653d6ec7194345188c7103f [file] [log] [blame]
Zdenek Behan12ad1c52010-08-03 16:34:37 -07001# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2# Distributed under the terms of the GNU General Public License v2
3
4#
5# Original Author: The Chromium OS Authors <chromium-os-dev@chromium.org>
6# Purpose: Eclass for handling autotest test packages
7#
8
Zdenek Behan9e98f322010-08-26 18:56:54 -07009RDEPEND="( autotest? ( >=chromeos-base/autotest-0.0.1-r3 ) )"
Zdenek Behan14ae4892010-08-26 15:25:30 -070010# HACK: all packages should get autotest-deps for now
11if ! [ "${PN}" = "autotest-deps" ]; then
12 RDEPEND="${RDEPEND} ( autotest? ( chromeos-base/autotest-deps ) )"
13fi
Zdenek Behan12ad1c52010-08-03 16:34:37 -070014DEPEND="${RDEPEND}"
15
Zdenek Behan14ae4892010-08-26 15:25:30 -070016IUSE="buildcheck autotest"
Zdenek Behan12ad1c52010-08-03 16:34:37 -070017
18# Ensure the configures run by autotest pick up the right config.site
Zdenek Behan99bf5842010-08-04 17:23:38 -070019export CONFIG_SITE="/usr/share/config.site"
20export AUTOTEST_WORKDIR="${WORKDIR}/autotest-work"
Zdenek Behan12ad1c52010-08-03 16:34:37 -070021
Zdenek Behancd997a32010-08-20 12:55:44 -070022# @ECLASS-VARIABLE: AUTOTEST_CLIENT_*
23# @DESCRIPTION:
24# Location of the appropriate test directory inside ${S}
25: ${AUTOTEST_CLIENT_TESTS:=client/tests}
26: ${AUTOTEST_CLIENT_SITE_TESTS:=client/site_tests}
27: ${AUTOTEST_SERVER_TESTS:=server/tests}
28: ${AUTOTEST_SERVER_SITE_TESTS:=server/site_tests}
Zdenek Behan41463c92010-08-23 12:12:31 -070029: ${AUTOTEST_CONFIG:=client/config}
30: ${AUTOTEST_DEPS:=client/deps}
31: ${AUTOTEST_PROFILERS:=client/profilers}
32
33# @ECLASS-VARIABLE: AUTOTEST_*_LIST
34# @DESCRIPTION:
35# The list of deps/configs/profilers provided with this package
36: ${AUTOTEST_CONFIG_LIST:=*}
37: ${AUTOTEST_DEPS_LIST:=*}
38: ${AUTOTEST_PROFILERS_LIST:=*}
Zdenek Behancd997a32010-08-20 12:55:44 -070039
Zdenek Behand012b762010-09-01 21:22:26 -070040# @ECLASS-VARIABLE: AUTOTEST_FORCE_LIST
41# @DESCRIPTION:
42# Sometimes we just want to forget about useflags and build what's inside
43: ${AUTOTEST_FORCE_TEST_LIST:=}
44
Zdenek Behanb1f7c892010-09-22 12:25:38 -070045# @ECLASS-VARIABLE: AUTOTEST_FILE_MASK
46# @DESCRIPTION:
47# The list of 'find' expressions to find in the resulting image and delete
48: ${AUTOTEST_FILE_MASK:=}
49
Zdenek Behan7922d132010-09-24 14:18:05 -070050function fast_cp() {
51 cp -l "$@" || cp "$@"
52}
53
Zdenek Behand012b762010-09-01 21:22:26 -070054function get_test_list() {
55 if [ -n "${AUTOTEST_FORCE_TEST_LIST}" ]; then
56 # list forced
57 echo "${AUTOTEST_FORCE_TEST_LIST}"
58 return
59 fi
60
61 # we cache the result of this operation in AUTOTEST_TESTS,
62 # because it's expensive and does not change over the course of one ebuild run
63 local result="${IUSE_TESTS//[+-]tests_/}"
64 result="${result//tests_/}"
65
66 result=$(for test in ${result}; do use tests_${test} && echo -n "${test} "; done)
67 echo "${result}"
68}
69
Zdenek Behan12ad1c52010-08-03 16:34:37 -070070# Pythonify the list of packages
71function pythonify_test_list() {
72 local result
Zdenek Behand012b762010-09-01 21:22:26 -070073 result=$(for test in $*; do echo -n "${test},"; done)
Zdenek Behan31315b32010-08-05 16:34:39 -070074 echo ${result}
Zdenek Behan12ad1c52010-08-03 16:34:37 -070075}
76
77# Create python package init files for top level test case dirs.
78function touch_init_py() {
79 local dirs=${1}
80 for base_dir in $dirs
81 do
82 local sub_dirs="$(find ${base_dir} -maxdepth 1 -type d)"
83 for sub_dir in ${sub_dirs}
84 do
85 touch ${sub_dir}/__init__.py
86 done
87 touch ${base_dir}/__init__.py
88 done
89}
90
91function setup_cross_toolchain() {
92 if tc-is-cross-compiler ; then
93 tc-export CC CXX AR RANLIB LD NM STRIP
94 export PKG_CONFIG_PATH="${ROOT}/usr/lib/pkgconfig/"
95 export CCFLAGS="$CFLAGS"
96 fi
97
98 # TODO(fes): Check for /etc/hardened for now instead of the hardened
99 # use flag because we aren't enabling hardened on the target board.
100 # Rather, right now we're using hardened only during toolchain compile.
101 # Various tests/etc. use %ebx in here, so we have to turn off PIE when
102 # using the hardened compiler
103 if use x86 ; then
104 if use hardened ; then
105 #CC="${CC} -nopie"
106 append-flags -nopie
107 fi
108 fi
109}
110
111function create_autotest_workdir() {
112 local dst=${1}
113
114 # create a working enviroment for pre-building
Zdenek Behan99bf5842010-08-04 17:23:38 -0700115 ln -sf "${SYSROOT}"/usr/local/autotest/{conmux,tko,utils,global_config.ini,shadow_config.ini} "${dst}"/
116
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700117 # NOTE: in order to make autotest not notice it's running from /usr/local/, we need
118 # to make sure the binaries are real, because they do the path magic
119 local root_path base_path
120 for base_path in client client/bin; do
121 root_path="${SYSROOT}/usr/local/autotest/${base_path}"
122 mkdir -p "${dst}/${base_path}"
123
Zdenek Behan451e07f2010-08-06 11:25:37 -0700124 # Skip bin, because it is processed separately, and test-provided dirs
125 # Also don't symlink to packages, because that kills the build
Zdenek Behan41463c92010-08-23 12:12:31 -0700126 for entry in $(ls "${root_path}" | \
127 grep -v "\(bin\|tests\|site_tests\|config\|deps\|profilers\|packages\)$"); do
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700128 ln -sf "${root_path}/${entry}" "${dst}/${base_path}/"
129 done
130 done
131 # replace the important binaries with real copies
132 for base_path in autotest autotest_client; do
133 root_path="${SYSROOT}/usr/local/autotest/client/bin/${base_path}"
134 rm "${dst}/client/bin/${base_path}"
135 cp -f ${root_path} "${dst}/client/bin/${base_path}"
136 done
Zdenek Behan448ddaa2010-08-24 11:18:50 -0700137
138 # Selectively pull in deps that are not provided by the current test package
139 for base_path in config deps profilers; do
140 for dir in "${SYSROOT}/usr/local/autotest/client/${base_path}"/*; do
141 if [ -d "${dir}" ] && \
142 ! [ -d "${AUTOTEST_WORKDIR}/client/${base_path}/$(basename ${dir})" ]; then
143 # directory does not exist, create a symlink
144 ln -sf "${dir}" "${AUTOTEST_WORKDIR}/client/${base_path}/$(basename ${dir})"
145 fi
146 done
147 done
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700148}
149
150function print_test_dirs() {
151 local testroot="${1}"
152
153 pushd "${testroot}" 1> /dev/null
154 for test in *; do
155 if [ -d "${test}" ] && [ -f "${test}/${test}".py ]; then
156 echo "${test}"
157 fi
158 done
159 popd 1> /dev/null
160}
161
Zdenek Behaneee7bc22010-08-20 18:33:13 -0700162# checks IUSE_TESTS and sees if at least one of these is enabled
163function are_we_used() {
Zdenek Behan14ae4892010-08-26 15:25:30 -0700164 if ! use autotest; then
165 # unused
166 return 1
167 fi
168
Zdenek Behand012b762010-09-01 21:22:26 -0700169 [ -n "$(get_test_list)" ] && return 0
Zdenek Behaneee7bc22010-08-20 18:33:13 -0700170
171 # unused
172 return 1
173}
174
Zdenek Behan99bf5842010-08-04 17:23:38 -0700175function autotest_src_prepare() {
Zdenek Behaneee7bc22010-08-20 18:33:13 -0700176 are_we_used || return 0
177 einfo "Preparing tests"
178
179 # FIXME: These directories are needed, autotest quietly dies if
180 # they don't even exist. They may, however, stay empty.
Zdenek Behanc02fc472010-08-13 12:55:07 -0700181 mkdir -p "${AUTOTEST_WORKDIR}"/client/tests
182 mkdir -p "${AUTOTEST_WORKDIR}"/client/site_tests
Zdenek Behan41463c92010-08-23 12:12:31 -0700183 mkdir -p "${AUTOTEST_WORKDIR}"/client/config
184 mkdir -p "${AUTOTEST_WORKDIR}"/client/deps
185 mkdir -p "${AUTOTEST_WORKDIR}"/client/profilers
Zdenek Behanc02fc472010-08-13 12:55:07 -0700186 mkdir -p "${AUTOTEST_WORKDIR}"/server/tests
187 mkdir -p "${AUTOTEST_WORKDIR}"/server/site_tests
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700188
Zdenek Behand012b762010-09-01 21:22:26 -0700189 TEST_LIST=$(get_test_list)
190
Zdenek Behan448ddaa2010-08-24 11:18:50 -0700191 # Pull in the individual test cases.
Zdenek Behan31315b32010-08-05 16:34:39 -0700192 for l1 in client server; do
193 for l2 in site_tests tests; do
Zdenek Behancd997a32010-08-20 12:55:44 -0700194 # pick up the indicated location of test sources
195 eval srcdir=${WORKDIR}/${P}/\${AUTOTEST_${l1^^*}_${l2^^*}}
196
Zdenek Behand012b762010-09-01 21:22:26 -0700197 # test does have this directory
198 for test in ${TEST_LIST}; do
199 if [ -d "${srcdir}/${test}" ]; then
Zdenek Behan7922d132010-09-24 14:18:05 -0700200 fast_cp -fpr "${srcdir}/${test}" "${AUTOTEST_WORKDIR}/${l1}/${l2}"/ || die
Zdenek Behand012b762010-09-01 21:22:26 -0700201 fi
202 done
Zdenek Behan31315b32010-08-05 16:34:39 -0700203 done
204 done
Zdenek Behan99bf5842010-08-04 17:23:38 -0700205
Zdenek Behan448ddaa2010-08-24 11:18:50 -0700206 # Pull in all the deps provided by this package, selectively.
Zdenek Behan41463c92010-08-23 12:12:31 -0700207 for l2 in config deps profilers; do
208 # pick up the indicated location of test sources
209 eval srcdir=${WORKDIR}/${P}/\${AUTOTEST_${l2^^*}}
210
211 if [ -d "${srcdir}" ]; then # test does have this directory
212 pushd "${srcdir}" 1> /dev/null
213 eval deplist=\${AUTOTEST_${l2^^*}_LIST}
214
215 if [ "${deplist}" = "*" ]; then
Zdenek Behan7922d132010-09-24 14:18:05 -0700216 fast_cp -fpr * "${AUTOTEST_WORKDIR}/client/${l2}"
Zdenek Behan41463c92010-08-23 12:12:31 -0700217 else
218 for dir in ${deplist}; do
Zdenek Behan7922d132010-09-24 14:18:05 -0700219 fast_cp -fpr "${dir}" "${AUTOTEST_WORKDIR}/client/${l2}"/ || die
Zdenek Behan41463c92010-08-23 12:12:31 -0700220 done
221 fi
222 popd 1> /dev/null
223 fi
224 done
225
Zdenek Behan474fbae2010-08-20 21:13:22 -0700226 # FIXME: We'd like if this were not necessary, and autotest supported out-of-tree build
Zdenek Behan99bf5842010-08-04 17:23:38 -0700227 create_autotest_workdir "${AUTOTEST_WORKDIR}"
Zdenek Behaneee7bc22010-08-20 18:33:13 -0700228
Zdenek Behanf93c208a2010-09-21 18:53:58 -0700229 # Each test directory needs to be visited and have an __init__.py created.
230 # However, that only applies to the directories which have a main .py file.
Zdenek Behanedb527a2010-09-22 12:24:14 -0700231 pushd "${AUTOTEST_WORKDIR}" > /dev/null || die "AUTOTEST_WORKDIR does not exist?!"
Zdenek Behanf93c208a2010-09-21 18:53:58 -0700232 for dir in client/tests client/site_tests server/tests server/site_tests; do
Zdenek Behanedb527a2010-09-22 12:24:14 -0700233 pushd "${dir}" > /dev/null || continue
Zdenek Behanf93c208a2010-09-21 18:53:58 -0700234 for sub in *; do
235 [ -f "${sub}/${sub}.py" ] || continue
Zdenek Behan1ec164a2010-08-17 19:06:31 -0700236
Zdenek Behanf93c208a2010-09-21 18:53:58 -0700237 touch_init_py ${sub}
238 done
Zdenek Behanedb527a2010-09-22 12:24:14 -0700239 popd > /dev/null
Zdenek Behan1ec164a2010-08-17 19:06:31 -0700240 done
Zdenek Behanedb527a2010-09-22 12:24:14 -0700241 popd > /dev/null
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700242
243 # Cleanup checked-in binaries that don't support the target architecture
244 [[ ${E_MACHINE} == "" ]] && return 0;
Zdenek Behan092f4632010-09-21 19:34:01 -0700245 rm -fv $( scanelf -RmyBF%a "${AUTOTEST_WORKDIR}" | grep -v -e ^${E_MACHINE} )
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700246}
247
248function autotest_src_compile() {
Zdenek Behan48096a82010-09-23 19:39:26 -0700249 if ! are_we_used; then
250 ewarn "***************************************************************"
251 ewarn "* Not building any tests, because the requested list is empty *"
252 ewarn "***************************************************************"
253 return 0
254 fi
Zdenek Behaneee7bc22010-08-20 18:33:13 -0700255 einfo "Compiling tests"
256
Zdenek Behan31315b32010-08-05 16:34:39 -0700257 pushd "${AUTOTEST_WORKDIR}" 1> /dev/null
Zdenek Behan99bf5842010-08-04 17:23:38 -0700258
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700259 setup_cross_toolchain
260
261 if use opengles ; then
262 graphics_backend=OPENGLES
263 else
264 graphics_backend=OPENGL
265 fi
266
Zdenek Behan9e979bb2010-09-23 17:32:05 -0700267 # HACK: Some of the autotests depend on SYSROOT being defined, and die
268 # a gruesome death if it isn't. But SYSROOT does not need to exist, for
269 # example on the host, it doesn't. Let's define a compatible variable
270 # here in case we have none.
271 export SYSROOT=${SYSROOT:-"/"}
272
Zdenek Behan8ad65252010-09-07 12:35:19 -0700273 # This only prints the tests that have the associated .py
274 # (and therefore a setup function)
275 local prebuild_test_dirs="
276 client/tests client/site_tests
277 server/tests server/site_tests"
278 TESTS=$(\
279 for dir in ${prebuild_test_dirs}; do
280 print_test_dirs "${AUTOTEST_WORKDIR}/${dir}"
281 done | sort | uniq
282 )
283 einfo "Building tests ($(echo ${TESTS}|wc -w)):"
284 einfo "${TESTS}"
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700285
Zdenek Behan402fea02010-09-14 13:50:09 -0700286 NORMAL=$(echo -e "\e[0m")
287 GREEN=$(echo -e "\e[1;32m")
288 RED=$(echo -e "\e[1;31m")
289
Zdenek Behan448ddaa2010-08-24 11:18:50 -0700290 # Call autotest to prebuild all test cases.
Zdenek Behan402fea02010-09-14 13:50:09 -0700291 # Parse output through a colorifying sed script
292 ( GRAPHICS_BACKEND="$graphics_backend" LOGNAME=${SUDO_USER} \
Zdenek Behand012b762010-09-01 21:22:26 -0700293 client/bin/autotest_client --quiet \
294 --client_test_setup=$(pythonify_test_list ${TESTS}) \
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700295 || ! use buildcheck || die "Tests failed to build."
Zdenek Behan402fea02010-09-14 13:50:09 -0700296 ) | sed -e "s/\(INFO:root:setup\)/${GREEN}* \1${NORMAL}/" \
297 -e "s/\(ERROR:root:\[.*\]\)/${RED}\1${NORMAL}/"
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700298
299 # Cleanup some temp files after compiling
Zdenek Behanb1f7c892010-09-22 12:25:38 -0700300 for mask in '*.[do]' ${AUTOTEST_FILE_MASK}; do
301 einfo "Purging ${mask}"
302 find . -name "${mask}" -delete
303 done
Zdenek Behan31315b32010-08-05 16:34:39 -0700304
305 popd 1> /dev/null
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700306}
307
308function autotest_src_install() {
Zdenek Behaneee7bc22010-08-20 18:33:13 -0700309 are_we_used || return 0
310 einfo "Installing tests"
311
Zdenek Behan448ddaa2010-08-24 11:18:50 -0700312 # Install all test cases, after setup has been called on them.
313 # We install everything, because nothing else is copied into the
314 # testcase directories besides what this package provides.
Zdenek Behan1ec164a2010-08-17 19:06:31 -0700315 local instdirs="
316 client/tests
317 client/site_tests
Zdenek Behan1ec164a2010-08-17 19:06:31 -0700318 server/tests
319 server/site_tests"
320
321 for dir in ${instdirs}; do
Zdenek Behancd997a32010-08-20 12:55:44 -0700322 [ -d "${AUTOTEST_WORKDIR}/${dir}" ] || continue
Zdenek Behan1ec164a2010-08-17 19:06:31 -0700323
324 insinto /usr/local/autotest/$(dirname ${dir})
325 doins -r "${AUTOTEST_WORKDIR}/${dir}"
326 done
327
Zdenek Behan448ddaa2010-08-24 11:18:50 -0700328 # Install the deps, configs, profilers.
329 # Difference from above is, we don't install the whole thing, just
330 # the stuff provided by this package, by looking at AUTOTEST_*_LIST.
331 instdirs="
332 config
333 deps
334 profilers"
335
336 for dir in ${instdirs}; do
337 [ -d "${AUTOTEST_WORKDIR}/client/${dir}" ] || continue
338
339 insinto /usr/local/autotest/client/${dir}
340
341 eval provided=\${AUTOTEST_${dir^^*}_LIST}
342 # * means provided all, figure out the list from ${S}
343 if [ "${provided}" = "*" ]; then
344 if eval pushd "${WORKDIR}/${P}/\${AUTOTEST_${dir^^*}}" &> /dev/null; then
345 provided=$(ls)
346 popd 1> /dev/null
347 else
348 provided=""
349 fi
350 fi
351
352 for item in ${provided}; do
353 doins -r "${AUTOTEST_WORKDIR}/client/${dir}/${item}"
354 done
355 done
356
Zdenek Behan1ec164a2010-08-17 19:06:31 -0700357 # TODO: Not all needs to be executable, but it's hard to pick selectively.
358 # The source repo should already contain stuff with the right permissions.
359 chmod -R a+x "${D}"/usr/local/autotest/*
Zdenek Behan12ad1c52010-08-03 16:34:37 -0700360}
361
Zdenek Behan474fbae2010-08-20 21:13:22 -0700362EXPORT_FUNCTIONS src_compile src_prepare src_install