17ec681f3Smrg#!/usr/bin/env python3
201e04c3fSmrg# encoding=utf-8
37ec681f3Smrg# Copyright 2017-2018 Intel Corporation
401e04c3fSmrg
501e04c3fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
601e04c3fSmrg# of this software and associated documentation files (the "Software"), to deal
701e04c3fSmrg# in the Software without restriction, including without limitation the rights
801e04c3fSmrg# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
901e04c3fSmrg# copies of the Software, and to permit persons to whom the Software is
1001e04c3fSmrg# furnished to do so, subject to the following conditions:
1101e04c3fSmrg
1201e04c3fSmrg# The above copyright notice and this permission notice shall be included in
1301e04c3fSmrg# all copies or substantial portions of the Software.
1401e04c3fSmrg
1501e04c3fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1801e04c3fSmrg# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2001e04c3fSmrg# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2101e04c3fSmrg# SOFTWARE.
2201e04c3fSmrg
2301e04c3fSmrg"""Script to install megadriver symlinks for meson."""
2401e04c3fSmrg
2501e04c3fSmrgimport argparse
2601e04c3fSmrgimport os
2701e04c3fSmrg
2801e04c3fSmrg
2901e04c3fSmrgdef main():
3001e04c3fSmrg    parser = argparse.ArgumentParser()
3101e04c3fSmrg    parser.add_argument('megadriver')
3201e04c3fSmrg    parser.add_argument('libdir')
3301e04c3fSmrg    parser.add_argument('drivers', nargs='+')
3401e04c3fSmrg    args = parser.parse_args()
3501e04c3fSmrg
3601e04c3fSmrg    if os.path.isabs(args.libdir):
37993e1d59Smrg        destdir = os.environ.get('DESTDIR')
38993e1d59Smrg        if destdir:
39993e1d59Smrg            to = os.path.join(destdir, args.libdir[1:])
40993e1d59Smrg        else:
41993e1d59Smrg            to = args.libdir
4201e04c3fSmrg    else:
4301e04c3fSmrg        to = os.path.join(os.environ['MESON_INSTALL_DESTDIR_PREFIX'], args.libdir)
4401e04c3fSmrg
4501e04c3fSmrg    master = os.path.join(to, os.path.basename(args.megadriver))
4601e04c3fSmrg
4701e04c3fSmrg    if not os.path.exists(to):
4801e04c3fSmrg        if os.path.lexists(to):
4901e04c3fSmrg            os.unlink(to)
5001e04c3fSmrg        os.makedirs(to)
5101e04c3fSmrg
5201e04c3fSmrg    for driver in args.drivers:
5301e04c3fSmrg        abs_driver = os.path.join(to, driver)
5401e04c3fSmrg
5501e04c3fSmrg        if os.path.lexists(abs_driver):
5601e04c3fSmrg            os.unlink(abs_driver)
5701e04c3fSmrg        print('installing {} to {}'.format(args.megadriver, abs_driver))
5801e04c3fSmrg        os.link(master, abs_driver)
5901e04c3fSmrg
6001e04c3fSmrg        try:
6101e04c3fSmrg            ret = os.getcwd()
6201e04c3fSmrg            os.chdir(to)
6301e04c3fSmrg
6401e04c3fSmrg            name, ext = os.path.splitext(driver)
6501e04c3fSmrg            while ext != '.so':
6601e04c3fSmrg                if os.path.lexists(name):
6701e04c3fSmrg                    os.unlink(name)
6801e04c3fSmrg                os.symlink(driver, name)
6901e04c3fSmrg                name, ext = os.path.splitext(name)
7001e04c3fSmrg        finally:
7101e04c3fSmrg            os.chdir(ret)
7253c12917Smaya
7353c12917Smaya    # Remove meson-created master .so and symlinks
7401e04c3fSmrg    os.unlink(master)
7553c12917Smaya    name, ext = os.path.splitext(master)
7653c12917Smaya    while ext != '.so':
7753c12917Smaya        if os.path.lexists(name):
7853c12917Smaya            os.unlink(name)
7953c12917Smaya        name, ext = os.path.splitext(name)
8001e04c3fSmrg
8101e04c3fSmrg
8201e04c3fSmrgif __name__ == '__main__':
8301e04c3fSmrg    main()
84