ocssw V2020
fix_mac_rpath.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
3 # This program looks for shared library references that need to be changed to RPATH.
4 # It also copies gcc libs into OCSSW/opt/lib for seadas distribution
5 
6 import subprocess
7 import os
8 import shutil
9 
10 rerunNeeded = True
11 while rerunNeeded:
12  rerunNeeded = False
13  # set the rpath in opt/libs
14  os.chdir(os.path.join(os.environ['LIB3_DIR'], "lib"))
15  for fileName in os.listdir('.'):
16  if os.path.isfile(fileName):
17  if ".dylib" in fileName:
18  #print (fileName)
19  p = subprocess.Popen(["otool", "-D", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
20  out, err = p.communicate()
21  #print("out=",out)
22  parts = out.decode("utf-8").split()
23  parts = parts[1].split("/")
24  rpath = parts[0]
25  if not "@rpath" in parts[0]:
26  name = parts[len(parts)-1]
27  id = "@rpath/" + name
28  # print(fileName, id)
29  subprocess.call(["install_name_tool", "-id", id, fileName])
30 
31  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
32  out, err = p.communicate()
33  #print("out=",out)
34  lines = out.decode("utf-8").split('\n')
35  for line in lines:
36  if '/opt/local/lib/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line or ('/' not in line and 'compatibility' in line):
37  #print(' ' + line)
38  libPath = line.split()[0]
39  parts = libPath.split('/')
40  libName = parts[len(parts)-1]
41  newName = '@rpath/' + libName
42  #print(' ' + libPath + ' -> ' + newName)
43  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
44  if os.environ['LIB3_DIR'] not in libPath:
45  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
46  rerunNeeded = True
47  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
48  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
49 
50 
51  # set the rpath in opt/bin
52  os.chdir(os.path.join(os.environ['LIB3_DIR'], "bin"))
53  for fileName in os.listdir('.'):
54  if os.path.isfile(fileName):
55  line = subprocess.check_output(['file', fileName]).decode("utf-8")
56  if "Mach-O 64-bit executable" in line:
57  #print ('------' + fileName)
58  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
59  out, err = p.communicate()
60  #print("out=",out)
61  lines = out.decode("utf-8").split('\n')
62  for line in lines:
63  if '/opt/local/lib/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line:
64  print(' ' + line)
65  libPath = line.split()[0]
66  parts = libPath.split('/')
67  libName = parts[len(parts)-1]
68  newName = '@rpath/' + libName
69  #print(' ' + libPath + ' -> ' + newName)
70  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
71  if os.environ['LIB3_DIR'] not in libPath:
72  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
73  rerunNeeded = True
74  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
75  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
76 
77 
78  # set the rpath in OCSSW/libs
79  os.chdir(os.path.join(os.environ['OCSSWROOT'], "lib"))
80  for fileName in os.listdir('.'):
81  if os.path.isfile(fileName):
82  if ".dylib" in fileName:
83  #print (fileName)
84  p = subprocess.Popen(["otool", "-D", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
85  out, err = p.communicate()
86  #print("out=",out)
87  parts = out.decode("utf-8").split()
88  parts = parts[1].split("/")
89  rpath = parts[0]
90  if not "@rpath" in parts[0]:
91  name = parts[len(parts)-1]
92  id = "@rpath/" + name
93  # print(fileName, id)
94  subprocess.call(["install_name_tool", "-id", id, fileName])
95 
96  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
97  out, err = p.communicate()
98  #print("out=",out)
99  lines = out.decode("utf-8").split('\n')
100  for line in lines:
101  if '/opt/local/lib/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line:
102  #print(' ' + line)
103  libPath = line.split()[0]
104  parts = libPath.split('/')
105  libName = parts[len(parts)-1]
106  newName = '@rpath/' + libName
107  #print(' ' + libPath + ' -> ' + newName)
108  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
109  if os.environ['LIB3_DIR'] not in libPath:
110  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
111  rerunNeeded = True
112  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
113  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
114 
115 
116  # set the rpath in OCSSW/bin
117  os.chdir(os.path.join(os.environ['OCSSWROOT'], "bin"))
118  for fileName in os.listdir('.'):
119  if os.path.isfile(fileName):
120  line = subprocess.check_output(['file', fileName]).decode("utf-8")
121  if "Mach-O 64-bit executable" in line:
122  #print ('------' + fileName)
123  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
124  out, err = p.communicate()
125  #print("out=",out)
126  lines = out.decode("utf-8").split('\n')
127  for line in lines:
128  if '/opt/local/lib/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line or '%s/lib' % (os.environ['OCSSWROOT']) in line:
129  #print(' ' + line)
130  libPath = line.split()[0]
131  parts = libPath.split('/')
132  libName = parts[len(parts)-1]
133  newName = '@rpath/' + libName
134  #print(' ' + libPath + ' -> ' + newName)
135  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
136  if os.environ['LIB3_DIR'] not in libPath:
137  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
138  rerunNeeded = True
139  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
140  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
141 
142 
143  # set the rpath in OCSSW/../viirs_l1/bin
144  os.chdir(os.path.join(os.environ['OCSSWROOT'], "../viirs_l1/bin"))
145  for fileName in os.listdir('.'):
146  if os.path.isfile(fileName):
147  line = subprocess.check_output(['file', fileName]).decode("utf-8")
148  if "Mach-O 64-bit executable" in line:
149  #print ('------' + fileName)
150  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
151  out, err = p.communicate()
152  #print("out=",out)
153  lines = out.decode("utf-8").split('\n')
154  for line in lines:
155  if '/opt/local/lib/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line or '%s/lib' % (os.environ['OCSSWROOT']) in line:
156  #print(' ' + line)
157  libPath = line.split()[0]
158  parts = libPath.split('/')
159  libName = parts[len(parts)-1]
160  newName = '@rpath/' + libName
161  #print(' ' + libPath + ' -> ' + newName)
162  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
163  if os.environ['LIB3_DIR'] not in libPath:
164  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
165  rerunNeeded = True
166  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
167  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
168