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 parser.add_argument('--libname-suffix', required=True) 46 args = parser.parse_args() 47 48 # Not neccesarily at the end, there might be a version suffix, but let's 49 # make sure that the same suffix is in the megadriver lib name. 50 assert '.' + args.libname_suffix in args.megadriver 51 52 to = resolve_libdir(args.libdir) 53 if args.megadriver_libdir: 54 md_to = resolve_libdir(args.megadriver_libdir) 55 else: 56 md_to = to 57 58 basename = os.path.basename(args.megadriver) 59 master = os.path.join(to, basename) 60 61 if not os.path.exists(to): 62 if os.path.lexists(to): 63 os.unlink(to) 64 os.makedirs(to) 65 66 for driver in args.drivers: 67 abs_driver = os.path.join(to, driver) 68 69 if os.path.lexists(abs_driver): 70 os.unlink(abs_driver) 71 72 symlink = os.path.relpath(os.path.join(md_to, basename), start=to) 73 74 print(f'Installing symlink pointing to {symlink} to {abs_driver}') 75 os.symlink(symlink, abs_driver) 76 77 try: 78 ret = os.getcwd() 79 os.chdir(to) 80 81 name, ext = os.path.splitext(driver) 82 while ext != '.' + args.libname_suffix: 83 if os.path.lexists(name): 84 os.unlink(name) 85 os.symlink(driver, name) 86 name, ext = os.path.splitext(name) 87 finally: 88 os.chdir(ret) 89 90 # Remove meson-created symlinks 91 name, ext = os.path.splitext(master) 92 while ext != '.' + args.libname_suffix: 93 if os.path.lexists(name): 94 os.unlink(name) 95 name, ext = os.path.splitext(name) 96 97 98if __name__ == '__main__': 99 main() 100