NASA Logo
Ocean Color Science Software

ocssw V2022
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 libs from homebrew and X11 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 
14  # set the rpath in opt/libs
15  os.chdir(os.path.join(os.environ['LIB3_DIR'], "lib"))
16  for fileName in os.listdir('.'):
17  if os.path.isfile(fileName):
18  if ".dylib" in fileName:
19  #print (fileName)
20  p = subprocess.Popen(["otool", "-D", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
21  out, err = p.communicate()
22  #print("out=",out)
23  parts = out.decode("utf-8").split()
24  parts = parts[1].split("/")
25  rpath = parts[0]
26  if not "@rpath" in parts[0]:
27  name = parts[-1]
28  id = "@rpath/" + name
29  # print(fileName, id)
30  subprocess.call(["install_name_tool", "-id", id, fileName])
31 
32  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
33  out, err = p.communicate()
34  #print("out=",out)
35  lines = out.decode("utf-8").split('\n')
36  for line in lines:
37  if '/opt/homebrew/' in line or '/usr/local/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line or ('/' not in line and 'compatibility' in line):
38  #print(' ' + line)
39  libPath = line.split()[0]
40  parts = libPath.split('/')
41  libName = parts[-1]
42  newName = '@rpath/' + libName
43  #print(' ' + libPath + ' -> ' + newName)
44  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
45  if os.environ['LIB3_DIR'] not in libPath:
46  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
47  rerunNeeded = True
48  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
49  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
50 
51 
52  # set the rpath in opt/bin
53  os.chdir(os.path.join(os.environ['LIB3_DIR'], "bin"))
54  for fileName in os.listdir('.'):
55  if os.path.isfile(fileName):
56  line = subprocess.check_output(['file', fileName]).decode("utf-8")
57  if "Mach-O 64-bit executable" in line:
58  #print ('------' + fileName)
59  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
60  out, err = p.communicate()
61  #print("out=",out)
62  lines = out.decode("utf-8").split('\n')
63  for line in lines:
64  if '/opt/homebrew/' in line or '/usr/local/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line:
65  print(' ' + line)
66  libPath = line.split()[0]
67  parts = libPath.split('/')
68  libName = parts[-1]
69  newName = '@rpath/' + libName
70  #print(' ' + libPath + ' -> ' + newName)
71  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
72  if os.environ['LIB3_DIR'] not in libPath:
73  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
74  rerunNeeded = True
75  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
76  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
77 
78 
79  # set the rpath in OCSSW/libs
80  os.chdir(os.path.join(os.environ['OCSSWROOT'], "lib"))
81  for fileName in os.listdir('.'):
82  if os.path.isfile(fileName):
83  if ".dylib" in fileName:
84  #print (fileName)
85  p = subprocess.Popen(["otool", "-D", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
86  out, err = p.communicate()
87  #print("out=",out)
88  parts = out.decode("utf-8").split()
89  parts = parts[1].split("/")
90  rpath = parts[0]
91  if not "@rpath" in parts[0]:
92  name = parts[-1]
93  id = "@rpath/" + name
94  # print(fileName, id)
95  subprocess.call(["install_name_tool", "-id", id, fileName])
96 
97  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98  out, err = p.communicate()
99  #print("out=",out)
100  lines = out.decode("utf-8").split('\n')
101  for line in lines:
102  if '/opt/homebrew/' in line or '/usr/local/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line:
103  #print(' ' + line)
104  libPath = line.split()[0]
105  parts = libPath.split('/')
106  libName = parts[-1]
107  newName = '@rpath/' + libName
108  #print(' ' + libPath + ' -> ' + newName)
109  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
110  if os.environ['LIB3_DIR'] not in libPath:
111  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
112  rerunNeeded = True
113  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
114  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
115 
116 
117  # set the rpath in OCSSW/bin
118  os.chdir(os.path.join(os.environ['OCSSWROOT'], "bin"))
119  for fileName in os.listdir('.'):
120  if os.path.isfile(fileName):
121  line = subprocess.check_output(['file', fileName]).decode("utf-8")
122  if "Mach-O 64-bit executable" in line:
123  #print ('------' + fileName)
124  p = subprocess.Popen(["otool", "-L", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
125  out, err = p.communicate()
126  #print("out=",out)
127  lines = out.decode("utf-8").split('\n')
128  for line in lines:
129  if '/opt/homebrew/' in line or '/usr/local/' in line or '/opt/X11/lib/' in line or os.environ['LIB3_DIR'] in line or '%s/lib' % (os.environ['OCSSWROOT']) in line:
130  #print(' ' + line)
131  libPath = line.split()[0]
132  parts = libPath.split('/')
133  libName = parts[-1]
134  newName = '@rpath/' + libName
135  #print(' ' + libPath + ' -> ' + newName)
136  subprocess.call(["install_name_tool", "-change", libPath, newName, fileName])
137  if os.environ['LIB3_DIR'] not in libPath:
138  if not os.path.isfile(os.environ['LIB3_DIR'] + "/lib/" + libName):
139  rerunNeeded = True
140  print("copying", libPath, os.environ['LIB3_DIR'] + "/lib" )
141  shutil.copy(libPath, os.environ['LIB3_DIR'] + "/lib")
142 
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38