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