2 # -*- coding: iso-8859-1 -*-
4 ################################################################################
6 # 2008 June 27th by Marcus 'EgS' Eggenberger <egs@quassel-irc.org> #
8 # The author disclaims copyright to this source code. #
9 # This Python Script is in the PUBLIC DOMAIN. #
11 ################################################################################
13 # ==============================
15 # ==============================
20 from subprocess import Popen, PIPE
22 # Handling Qt properties
25 # ==============================
27 # ==============================
28 QT_CONFIG = """[Paths]
32 QT_CONFIG_NOBUNDLE = """[Paths]
38 class InstallQt(object):
39 def __init__(self, appdir, bundle=True, requestedPlugins=[], skipInstallQtConf=False):
42 self.frameworkDir = self.appDir + "/Frameworks"
43 self.pluginDir = self.appDir + "/plugins"
44 self.executableDir = self.appDir
46 self.executableDir += "/MacOS"
48 self.installedFrameworks = set()
50 self.findFrameworkPath()
52 executables = [self.executableDir + "/" + executable for executable in os.listdir(self.executableDir)]
53 for executable in executables:
54 self.resolveDependancies(executable)
56 self.findPluginsPath()
57 self.installPlugins(requestedPlugins)
58 if not skipInstallQtConf:
61 def findFrameworkPath(self):
62 self.sourceFrameworkPath = macosx_qt.qtProperty('QT_INSTALL_LIBS')
64 def findPluginsPath(self):
65 self.sourcePluginsPath = macosx_qt.qtProperty('QT_INSTALL_PLUGINS')
67 def findPlugin(self, pluginname):
68 qmakeProcess = Popen('find %s -name %s' % (self.sourcePluginsPath, pluginname), shell=True, stdout=PIPE, stderr=PIPE)
69 result = qmakeProcess.stdout.read().strip()
70 qmakeProcess.stdout.close()
76 def installPlugins(self, requestedPlugins):
78 os.mkdir(self.pluginDir)
82 for plugin in requestedPlugins:
83 if not plugin.isalnum():
84 print "Skipping library '%s'..." % plugin
87 pluginName = "lib%s.dylib" % plugin
90 pluginSource = self.findPlugin(pluginName)
92 print "WARNING: Requested library does not exist: '%s'" % plugin
95 pluginSubDir = os.path.dirname(pluginSource)
96 pluginSubDir = pluginSubDir.replace(self.sourcePluginsPath, '').strip('/')
98 os.mkdir("%s/%s" % (self.pluginDir, pluginSubDir))
102 os.system('cp "%s" "%s/%s"' % (pluginSource, self.pluginDir, pluginSubDir))
104 self.resolveDependancies("%s/%s/%s" % (self.pluginDir, pluginSubDir, pluginName))
106 def installQtConf(self):
107 qtConfName = self.appDir + "/qt.conf"
108 qtConfContent = QT_CONFIG_NOBUNDLE
110 qtConfContent = QT_CONFIG
111 qtConfName = self.appDir + "/Resources/qt.conf"
113 qtConf = open(qtConfName, 'w')
114 qtConf.write(qtConfContent)
117 def resolveDependancies(self, obj):
118 # obj must be either an application binary or a framework library
119 # print "resolving deps for:", obj
120 for framework, lib in self.determineDependancies(obj):
121 self.installFramework(framework)
122 self.changeDylPath(obj, framework, lib)
124 def installFramework(self, framework):
125 # skip if framework is already installed.
126 if framework in self.installedFrameworks:
129 self.installedFrameworks.add(framework)
131 # if the Framework-Folder is a Symlink we are in a Helper-Process ".app" (e.g. in QtWebEngine),
132 # in this case skip copying/installing on existing folders
133 skipExisting = False;
134 if os.path.islink(self.frameworkDir):
137 # ensure that the framework directory exists
139 os.mkdir(self.frameworkDir)
143 if not framework.startswith('/'):
144 framework = "%s/%s" % (self.sourceFrameworkPath, framework)
146 frameworkname = framework.split('/')[-1]
147 localframework = self.frameworkDir + "/" + frameworkname
149 # Framework already installed in previous run ... see above
150 if skipExisting and os.path.isdir(localframework):
154 os.system('cp -R "%s" "%s"' % (framework, self.frameworkDir))
157 os.system('find "%s" -name *debug* -exec rm -f {} \;' % localframework)
158 os.system('find "%s" -name Headers -exec rm -rf {} \; 2>/dev/null' % localframework)
160 # Install new Lib ID and Change Path to Frameworks for the Dynamic linker
161 for lib in os.listdir(localframework + "/Versions/Current"):
162 lib = "%s/Versions/Current/%s" % (localframework, lib)
163 otoolProcess = Popen('otool -D "%s"' % lib, shell=True, stdout=PIPE, stderr=PIPE)
165 libname = [line for line in otoolProcess.stdout][1].strip()
168 otoolProcess.stdout.close()
169 if otoolProcess.wait() == 1: # we found some Resource dir or similar -> skip
171 frameworkpath, libpath = libname.split(frameworkname)
173 newlibname = "@executable_path/../%s%s" % (frameworkname, libpath)
175 newlibname = "@executable_path/%s%s" % (frameworkname, libpath)
176 # print 'install_name_tool -id "%s" "%s"' % (newlibname, lib)
177 os.system('chmod +w "%s"' % (lib))
178 os.system('install_name_tool -id "%s" "%s"' % (newlibname, lib))
180 self.resolveDependancies(lib)
182 def determineDependancies(self, app):
183 otoolPipe = Popen('otool -L "%s"' % app, shell=True, stdout=PIPE).stdout
184 otoolOutput = [line for line in otoolPipe]
186 libs = [line.split()[0] for line in otoolOutput[1:] if ("Qt" in line or "phonon" in line) and "@executable_path" not in line]
187 frameworks = [lib[:lib.find(".framework") + len(".framework")] for lib in libs]
188 frameworks = [framework[framework.rfind('/') + 1:] for framework in frameworks]
189 return zip(frameworks, libs)
191 def changeDylPath(self, obj, framework, lib):
192 newlibname = framework + lib.split(framework)[1]
194 newlibname = "@executable_path/../Frameworks/%s" % newlibname
196 newlibname = "@executable_path/Frameworks/%s" % newlibname
198 # print 'install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj)
199 os.system('chmod +w "%s"' % (lib))
200 os.system('chmod +w "%s"' % (obj))
201 os.system('install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj))
203 if __name__ == "__main__":
204 if len(sys.argv) < 2:
205 print "Wrong Argument Count (Syntax: %s [--nobundle] [--plugins=plugin1,plugin2,...] $TARGET_APP)" % sys.argv[0]
212 while offset < len(sys.argv) and sys.argv[offset].startswith("--"):
213 if sys.argv[offset] == "--nobundle":
216 if sys.argv[offset].startswith("--plugins="):
217 plugins = sys.argv[offset].split('=')[1].split(',')
221 targetDir = sys.argv[offset]
223 targetDir += "/Contents"
225 InstallQt(targetDir, bundle, plugins)
228 webenginetarget = targetDir + '/Frameworks/QtWebEngineCore.framework/Helpers/QtWebEngineProcess.app/Contents'
230 if os.path.isdir(webenginetarget):
231 os.system('ln -s ../../../../../../ "%s"/Frameworks' % webenginetarget)
232 InstallQt(webenginetarget, bundle, [], True)