File: relocate_libraries.py

package info (click to toggle)
deal.ii 9.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 236,560 kB
  • sloc: cpp: 334,965; ansic: 72,687; python: 2,374; sh: 841; perl: 761; xml: 437; makefile: 82; javascript: 14
file content (123 lines) | stat: -rwxr-xr-x 3,982 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3

## ---------------------------------------------------------------------
##
## Copyright (C) 2016 by the deal.II authors
##
## This file is part of the deal.II library.
##
## The deal.II library is free software; you can use it, redistribute
## it, and/or modify it under the terms of the GNU Lesser General
## Public License as published by the Free Software Foundation; either
## version 2.1 of the License, or (at your option) any later version.
## The full text of the license can be found in the file LICENSE.md at
## the top level directory of deal.II.
##
## ---------------------------------------------------------------------

## This script runs install_name_tool on each library under basedir (first argument
## on command line) and makes sure that all libraries are installed with absolute
## id name. This makes Mac OS X happy about not using DYLD_LIBRARY_PATH, which is generally
## a bad thing. 

import os
import sys
from subprocess import check_output as run

basedir = '/Applications/deal.II.app/Contents/Resources/opt/'
if len(sys.argv) > 1:
    basedir = sys.argv[1]

## Lib object
# 
# Stores the name of a library, its install name, its location and all its dependencies, as given by `otool -L`

class Lib(object):
    location = None
    install_name = None
    name = None
    deps = None
    def __str__(self):
        mystr = ""
        mystr +=  "Name        :"+str(self.name)
        mystr +="\nInstall name:"+str(self.install_name)
        mystr +="\nLocation    :"+str(self.location)
        mystr +="\nDeps        : ... "
        return mystr

# Walk the current tree, and extract all libraries
def get_libs():
    libraries = []

    for root, dirs, files in os.walk(basedir):
        for f in files:
            filename = os.path.join(root, f)
            if os.path.isfile(filename) and f.endswith("dylib"):
                a = Lib()
                a.name = f
                a.install_name = run(["otool", "-D", filename]).split('\n')[1]
                a.location= os.path.join(root, f)
                long_deps = run(["otool", "-L", a.location]).split('\n')
                a.deps = [dep.partition(' ')[0][1::] for dep in long_deps[2:-1]]
                libraries += [a]
    return libraries


# # Fix all install names first
# 
# Some will fail, because they are either stub files, or system files...

# In[ ]:

libraries = get_libs()
c = 0
failed = []
for c in range(len(libraries)):
    i = libraries[c]
    if os.path.islink(i.location):
        continue
    if i.install_name != i.location:
        try:
            run(["install_name_tool",'-id',i.location, i.location])
        except:
            print("Failed: ",i.name)
            print( "(",libraries[c].name,")")
            failed += [c]
print (failed)
print ('Removing failed libs...')
for fail in reversed(failed):
    print ("Removing from list",libraries[fail].name)
    del libraries[fail]


# # Fix all dependencies with absolute paths

for c  in xrange(len(libraries)):
    this_lib = libraries[c]
    if this_lib.install_name != this_lib.location:
        print('Not valid:', i.name)
    else:
        lib = this_lib.name
        command = ['install_name_tool']
        for dep in this_lib.deps:
            for loc in libraries:
                if dep.find(loc.name) != -1 and dep != loc.install_name:
                    command += ['-change', dep, loc.install_name]
                    break
        try:
            if len(command) != 1:
                command += [this_lib.location]
                print('Processing', lib)
                print('======================\n\n')
                print(" ".join(command))
                print('======================\n\n')
                run(command)
        except:
            print('\n\n*********************************************')
            print('Last command failed!')
            print(" ".join(command))
            print('*********************************************\n\n')