xref: /aosp_15_r20/external/mesa3d/bin/install_megadrivers.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1#!/usr/bin/env python3
2# encoding=utf-8
3# Copyright 2017-2018 Intel Corporation
4
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23"""Script to install megadriver symlinks for meson."""
24
25import argparse
26import os
27
28
29def resolve_libdir(libdir):
30    if os.path.isabs(libdir):
31        destdir = os.environ.get('DESTDIR')
32        if destdir:
33            return os.path.join(destdir, libdir[1:])
34        else:
35            return libdir
36    return os.path.join(os.environ['MESON_INSTALL_DESTDIR_PREFIX'], libdir)
37
38
39def main():
40    parser = argparse.ArgumentParser()
41    parser.add_argument('megadriver')
42    parser.add_argument('libdir')
43    parser.add_argument('drivers', nargs='+')
44    parser.add_argument('--megadriver_libdir')
45    args = parser.parse_args()
46
47    to = resolve_libdir(args.libdir)
48    if args.megadriver_libdir:
49        md_to = resolve_libdir(args.megadriver_libdir)
50    else:
51        md_to = to
52
53    basename = os.path.basename(args.megadriver)
54    master = os.path.join(to, basename)
55
56    if not os.path.exists(to):
57        if os.path.lexists(to):
58            os.unlink(to)
59        os.makedirs(to)
60
61    for driver in args.drivers:
62        abs_driver = os.path.join(to, driver)
63
64        if os.path.lexists(abs_driver):
65            os.unlink(abs_driver)
66
67        symlink = os.path.relpath(os.path.join(md_to, basename), start=to)
68
69        print(f'Installing symlink pointing to {symlink} to {abs_driver}')
70        os.symlink(symlink, abs_driver)
71
72        try:
73            ret = os.getcwd()
74            os.chdir(to)
75
76            name, ext = os.path.splitext(driver)
77            while ext != '.so':
78                if os.path.lexists(name):
79                    os.unlink(name)
80                os.symlink(driver, name)
81                name, ext = os.path.splitext(name)
82        finally:
83            os.chdir(ret)
84
85    # Remove meson-created .so symlinks
86    name, ext = os.path.splitext(master)
87    while ext != '.so':
88        if os.path.lexists(name):
89            os.unlink(name)
90        name, ext = os.path.splitext(name)
91
92
93if __name__ == '__main__':
94    main()
95