1# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from autotest_lib.client.common_lib import utils
6
7
8def has_hal(hal_library_name, host=None, accept_64bit=True, accept_32bit=True):
9    """Detect if the host has the given HAL.
10
11    Note that a board can have several HALs, even of a same type.  libhardware
12    picks among them at runtime based on values in system properties.  So if
13    hal_library_name == 'gralloc', we might find that we have both
14    gralloc.brilloemulator.so and gralloc.default.so.  This function will
15    not speculate about which will be loaded at runtime.
16
17    @param hal_library_name: string name of the hal (e.g. gralloc or camera).
18    @param host: optional host object representing a remote DUT.  If None,
19            then we'll look for the HAL on localhost.
20    @param accept_64bit: True iff a 64 bit version of the library suffices.
21    @param accept_32bit: True iff a 32 bit version of the library suffices.
22
23    @return True iff an appropriate library is found on the device.
24
25    """
26    run = utils.run if host is None else host.run
27    paths = []
28    if accept_64bit:
29        paths.append('/system/lib64/hw')
30    if accept_32bit:
31        paths.append('/system/lib/hw')
32    for path in paths:
33        result = run('find %s -name %s.*.so 2>/dev/null' %
34                     (path, hal_library_name), ignore_status=True)
35        if result.exit_status == 0 and result.stdout.strip():
36            return True
37
38    return False
39