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