summaryrefslogtreecommitdiff
path: root/compiler/one-cmds/onelib/backends.py
blob: 9d7dad17e174f295221d80fe2df0135a176d5983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python

# Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import glob
import ntpath
import os
"""
[one hierarchy]
one
├── backends
├── bin
├── doc
├── include
├── lib
├── optimization
└── test

The list where `one-XXXX` finds its backends
- `bin` folder where `one-XXXX` exists
- `backends` folder

NOTE If there are backends of the same name in different places,
    the closer to the top in the list, the higher the priority.
"""


def get_list(cmdname):
    dir_path = os.path.dirname(os.path.realpath(__file__))
    backend_set = set()

    # bin folder
    files = [f for f in glob.glob(dir_path + '/../*-' + cmdname)]
    # backends folder
    files += [
        f
        for f in glob.glob(dir_path + '/../../backends/**/*-' + cmdname, recursive=True)
    ]
    # TODO find backends in `$PATH`

    backends_list = []
    for cand in files:
        base = ntpath.basename(cand)
        if (not base in backend_set) and os.path.isfile(cand) and os.access(
                cand, os.X_OK):
            backend_set.add(base)
            backends_list.append(cand)

    return backends_list


def search_driver(driver):
    dir_path = os.path.dirname(os.path.realpath(__file__))

    # CASE 1: one/bin/{driver} is found
    driver_path = dir_path + '/../' + driver
    if os.path.isfile(driver_path) and os.access(driver_path, os.X_OK):
        return driver_path

    # CASE 2: one/backends/**/bin/{driver} is found
    for driver_path in glob.glob(
            dir_path + '/../../backends/**/bin/' + driver, recursive=True):
        if os.path.isfile(driver_path) and os.access(driver_path, os.X_OK):
            return driver_path

    # CASE 3: {driver} is found in nowhere
    return None